Annotation of src/usr.bin/cvs/entries.c, Revision 1.53
1.53 ! joris 1: /* $OpenBSD: entries.c,v 1.52 2005/12/03 15:02:55 joris 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/stat.h>
28:
1.26 xsa 29: #include <errno.h>
30: #include <fcntl.h>
1.1 jfb 31: #include <stdio.h>
32: #include <stdlib.h>
1.26 xsa 33: #include <string.h>
1.1 jfb 34: #include <unistd.h>
35:
1.34 xsa 36: #include "cvs.h"
1.1 jfb 37: #include "log.h"
38:
39:
1.42 xsa 40: #define CVS_ENTRIES_NFIELDS 6
41: #define CVS_ENTRIES_DELIM '/'
1.1 jfb 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: */
1.42 xsa 51: CVSENTRIES *
1.2 jfb 52: cvs_ent_open(const char *dir, int flags)
1.1 jfb 53: {
54: size_t len;
1.36 xsa 55: int exists, nodir;
1.52 joris 56: char bpath[MAXPATHLEN], *p;
1.45 xsa 57: char cdpath[MAXPATHLEN], ebuf[CVS_ENT_MAXLINELEN], entpath[MAXPATHLEN];
58: char mode[4];
1.1 jfb 59: FILE *fp;
1.8 jfb 60: struct stat st;
1.1 jfb 61: struct cvs_ent *ent;
62: CVSENTRIES *ep;
63:
1.12 jfb 64: exists = 0;
1.36 xsa 65: nodir = 1;
1.3 jfb 66: memset(mode, 0, sizeof(mode));
1.8 jfb 67:
1.36 xsa 68: /*
69: * Check if the CVS/ dir does exist. If it does,
70: * maybe the Entries file was deleted by accident,
71: * display error message. Else we might be doing a fresh
72: * update or checkout of a module.
73: */
74: len = cvs_path_cat(dir, CVS_PATH_CVSDIR, cdpath, sizeof(cdpath));
1.37 xsa 75: if (len >= sizeof(cdpath))
1.36 xsa 76: return (NULL);
1.37 xsa 77:
1.36 xsa 78: if ((stat(cdpath, &st) == 0) && S_ISDIR(st.st_mode))
79: nodir = 0; /* the CVS/ directory does exist */
80:
1.52 joris 81: len = cvs_path_cat(dir, CVS_PATH_BACKUPENTRIES, bpath, sizeof(bpath));
82: if (len >= sizeof(entpath))
83: return (NULL);
84:
1.32 jfb 85: len = cvs_path_cat(dir, CVS_PATH_ENTRIES, entpath, sizeof(entpath));
1.35 joris 86: if (len >= sizeof(entpath))
1.26 xsa 87: return (NULL);
1.8 jfb 88:
1.4 jfb 89: switch (flags & O_ACCMODE) {
1.8 jfb 90: case O_WRONLY:
1.4 jfb 91: case O_RDWR:
1.8 jfb 92: /* we have to use append otherwise the file gets truncated */
93: mode[0] = 'w';
1.4 jfb 94: mode[1] = '+';
1.8 jfb 95: break;
1.4 jfb 96: case O_RDONLY:
1.3 jfb 97: mode[0] = 'r';
1.4 jfb 98: break;
1.3 jfb 99: }
100:
1.8 jfb 101: /* we can use 'r' if the file already exists */
1.12 jfb 102: if (stat(entpath, &st) == 0) {
103: exists = 1;
1.8 jfb 104: mode[0] = 'r';
1.12 jfb 105: }
1.8 jfb 106:
1.3 jfb 107: fp = fopen(entpath, mode);
1.1 jfb 108: if (fp == NULL) {
1.44 xsa 109: if (nodir == 0)
1.36 xsa 110: cvs_log(LP_ERRNO, "cannot open %s for %s", entpath,
111: mode[1] == '+' ? "writing" : "reading");
1.1 jfb 112: return (NULL);
113: }
114:
1.53 ! joris 115: ep = (CVSENTRIES *)xmalloc(sizeof(CVSENTRIES));
1.5 jfb 116: memset(ep, 0, sizeof(*ep));
117:
1.53 ! joris 118: ep->cef_path = xstrdup(entpath);
! 119: ep->cef_bpath = xstrdup(bpath);
1.4 jfb 120: ep->cef_cur = NULL;
121: TAILQ_INIT(&(ep->cef_ent));
1.3 jfb 122:
1.43 xsa 123: while (fgets(ebuf, (int)sizeof(ebuf), fp) != NULL) {
1.1 jfb 124: len = strlen(ebuf);
125: if ((len > 0) && (ebuf[len - 1] == '\n'))
126: ebuf[--len] = '\0';
1.31 jfb 127: if ((ebuf[0] == 'D') && (ebuf[1] == '\0'))
1.7 jfb 128: break;
1.1 jfb 129: ent = cvs_ent_parse(ebuf);
130: if (ent == NULL)
131: continue;
132:
1.4 jfb 133: TAILQ_INSERT_TAIL(&(ep->cef_ent), ent, ce_list);
1.1 jfb 134: }
1.52 joris 135:
1.12 jfb 136: if (ferror(fp)) {
1.18 krapht 137: cvs_log(LP_ERRNO, "read error on %s", entpath);
1.25 jfb 138: (void)fclose(fp);
1.12 jfb 139: cvs_ent_close(ep);
140: return (NULL);
141: }
1.1 jfb 142:
1.4 jfb 143: /* only keep a pointer to the open file if we're in writing mode */
1.16 jfb 144: if ((flags & O_WRONLY) || (flags & O_RDWR))
1.8 jfb 145: ep->cef_flags |= CVS_ENTF_WR;
1.16 jfb 146:
147: (void)fclose(fp);
1.4 jfb 148:
1.52 joris 149: /*
150: * look for Entries.Log and add merge it together with our
151: * list of things.
152: */
153: len = cvs_path_cat(dir, CVS_PATH_LOGENTRIES, entpath, sizeof(entpath));
154: if (len >= sizeof(entpath)) {
155: cvs_ent_close(ep);
156: return (NULL);
157: }
158:
159: fp = fopen(entpath, "r");
160: if (fp != NULL) {
161: while (fgets(ebuf, (int)sizeof(ebuf), fp) != NULL) {
162: len = strlen(ebuf);
163: if ((len > 0) && (ebuf[len - 1] == '\n'))
164: ebuf[--len] = '\0';
165:
166: p = &ebuf[2];
167: ent = cvs_ent_parse(p);
168: if (ent == NULL)
169: continue;
170:
171: if (ebuf[0] == 'A')
172: cvs_ent_add(ep, ent);
173: else if (ebuf[0] == 'R')
174: cvs_ent_remove(ep, ent->ce_name, 0);
175: }
176: (void)fclose(fp);
177:
178: /* always un-synced here, because we
179: * just added or removed entries.
180: */
181: ep->cef_flags &= ~CVS_ENTF_SYNC;
182: } else {
183: if (exists == 1)
184: ep->cef_flags |= CVS_ENTF_SYNC;
185: }
1.12 jfb 186:
1.1 jfb 187: return (ep);
188: }
189:
190:
191: /*
192: * cvs_ent_close()
193: *
1.5 jfb 194: * Close the Entries file <ep> and free all data. Any reference to entries
195: * structure within that file become invalid.
1.1 jfb 196: */
197: void
198: cvs_ent_close(CVSENTRIES *ep)
199: {
1.5 jfb 200: struct cvs_ent *ent;
201:
1.41 xsa 202: if ((cvs_noexec == 0) && (ep->cef_flags & CVS_ENTF_WR) &&
1.8 jfb 203: !(ep->cef_flags & CVS_ENTF_SYNC)) {
204: /* implicit sync with disk */
205: (void)cvs_ent_write(ep);
206: }
207:
1.5 jfb 208: if (ep->cef_path != NULL)
1.53 ! joris 209: xfree(ep->cef_path);
1.5 jfb 210:
1.52 joris 211: if (ep->cef_bpath != NULL)
1.53 ! joris 212: xfree(ep->cef_bpath);
1.52 joris 213:
1.5 jfb 214: while (!TAILQ_EMPTY(&(ep->cef_ent))) {
215: ent = TAILQ_FIRST(&(ep->cef_ent));
216: TAILQ_REMOVE(&(ep->cef_ent), ent, ce_list);
217: cvs_ent_free(ent);
218: }
219:
1.53 ! joris 220: xfree(ep);
1.1 jfb 221: }
222:
223:
224: /*
225: * cvs_ent_add()
226: *
1.8 jfb 227: * Add the entry <ent> to the Entries file <ef>. The disk contents are not
228: * modified until a call to cvs_ent_write() is performed. This is done
229: * implicitly on a call to cvs_ent_close() on an Entries file that has been
230: * opened for writing.
1.7 jfb 231: * Returns 0 on success, or -1 on failure.
1.1 jfb 232: */
233: int
234: cvs_ent_add(CVSENTRIES *ef, struct cvs_ent *ent)
235: {
1.16 jfb 236: if (!(ef->cef_flags & CVS_ENTF_WR)) {
1.3 jfb 237: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
238: return (-1);
239: }
1.1 jfb 240:
1.21 jfb 241: if (cvs_ent_get(ef, ent->ce_name) != NULL) {
242: cvs_log(LP_ERR, "attempt to add duplicate entry for `%s'",
243: ent->ce_name);
1.1 jfb 244: return (-1);
1.21 jfb 245: }
1.1 jfb 246:
1.8 jfb 247: TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
248:
249: ef->cef_flags &= ~CVS_ENTF_SYNC;
1.3 jfb 250:
251: return (0);
252: }
253:
254:
255: /*
256: * cvs_ent_addln()
257: *
258: * Add a line to the Entries file.
259: */
260: int
261: cvs_ent_addln(CVSENTRIES *ef, const char *line)
262: {
263: struct cvs_ent *ent;
264:
1.16 jfb 265: if (!(ef->cef_flags & CVS_ENTF_WR)) {
1.3 jfb 266: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
267: return (-1);
268: }
269:
270: ent = cvs_ent_parse(line);
271: if (ent == NULL)
272: return (-1);
273:
274: if (cvs_ent_get(ef, ent->ce_name) != NULL)
275: return (-1);
1.1 jfb 276:
1.4 jfb 277: TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
1.8 jfb 278: ef->cef_flags &= ~CVS_ENTF_SYNC;
279:
1.1 jfb 280: return (0);
281: }
282:
283:
284: /*
1.9 jfb 285: * cvs_ent_remove()
286: *
287: * Remove an entry from the Entries file <ef>. The entry's name is given
288: * by <name>.
289: */
290: int
1.51 joris 291: cvs_ent_remove(CVSENTRIES *ef, const char *name, int useprev)
1.9 jfb 292: {
293: struct cvs_ent *ent;
1.44 xsa 294:
295: cvs_log(LP_TRACE, "cvs_ent_remove(%s)", name);
1.16 jfb 296:
1.9 jfb 297: ent = cvs_ent_get(ef, name);
298: if (ent == NULL)
299: return (-1);
300:
1.22 jfb 301: if (ef->cef_cur == ent) {
302: /* if this element was the last one retrieved through a
303: * call to cvs_ent_next(), point to the next element to avoid
304: * keeping an invalid reference.
305: */
1.51 joris 306: if (useprev) {
307: ef->cef_cur = TAILQ_PREV(ef->cef_cur,
308: cvsentrieshead, ce_list);
309: } else {
310: ef->cef_cur = TAILQ_NEXT(ef->cef_cur, ce_list);
311: }
1.22 jfb 312: }
1.9 jfb 313: TAILQ_REMOVE(&(ef->cef_ent), ent, ce_list);
314: cvs_ent_free(ent);
315:
316: ef->cef_flags &= ~CVS_ENTF_SYNC;
317:
318: return (0);
319: }
320:
321:
322: /*
1.1 jfb 323: * cvs_ent_get()
324: *
325: * Get the CVS entry from the Entries file <ef> whose 'name' portion matches
326: * <file>.
327: * Returns a pointer to the cvs entry structure on success, or NULL on failure.
328: */
1.42 xsa 329: struct cvs_ent *
1.1 jfb 330: cvs_ent_get(CVSENTRIES *ef, const char *file)
331: {
1.39 xsa 332: struct cvs_ent *ent;
1.1 jfb 333:
1.39 xsa 334: TAILQ_FOREACH(ent, &(ef->cef_ent), ce_list)
335: if (strcmp(ent->ce_name, file) == 0)
336: return (ent);
1.1 jfb 337:
338: return (NULL);
339: }
340:
341:
342: /*
343: * cvs_ent_next()
344: *
1.4 jfb 345: * This function is used to iterate over the entries in an Entries file. The
346: * first call will return the first entry of the file and each subsequent call
347: * will return the entry following the last one returned.
1.1 jfb 348: * Returns a pointer to the cvs entry structure on success, or NULL on failure.
349: */
1.42 xsa 350: struct cvs_ent *
1.1 jfb 351: cvs_ent_next(CVSENTRIES *ef)
352: {
1.5 jfb 353: if (ef->cef_cur == NULL)
1.4 jfb 354: ef->cef_cur = TAILQ_FIRST(&(ef->cef_ent));
1.5 jfb 355: else
356: ef->cef_cur = TAILQ_NEXT(ef->cef_cur, ce_list);
357: return (ef->cef_cur);
1.1 jfb 358: }
359:
360:
361: /*
362: * cvs_ent_parse()
363: *
1.25 jfb 364: * Parse a single line from a CVS/Entries file and return a cvs_ent structure
1.1 jfb 365: * containing all the parsed information.
366: */
367: struct cvs_ent*
368: cvs_ent_parse(const char *entry)
369: {
370: int i;
1.10 jfb 371: char *fields[CVS_ENTRIES_NFIELDS], *buf, *sp, *dp;
1.39 xsa 372: struct cvs_ent *ent;
1.1 jfb 373:
1.53 ! joris 374: buf = xstrdup(entry);
1.10 jfb 375: sp = buf;
376: i = 0;
377: do {
378: dp = strchr(sp, CVS_ENTRIES_DELIM);
379: if (dp != NULL)
380: *(dp++) = '\0';
381: fields[i++] = sp;
382: sp = dp;
383: } while ((dp != NULL) && (i < CVS_ENTRIES_NFIELDS));
384:
385: if (i < CVS_ENTRIES_NFIELDS) {
386: cvs_log(LP_ERR, "missing fields in entry line `%s'", entry);
387: return (NULL);
388: }
389:
1.53 ! joris 390: ent = (struct cvs_ent *)xmalloc(sizeof(*ent));
1.39 xsa 391: memset(ent, 0, sizeof(*ent));
392: ent->ce_buf = buf;
1.1 jfb 393:
1.10 jfb 394: if (*fields[0] == '\0')
1.39 xsa 395: ent->ce_type = CVS_ENT_FILE;
1.10 jfb 396: else if (*fields[0] == 'D')
1.39 xsa 397: ent->ce_type = CVS_ENT_DIR;
1.10 jfb 398: else
1.39 xsa 399: ent->ce_type = CVS_ENT_NONE;
1.1 jfb 400:
1.39 xsa 401: ent->ce_status = CVS_ENT_REG;
402: ent->ce_name = fields[1];
1.48 joris 403: ent->processed = 0;
1.1 jfb 404:
1.39 xsa 405: if (ent->ce_type == CVS_ENT_FILE) {
1.24 jfb 406: if (*fields[2] == '-') {
1.39 xsa 407: ent->ce_status = CVS_ENT_REMOVED;
1.24 jfb 408: sp = fields[2] + 1;
409: } else {
410: sp = fields[2];
1.31 jfb 411: if ((fields[2][0] == '0') && (fields[2][1] == '\0'))
1.39 xsa 412: ent->ce_status = CVS_ENT_ADDED;
1.24 jfb 413: }
1.38 joris 414:
1.39 xsa 415: if ((ent->ce_rev = rcsnum_parse(sp)) == NULL) {
416: cvs_ent_free(ent);
1.29 jfb 417: return (NULL);
418: }
1.24 jfb 419:
1.38 joris 420: if (cvs_cmdop == CVS_OP_SERVER) {
421: if (!strcmp(fields[3], "up to date"))
1.39 xsa 422: ent->ce_status = CVS_ENT_UPTODATE;
1.38 joris 423: } else {
1.50 joris 424: if ((strcmp(fields[3], CVS_DATE_DUMMY) == 0) ||
425: (strncmp(fields[3], "Initial ", 8) == 0))
1.39 xsa 426: ent->ce_mtime = CVS_DATE_DMSEC;
1.38 joris 427: else
1.39 xsa 428: ent->ce_mtime = cvs_date_parse(fields[3]);
1.38 joris 429: }
1.29 jfb 430: }
1.24 jfb 431:
1.39 xsa 432: ent->ce_opts = fields[4];
433: ent->ce_tag = fields[5];
434: return (ent);
1.5 jfb 435: }
436:
437: /*
438: * cvs_ent_free()
439: *
440: * Free a single CVS entries structure.
441: */
442: void
443: cvs_ent_free(struct cvs_ent *ent)
444: {
445: if (ent->ce_rev != NULL)
446: rcsnum_free(ent->ce_rev);
447: if (ent->ce_buf != NULL)
1.53 ! joris 448: xfree(ent->ce_buf);
! 449: xfree(ent);
1.5 jfb 450: }
1.8 jfb 451:
452: /*
453: * cvs_ent_write()
454: *
455: * Explicitly write the contents of the Entries file <ef> to disk.
456: * Returns 0 on success, or -1 on failure.
457: */
458: int
459: cvs_ent_write(CVSENTRIES *ef)
460: {
1.13 jfb 461: size_t len;
462: char revbuf[64], timebuf[32];
1.8 jfb 463: struct cvs_ent *ent;
1.32 jfb 464: FILE *fp;
1.8 jfb 465:
466: if (ef->cef_flags & CVS_ENTF_SYNC)
467: return (0);
468:
1.52 joris 469: if ((fp = fopen(ef->cef_bpath, "w")) == NULL) {
470: cvs_log(LP_ERRNO, "failed to open Entries `%s'", ef->cef_bpath);
1.32 jfb 471: return (-1);
1.16 jfb 472: }
473:
1.8 jfb 474: TAILQ_FOREACH(ent, &(ef->cef_ent), ce_list) {
1.15 jfb 475: if (ent->ce_type == CVS_ENT_DIR) {
1.32 jfb 476: putc('D', fp);
1.15 jfb 477: timebuf[0] = '\0';
478: revbuf[0] = '\0';
1.19 deraadt 479: } else {
1.15 jfb 480: rcsnum_tostr(ent->ce_rev, revbuf, sizeof(revbuf));
1.49 xsa 481: if ((ent->ce_mtime == CVS_DATE_DMSEC) &&
482: (ent->ce_status != CVS_ENT_ADDED))
1.15 jfb 483: strlcpy(timebuf, CVS_DATE_DUMMY,
484: sizeof(timebuf));
1.47 joris 485: else if (ent->ce_status == CVS_ENT_ADDED) {
486: strlcpy(timebuf, "Initial ", sizeof(timebuf));
487: strlcat(timebuf, ent->ce_name, sizeof(timebuf));
488: } else {
1.15 jfb 489: ctime_r(&(ent->ce_mtime), timebuf);
490: len = strlen(timebuf);
491: if ((len > 0) && (timebuf[len - 1] == '\n'))
492: timebuf[--len] = '\0';
493: }
1.38 joris 494: }
495:
496: if (cvs_cmdop == CVS_OP_SERVER) {
497: if (ent->ce_status == CVS_ENT_UPTODATE)
498: strlcpy(timebuf, "up to date", sizeof(timebuf));
499: else
500: timebuf[0] = '\0';
1.15 jfb 501: }
1.8 jfb 502:
1.32 jfb 503: fprintf(fp, "/%s/%s%s/%s/%s/%s\n", ent->ce_name,
1.27 joris 504: (ent->ce_status == CVS_ENT_REMOVED) ? "-" : "", revbuf,
1.49 xsa 505: timebuf, (ent->ce_opts != NULL) ? ent->ce_opts : "",
506: (ent->ce_tag != NULL) ? ent->ce_tag : "");
1.8 jfb 507: }
508:
509: /* terminating line */
1.32 jfb 510: putc('D', fp);
511: putc('\n', fp);
1.8 jfb 512:
513: ef->cef_flags |= CVS_ENTF_SYNC;
1.32 jfb 514: fclose(fp);
1.52 joris 515:
516: /* rename Entries.Backup to Entries */
517: cvs_rename(ef->cef_bpath, ef->cef_path);
518:
519: /* remove Entries.Log */
520: cvs_unlink(CVS_PATH_LOGENTRIES);
521:
1.8 jfb 522: return (0);
1.1 jfb 523: }