Annotation of src/usr.bin/cvs/hist.c, Revision 1.11
1.11 ! xsa 1: /* $OpenBSD: hist.c,v 1.10 2005/08/14 19:49:18 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:
27: #include <sys/stat.h>
28:
1.5 xsa 29: #include <fcntl.h>
1.1 jfb 30: #include <stdio.h>
31: #include <stdlib.h>
1.5 xsa 32: #include <string.h>
1.1 jfb 33: #include <unistd.h>
34:
1.5 xsa 35: #include "cvs.h"
1.1 jfb 36: #include "log.h"
37:
1.7 xsa 38: #define CVS_HIST_BUFSIZE 8192
1.1 jfb 39:
40:
41:
1.7 xsa 42: static int cvs_hist_fillbuf(CVSHIST *);
43: static int cvs_hist_fmt(const struct cvs_hent *, char *, size_t);
1.1 jfb 44:
45:
46: /*
47: * cvs_hist_open()
48: *
49: * Open a CVS history file.
50: * Returns the number of entries in the file on success, or -1 on error.
51: */
1.8 xsa 52: CVSHIST *
1.1 jfb 53: cvs_hist_open(const char *path)
54: {
55: CVSHIST *histp;
56:
57: histp = (CVSHIST *)malloc(sizeof(*histp));
58: if (histp == NULL) {
59: cvs_log(LP_ERRNO, "failed to allocate CVS history");
60: return (NULL);
61: }
62: memset(histp, 0, sizeof(*histp));
63:
1.10 xsa 64: histp->chf_buf = (char *)malloc((size_t)CVS_HIST_BUFSIZE);
1.1 jfb 65: if (histp->chf_buf == NULL) {
66: cvs_log(LP_ERRNO,
67: "failed to allocate CVS history parse buffer");
68: free(histp);
69: return (NULL);
70: }
71: histp->chf_blen = CVS_HIST_BUFSIZE;
72: histp->chf_off = 0;
73:
74: histp->chf_sindex = 0;
75: histp->chf_cindex = 0;
76: histp->chf_nbhent = 0;
1.11 ! xsa 77:
! 78: cvs_log(LP_TRACE, "cvs_hist_open(%s)", path);
1.1 jfb 79:
80: histp->chf_fd = open(path, O_RDONLY, 0);
81: if (histp->chf_fd == -1) {
82: cvs_log(LP_ERRNO,
83: "failed to open CVS history file `%s'", path);
1.9 xsa 84: cvs_nolog = 1;
1.1 jfb 85: free(histp->chf_buf);
86: free(histp);
87: return (NULL);
88: }
89:
90: cvs_hist_fillbuf(histp);
91:
92: return (histp);
93: }
94:
95:
96: /*
97: * cvs_hist_close()
98: *
99: * Close the CVS history file previously opened by a call to cvs_hist_open()
100: */
101: void
102: cvs_hist_close(CVSHIST *histp)
103: {
104: if (histp->chf_fd >= 0)
105: (void)close(histp->chf_fd);
106: free(histp->chf_buf);
107: free(histp);
108: }
109:
110:
111: /*
112: * cvs_hist_getnext()
113: *
114: * Get the next entry from the history file <histp>. Whenever using this
115: * function, it should be assumed that the return value of the previous call
116: * to cvs_hist_getnext() is now invalid.
117: * Returns the next entry from the file on success, or NULL on failure or if
118: * no entries are left.
119: */
1.8 xsa 120: struct cvs_hent *
1.1 jfb 121: cvs_hist_getnext(CVSHIST *histp)
122: {
123: if (histp->chf_cindex == histp->chf_nbhent) {
124: /* no more cached entries, refill buf and parse */
125: cvs_hist_fillbuf(histp);
126: cvs_hist_parse(histp);
127: }
128: return (&(histp->chf_hent[histp->chf_cindex++]));
129: }
130:
131:
132: /*
133: * cvs_hist_append()
134: *
135: * Append a history entry to the history file <histp>. The file offset is
136: * first set to the end of the file.
137: * Returns 0 on success, or -1 on failure.
138: */
139: int
140: cvs_hist_append(CVSHIST *histp, struct cvs_hent *hentp)
141: {
142: char hbuf[128];
1.9 xsa 143:
144: if (cvs_nolog == 1)
145: return (0);
1.1 jfb 146:
147: if (cvs_hist_fmt(hentp, hbuf, sizeof(hbuf)) < 0) {
148: cvs_log(LP_ERR, "failed to append CVS history entry");
149: return (-1);
150: }
151:
152: /* position ourself at the end */
153: if (lseek(histp->chf_fd, (off_t)0, SEEK_END) == -1) {
154: cvs_log(LP_ERRNO, "failed to seek to end of CVS history file");
155: return (-1);
156: }
157:
158: if (write(histp->chf_fd, hbuf, strlen(hbuf)) == -1) {
159: cvs_log(LP_ERR, "failed to write CVS history entry to file");
160: return (-1);
161: }
162:
163: return (0);
164: }
165:
166:
167:
168: /*
169: * cvs_hist_fillbuf()
170: *
171: * Fill the history file's internal buffer for future parsing.
172: */
173: static int
174: cvs_hist_fillbuf(CVSHIST *histp)
175: {
176: ssize_t ret;
177:
178: /* reposition ourself in case we're missing the start of a record */
179: if (lseek(histp->chf_fd, histp->chf_off, SEEK_SET) == -1) {
180: cvs_log(LP_ERRNO, "failed to seek in CVS history file");
181: return (-1);
182: }
183: ret = read(histp->chf_fd, histp->chf_buf, histp->chf_blen);
184: if (ret == -1) {
185: cvs_log(LP_ERRNO, "failed to buffer CVS history file");
186: return (-1);
1.2 deraadt 187: } else {
1.1 jfb 188: histp->chf_bused = (size_t)ret;
189: }
190:
191: return (ret);
192: }
193:
194:
195: /*
196: * cvs_hist_parse()
197: *
198: * Parse the current contents of the internal buffer of <histp> and regenerate
199: * the buffered history entries.
200: * Returns the number of entries parsed on success, or -1 on failure.
201: */
202: int
203: cvs_hist_parse(CVSHIST *histp)
204: {
205: u_int i, fld;
206: char *fields[CVS_HIST_NBFLD], *sp, *bep, *ep, *errp;
207:
208: sp = histp->chf_buf;
209: bep = histp->chf_buf + histp->chf_bused - 1;
210:
211: for (i = 0; i < CVS_HIST_CACHE; i++) {
212: ep = memchr(sp, '\n', bep - sp);
213: if (ep == NULL) {
214: /*
215: * No record or incomplete record left to parse,
216: * so adjust the next read offset in consequence.
217: */
218: histp->chf_off += (off_t)(sp - histp->chf_buf);
219: break;
1.2 deraadt 220: } else if (ep == bep) {
1.1 jfb 221: histp->chf_off += (off_t)histp->chf_bused;
222: }
223: *(ep++) = '\0';
224:
225: printf("hist(%s)\n", sp);
226:
227: histp->chf_hent[i].ch_event = *sp++;
228:
229: /* split the record in fields */
230: fields[0] = sp;
231:
232: fld = 1;
233: while (sp < ep) {
234: if (*sp == '|') {
235: *sp = '\0';
236: fields[fld++] = sp + 1;
237: }
238: if (fld == CVS_HIST_NBFLD)
239: break;
240: sp++;
241: }
242: #if 0
243: for (fld = 0; fld < CVS_HIST_NBFLD; fld++)
244: printf("fields[%u] = `%s'\n", fld, fields[fld]);
245: #endif
246:
247: histp->chf_hent[i].ch_date = (time_t)strtol(fields[0],
248: &errp, 16);
249: if (*errp != '\0') {
250: cvs_log(LP_ERR,
251: "parse error in date field of CVS history entry");
252: continue;
253: }
254:
255: histp->chf_hent[i].ch_user = fields[1];
256: histp->chf_hent[i].ch_curdir = fields[2];
257: histp->chf_hent[i].ch_repo = fields[3];
258: histp->chf_hent[i].ch_rev = rcsnum_alloc();
259: rcsnum_aton(fields[4], NULL, histp->chf_hent[i].ch_rev);
260: histp->chf_hent[i].ch_arg = fields[5];
261: sp = ep;
262: }
263:
264: /* update indexes */
265: histp->chf_sindex += histp->chf_nbhent;
266: histp->chf_nbhent = i;
267: histp->chf_cindex = 0;
268:
269:
270: return (i);
271: }
272:
273:
274: /*
275: * cvs_hist_fmt()
276: *
277: * Format the contents of the CVS history entry <ent> into the format used in
278: * the CVS `history' file, and store the resulting string in <buf>, which is
279: * of size <blen>.
280: */
281: static int
282: cvs_hist_fmt(const struct cvs_hent *ent, char *buf, size_t blen)
283: {
284: char numbuf[64];
1.4 joris 285: int len;
1.1 jfb 286:
1.3 tedu 287: if (rcsnum_tostr(ent->ch_rev, numbuf, sizeof(numbuf)) == NULL)
1.1 jfb 288: return (-1);
289:
1.4 joris 290: len = snprintf(buf, blen, "%c%8x|%s|%s|%s|%s|%s",
1.1 jfb 291: ent->ch_event, ent->ch_date, ent->ch_user, ent->ch_curdir,
1.4 joris 292: ent->ch_repo, numbuf, ent->ch_arg);
293: if (len >= (int)blen || len == -1)
294: return (-1);
295: return (len);
1.1 jfb 296: }