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