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 static int append
__P((SCR
*, db_recno_t
, CHAR_T
*, size_t, lnop_t
, int));
34 * Front-end to db_get, special case handling for empty files.
36 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
39 db_eget(SCR
*sp
, db_recno_t lno
, CHAR_T
**pp
, size_t *lenp
, int *isemptyp
)
51 /* If the line exists, simply return it. */
52 if (!db_get(sp
, lno
, 0, pp
, lenp
))
56 * If the user asked for line 0 or line 1, i.e. the only possible
57 * line in an empty file, find the last line of the file; db_last
60 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
63 /* If the file isn't empty, fail loudly. */
64 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
77 * Look in the text buffers for a line, followed by the cache, followed
80 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
83 db_get(SCR
*sp
, db_recno_t lno
, u_int32_t flags
, CHAR_T
**pp
, size_t *lenp
)
84 /* Line number. */ /* Pointer store. */ /* Length store. */
95 * The underlying recno stuff handles zero by returning NULL, but
96 * have to have an OOB condition for the look-aside into the input
102 /* Check for no underlying file. */
103 if ((ep
= sp
->ep
) == NULL
) {
104 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
108 if (LF_ISSET(DBG_NOCACHE
))
112 * Look-aside into the TEXT buffers and see if the line we want
115 if (F_ISSET(sp
, SC_TINPUT
)) {
116 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
117 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
118 if (l1
<= lno
&& l2
>= lno
) {
119 #if defined(DEBUG) && 0
121 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
123 for (tp
= sp
->tiq
.cqh_first
;
124 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
132 * Adjust the line number for the number of lines used
133 * by the text input buffers.
139 /* Look-aside into the cache, and see if the line we want is there. */
140 if (lno
== sp
->c_lno
) {
141 #if defined(DEBUG) && 0
142 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
155 /* data.size contains length in bytes */
156 BINC_GOTO(sp
, CHAR_T
, sp
->c_lp
, sp
->c_blen
, nlen
);
158 /* Get the line from the underlying database. */
159 memset(&key
, 0, sizeof(key
));
161 key
.size
= sizeof(lno
);
162 memset(&data
, 0, sizeof(data
));
163 data
.data
= sp
->c_lp
;
164 data
.ulen
= sp
->c_blen
;
165 data
.flags
= DB_DBT_USERMEM
;
166 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
167 case DB_BUFFER_SMALL
:
173 err1
: if (LF_ISSET(DBG_FATAL
))
174 err2
: db_err(sp
, lno
);
176 err3
: if (lenp
!= NULL
)
185 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
186 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
187 F_SET(sp
, SC_CONV_ERROR
);
188 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
193 /* Reset the cache. */
194 if (wp
!= data
.data
) {
195 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
196 MEMCPYW(sp
->c_lp
, wp
, wlen
);
201 #if defined(DEBUG) && 0
202 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
213 * Delete a line from the file.
215 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
218 db_delete(SCR
*sp
, db_recno_t lno
)
223 #if defined(DEBUG) && 0
224 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
226 /* Check for no underlying file. */
227 if ((ep
= sp
->ep
) == NULL
) {
228 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
231 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
232 ex_emsg(sp
, NULL
, EXM_LOCKED
);
236 /* Update marks, @ and global commands. */
237 if (line_insdel(sp
, LINE_DELETE
, lno
))
240 /* Log before change. */
241 log_line(sp
, lno
, LOG_LINE_DELETE_B
);
244 memset(&key
, 0, sizeof(key
));
246 key
.size
= sizeof(lno
);
247 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
248 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
253 /* Flush the cache, update line count, before screen update. */
254 update_cache(sp
, LINE_DELETE
, lno
);
256 /* File now modified. */
257 if (F_ISSET(ep
, F_FIRSTMODIFY
))
259 F_SET(ep
, F_MODIFIED
);
261 /* Log after change. */
262 log_line(sp
, lno
, LOG_LINE_DELETE_F
);
265 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
268 /* maybe this could be simpler
270 * DB3 behaves differently from DB1
272 * if lno != 0 just go to lno and put the new line after it
273 * if lno == 0 then if there are any record, put in front of the first
274 * otherwise just append to the end thus creating the first
278 append(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
, lnop_t op
, int update
)
287 /* Check for no underlying file. */
288 if ((ep
= sp
->ep
) == NULL
) {
289 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
292 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
293 ex_emsg(sp
, NULL
, EXM_LOCKED
);
297 /* Log before change. */
298 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_B
);
301 memset(&key
, 0, sizeof(key
));
303 key
.size
= sizeof(lno
);
304 memset(&data
, 0, sizeof(data
));
306 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
309 INT2FILE(sp
, p
, len
, fp
, flen
);
312 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
317 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
319 (void)dbcp_put
->c_close(dbcp_put
);
322 ? "004|unable to append to line %lu"
323 : "005|unable to insert at line %lu",
328 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
329 if (sp
->db_error
!= DB_NOTFOUND
)
334 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
339 key
.size
= sizeof(lno
);
342 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
348 (void)dbcp_put
->c_close(dbcp_put
);
350 /* Flush the cache, update line count, before screen update. */
351 update_cache(sp
, LINE_INSERT
, lno
);
353 /* File now dirty. */
354 if (F_ISSET(ep
, F_FIRSTMODIFY
))
356 F_SET(ep
, F_MODIFIED
);
358 /* Log after change. */
359 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_F
);
361 /* Update marks, @ and global commands. */
362 rval
= line_insdel(sp
, LINE_INSERT
, lno
+ 1);
367 * comment copied from db_append
369 * Nasty hack. If multiple lines are input by the user, they aren't
370 * committed until an <ESC> is entered. The problem is the screen was
371 * updated/scrolled as each line was entered. So, when this routine
372 * is called to copy the new lines from the cut buffer into the file,
373 * it has to know not to update the screen again.
375 return (scr_update(sp
, lno
+ 1, LINE_INSERT
, update
) || rval
);
380 * Append a line into the file.
382 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
385 db_append(SCR
*sp
, int update
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
387 #if defined(DEBUG) && 0
388 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
392 return append(sp
, lno
, p
, len
, LINE_APPEND
, update
);
397 * Insert a line into the file.
399 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
402 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
404 #if defined(DEBUG) && 0
405 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
406 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
408 return append(sp
, lno
- 1, p
, len
, LINE_INSERT
, 1);
413 * Store a line in the file.
415 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
418 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
425 #if defined(DEBUG) && 0
426 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
427 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
429 /* Check for no underlying file. */
430 if ((ep
= sp
->ep
) == NULL
) {
431 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
434 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
435 ex_emsg(sp
, NULL
, EXM_LOCKED
);
439 /* Log before change. */
440 log_line(sp
, lno
, LOG_LINE_RESET_B
);
442 INT2FILE(sp
, p
, len
, fp
, flen
);
445 memset(&key
, 0, sizeof(key
));
447 key
.size
= sizeof(lno
);
448 memset(&data
, 0, sizeof(data
));
451 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
452 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
456 /* Flush the cache, update line count, before screen update. */
457 update_cache(sp
, LINE_RESET
, lno
);
459 /* File now dirty. */
460 if (F_ISSET(ep
, F_FIRSTMODIFY
))
462 F_SET(ep
, F_MODIFIED
);
464 /* Log after change. */
465 log_line(sp
, lno
, LOG_LINE_RESET_F
);
468 return (scr_update(sp
, lno
, LINE_RESET
, 1));
473 * Return if a line exists.
475 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
478 db_exist(SCR
*sp
, db_recno_t lno
)
482 /* Check for no underlying file. */
483 if ((ep
= sp
->ep
) == NULL
) {
484 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
492 * Check the last-line number cache. Adjust the cached line
493 * number for the lines used by the text input buffers.
495 if (ep
->c_nlines
!= OOBLNO
)
496 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
497 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
498 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
500 /* Go get the line. */
501 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
506 * Return the number of lines in the file.
508 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
511 db_last(SCR
*sp
, db_recno_t
*lnop
)
520 /* Check for no underlying file. */
521 if ((ep
= sp
->ep
) == NULL
) {
522 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
527 * Check the last-line number cache. Adjust the cached line
528 * number for the lines used by the text input buffers.
530 if (ep
->c_nlines
!= OOBLNO
) {
531 *lnop
= ep
->c_nlines
;
532 if (F_ISSET(sp
, SC_TINPUT
))
533 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
534 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
538 memset(&key
, 0, sizeof(key
));
540 key
.size
= sizeof(lno
);
541 memset(&data
, 0, sizeof(data
));
543 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
545 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
550 (void)dbcp
->c_close(dbcp
);
553 msgq(sp
, M_DBERR
, "007|unable to get last line");
560 memcpy(&lno
, key
.data
, sizeof(lno
));
562 if (lno
!= sp
->c_lno
) {
563 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
565 /* Fill the cache. */
566 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
567 MEMCPYW(sp
->c_lp
, wp
, wlen
);
573 (void)dbcp
->c_close(dbcp
);
575 /* Return the value. */
576 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
577 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
578 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
584 * Report a line error.
586 * PUBLIC: void db_err __P((SCR *, db_recno_t));
589 db_err(SCR
*sp
, db_recno_t lno
)
592 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
597 * Update all of the screens that are backed by the file that
600 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
601 * PUBLIC: lnop_t op, int current));
604 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
610 if (F_ISSET(sp
, SC_EX
))
613 /* XXXX goes outside of window */
616 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
618 for (tsp
= wp
->scrq
.cqh_first
;
619 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
620 if (sp
!= tsp
&& tsp
->ep
== ep
)
621 if (vs_change(tsp
, lno
, op
))
623 return (current
? vs_change(sp
, lno
, op
) : 0);
627 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
630 update_cache(SCR
*sp
, lnop_t op
, db_recno_t lno
)
637 /* Flush the cache, update line count, before screen update. */
638 /* The flushing is probably not needed, since it was incorrect
639 * for db_insert. It might be better to adjust it, like
640 * marks, @ and global
642 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
643 scrp
= scrp
->eq
.cqe_next
)
647 if (lno
<= scrp
->c_lno
)
648 scrp
->c_lno
= OOBLNO
;
651 if (lno
== scrp
->c_lno
)
652 scrp
->c_lno
= OOBLNO
;
656 if (ep
->c_nlines
!= OOBLNO
)
668 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
671 line_insdel(SCR
*sp
, lnop_t op
, db_recno_t lno
)
675 /* Update marks, @ and global commands. */
677 if (mark_insdel(sp
, op
, lno
))
679 if (ex_g_insdel(sp
, op
, lno
))
685 #ifdef USE_DB4_LOGGING
686 #define VI_DB_INIT_LOG DB_INIT_LOG
688 #define VI_DB_INIT_LOG 0
692 * PUBLIC: int db_setup __P((SCR *, EXF *));
695 db_setup(SCR
*sp
, EXF
*ep
)
697 char path
[MAXPATHLEN
];
701 (void)snprintf(path
, sizeof(path
), "%s/vi.XXXXXX", O_STR(sp
, O_RECDIR
));
702 if ((fd
= mkstemp(path
)) == -1) {
703 msgq(sp
, M_SYSERR
, "%s", path
);
708 if (mkdir(path
, S_IRWXU
)) {
709 msgq(sp
, M_SYSERR
, "%s", path
);
712 if (db_env_create(&env
, 0)) {
713 msgq(sp
, M_ERR
, "env_create");
716 #ifdef USE_DB4_LOGGING
717 if ((sp
->db_error
= vi_db_init_recover(env
))) {
718 msgq(sp
, M_DBERR
, "init_recover");
721 if ((sp
->db_error
= __vi_init_recover(env
))) {
722 msgq(sp
, M_DBERR
, "init_recover");
726 if ((sp
->db_error
= db_env_open(env
, path
,
727 DB_PRIVATE
| DB_CREATE
| DB_INIT_MPOOL
| VI_DB_THREAD
728 | VI_DB_INIT_LOG
, 0)) != 0) {
729 msgq(sp
, M_DBERR
, "env->open");
733 if ((ep
->env_path
= strdup(path
)) == NULL
) {
734 msgq(sp
, M_SYSERR
, NULL
);
744 /* Round up v to the nearest power of 2 */
745 static size_t round_up(size_t v
)
757 * PUBLIC: int db_msg_open __P((SCR *, char *, DB **));
759 int db_msg_open(SCR
*sp
, char *file
, DB
**dbp
)
761 return (sp
->db_error
= db_create(dbp
, 0, 0)) != 0 ||
762 (sp
->db_error
= (*dbp
)->set_re_source(*dbp
, file
)) != 0 ||
763 (sp
->db_error
= db_open(*dbp
, NULL
, DB_RECNO
, 0, 0)) != 0;
767 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
770 db_init(SCR
*sp
, EXF
*ep
, char *rcv_name
, char *oname
, size_t psize
, int *open_err
)
772 if (db_setup(sp
, ep
))
775 /* Open a db structure. */
776 if ((sp
->db_error
= db_create(&ep
->db
, 0, 0)) != 0) {
777 msgq(sp
, M_DBERR
, "db_create");
781 ep
->db
->set_re_delim(ep
->db
, '\n'); /* Always set. */
782 ep
->db
->set_pagesize(ep
->db
, round_up(psize
));
783 ep
->db
->set_flags(ep
->db
, DB_RENUMBER
| DB_SNAPSHOT
);
784 if (rcv_name
== NULL
)
785 ep
->db
->set_re_source(ep
->db
, oname
);
788 * Don't let db use mmap when using fcntl for locking
790 #ifdef HAVE_LOCK_FCNTL
791 #define NOMMAPIFFCNTL DB_NOMMAP
793 #define NOMMAPIFFCNTL 0
796 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
798 if ((sp
->db_error
= db_open(ep
->db
, ep
->rcv_path
, DB_RECNO
,
799 ((rcv_name
== 0) ? DB_TRUNCATE
: 0) | VI_DB_THREAD
| NOMMAPIFFCNTL
,
800 _DB_OPEN_MODE
)) != 0) {
802 M_DBERR
, rcv_name
== NULL
? oname
: rcv_name
, "%s");
805 * Historically, vi permitted users to edit files that couldn't
806 * be read. This isn't useful for single files from a command
807 * line, but it's quite useful for "vi *.c", since you can skip
808 * past files that you can't read.
810 ep
->db
= NULL
; /* Don't close it; it wasn't opened */
816 /* re_source is loaded into the database.
817 * Close it and reopen it in the environment.
819 if ((sp
->db_error
= ep
->db
->close(ep
->db
, 0))) {
820 msgq(sp
, M_DBERR
, "close");
823 if ((sp
->db_error
= db_create(&ep
->db
, ep
->env
, 0)) != 0) {
824 msgq(sp
, M_DBERR
, "db_create 2");
827 if ((sp
->db_error
= db_open(ep
->db
, ep
->rcv_path
, DB_RECNO
,
828 VI_DB_THREAD
| NOMMAPIFFCNTL
, _DB_OPEN_MODE
)) != 0) {
830 M_DBERR
, ep
->rcv_path
, "%s");