Annotation of src/usr.bin/cvs/entries.c, Revision 1.8
1.1 jfb 1: /* $OpenBSD$ */
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:
40: #define CVS_ENTRIES_NFIELDS 5
41: #define CVS_ENTRIES_DELIM '/'
42:
43:
1.3 jfb 44:
45: static struct cvs_ent* cvs_ent_clone (const struct cvs_ent *);
46:
47:
48:
1.1 jfb 49: /*
50: * cvs_ent_open()
51: *
52: * Open the CVS Entries file for the directory <dir>.
53: * Returns a pointer to the CVSENTRIES file structure on success, or NULL
54: * on failure.
55: */
56:
57: CVSENTRIES*
1.2 jfb 58: cvs_ent_open(const char *dir, int flags)
1.1 jfb 59: {
60: size_t len;
1.3 jfb 61: char entpath[MAXPATHLEN], ebuf[128], mode[4];
1.1 jfb 62: FILE *fp;
1.8 ! jfb 63: struct stat st;
1.1 jfb 64: struct cvs_ent *ent;
65: CVSENTRIES *ep;
66:
1.3 jfb 67: memset(mode, 0, sizeof(mode));
1.8 ! jfb 68:
! 69: snprintf(entpath, sizeof(entpath), "%s/" CVS_PATH_ENTRIES, dir);
! 70:
1.4 jfb 71: switch (flags & O_ACCMODE) {
1.8 ! jfb 72: case O_WRONLY:
1.4 jfb 73: case O_RDWR:
1.8 ! jfb 74: /* we have to use append otherwise the file gets truncated */
! 75: mode[0] = 'w';
1.4 jfb 76: mode[1] = '+';
1.8 ! jfb 77: break;
1.4 jfb 78: case O_RDONLY:
1.3 jfb 79: mode[0] = 'r';
1.4 jfb 80: break;
1.3 jfb 81: }
82:
1.8 ! jfb 83: /* we can use 'r' if the file already exists */
! 84: if (stat(entpath, &st) == 0)
! 85: mode[0] = 'r';
! 86:
! 87:
1.3 jfb 88: fp = fopen(entpath, mode);
1.1 jfb 89: if (fp == NULL) {
1.8 ! jfb 90: cvs_log(LP_ERRNO, "cannot open %s for %s", entpath,
! 91: mode[1] == '+' ? "writing" : "reading");
1.1 jfb 92: return (NULL);
93: }
94:
95: ep = (CVSENTRIES *)malloc(sizeof(CVSENTRIES));
96: if (ep == NULL) {
97: cvs_log(LP_ERRNO, "failed to allocate Entries data");
98: (void)fclose(fp);
99: return (NULL);
100: }
1.5 jfb 101: memset(ep, 0, sizeof(*ep));
102:
1.1 jfb 103: ep->cef_path = strdup(dir);
104: if (ep->cef_path == NULL) {
105: cvs_log(LP_ERRNO, "failed to copy Entries path");
106: free(ep);
107: (void)fclose(fp);
108: return (NULL);
109: }
110:
1.4 jfb 111: ep->cef_cur = NULL;
112: TAILQ_INIT(&(ep->cef_ent));
1.3 jfb 113:
1.8 ! jfb 114: rewind(fp);
! 115:
1.1 jfb 116: while (fgets(ebuf, sizeof(ebuf), fp) != NULL) {
117: len = strlen(ebuf);
118: if ((len > 0) && (ebuf[len - 1] == '\n'))
119: ebuf[--len] = '\0';
1.7 jfb 120: if (strcmp(ebuf, "D") == 0)
121: break;
1.1 jfb 122: ent = cvs_ent_parse(ebuf);
123: if (ent == NULL)
124: continue;
125:
1.4 jfb 126: TAILQ_INSERT_TAIL(&(ep->cef_ent), ent, ce_list);
1.1 jfb 127: }
128:
1.4 jfb 129: /* only keep a pointer to the open file if we're in writing mode */
1.8 ! jfb 130: if ((flags & O_WRONLY) || (flags & O_RDWR)) {
! 131: ep->cef_flags |= CVS_ENTF_WR;
1.4 jfb 132: ep->cef_file = fp;
1.8 ! jfb 133: }
1.4 jfb 134: else
135: (void)fclose(fp);
136:
1.8 ! jfb 137: ep->cef_flags |= CVS_ENTF_SYNC;
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:
149: void
150: cvs_ent_close(CVSENTRIES *ep)
151: {
1.5 jfb 152: struct cvs_ent *ent;
153:
1.8 ! jfb 154: if ((ep->cef_flags & CVS_ENTF_WR) &&
! 155: !(ep->cef_flags & CVS_ENTF_SYNC)) {
! 156: /* implicit sync with disk */
! 157: (void)cvs_ent_write(ep);
! 158: }
! 159:
1.5 jfb 160: if (ep->cef_file != NULL)
1.6 jfb 161: (void)fclose(ep->cef_file);
1.5 jfb 162: if (ep->cef_path != NULL)
163: free(ep->cef_path);
164:
165: while (!TAILQ_EMPTY(&(ep->cef_ent))) {
166: ent = TAILQ_FIRST(&(ep->cef_ent));
167: TAILQ_REMOVE(&(ep->cef_ent), ent, ce_list);
168: cvs_ent_free(ent);
169: }
170:
1.1 jfb 171: free(ep);
172: }
173:
174:
175: /*
176: * cvs_ent_add()
177: *
1.8 ! jfb 178: * Add the entry <ent> to the Entries file <ef>. The disk contents are not
! 179: * modified until a call to cvs_ent_write() is performed. This is done
! 180: * implicitly on a call to cvs_ent_close() on an Entries file that has been
! 181: * opened for writing.
1.7 jfb 182: * Returns 0 on success, or -1 on failure.
1.1 jfb 183: */
184:
185: int
186: cvs_ent_add(CVSENTRIES *ef, struct cvs_ent *ent)
187: {
1.3 jfb 188: if (ef->cef_file == NULL) {
189: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
190: return (-1);
191: }
1.1 jfb 192:
193: if (cvs_ent_get(ef, ent->ce_name) != NULL)
194: return (-1);
195:
1.8 ! jfb 196: TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
! 197:
! 198: ef->cef_flags &= ~CVS_ENTF_SYNC;
1.3 jfb 199:
200: return (0);
201: }
202:
203:
204: /*
205: * cvs_ent_addln()
206: *
207: * Add a line to the Entries file.
208: */
209:
210: int
211: cvs_ent_addln(CVSENTRIES *ef, const char *line)
212: {
213: struct cvs_ent *ent;
214:
215: if (ef->cef_file == NULL) {
216: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
217: return (-1);
218: }
219:
220: ent = cvs_ent_parse(line);
221: if (ent == NULL)
222: return (-1);
223:
224: if (cvs_ent_get(ef, ent->ce_name) != NULL)
225: return (-1);
1.1 jfb 226:
1.4 jfb 227: TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
1.8 ! jfb 228: ef->cef_flags &= ~CVS_ENTF_SYNC;
! 229:
1.1 jfb 230: return (0);
231: }
232:
233:
234: /*
235: * cvs_ent_get()
236: *
237: * Get the CVS entry from the Entries file <ef> whose 'name' portion matches
238: * <file>.
239: * Returns a pointer to the cvs entry structure on success, or NULL on failure.
240: */
241:
242: struct cvs_ent*
243: cvs_ent_get(CVSENTRIES *ef, const char *file)
244: {
1.4 jfb 245: struct cvs_ent *ep;
1.1 jfb 246:
1.4 jfb 247: TAILQ_FOREACH(ep, &(ef->cef_ent), ce_list)
248: if (strcmp(ep->ce_name, file) == 0)
249: return (ep);
1.1 jfb 250:
251: return (NULL);
252: }
253:
254:
255: /*
256: * cvs_ent_next()
257: *
1.4 jfb 258: * This function is used to iterate over the entries in an Entries file. The
259: * first call will return the first entry of the file and each subsequent call
260: * will return the entry following the last one returned.
1.1 jfb 261: * Returns a pointer to the cvs entry structure on success, or NULL on failure.
262: */
263:
264: struct cvs_ent*
265: cvs_ent_next(CVSENTRIES *ef)
266: {
1.5 jfb 267: if (ef->cef_cur == NULL)
1.4 jfb 268: ef->cef_cur = TAILQ_FIRST(&(ef->cef_ent));
1.5 jfb 269: else
270: ef->cef_cur = TAILQ_NEXT(ef->cef_cur, ce_list);
271: return (ef->cef_cur);
1.1 jfb 272: }
273:
274:
275: /*
276: * cvs_ent_parse()
277: *
278: * Parse a single line from a CVS/Entries file and return a cvs_entry structure
279: * containing all the parsed information.
280: */
281:
282: struct cvs_ent*
283: cvs_ent_parse(const char *entry)
284: {
285: int i;
286: char *fields[CVS_ENTRIES_NFIELDS], *sp, *dp;
287: struct cvs_ent *entp;
288:
289: entp = (struct cvs_ent *)malloc(sizeof(*entp));
290: if (entp == NULL) {
291: cvs_log(LP_ERRNO, "failed to allocate CVS entry");
292: return (NULL);
293: }
1.5 jfb 294: memset(entp, 0, sizeof(*entp));
1.1 jfb 295:
296: entp->ce_rev = rcsnum_alloc();
297: if (entp->ce_rev == NULL) {
298: free(entp);
299: return (NULL);
300: }
301:
302: entp->ce_line = strdup(entry);
303: if (entp->ce_line == NULL) {
1.5 jfb 304: cvs_ent_free(entp);
1.1 jfb 305: return (NULL);
306: }
307:
308: entp->ce_buf = strdup(entry);
309: if (entp->ce_buf == NULL) {
1.5 jfb 310: cvs_ent_free(entp);
1.1 jfb 311: return (NULL);
312: }
313: sp = entp->ce_buf;
314:
315: if (*sp == CVS_ENTRIES_DELIM)
316: entp->ce_type = CVS_ENT_FILE;
317: else if (*sp == 'D') {
318: entp->ce_type = CVS_ENT_DIR;
319: sp++;
320: }
321: else {
322: /* unknown entry, ignore for future expansion */
323: entp->ce_type = CVS_ENT_NONE;
324: sp++;
325: }
326:
327: sp++;
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: entp->ce_name = fields[0];
338:
339: if (entp->ce_type == CVS_ENT_FILE) {
340: rcsnum_aton(fields[1], NULL, entp->ce_rev);
341: entp->ce_timestamp = fields[2];
342: entp->ce_opts = fields[3];
343: entp->ce_tag = fields[4];
344: }
345:
346: return (entp);
1.5 jfb 347: }
348:
349:
350: /*
351: * cvs_ent_free()
352: *
353: * Free a single CVS entries structure.
354: */
355:
356: void
357: cvs_ent_free(struct cvs_ent *ent)
358: {
359: if (ent->ce_rev != NULL)
360: rcsnum_free(ent->ce_rev);
361: if (ent->ce_line != NULL)
362: free(ent->ce_line);
363: if (ent->ce_buf != NULL)
364: free(ent->ce_buf);
365: free(ent);
366: }
367:
368:
369: /*
370: * cvs_ent_getent()
371: *
372: * Get a single entry from the CVS/Entries file of the basename portion of
373: * path <path> and return that entry. That entry must later be freed using
374: * cvs_ent_free().
375: */
376:
377: struct cvs_ent*
378: cvs_ent_getent(const char *path)
379: {
380: char base[MAXPATHLEN], file[MAXPATHLEN];
381: CVSENTRIES *entf;
382: struct cvs_ent *ep;
383:
384: cvs_splitpath(path, base, sizeof(base), file, sizeof(file));
385:
386: entf = cvs_ent_open(base, O_RDONLY);
387: if (entf == NULL)
388: return (NULL);
389:
390: ep = cvs_ent_get(entf, file);
391: if (ep != NULL) {
392: /* take it out of the queue so it doesn't get freed */
393: TAILQ_REMOVE(&(entf->cef_ent), ep, ce_list);
394: }
395:
396: cvs_ent_close(entf);
397: return (ep);
1.8 ! jfb 398: }
! 399:
! 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:
! 408: int
! 409: cvs_ent_write(CVSENTRIES *ef)
! 410: {
! 411: char revbuf[64];
! 412: struct cvs_ent *ent;
! 413:
! 414: if (ef->cef_file == NULL)
! 415: return (-1);
! 416:
! 417: if (ef->cef_flags & CVS_ENTF_SYNC)
! 418: return (0);
! 419:
! 420: /* reposition ourself at beginning of file */
! 421: rewind(ef->cef_file);
! 422: TAILQ_FOREACH(ent, &(ef->cef_ent), ce_list) {
! 423: if (ent->ce_type == CVS_ENT_DIR)
! 424: putc('D', ef->cef_file);
! 425:
! 426: rcsnum_tostr(ent->ce_rev, revbuf, sizeof(revbuf));
! 427: fprintf(ef->cef_file, "/%s/%s/%s/%s/%s\n", ent->ce_name,
! 428: revbuf, ent->ce_timestamp, "", "");
! 429: }
! 430:
! 431: /* terminating line */
! 432: fprintf(ef->cef_file, "D\n");
! 433:
! 434: ef->cef_flags |= CVS_ENTF_SYNC;
! 435:
! 436: return (0);
1.1 jfb 437: }