Annotation of src/usr.bin/cvs/entries.c, Revision 1.5
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;
63: struct cvs_ent *ent;
64: CVSENTRIES *ep;
65:
1.3 jfb 66: memset(mode, 0, sizeof(mode));
1.4 jfb 67: switch (flags & O_ACCMODE) {
68: case O_RDWR:
69: mode[1] = '+';
70: /* fallthrough */
71: case O_RDONLY:
1.3 jfb 72: mode[0] = 'r';
1.4 jfb 73: break;
74: case O_WRONLY:
1.3 jfb 75: mode[0] = 'w';
1.4 jfb 76: break;
1.3 jfb 77: }
78:
1.1 jfb 79: snprintf(entpath, sizeof(entpath), "%s/" CVS_PATH_ENTRIES, dir);
1.3 jfb 80: fp = fopen(entpath, mode);
1.1 jfb 81: if (fp == NULL) {
82: cvs_log(LP_ERRNO, "cannot open CVS/Entries for reading",
83: entpath);
84: return (NULL);
85: }
86:
87: ep = (CVSENTRIES *)malloc(sizeof(CVSENTRIES));
88: if (ep == NULL) {
89: cvs_log(LP_ERRNO, "failed to allocate Entries data");
90: (void)fclose(fp);
91: return (NULL);
92: }
1.5 ! jfb 93: memset(ep, 0, sizeof(*ep));
! 94:
1.1 jfb 95: ep->cef_path = strdup(dir);
96: if (ep->cef_path == NULL) {
97: cvs_log(LP_ERRNO, "failed to copy Entries path");
98: free(ep);
99: (void)fclose(fp);
100: return (NULL);
101: }
102:
1.4 jfb 103: ep->cef_cur = NULL;
104: TAILQ_INIT(&(ep->cef_ent));
1.3 jfb 105:
1.1 jfb 106: while (fgets(ebuf, sizeof(ebuf), fp) != NULL) {
107: len = strlen(ebuf);
108: if ((len > 0) && (ebuf[len - 1] == '\n'))
109: ebuf[--len] = '\0';
110: ent = cvs_ent_parse(ebuf);
111: if (ent == NULL)
112: continue;
113:
1.4 jfb 114: TAILQ_INSERT_TAIL(&(ep->cef_ent), ent, ce_list);
1.1 jfb 115: }
116:
1.4 jfb 117: /* only keep a pointer to the open file if we're in writing mode */
118: if ((flags & O_WRONLY) || (flags & O_RDWR))
119: ep->cef_file = fp;
120: else
121: (void)fclose(fp);
122:
1.1 jfb 123: return (ep);
124: }
125:
126:
127: /*
128: * cvs_ent_close()
129: *
1.5 ! jfb 130: * Close the Entries file <ep> and free all data. Any reference to entries
! 131: * structure within that file become invalid.
1.1 jfb 132: */
133:
134: void
135: cvs_ent_close(CVSENTRIES *ep)
136: {
1.5 ! jfb 137: struct cvs_ent *ent;
! 138:
! 139: if (ep->cef_file != NULL)
! 140: (void)fclose(ep);
! 141: if (ep->cef_path != NULL)
! 142: free(ep->cef_path);
! 143:
! 144: while (!TAILQ_EMPTY(&(ep->cef_ent))) {
! 145: ent = TAILQ_FIRST(&(ep->cef_ent));
! 146: TAILQ_REMOVE(&(ep->cef_ent), ent, ce_list);
! 147: cvs_ent_free(ent);
! 148: }
! 149:
1.1 jfb 150: free(ep);
151: }
152:
153:
154: /*
155: * cvs_ent_add()
156: *
1.3 jfb 157: * Add the entry <ent> to the Entries file <ef>.
1.1 jfb 158: */
159:
160: int
161: cvs_ent_add(CVSENTRIES *ef, struct cvs_ent *ent)
162: {
163: void *tmp;
1.3 jfb 164:
165: if (ef->cef_file == NULL) {
166: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
167: return (-1);
168: }
1.1 jfb 169:
170: if (cvs_ent_get(ef, ent->ce_name) != NULL)
171: return (-1);
172:
1.3 jfb 173: if (fseek(ef->cef_file, (long)0, SEEK_END) == -1) {
174: cvs_log(LP_ERRNO, "failed to seek to end of CVS/Entries file");
1.1 jfb 175: return (-1);
176: }
1.3 jfb 177: fprintf(ef->cef_file, "%s\n", ent->ce_line);
178:
1.4 jfb 179: TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
1.3 jfb 180: return (0);
181: }
182:
183:
184: /*
185: * cvs_ent_addln()
186: *
187: * Add a line to the Entries file.
188: */
189:
190: int
191: cvs_ent_addln(CVSENTRIES *ef, const char *line)
192: {
193: void *tmp;
194: struct cvs_ent *ent;
195:
196: if (ef->cef_file == NULL) {
197: cvs_log(LP_ERR, "Entries file is opened in read-only mode");
198: return (-1);
199: }
200:
201: ent = cvs_ent_parse(line);
202: if (ent == NULL)
203: return (-1);
204:
205: if (cvs_ent_get(ef, ent->ce_name) != NULL)
206: return (-1);
1.1 jfb 207:
1.4 jfb 208: TAILQ_INSERT_TAIL(&(ef->cef_ent), ent, ce_list);
1.1 jfb 209: return (0);
210: }
211:
212:
213: /*
214: * cvs_ent_get()
215: *
216: * Get the CVS entry from the Entries file <ef> whose 'name' portion matches
217: * <file>.
218: * Returns a pointer to the cvs entry structure on success, or NULL on failure.
219: */
220:
221: struct cvs_ent*
222: cvs_ent_get(CVSENTRIES *ef, const char *file)
223: {
224: u_int i;
1.4 jfb 225: struct cvs_ent *ep;
1.1 jfb 226:
1.4 jfb 227: TAILQ_FOREACH(ep, &(ef->cef_ent), ce_list)
228: if (strcmp(ep->ce_name, file) == 0)
229: return (ep);
1.1 jfb 230:
231: return (NULL);
232: }
233:
234:
235: /*
236: * cvs_ent_next()
237: *
1.4 jfb 238: * This function is used to iterate over the entries in an Entries file. The
239: * first call will return the first entry of the file and each subsequent call
240: * will return the entry following the last one returned.
1.1 jfb 241: * Returns a pointer to the cvs entry structure on success, or NULL on failure.
242: */
243:
244: struct cvs_ent*
245: cvs_ent_next(CVSENTRIES *ef)
246: {
1.5 ! jfb 247: if (ef->cef_cur == NULL)
1.4 jfb 248: ef->cef_cur = TAILQ_FIRST(&(ef->cef_ent));
1.5 ! jfb 249: else
! 250: ef->cef_cur = TAILQ_NEXT(ef->cef_cur, ce_list);
! 251: return (ef->cef_cur);
1.1 jfb 252: }
253:
254:
255: /*
256: * cvs_ent_parse()
257: *
258: * Parse a single line from a CVS/Entries file and return a cvs_entry structure
259: * containing all the parsed information.
260: */
261:
262: struct cvs_ent*
263: cvs_ent_parse(const char *entry)
264: {
265: int i;
266: char *fields[CVS_ENTRIES_NFIELDS], *sp, *dp;
267: struct cvs_ent *entp;
268:
269: entp = (struct cvs_ent *)malloc(sizeof(*entp));
270: if (entp == NULL) {
271: cvs_log(LP_ERRNO, "failed to allocate CVS entry");
272: return (NULL);
273: }
1.5 ! jfb 274: memset(entp, 0, sizeof(*entp));
1.1 jfb 275:
276: entp->ce_rev = rcsnum_alloc();
277: if (entp->ce_rev == NULL) {
278: free(entp);
279: return (NULL);
280: }
281:
282: entp->ce_line = strdup(entry);
283: if (entp->ce_line == NULL) {
1.5 ! jfb 284: cvs_ent_free(entp);
1.1 jfb 285: return (NULL);
286: }
287:
288: entp->ce_buf = strdup(entry);
289: if (entp->ce_buf == NULL) {
1.5 ! jfb 290: cvs_ent_free(entp);
1.1 jfb 291: return (NULL);
292: }
293: sp = entp->ce_buf;
294:
295: if (*sp == CVS_ENTRIES_DELIM)
296: entp->ce_type = CVS_ENT_FILE;
297: else if (*sp == 'D') {
298: entp->ce_type = CVS_ENT_DIR;
299: sp++;
300: }
301: else {
302: /* unknown entry, ignore for future expansion */
303: entp->ce_type = CVS_ENT_NONE;
304: sp++;
305: }
306:
307: sp++;
308: i = 0;
309: do {
310: dp = strchr(sp, CVS_ENTRIES_DELIM);
311: if (dp != NULL)
312: *(dp++) = '\0';
313: fields[i++] = sp;
314: sp = dp;
315: } while ((dp != NULL) && (i < CVS_ENTRIES_NFIELDS));
316:
317: entp->ce_name = fields[0];
318:
319: if (entp->ce_type == CVS_ENT_FILE) {
320: rcsnum_aton(fields[1], NULL, entp->ce_rev);
321: entp->ce_timestamp = fields[2];
322: entp->ce_opts = fields[3];
323: entp->ce_tag = fields[4];
324: }
325:
326: return (entp);
1.5 ! jfb 327: }
! 328:
! 329:
! 330: /*
! 331: * cvs_ent_free()
! 332: *
! 333: * Free a single CVS entries structure.
! 334: */
! 335:
! 336: void
! 337: cvs_ent_free(struct cvs_ent *ent)
! 338: {
! 339: if (ent->ce_rev != NULL)
! 340: rcsnum_free(ent->ce_rev);
! 341: if (ent->ce_line != NULL)
! 342: free(ent->ce_line);
! 343: if (ent->ce_buf != NULL)
! 344: free(ent->ce_buf);
! 345: free(ent);
! 346: }
! 347:
! 348:
! 349: /*
! 350: * cvs_ent_getent()
! 351: *
! 352: * Get a single entry from the CVS/Entries file of the basename portion of
! 353: * path <path> and return that entry. That entry must later be freed using
! 354: * cvs_ent_free().
! 355: */
! 356:
! 357: struct cvs_ent*
! 358: cvs_ent_getent(const char *path)
! 359: {
! 360: char base[MAXPATHLEN], file[MAXPATHLEN];
! 361: CVSENTRIES *entf;
! 362: struct cvs_ent *ep;
! 363:
! 364: cvs_splitpath(path, base, sizeof(base), file, sizeof(file));
! 365:
! 366: entf = cvs_ent_open(base, O_RDONLY);
! 367: if (entf == NULL)
! 368: return (NULL);
! 369:
! 370: ep = cvs_ent_get(entf, file);
! 371: if (ep != NULL) {
! 372: /* take it out of the queue so it doesn't get freed */
! 373: TAILQ_REMOVE(&(entf->cef_ent), ep, ce_list);
! 374: }
! 375:
! 376: cvs_ent_close(entf);
! 377: return (ep);
1.1 jfb 378: }