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