2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "Id: db.c,v 10.48 2002/06/08 19:32:52 skimo Exp (Berkeley) Date: 2002/06/08 19:32:52 ";
16 #include <sys/types.h>
17 #include <sys/queue.h>
21 #include <bitstring.h>
30 #include "dbinternal.h"
33 static int append
__P((SCR
*, db_recno_t
, const CHAR_T
*, size_t, lnop_t
, int));
37 * Front-end to db_get, special case handling for empty files.
39 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
42 db_eget(SCR
*sp
, db_recno_t lno
, CHAR_T
**pp
, size_t *lenp
, int *isemptyp
)
54 /* If the line exists, simply return it. */
55 if (!db_get(sp
, lno
, 0, pp
, lenp
))
59 * If the user asked for line 0 or line 1, i.e. the only possible
60 * line in an empty file, find the last line of the file; db_last
63 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
66 /* If the file isn't empty, fail loudly. */
67 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
80 * Look in the text buffers for a line, followed by the cache, followed
83 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
86 db_get(SCR
*sp
, db_recno_t lno
, u_int32_t flags
, CHAR_T
**pp
, size_t *lenp
)
87 /* Line number. */ /* Pointer store. */ /* Length store. */
98 * The underlying recno stuff handles zero by returning NULL, but
99 * have to have an OOB condition for the look-aside into the input
105 /* Check for no underlying file. */
106 if ((ep
= sp
->ep
) == NULL
) {
107 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
111 if (LF_ISSET(DBG_NOCACHE
))
115 * Look-aside into the TEXT buffers and see if the line we want
118 if (F_ISSET(sp
, SC_TINPUT
)) {
119 l1
= TAILQ_FIRST(&sp
->tiq
)->lno
;
120 l2
= TAILQ_LAST(&sp
->tiq
, _texth
)->lno
;
121 if (l1
<= lno
&& l2
>= lno
) {
122 #if defined(DEBUG) && 0
124 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
126 for (tp
= TAILQ_FIRST(&sp
->tiq
);
127 tp
->lno
!= lno
; tp
= TAILQ_NEXT(tp
, q
));
135 * Adjust the line number for the number of lines used
136 * by the text input buffers.
142 /* Look-aside into the cache, and see if the line we want is there. */
143 if (lno
== sp
->c_lno
) {
144 #if defined(DEBUG) && 0
145 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
158 /* data.size contains length in bytes */
159 BINC_GOTO(sp
, CHAR_T
, sp
->c_lp
, sp
->c_blen
, nlen
);
161 /* Get the line from the underlying database. */
162 memset(&key
, 0, sizeof(key
));
164 key
.size
= sizeof(lno
);
165 memset(&data
, 0, sizeof(data
));
166 data
.data
= sp
->c_lp
;
167 data
.ulen
= sp
->c_blen
;
168 data
.flags
= DB_DBT_USERMEM
;
169 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
170 case DB_BUFFER_SMALL
:
176 err1
: if (LF_ISSET(DBG_FATAL
))
177 err2
: db_err(sp
, lno
);
179 err3
: if (lenp
!= NULL
)
188 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
189 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
190 F_SET(sp
, SC_CONV_ERROR
);
191 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
196 /* Reset the cache. */
197 if (wp
!= data
.data
) {
198 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
199 MEMCPYW(sp
->c_lp
, wp
, wlen
);
204 #if defined(DEBUG) && 0
205 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
216 * Delete a line from the file.
218 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
221 db_delete(SCR
*sp
, db_recno_t lno
)
226 #if defined(DEBUG) && 0
227 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
229 /* Check for no underlying file. */
230 if ((ep
= sp
->ep
) == NULL
) {
231 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
234 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
235 ex_emsg(sp
, NULL
, EXM_LOCKED
);
239 /* Update marks, @ and global commands. */
240 if (line_insdel(sp
, LINE_DELETE
, lno
))
243 /* Log before change. */
244 log_line(sp
, lno
, LOG_LINE_DELETE_B
);
247 memset(&key
, 0, sizeof(key
));
249 key
.size
= sizeof(lno
);
250 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
251 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
256 /* Flush the cache, update line count, before screen update. */
257 update_cache(sp
, LINE_DELETE
, lno
);
259 /* File now modified. */
260 if (F_ISSET(ep
, F_FIRSTMODIFY
))
262 F_SET(ep
, F_MODIFIED
);
264 /* Log after change. */
265 log_line(sp
, lno
, LOG_LINE_DELETE_F
);
268 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
271 /* maybe this could be simpler
273 * DB3 behaves differently from DB1
275 * if lno != 0 just go to lno and put the new line after it
276 * if lno == 0 then if there are any record, put in front of the first
277 * otherwise just append to the end thus creating the first
281 append(SCR
*sp
, db_recno_t lno
, const CHAR_T
*p
, size_t len
, lnop_t op
, int update
)
290 /* Check for no underlying file. */
291 if ((ep
= sp
->ep
) == NULL
) {
292 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
295 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
296 ex_emsg(sp
, NULL
, EXM_LOCKED
);
300 /* Log before change. */
301 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_B
);
304 memset(&key
, 0, sizeof(key
));
306 key
.size
= sizeof(lno
);
307 memset(&data
, 0, sizeof(data
));
309 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
312 INT2FILE(sp
, p
, len
, fp
, flen
);
315 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
318 data
.data
= __UNCONST(fp
);
320 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
322 (void)dbcp_put
->c_close(dbcp_put
);
325 ? "004|unable to append to line %lu"
326 : "005|unable to insert at line %lu",
331 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
332 if (sp
->db_error
!= DB_NOTFOUND
)
335 data
.data
= __UNCONST(fp
);
337 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
342 key
.size
= sizeof(lno
);
343 data
.data
= __UNCONST(fp
);
345 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
351 (void)dbcp_put
->c_close(dbcp_put
);
353 /* Flush the cache, update line count, before screen update. */
354 update_cache(sp
, LINE_INSERT
, lno
);
356 /* File now dirty. */
357 if (F_ISSET(ep
, F_FIRSTMODIFY
))
359 F_SET(ep
, F_MODIFIED
);
361 /* Log after change. */
362 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_F
);
364 /* Update marks, @ and global commands. */
365 rval
= line_insdel(sp
, LINE_INSERT
, lno
+ 1);
370 * comment copied from db_append
372 * Nasty hack. If multiple lines are input by the user, they aren't
373 * committed until an <ESC> is entered. The problem is the screen was
374 * updated/scrolled as each line was entered. So, when this routine
375 * is called to copy the new lines from the cut buffer into the file,
376 * it has to know not to update the screen again.
378 return (scr_update(sp
, lno
+ 1, LINE_INSERT
, update
) || rval
);
383 * Append a line into the file.
385 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, const CHAR_T *, size_t));
388 db_append(SCR
*sp
, int update
, db_recno_t lno
, const CHAR_T
*p
, size_t len
)
390 #if defined(DEBUG) && 0
391 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
395 return append(sp
, lno
, p
, len
, LINE_APPEND
, update
);
400 * Insert a line into the file.
402 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
405 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
407 #if defined(DEBUG) && 0
408 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
409 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
411 return append(sp
, lno
- 1, p
, len
, LINE_INSERT
, 1);
416 * Store a line in the file.
418 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
421 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
428 #if defined(DEBUG) && 0
429 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
430 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
432 /* Check for no underlying file. */
433 if ((ep
= sp
->ep
) == NULL
) {
434 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
437 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
438 ex_emsg(sp
, NULL
, EXM_LOCKED
);
442 /* Log before change. */
443 log_line(sp
, lno
, LOG_LINE_RESET_B
);
445 INT2FILE(sp
, p
, len
, fp
, flen
);
448 memset(&key
, 0, sizeof(key
));
450 key
.size
= sizeof(lno
);
451 memset(&data
, 0, sizeof(data
));
452 data
.data
= __UNCONST(fp
);
454 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
455 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
459 /* Flush the cache, update line count, before screen update. */
460 update_cache(sp
, LINE_RESET
, lno
);
462 /* File now dirty. */
463 if (F_ISSET(ep
, F_FIRSTMODIFY
))
465 F_SET(ep
, F_MODIFIED
);
467 /* Log after change. */
468 log_line(sp
, lno
, LOG_LINE_RESET_F
);
471 return (scr_update(sp
, lno
, LINE_RESET
, 1));
476 * Return if a line exists.
478 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
481 db_exist(SCR
*sp
, db_recno_t lno
)
485 /* Check for no underlying file. */
486 if ((ep
= sp
->ep
) == NULL
) {
487 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
495 * Check the last-line number cache. Adjust the cached line
496 * number for the lines used by the text input buffers.
498 if (ep
->c_nlines
!= OOBLNO
)
499 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
500 ep
->c_nlines
+ TAILQ_LAST(&sp
->tiq
, _texth
)->lno
-
501 TAILQ_FIRST(&sp
->tiq
)->lno
: ep
->c_nlines
));
503 /* Go get the line. */
504 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
509 * Return the number of lines in the file.
511 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
514 db_last(SCR
*sp
, db_recno_t
*lnop
)
523 /* Check for no underlying file. */
524 if ((ep
= sp
->ep
) == NULL
) {
525 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
530 * Check the last-line number cache. Adjust the cached line
531 * number for the lines used by the text input buffers.
533 if (ep
->c_nlines
!= OOBLNO
) {
534 *lnop
= ep
->c_nlines
;
535 if (F_ISSET(sp
, SC_TINPUT
))
536 *lnop
+= TAILQ_LAST(&sp
->tiq
, _texth
)->lno
-
537 TAILQ_FIRST(&sp
->tiq
)->lno
;
541 memset(&key
, 0, sizeof(key
));
543 key
.size
= sizeof(lno
);
544 memset(&data
, 0, sizeof(data
));
546 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
548 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
553 (void)dbcp
->c_close(dbcp
);
556 msgq(sp
, M_DBERR
, "007|unable to get last line");
563 memcpy(&lno
, key
.data
, sizeof(lno
));
565 if (lno
!= sp
->c_lno
) {
566 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
568 /* Fill the cache. */
569 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
570 MEMCPYW(sp
->c_lp
, wp
, wlen
);
576 (void)dbcp
->c_close(dbcp
);
578 /* Return the value. */
579 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
580 TAILQ_LAST(&sp
->tiq
, _texth
)->lno
> lno
?
581 TAILQ_LAST(&sp
->tiq
, _texth
)->lno
: lno
);
587 * Report a line error.
589 * PUBLIC: void db_err __P((SCR *, db_recno_t));
592 db_err(SCR
*sp
, db_recno_t lno
)
595 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
600 * Update all of the screens that are backed by the file that
603 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
604 * PUBLIC: lnop_t op, int current));
607 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
613 if (F_ISSET(sp
, SC_EX
))
616 /* XXXX goes outside of window */
619 TAILQ_FOREACH(wp
, &sp
->gp
->dq
, q
)
620 TAILQ_FOREACH(tsp
, &wp
->scrq
, q
)
621 if (sp
!= tsp
&& tsp
->ep
== ep
)
622 if (vs_change(tsp
, lno
, op
))
624 return (current
? vs_change(sp
, lno
, op
) : 0);
628 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
631 update_cache(SCR
*sp
, lnop_t op
, db_recno_t lno
)
638 /* Flush the cache, update line count, before screen update. */
639 /* The flushing is probably not needed, since it was incorrect
640 * for db_insert. It might be better to adjust it, like
641 * marks, @ and global
643 TAILQ_FOREACH(scrp
, &ep
->scrq
, eq
)
647 if (lno
<= scrp
->c_lno
)
648 scrp
->c_lno
= OOBLNO
;
651 if (lno
== scrp
->c_lno
)
652 scrp
->c_lno
= OOBLNO
;
658 if (ep
->c_nlines
!= OOBLNO
)
673 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
676 line_insdel(SCR
*sp
, lnop_t op
, db_recno_t lno
)
680 /* Update marks, @ and global commands. */
682 if (mark_insdel(sp
, op
, lno
))
684 if (ex_g_insdel(sp
, op
, lno
))
690 #ifdef USE_DB4_LOGGING
691 #define VI_DB_INIT_LOG DB_INIT_LOG
693 #define VI_DB_INIT_LOG 0
697 * PUBLIC: int db_setup __P((SCR *, EXF *));
700 db_setup(SCR
*sp
, EXF
*ep
)
702 char path
[MAXPATHLEN
];
706 (void)snprintf(path
, sizeof(path
), "%s/vi.XXXXXX", O_STR(sp
, O_RECDIR
));
707 if ((fd
= mkstemp(path
)) == -1) {
708 msgq(sp
, M_SYSERR
, "%s", path
);
713 if (mkdir(path
, S_IRWXU
)) {
714 msgq(sp
, M_SYSERR
, "%s", path
);
717 if (db_env_create(&env
, 0)) {
718 msgq(sp
, M_ERR
, "env_create");
721 #ifdef USE_DB4_LOGGING
722 if ((sp
->db_error
= vi_db_init_recover(env
))) {
723 msgq(sp
, M_DBERR
, "init_recover");
726 if ((sp
->db_error
= __vi_init_recover(env
))) {
727 msgq(sp
, M_DBERR
, "init_recover");
731 if ((sp
->db_error
= db_env_open(env
, path
,
732 DB_PRIVATE
| DB_CREATE
| DB_INIT_MPOOL
| VI_DB_THREAD
733 | VI_DB_INIT_LOG
, 0)) != 0) {
734 msgq(sp
, M_DBERR
, "env->open");
738 if ((ep
->env_path
= strdup(path
)) == NULL
) {
739 msgq(sp
, M_SYSERR
, NULL
);
749 /* Round up v to the nearest power of 2 */
750 static size_t round_up(size_t v
)
762 * PUBLIC: int db_msg_open __P((SCR *, const char *, DB **));
764 int db_msg_open(SCR
*sp
, const char *file
, DB
**dbp
)
766 return (sp
->db_error
= db_create(dbp
, 0, 0)) != 0 ||
767 (sp
->db_error
= (*dbp
)->set_re_source(*dbp
, file
)) != 0 ||
768 (sp
->db_error
= db_open(*dbp
, NULL
, DB_RECNO
, 0, 0)) != 0;
772 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
775 db_init(SCR
*sp
, EXF
*ep
, char *rcv_name
, char *oname
, size_t psize
, int *open_err
)
777 if (db_setup(sp
, ep
))
780 /* Open a db structure. */
781 if ((sp
->db_error
= db_create(&ep
->db
, 0, 0)) != 0) {
782 msgq(sp
, M_DBERR
, "db_create");
786 ep
->db
->set_re_delim(ep
->db
, '\n'); /* Always set. */
787 ep
->db
->set_pagesize(ep
->db
, round_up(psize
));
788 ep
->db
->set_flags(ep
->db
, DB_RENUMBER
| DB_SNAPSHOT
);
789 if (rcv_name
== NULL
)
790 ep
->db
->set_re_source(ep
->db
, oname
);
793 * Don't let db use mmap when using fcntl for locking
795 #ifdef HAVE_LOCK_FCNTL
796 #define NOMMAPIFFCNTL DB_NOMMAP
798 #define NOMMAPIFFCNTL 0
801 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
803 if ((sp
->db_error
= db_open(ep
->db
, ep
->rcv_path
, DB_RECNO
,
804 ((rcv_name
== 0) ? DB_TRUNCATE
: 0) | VI_DB_THREAD
| NOMMAPIFFCNTL
,
805 _DB_OPEN_MODE
)) != 0) {
807 M_DBERR
, rcv_name
== NULL
? oname
: rcv_name
, "%s");
810 * Historically, vi permitted users to edit files that couldn't
811 * be read. This isn't useful for single files from a command
812 * line, but it's quite useful for "vi *.c", since you can skip
813 * past files that you can't read.
815 ep
->db
= NULL
; /* Don't close it; it wasn't opened */
821 /* re_source is loaded into the database.
822 * Close it and reopen it in the environment.
824 if ((sp
->db_error
= ep
->db
->close(ep
->db
, 0))) {
825 msgq(sp
, M_DBERR
, "close");
828 if ((sp
->db_error
= db_create(&ep
->db
, ep
->env
, 0)) != 0) {
829 msgq(sp
, M_DBERR
, "db_create 2");
832 if ((sp
->db_error
= db_open(ep
->db
, ep
->rcv_path
, DB_RECNO
,
833 VI_DB_THREAD
| NOMMAPIFFCNTL
, _DB_OPEN_MODE
)) != 0) {
835 M_DBERR
, ep
->rcv_path
, "%s");