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