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>
31 static int append
__P((SCR
*, db_recno_t
, CHAR_T
*, size_t, lnop_t
, int));
35 * Front-end to db_get, special case handling for empty files.
37 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
40 db_eget(SCR
*sp
, db_recno_t lno
, CHAR_T
**pp
, size_t *lenp
, int *isemptyp
)
52 /* If the line exists, simply return it. */
53 if (!db_get(sp
, lno
, 0, pp
, lenp
))
57 * If the user asked for line 0 or line 1, i.e. the only possible
58 * line in an empty file, find the last line of the file; db_last
61 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
64 /* If the file isn't empty, fail loudly. */
65 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
78 * Look in the text buffers for a line, followed by the cache, followed
81 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
84 db_get(SCR
*sp
, db_recno_t lno
, u_int32_t flags
, CHAR_T
**pp
, size_t *lenp
)
85 /* Line number. */ /* Pointer store. */ /* Length store. */
96 * The underlying recno stuff handles zero by returning NULL, but
97 * have to have an OOB condition for the look-aside into the input
103 /* Check for no underlying file. */
104 if ((ep
= sp
->ep
) == NULL
) {
105 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
109 if (LF_ISSET(DBG_NOCACHE
))
113 * Look-aside into the TEXT buffers and see if the line we want
116 if (F_ISSET(sp
, SC_TINPUT
)) {
117 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
118 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
119 if (l1
<= lno
&& l2
>= lno
) {
120 #if defined(DEBUG) && 0
122 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
124 for (tp
= sp
->tiq
.cqh_first
;
125 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
133 * Adjust the line number for the number of lines used
134 * by the text input buffers.
140 /* Look-aside into the cache, and see if the line we want is there. */
141 if (lno
== sp
->c_lno
) {
142 #if defined(DEBUG) && 0
143 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
156 /* data.size contains length in bytes */
157 BINC_GOTO(sp
, CHAR_T
, sp
->c_lp
, sp
->c_blen
, nlen
);
159 /* Get the line from the underlying database. */
160 memset(&key
, 0, sizeof(key
));
162 key
.size
= sizeof(lno
);
163 memset(&data
, 0, sizeof(data
));
164 data
.data
= sp
->c_lp
;
165 data
.ulen
= sp
->c_blen
;
166 data
.flags
= DB_DBT_USERMEM
;
167 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
168 case DB_BUFFER_SMALL
:
174 err1
: if (LF_ISSET(DBG_FATAL
))
175 err2
: db_err(sp
, lno
);
177 err3
: if (lenp
!= NULL
)
186 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
187 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
188 F_SET(sp
, SC_CONV_ERROR
);
189 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
194 /* Reset the cache. */
195 if (wp
!= data
.data
) {
196 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
197 MEMCPYW(sp
->c_lp
, wp
, wlen
);
202 #if defined(DEBUG) && 0
203 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
214 * Delete a line from the file.
216 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
219 db_delete(SCR
*sp
, db_recno_t lno
)
224 #if defined(DEBUG) && 0
225 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
227 /* Check for no underlying file. */
228 if ((ep
= sp
->ep
) == NULL
) {
229 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
232 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
233 ex_emsg(sp
, NULL
, EXM_LOCKED
);
237 /* Update marks, @ and global commands. */
238 if (line_insdel(sp
, LINE_DELETE
, lno
))
241 /* Log before change. */
242 log_line(sp
, lno
, LOG_LINE_DELETE_B
);
245 memset(&key
, 0, sizeof(key
));
247 key
.size
= sizeof(lno
);
248 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
249 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
254 /* Flush the cache, update line count, before screen update. */
255 update_cache(sp
, LINE_DELETE
, lno
);
257 /* File now modified. */
258 if (F_ISSET(ep
, F_FIRSTMODIFY
))
260 F_SET(ep
, F_MODIFIED
);
262 /* Log after change. */
263 log_line(sp
, lno
, LOG_LINE_DELETE_F
);
266 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
269 /* maybe this could be simpler
271 * DB3 behaves differently from DB1
273 * if lno != 0 just go to lno and put the new line after it
274 * if lno == 0 then if there are any record, put in front of the first
275 * otherwise just append to the end thus creating the first
279 append(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
, lnop_t op
, int update
)
288 /* Check for no underlying file. */
289 if ((ep
= sp
->ep
) == NULL
) {
290 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
293 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
294 ex_emsg(sp
, NULL
, EXM_LOCKED
);
298 /* Log before change. */
299 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_B
);
302 memset(&key
, 0, sizeof(key
));
304 key
.size
= sizeof(lno
);
305 memset(&data
, 0, sizeof(data
));
307 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
310 INT2FILE(sp
, p
, len
, fp
, flen
);
313 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
318 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
320 (void)dbcp_put
->c_close(dbcp_put
);
323 ? "004|unable to append to line %lu"
324 : "005|unable to insert at line %lu",
329 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
330 if (sp
->db_error
!= DB_NOTFOUND
)
335 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
340 key
.size
= sizeof(lno
);
343 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
349 (void)dbcp_put
->c_close(dbcp_put
);
351 /* Flush the cache, update line count, before screen update. */
352 update_cache(sp
, LINE_INSERT
, lno
);
354 /* File now dirty. */
355 if (F_ISSET(ep
, F_FIRSTMODIFY
))
357 F_SET(ep
, F_MODIFIED
);
359 /* Log after change. */
360 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_F
);
362 /* Update marks, @ and global commands. */
363 rval
= line_insdel(sp
, LINE_INSERT
, lno
+ 1);
368 * comment copied from db_append
370 * Nasty hack. If multiple lines are input by the user, they aren't
371 * committed until an <ESC> is entered. The problem is the screen was
372 * updated/scrolled as each line was entered. So, when this routine
373 * is called to copy the new lines from the cut buffer into the file,
374 * it has to know not to update the screen again.
376 return (scr_update(sp
, lno
+ 1, LINE_INSERT
, update
) || rval
);
381 * Append a line into the file.
383 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
386 db_append(SCR
*sp
, int update
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
388 #if defined(DEBUG) && 0
389 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
393 return append(sp
, lno
, p
, len
, LINE_APPEND
, update
);
398 * Insert a line into the file.
400 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
403 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
405 #if defined(DEBUG) && 0
406 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
407 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
409 return append(sp
, lno
- 1, p
, len
, LINE_INSERT
, 1);
414 * Store a line in the file.
416 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
419 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
426 #if defined(DEBUG) && 0
427 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
428 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
430 /* Check for no underlying file. */
431 if ((ep
= sp
->ep
) == NULL
) {
432 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
435 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
436 ex_emsg(sp
, NULL
, EXM_LOCKED
);
440 /* Log before change. */
441 log_line(sp
, lno
, LOG_LINE_RESET_B
);
443 INT2FILE(sp
, p
, len
, fp
, flen
);
446 memset(&key
, 0, sizeof(key
));
448 key
.size
= sizeof(lno
);
449 memset(&data
, 0, sizeof(data
));
452 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
453 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
457 /* Flush the cache, update line count, before screen update. */
458 update_cache(sp
, LINE_RESET
, lno
);
460 /* File now dirty. */
461 if (F_ISSET(ep
, F_FIRSTMODIFY
))
463 F_SET(ep
, F_MODIFIED
);
465 /* Log after change. */
466 log_line(sp
, lno
, LOG_LINE_RESET_F
);
469 return (scr_update(sp
, lno
, LINE_RESET
, 1));
474 * Return if a line exists.
476 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
479 db_exist(SCR
*sp
, db_recno_t lno
)
483 /* Check for no underlying file. */
484 if ((ep
= sp
->ep
) == NULL
) {
485 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
493 * Check the last-line number cache. Adjust the cached line
494 * number for the lines used by the text input buffers.
496 if (ep
->c_nlines
!= OOBLNO
)
497 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
498 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
499 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
501 /* Go get the line. */
502 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
507 * Return the number of lines in the file.
509 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
512 db_last(SCR
*sp
, db_recno_t
*lnop
)
521 /* Check for no underlying file. */
522 if ((ep
= sp
->ep
) == NULL
) {
523 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
528 * Check the last-line number cache. Adjust the cached line
529 * number for the lines used by the text input buffers.
531 if (ep
->c_nlines
!= OOBLNO
) {
532 *lnop
= ep
->c_nlines
;
533 if (F_ISSET(sp
, SC_TINPUT
))
534 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
535 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
539 memset(&key
, 0, sizeof(key
));
541 key
.size
= sizeof(lno
);
542 memset(&data
, 0, sizeof(data
));
544 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
546 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
551 (void)dbcp
->c_close(dbcp
);
554 msgq(sp
, M_DBERR
, "007|unable to get last line");
561 memcpy(&lno
, key
.data
, sizeof(lno
));
563 if (lno
!= sp
->c_lno
) {
564 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
566 /* Fill the cache. */
567 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
568 MEMCPYW(sp
->c_lp
, wp
, wlen
);
574 (void)dbcp
->c_close(dbcp
);
576 /* Return the value. */
577 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
578 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
579 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
585 * Report a line error.
587 * PUBLIC: void db_err __P((SCR *, db_recno_t));
590 db_err(SCR
*sp
, db_recno_t lno
)
593 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
598 * Update all of the screens that are backed by the file that
601 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
602 * PUBLIC: lnop_t op, int current));
605 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
611 if (F_ISSET(sp
, SC_EX
))
614 /* XXXX goes outside of window */
617 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
619 for (tsp
= wp
->scrq
.cqh_first
;
620 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
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 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
644 scrp
= scrp
->eq
.cqe_next
)
648 if (lno
<= scrp
->c_lno
)
649 scrp
->c_lno
= OOBLNO
;
652 if (lno
== scrp
->c_lno
)
653 scrp
->c_lno
= OOBLNO
;
657 if (ep
->c_nlines
!= OOBLNO
)
669 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
672 line_insdel(SCR
*sp
, lnop_t op
, db_recno_t lno
)
676 /* Update marks, @ and global commands. */
678 if (mark_insdel(sp
, op
, lno
))
680 if (ex_g_insdel(sp
, op
, lno
))
686 #ifdef USE_DB4_LOGGING
687 #define VI_DB_INIT_LOG DB_INIT_LOG
689 #define VI_DB_INIT_LOG 0
693 * PUBLIC: int db_setup __P((SCR *, EXF *));
696 db_setup(SCR
*sp
, EXF
*ep
)
698 char path
[MAXPATHLEN
];
702 (void)snprintf(path
, sizeof(path
), "%s/vi.XXXXXX", O_STR(sp
, O_RECDIR
));
703 if ((fd
= mkstemp(path
)) == -1) {
704 msgq(sp
, M_SYSERR
, "%s", path
);
709 if (mkdir(path
, S_IRWXU
)) {
710 msgq(sp
, M_SYSERR
, "%s", path
);
713 if (db_env_create(&env
, 0)) {
714 msgq(sp
, M_ERR
, "env_create");
717 #ifdef USE_DB4_LOGGING
718 if ((sp
->db_error
= vi_db_init_recover(env
))) {
719 msgq(sp
, M_DBERR
, "init_recover");
722 if ((sp
->db_error
= __vi_init_recover(env
))) {
723 msgq(sp
, M_DBERR
, "init_recover");
727 if ((sp
->db_error
= db_env_open(env
, path
,
728 DB_PRIVATE
| DB_CREATE
| DB_INIT_MPOOL
| VI_DB_THREAD
729 | VI_DB_INIT_LOG
, 0)) != 0) {
730 msgq(sp
, M_DBERR
, "env->open");
734 if ((ep
->env_path
= strdup(path
)) == NULL
) {
735 msgq(sp
, M_SYSERR
, NULL
);
745 /* Round up v to the nearest power of 2 */
746 static size_t round_up(size_t v
)
758 * PUBLIC: int db_msg_open __P((SCR *, char *, DB **));
760 int db_msg_open(SCR
*sp
, char *file
, DB
**dbp
)
762 return (sp
->db_error
= db_create(dbp
, 0, 0)) != 0 ||
763 (sp
->db_error
= (*dbp
)->set_re_source(*dbp
, file
)) != 0 ||
764 (sp
->db_error
= db_open(*dbp
, NULL
, DB_RECNO
, 0, 0)) != 0;
768 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
771 db_init(SCR
*sp
, EXF
*ep
, char *rcv_name
, char *oname
, size_t psize
, int *open_err
)
773 if (db_setup(sp
, ep
))
776 /* Open a db structure. */
777 if ((sp
->db_error
= db_create(&ep
->db
, 0, 0)) != 0) {
778 msgq(sp
, M_DBERR
, "db_create");
782 ep
->db
->set_re_delim(ep
->db
, '\n'); /* Always set. */
783 ep
->db
->set_pagesize(ep
->db
, round_up(psize
));
784 ep
->db
->set_flags(ep
->db
, DB_RENUMBER
| DB_SNAPSHOT
);
785 if (rcv_name
== NULL
)
786 ep
->db
->set_re_source(ep
->db
, oname
);
789 * Don't let db use mmap when using fcntl for locking
791 #ifdef HAVE_LOCK_FCNTL
792 #define NOMMAPIFFCNTL DB_NOMMAP
794 #define NOMMAPIFFCNTL 0
797 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
799 if ((sp
->db_error
= db_open(ep
->db
, ep
->rcv_path
, DB_RECNO
,
800 ((rcv_name
== 0) ? DB_TRUNCATE
: 0) | VI_DB_THREAD
| NOMMAPIFFCNTL
,
801 _DB_OPEN_MODE
)) != 0) {
803 M_DBERR
, rcv_name
== NULL
? oname
: rcv_name
, "%s");
806 * Historically, vi permitted users to edit files that couldn't
807 * be read. This isn't useful for single files from a command
808 * line, but it's quite useful for "vi *.c", since you can skip
809 * past files that you can't read.
811 ep
->db
= NULL
; /* Don't close it; it wasn't opened */
817 /* re_source is loaded into the database.
818 * Close it and reopen it in the environment.
820 if ((sp
->db_error
= ep
->db
->close(ep
->db
, 0))) {
821 msgq(sp
, M_DBERR
, "close");
824 if ((sp
->db_error
= db_create(&ep
->db
, ep
->env
, 0)) != 0) {
825 msgq(sp
, M_DBERR
, "db_create 2");
828 if ((sp
->db_error
= db_open(ep
->db
, ep
->rcv_path
, DB_RECNO
,
829 VI_DB_THREAD
| NOMMAPIFFCNTL
, _DB_OPEN_MODE
)) != 0) {
831 M_DBERR
, ep
->rcv_path
, "%s");