Annotation of src/usr.bin/cvs/hist.c, Revision 1.14
1.14 ! ray 1: /* $OpenBSD: hist.c,v 1.13 2006/01/02 08:11:56 xsa Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.3 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.3 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.3 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.3 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.3 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
1.13 xsa 27: #include "includes.h"
1.1 jfb 28:
1.5 xsa 29: #include "cvs.h"
1.1 jfb 30: #include "log.h"
31:
1.7 xsa 32: #define CVS_HIST_BUFSIZE 8192
1.1 jfb 33:
34:
35:
1.7 xsa 36: static int cvs_hist_fillbuf(CVSHIST *);
37: static int cvs_hist_fmt(const struct cvs_hent *, char *, size_t);
1.1 jfb 38:
39:
40: /*
41: * cvs_hist_open()
42: *
43: * Open a CVS history file.
44: * Returns the number of entries in the file on success, or -1 on error.
45: */
1.8 xsa 46: CVSHIST *
1.1 jfb 47: cvs_hist_open(const char *path)
48: {
49: CVSHIST *histp;
50:
1.14 ! ray 51: histp = xcalloc(1, sizeof(*histp));
1.1 jfb 52:
1.14 ! ray 53: histp->chf_buf = xmalloc((size_t)CVS_HIST_BUFSIZE);
1.1 jfb 54: histp->chf_blen = CVS_HIST_BUFSIZE;
55: histp->chf_off = 0;
56:
57: histp->chf_sindex = 0;
58: histp->chf_cindex = 0;
59: histp->chf_nbhent = 0;
1.11 xsa 60:
61: cvs_log(LP_TRACE, "cvs_hist_open(%s)", path);
1.1 jfb 62:
63: histp->chf_fd = open(path, O_RDONLY, 0);
64: if (histp->chf_fd == -1) {
65: cvs_log(LP_ERRNO,
66: "failed to open CVS history file `%s'", path);
1.9 xsa 67: cvs_nolog = 1;
1.12 joris 68: xfree(histp->chf_buf);
69: xfree(histp);
1.1 jfb 70: return (NULL);
71: }
72:
73: cvs_hist_fillbuf(histp);
74:
75: return (histp);
76: }
77:
78:
79: /*
80: * cvs_hist_close()
81: *
82: * Close the CVS history file previously opened by a call to cvs_hist_open()
83: */
84: void
85: cvs_hist_close(CVSHIST *histp)
86: {
87: if (histp->chf_fd >= 0)
88: (void)close(histp->chf_fd);
1.12 joris 89: xfree(histp->chf_buf);
90: xfree(histp);
1.1 jfb 91: }
92:
93:
94: /*
95: * cvs_hist_getnext()
96: *
97: * Get the next entry from the history file <histp>. Whenever using this
98: * function, it should be assumed that the return value of the previous call
99: * to cvs_hist_getnext() is now invalid.
100: * Returns the next entry from the file on success, or NULL on failure or if
101: * no entries are left.
102: */
1.8 xsa 103: struct cvs_hent *
1.1 jfb 104: cvs_hist_getnext(CVSHIST *histp)
105: {
106: if (histp->chf_cindex == histp->chf_nbhent) {
107: /* no more cached entries, refill buf and parse */
108: cvs_hist_fillbuf(histp);
109: cvs_hist_parse(histp);
110: }
111: return (&(histp->chf_hent[histp->chf_cindex++]));
112: }
113:
114:
115: /*
116: * cvs_hist_append()
117: *
118: * Append a history entry to the history file <histp>. The file offset is
119: * first set to the end of the file.
120: * Returns 0 on success, or -1 on failure.
121: */
122: int
123: cvs_hist_append(CVSHIST *histp, struct cvs_hent *hentp)
124: {
125: char hbuf[128];
1.9 xsa 126:
127: if (cvs_nolog == 1)
128: return (0);
1.1 jfb 129:
130: if (cvs_hist_fmt(hentp, hbuf, sizeof(hbuf)) < 0) {
131: cvs_log(LP_ERR, "failed to append CVS history entry");
132: return (-1);
133: }
134:
135: /* position ourself at the end */
136: if (lseek(histp->chf_fd, (off_t)0, SEEK_END) == -1) {
137: cvs_log(LP_ERRNO, "failed to seek to end of CVS history file");
138: return (-1);
139: }
140:
141: if (write(histp->chf_fd, hbuf, strlen(hbuf)) == -1) {
142: cvs_log(LP_ERR, "failed to write CVS history entry to file");
143: return (-1);
144: }
145:
146: return (0);
147: }
148:
149:
150:
151: /*
152: * cvs_hist_fillbuf()
153: *
154: * Fill the history file's internal buffer for future parsing.
155: */
156: static int
157: cvs_hist_fillbuf(CVSHIST *histp)
158: {
159: ssize_t ret;
160:
161: /* reposition ourself in case we're missing the start of a record */
162: if (lseek(histp->chf_fd, histp->chf_off, SEEK_SET) == -1) {
163: cvs_log(LP_ERRNO, "failed to seek in CVS history file");
164: return (-1);
165: }
166: ret = read(histp->chf_fd, histp->chf_buf, histp->chf_blen);
167: if (ret == -1) {
168: cvs_log(LP_ERRNO, "failed to buffer CVS history file");
169: return (-1);
1.2 deraadt 170: } else {
1.1 jfb 171: histp->chf_bused = (size_t)ret;
172: }
173:
174: return (ret);
175: }
176:
177:
178: /*
179: * cvs_hist_parse()
180: *
181: * Parse the current contents of the internal buffer of <histp> and regenerate
182: * the buffered history entries.
183: * Returns the number of entries parsed on success, or -1 on failure.
184: */
185: int
186: cvs_hist_parse(CVSHIST *histp)
187: {
188: u_int i, fld;
189: char *fields[CVS_HIST_NBFLD], *sp, *bep, *ep, *errp;
190:
191: sp = histp->chf_buf;
192: bep = histp->chf_buf + histp->chf_bused - 1;
193:
194: for (i = 0; i < CVS_HIST_CACHE; i++) {
195: ep = memchr(sp, '\n', bep - sp);
196: if (ep == NULL) {
197: /*
198: * No record or incomplete record left to parse,
199: * so adjust the next read offset in consequence.
200: */
201: histp->chf_off += (off_t)(sp - histp->chf_buf);
202: break;
1.2 deraadt 203: } else if (ep == bep) {
1.1 jfb 204: histp->chf_off += (off_t)histp->chf_bused;
205: }
206: *(ep++) = '\0';
207:
208: printf("hist(%s)\n", sp);
209:
210: histp->chf_hent[i].ch_event = *sp++;
211:
212: /* split the record in fields */
213: fields[0] = sp;
214:
215: fld = 1;
216: while (sp < ep) {
217: if (*sp == '|') {
218: *sp = '\0';
219: fields[fld++] = sp + 1;
220: }
221: if (fld == CVS_HIST_NBFLD)
222: break;
223: sp++;
224: }
225: #if 0
226: for (fld = 0; fld < CVS_HIST_NBFLD; fld++)
227: printf("fields[%u] = `%s'\n", fld, fields[fld]);
228: #endif
229:
230: histp->chf_hent[i].ch_date = (time_t)strtol(fields[0],
231: &errp, 16);
232: if (*errp != '\0') {
233: cvs_log(LP_ERR,
234: "parse error in date field of CVS history entry");
235: continue;
236: }
237:
238: histp->chf_hent[i].ch_user = fields[1];
239: histp->chf_hent[i].ch_curdir = fields[2];
240: histp->chf_hent[i].ch_repo = fields[3];
241: histp->chf_hent[i].ch_rev = rcsnum_alloc();
242: rcsnum_aton(fields[4], NULL, histp->chf_hent[i].ch_rev);
243: histp->chf_hent[i].ch_arg = fields[5];
244: sp = ep;
245: }
246:
247: /* update indexes */
248: histp->chf_sindex += histp->chf_nbhent;
249: histp->chf_nbhent = i;
250: histp->chf_cindex = 0;
251:
252:
253: return (i);
254: }
255:
256:
257: /*
258: * cvs_hist_fmt()
259: *
260: * Format the contents of the CVS history entry <ent> into the format used in
261: * the CVS `history' file, and store the resulting string in <buf>, which is
262: * of size <blen>.
263: */
264: static int
265: cvs_hist_fmt(const struct cvs_hent *ent, char *buf, size_t blen)
266: {
267: char numbuf[64];
1.4 joris 268: int len;
1.1 jfb 269:
1.3 tedu 270: if (rcsnum_tostr(ent->ch_rev, numbuf, sizeof(numbuf)) == NULL)
1.1 jfb 271: return (-1);
272:
1.4 joris 273: len = snprintf(buf, blen, "%c%8x|%s|%s|%s|%s|%s",
1.1 jfb 274: ent->ch_event, ent->ch_date, ent->ch_user, ent->ch_curdir,
1.4 joris 275: ent->ch_repo, numbuf, ent->ch_arg);
276: if (len >= (int)blen || len == -1)
277: return (-1);
278: return (len);
1.1 jfb 279: }