/* * log_init -- * Initialize the logging subsystem. * * PUBLIC: int log_init __P((SCR *, EXF *)); */ int log_init(SCR *sp, EXF *ep) { DB_LOGC *logc; DBT data; size_t nlen; /* * !!! * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER. * * Initialize the buffer. The logging subsystem has its own * buffers because the global ones are almost by definition * going to be in use when the log runs. */ sp->wp->l_lp = NULL; sp->wp->l_len = 0; ep->l_cursor.lno = 1; /* XXX Any valid recno. */ ep->l_cursor.cno = 0; ep->l_high = ep->l_cur = 1; if ((sp->db_error = ep->env->log_cursor(ep->env, &logc, 0)) != 0) { msgq(sp, M_DBERR, "env->log_cursor"); F_SET(ep, F_NOLOG); return (1); } nlen = 1024; retry: BINC_GOTO(sp, sp->wp->l_lp, sp->wp->l_len, nlen); memset(&data, 0, sizeof(data)); data.data = sp->wp->l_lp; data.ulen = sp->wp->l_len; data.flags = DB_DBT_USERMEM; switch ((sp->db_error = logc->get(logc, &ep->lsn_first, &data, DB_LAST))) { case ENOMEM: nlen = data.size; goto retry; default: alloc_err: msgq(sp, M_DBERR, "logc->get"); F_SET(ep, F_NOLOG); return (1); case 0: ; } MEMCPY(&ep->lsn_cur, &ep->lsn_first, 1); MEMCPY(&ep->lsn_high, &ep->lsn_first, 1); logc->close(logc, 0); ep->l_win = NULL; /*LOCK_INIT(sp->wp, ep);*/ return (0); }
/* * db_get -- * Look in the text buffers for a line, followed by the cache, followed * by the database. * * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *)); */ int db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp) /* Line number. */ /* Pointer store. */ /* Length store. */ { DBT data, key; EXF *ep; TEXT *tp; db_recno_t l1, l2; const CHAR_T *wp; size_t wlen; size_t nlen; /* * The underlying recno stuff handles zero by returning NULL, but * have to have an OOB condition for the look-aside into the input * buffer anyway. */ if (lno == 0) goto err1; /* Check for no underlying file. */ if ((ep = sp->ep) == NULL) { ex_emsg(sp, NULL, EXM_NOFILEYET); goto err3; } if (LF_ISSET(DBG_NOCACHE)) goto nocache; /* * Look-aside into the TEXT buffers and see if the line we want * is there. */ if (F_ISSET(sp, SC_TINPUT)) { l1 = ((TEXT *)sp->tiq.cqh_first)->lno; l2 = ((TEXT *)sp->tiq.cqh_last)->lno; if (l1 <= lno && l2 >= lno) { #if defined(DEBUG) && 0 vtrace(sp, "retrieve TEXT buffer line %lu\n", (u_long)lno); #endif for (tp = sp->tiq.cqh_first; tp->lno != lno; tp = tp->q.cqe_next); if (lenp != NULL) *lenp = tp->len; if (pp != NULL) *pp = tp->lb; return (0); } /* * Adjust the line number for the number of lines used * by the text input buffers. */ if (lno > l2) lno -= l2 - l1; } /* Look-aside into the cache, and see if the line we want is there. */ if (lno == sp->c_lno) { #if defined(DEBUG) && 0 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno); #endif if (lenp != NULL) *lenp = sp->c_len; if (pp != NULL) *pp = sp->c_lp; return (0); } sp->c_lno = OOBLNO; nocache: nlen = 1024; retry: /* data.size contains length in bytes */ BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen); /* Get the line from the underlying database. */ memset(&key, 0, sizeof(key)); key.data = &lno; key.size = sizeof(lno); memset(&data, 0, sizeof(data)); data.data = sp->c_lp; data.ulen = sp->c_blen; data.flags = DB_DBT_USERMEM; switch (ep->db->get(ep->db, NULL, &key, &data, 0)) { case DB_BUFFER_SMALL: nlen = data.size; goto retry; default: goto err2; case DB_NOTFOUND: err1: if (LF_ISSET(DBG_FATAL)) err2: db_err(sp, lno); alloc_err: err3: if (lenp != NULL) *lenp = 0; if (pp != NULL) *pp = NULL; return (1); case 0: ; } if (FILE2INT(sp, data.data, data.size, wp, wlen)) { if (!F_ISSET(sp, SC_CONV_ERROR)) { F_SET(sp, SC_CONV_ERROR); msgq(sp, M_ERR, "324|Conversion error on line %d", lno); } goto err3; } /* Reset the cache. */ if (wp != data.data) { BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen); MEMCPYW(sp->c_lp, wp, wlen); } sp->c_lno = lno; sp->c_len = wlen; #if defined(DEBUG) && 0 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno); #endif if (lenp != NULL) *lenp = wlen; if (pp != NULL) *pp = sp->c_lp; return (0); }