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