Annotation of src/usr.bin/cvs/entries.c, Revision 1.20
1.20 ! tedu 1: /* $OpenBSD: entries.c,v 1.19 2004/12/06 21:03:12 deraadt Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.20 ! tedu 4: * All rights reserved.
1.1 jfb 5: *
1.20 ! 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.20 ! 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.20 ! 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.20 ! tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
30: #include <stdio.h>
31: #include <fcntl.h>
32: #include <stdlib.h>
33: #include <unistd.h>
34: #include <string.h>
35:
36: #include "log.h"
37: #include "cvs.h"
38:
39:
1.10 jfb 40: #define CVS_ENTRIES_NFIELDS 6
1.1 jfb 41: #define CVS_ENTRIES_DELIM '/'
42:
43:
44: /*
45: * cvs_ent_open()
46: *
47: * Open the CVS Entries file for the directory <dir>.
48: * Returns a pointer to the CVSENTRIES file structure on success, or NULL
49: * on failure.
50: */
51: CVSENTRIES*
1.2 jfb 52: cvs_ent_open(const char *dir, int flags)
1.1 jfb 53: {
54: size_t len;
1.12 jfb 55: int exists;
1.3 jfb 56: char entpath[MAXPATHLEN], ebuf[128], mode[4];
1.1 jfb 57: FILE *fp;
1.8 jfb 58: struct stat st;
1.1 jfb 59: struct cvs_ent *ent;
60: CVSENTRIES *ep;
61:
1.12 jfb 62: exists = 0;
1.3 jfb 63: memset(mode, 0, sizeof(mode));
1.8 jfb 64:
65: snprintf(entpath, sizeof(entpath), "%s/" CVS_PATH_ENTRIES, dir);
66:
1.4 jfb 67: switch (flags & O_ACCMODE) {
1.8 jfb 68: case O_WRONLY:
1.4 jfb 69: case O_RDWR:
1.8 jfb 70: /* we have to use append otherwise the file gets truncated */
71: mode[0] = 'w';
1.4 jfb 72: mode[1] = '+';
1.8 jfb 73: break;
1.4 jfb 74: case O_RDONLY:
1.3 jfb 75: mode[0] = 'r';
1.4 jfb 76: break;
1.3 jfb 77: }
78:
1.8 jfb 79: /* we can use 'r' if the file already exists */
1.12 jfb 80: if (stat(entpath, &st) == 0) {
81: exists = 1;
1.8 jfb 82: mode[0] = 'r';
1.12 jfb 83: }
1.8 jfb 84:
1.3 jfb 85: fp = fopen(entpath, mode);
1.1 jfb 86: if (fp == NULL) {
1.8 jfb 87: cvs_log(LP_ERRNO, "cannot open %s for %s", entpath,
88: mode[1] == '+' ? "writing" : "reading");
1.1 jfb 89: return (NULL);
90: }
91:
92: ep = (CVSENTRIES *)malloc(sizeof(CVSENTRIES));
93: if (ep == NULL) {
94: cvs_log(LP_ERRNO, "failed to allocate Entries data");
95: (void)fclose(fp);
96: return (NULL);
97: }
1.5 jfb 98: memset(ep, 0, sizeof(*ep));
99:
1.17 joris 100: ep->cef_path = strdup(entpath);
1.1 jfb 101: if (ep->cef_path == NULL) {
102: cvs_log(LP_ERRNO, "failed to copy Entries path");
103: free(ep);
104: (void)fclose(fp);
105: return (NULL);
106: }
107:
1.4 jfb 108: ep->cef_cur = NULL;
109: TAILQ_INIT(&(ep->cef_ent));
1.3 jfb 110:
1.1 jfb 111: while (fgets(ebuf, sizeof(ebuf), fp) != NULL) {
112: len = strlen(ebuf);
113: if ((len > 0) && (ebuf[len - 1] == '\n'))
114: ebuf[--len] = '\0';
1.7 jfb 115: if (strcmp(ebuf, "D") == 0)
116: break;
1.1 jfb 117: ent = cvs_ent_parse(ebuf);
118: if (ent == NULL)
119: continue;
120:
1.4 jfb 121: TAILQ_INSERT_TAIL(&(ep->cef_ent), ent, ce_list);
1.1 jfb 122: }
1.12 jfb 123: if (ferror(fp)) {
1.18 krapht 124: cvs_log(LP_ERRNO, "read error on %s", entpath);
1.12 jfb 125: cvs_ent_close(ep);
126: return (NULL);
127: }
1.1 jfb 128:
1.4 jfb 129: /* only keep a pointer to the open file if we're in writing mode */
1.16 jfb 130: if ((flags & O_WRONLY) || (flags & O_RDWR))
1.8 jfb 131: ep->cef_flags |= CVS_ENTF_WR;
1.16 jfb 132:
133: (void)fclose(fp);
1.4 jfb 134:
1.12 jfb 135: if (exists)
136: ep->cef_flags |= CVS_ENTF_SYNC;
137:
1.1 jfb 138: return (ep);
139: }
140:
141:
142: /*
143: * cvs_ent_close()
144: *
1.5 jfb 145: * Close the Entries file <ep> and free all data. Any reference to entries
146: * structure within that file become invalid.
1.1 jfb 147: */
148: void
149: cvs_ent_close(CVSENTRIES *ep)
150: {
1.5 jfb 151: struct cvs_ent *ent;
152:
1.8 jfb 153: if ((ep->cef_flags & CVS_ENTF_WR) &&
154: !(ep->cef_flags & CVS_ENTF_SYNC)) {
155: /* implicit sync with disk */
156: (void)cvs_ent_write(ep);
157: }
158:
1.5 jfb 159: if (ep->cef_file != NULL)
1.6 jfb 160: (void)fclose(ep->cef_file);
1.5 jfb 161: if (ep->cef_path != NULL)
162: free(ep->cef_path);
163:
164: while (!TAILQ_EMPTY(&(ep->cef_ent))) {
165: ent = TAILQ_FIRST(&(ep->cef_ent));
166: TAILQ_REMOVE(&(ep->cef_ent), ent, ce_list);
167: cvs_ent_free(ent);
168: }
169:
1.1 jfb 170: free(ep);
171: }
172:
173:
174: /*
175: * cvs_ent_add()
176: *
1.8 jfb 177: * Add the entry <ent> to the Entries file <ef>. The disk contents are not
178: * modified until a call to cvs_ent_write() is performed. This is done
179: * implicitly on a call to cvs_ent_close() on an Entries file that has been
180: * opened for writing.
1.7 jfb 181: * Returns 0 on success, or -1 on failure.
1.1 jfb 182: */
183: int
184: cvs_ent_add(CVSENTRIES *ef, struct cvs_ent *ent)
185: {
1.16 jfb 186: if (!(ef->cef_flags & CVS_ENTF_WR)) {
1.3 jfb 187: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
188: return (-1);
189: }
1.1 jfb 190:
191: if (cvs_ent_get(ef, ent->ce_name) != NULL)
192: return (-1);
193:
1.8 jfb 194: TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
195:
196: ef->cef_flags &= ~CVS_ENTF_SYNC;
1.3 jfb 197:
198: return (0);
199: }
200:
201:
202: /*
203: * cvs_ent_addln()
204: *
205: * Add a line to the Entries file.
206: */
207: int
208: cvs_ent_addln(CVSENTRIES *ef, const char *line)
209: {
210: struct cvs_ent *ent;
211:
1.16 jfb 212: if (!(ef->cef_flags & CVS_ENTF_WR)) {
1.3 jfb 213: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
214: return (-1);
215: }
216:
217: ent = cvs_ent_parse(line);
218: if (ent == NULL)
219: return (-1);
220:
221: if (cvs_ent_get(ef, ent->ce_name) != NULL)
222: return (-1);
1.1 jfb 223:
1.4 jfb 224: TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
1.8 jfb 225: ef->cef_flags &= ~CVS_ENTF_SYNC;
226:
1.1 jfb 227: return (0);
228: }
229:
230:
231: /*
1.9 jfb 232: * cvs_ent_remove()
233: *
234: * Remove an entry from the Entries file <ef>. The entry's name is given
235: * by <name>.
236: */
237: int
238: cvs_ent_remove(CVSENTRIES *ef, const char *name)
239: {
240: struct cvs_ent *ent;
241:
1.16 jfb 242: if (!(ef->cef_flags & CVS_ENTF_WR)) {
243: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
244: return (-1);
245: }
246:
1.9 jfb 247: ent = cvs_ent_get(ef, name);
248: if (ent == NULL)
249: return (-1);
250:
251: TAILQ_REMOVE(&(ef->cef_ent), ent, ce_list);
252: cvs_ent_free(ent);
253:
254: ef->cef_flags &= ~CVS_ENTF_SYNC;
255:
256: return (0);
257: }
258:
259:
260: /*
1.1 jfb 261: * cvs_ent_get()
262: *
263: * Get the CVS entry from the Entries file <ef> whose 'name' portion matches
264: * <file>.
265: * Returns a pointer to the cvs entry structure on success, or NULL on failure.
266: */
267: struct cvs_ent*
268: cvs_ent_get(CVSENTRIES *ef, const char *file)
269: {
1.4 jfb 270: struct cvs_ent *ep;
1.1 jfb 271:
1.4 jfb 272: TAILQ_FOREACH(ep, &(ef->cef_ent), ce_list)
273: if (strcmp(ep->ce_name, file) == 0)
274: return (ep);
1.1 jfb 275:
276: return (NULL);
277: }
278:
279:
280: /*
281: * cvs_ent_next()
282: *
1.4 jfb 283: * This function is used to iterate over the entries in an Entries file. The
284: * first call will return the first entry of the file and each subsequent call
285: * will return the entry following the last one returned.
1.1 jfb 286: * Returns a pointer to the cvs entry structure on success, or NULL on failure.
287: */
288: struct cvs_ent*
289: cvs_ent_next(CVSENTRIES *ef)
290: {
1.5 jfb 291: if (ef->cef_cur == NULL)
1.4 jfb 292: ef->cef_cur = TAILQ_FIRST(&(ef->cef_ent));
1.5 jfb 293: else
294: ef->cef_cur = TAILQ_NEXT(ef->cef_cur, ce_list);
295: return (ef->cef_cur);
1.1 jfb 296: }
297:
298:
299: /*
300: * cvs_ent_parse()
301: *
302: * Parse a single line from a CVS/Entries file and return a cvs_entry structure
303: * containing all the parsed information.
304: */
305: struct cvs_ent*
306: cvs_ent_parse(const char *entry)
307: {
308: int i;
1.10 jfb 309: char *fields[CVS_ENTRIES_NFIELDS], *buf, *sp, *dp;
1.1 jfb 310: struct cvs_ent *entp;
311:
1.10 jfb 312: buf = strdup(entry);
313: if (buf == NULL) {
314: cvs_log(LP_ERRNO, "failed to allocate entry copy");
315: return (NULL);
316: }
317:
318: sp = buf;
319: i = 0;
320: do {
321: dp = strchr(sp, CVS_ENTRIES_DELIM);
322: if (dp != NULL)
323: *(dp++) = '\0';
324: fields[i++] = sp;
325: sp = dp;
326: } while ((dp != NULL) && (i < CVS_ENTRIES_NFIELDS));
327:
328: if (i < CVS_ENTRIES_NFIELDS) {
329: cvs_log(LP_ERR, "missing fields in entry line `%s'", entry);
330: return (NULL);
331: }
332:
1.1 jfb 333: entp = (struct cvs_ent *)malloc(sizeof(*entp));
334: if (entp == NULL) {
335: cvs_log(LP_ERRNO, "failed to allocate CVS entry");
336: return (NULL);
337: }
1.5 jfb 338: memset(entp, 0, sizeof(*entp));
1.10 jfb 339: entp->ce_buf = buf;
1.1 jfb 340:
341: entp->ce_rev = rcsnum_alloc();
342: if (entp->ce_rev == NULL) {
1.10 jfb 343: cvs_ent_free(entp);
1.1 jfb 344: return (NULL);
345: }
346:
1.10 jfb 347: if (*fields[0] == '\0')
1.1 jfb 348: entp->ce_type = CVS_ENT_FILE;
1.10 jfb 349: else if (*fields[0] == 'D')
1.1 jfb 350: entp->ce_type = CVS_ENT_DIR;
1.10 jfb 351: else
1.1 jfb 352: entp->ce_type = CVS_ENT_NONE;
353:
1.10 jfb 354: entp->ce_name = fields[1];
1.1 jfb 355:
356: if (entp->ce_type == CVS_ENT_FILE) {
1.10 jfb 357: rcsnum_aton(fields[2], NULL, entp->ce_rev);
1.13 jfb 358: entp->ce_mtime = cvs_datesec(fields[3], CVS_DATE_CTIME, 0);
1.10 jfb 359: entp->ce_opts = fields[4];
360: entp->ce_tag = fields[5];
1.1 jfb 361: }
362:
363: return (entp);
1.5 jfb 364: }
365:
366:
367: /*
368: * cvs_ent_free()
369: *
370: * Free a single CVS entries structure.
371: */
372: void
373: cvs_ent_free(struct cvs_ent *ent)
374: {
375: if (ent->ce_rev != NULL)
376: rcsnum_free(ent->ce_rev);
377: if (ent->ce_buf != NULL)
378: free(ent->ce_buf);
379: free(ent);
380: }
381:
382:
383: /*
384: * cvs_ent_getent()
385: *
386: * Get a single entry from the CVS/Entries file of the basename portion of
387: * path <path> and return that entry. That entry must later be freed using
388: * cvs_ent_free().
389: */
390: struct cvs_ent*
391: cvs_ent_getent(const char *path)
392: {
1.11 jfb 393: char base[MAXPATHLEN], *file;
1.5 jfb 394: CVSENTRIES *entf;
395: struct cvs_ent *ep;
396:
1.11 jfb 397: cvs_splitpath(path, base, sizeof(base), &file);
1.5 jfb 398:
399: entf = cvs_ent_open(base, O_RDONLY);
400: if (entf == NULL)
401: return (NULL);
402:
403: ep = cvs_ent_get(entf, file);
404: if (ep != NULL) {
405: /* take it out of the queue so it doesn't get freed */
406: TAILQ_REMOVE(&(entf->cef_ent), ep, ce_list);
407: }
408:
409: cvs_ent_close(entf);
410: return (ep);
1.8 jfb 411: }
412:
413:
414: /*
415: * cvs_ent_write()
416: *
417: * Explicitly write the contents of the Entries file <ef> to disk.
418: * Returns 0 on success, or -1 on failure.
419: */
420: int
421: cvs_ent_write(CVSENTRIES *ef)
422: {
1.13 jfb 423: size_t len;
424: char revbuf[64], timebuf[32];
1.8 jfb 425: struct cvs_ent *ent;
426:
427: if (ef->cef_flags & CVS_ENTF_SYNC)
428: return (0);
429:
1.16 jfb 430: if (ef->cef_file == NULL) {
431: ef->cef_file = fopen(ef->cef_path, "w");
432: if (ef->cef_file == NULL) {
433: cvs_log(LP_ERRNO, "failed to open Entries `%s'",
434: ef->cef_path);
435: return (-1);
436: }
437: }
438:
439:
1.8 jfb 440: /* reposition ourself at beginning of file */
441: rewind(ef->cef_file);
442: TAILQ_FOREACH(ent, &(ef->cef_ent), ce_list) {
1.15 jfb 443: if (ent->ce_type == CVS_ENT_DIR) {
1.8 jfb 444: putc('D', ef->cef_file);
1.15 jfb 445: timebuf[0] = '\0';
446: revbuf[0] = '\0';
1.19 deraadt 447: } else {
1.15 jfb 448: rcsnum_tostr(ent->ce_rev, revbuf, sizeof(revbuf));
449: if (ent->ce_mtime == CVS_DATE_DMSEC)
450: strlcpy(timebuf, CVS_DATE_DUMMY,
451: sizeof(timebuf));
452: else {
453: ctime_r(&(ent->ce_mtime), timebuf);
454: len = strlen(timebuf);
455: if ((len > 0) && (timebuf[len - 1] == '\n'))
456: timebuf[--len] = '\0';
457: }
458: }
1.8 jfb 459:
460: fprintf(ef->cef_file, "/%s/%s/%s/%s/%s\n", ent->ce_name,
1.13 jfb 461: revbuf, timebuf, "", "");
1.8 jfb 462: }
463:
464: /* terminating line */
465: fprintf(ef->cef_file, "D\n");
466:
467: ef->cef_flags |= CVS_ENTF_SYNC;
1.16 jfb 468: fclose(ef->cef_file);
469: ef->cef_file = NULL;
1.8 jfb 470:
471: return (0);
1.1 jfb 472: }