Annotation of src/usr.bin/cvs/entries.c, Revision 1.67
1.67 ! pyr 1: /* $OpenBSD: entries.c,v 1.66 2007/01/24 08:28:46 pyr Exp $ */
1.1 jfb 2: /*
1.57 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.57 joris 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.54 xsa 18: #include "includes.h"
1.1 jfb 19:
1.34 xsa 20: #include "cvs.h"
1.1 jfb 21: #include "log.h"
22:
1.42 xsa 23: #define CVS_ENTRIES_NFIELDS 6
24: #define CVS_ENTRIES_DELIM '/'
1.1 jfb 25:
1.57 joris 26: static struct cvs_ent_line *ent_get_line(CVSENTRIES *, const char *);
1.1 jfb 27:
1.42 xsa 28: CVSENTRIES *
1.57 joris 29: cvs_ent_open(const char *dir)
1.1 jfb 30: {
1.57 joris 31: FILE *fp;
1.1 jfb 32: size_t len;
1.57 joris 33: CVSENTRIES *ep;
34: char *p, buf[MAXPATHLEN];
1.1 jfb 35: struct cvs_ent *ent;
1.57 joris 36: struct cvs_ent_line *line;
1.36 xsa 37:
1.57 joris 38: ep = (CVSENTRIES *)xmalloc(sizeof(*ep));
39: memset(ep, 0, sizeof(*ep));
1.52 joris 40:
1.64 xsa 41: if (cvs_path_cat(dir, CVS_PATH_ENTRIES, buf, sizeof(buf)) >=
42: sizeof(buf))
43: fatal("cvs_ent_open: truncation");
44:
1.57 joris 45: ep->cef_path = xstrdup(buf);
1.8 jfb 46:
1.64 xsa 47: if (cvs_path_cat(dir, CVS_PATH_BACKUPENTRIES, buf, sizeof(buf)) >=
48: sizeof(buf))
49: fatal("cvs_ent_open: truncation");
50:
1.57 joris 51: ep->cef_bpath = xstrdup(buf);
1.3 jfb 52:
1.64 xsa 53: if (cvs_path_cat(dir, CVS_PATH_LOGENTRIES, buf, sizeof(buf)) >=
54: sizeof(buf))
55: fatal("cvs_ent_open: truncation");
56:
1.57 joris 57: ep->cef_lpath = xstrdup(buf);
1.1 jfb 58:
1.4 jfb 59: TAILQ_INIT(&(ep->cef_ent));
1.3 jfb 60:
1.57 joris 61: if ((fp = fopen(ep->cef_path, "r")) != NULL) {
62: while (fgets(buf, sizeof(buf), fp)) {
63: len = strlen(buf);
64: if (len > 0 && buf[len - 1] == '\n')
65: buf[len - 1] = '\0';
66:
67: if (buf[0] == 'D' && buf[1] == '\0')
68: break;
69:
70: line = (struct cvs_ent_line *)xmalloc(sizeof(*line));
71: line->buf = xstrdup(buf);
72: TAILQ_INSERT_TAIL(&(ep->cef_ent), line, entries_list);
73: }
1.52 joris 74:
1.25 jfb 75: (void)fclose(fp);
1.12 jfb 76: }
1.1 jfb 77:
1.57 joris 78: if ((fp = fopen(ep->cef_lpath, "r")) != NULL) {
79: while (fgets(buf, sizeof(buf), fp)) {
80: len = strlen(buf);
1.62 ray 81: if (len > 0 && buf[len - 1] == '\n')
82: buf[len - 1] = '\0';
1.57 joris 83:
84: p = &buf[1];
85:
86: if (buf[0] == 'A') {
87: line = xmalloc(sizeof(*line));
88: line->buf = xstrdup(p);
89: TAILQ_INSERT_TAIL(&(ep->cef_ent), line,
90: entries_list);
91: } else if (buf[0] == 'R') {
92: ent = cvs_ent_parse(p);
93: line = ent_get_line(ep, ent->ce_name);
1.65 joris 94: if (line != NULL) {
1.57 joris 95: TAILQ_REMOVE(&(ep->cef_ent), line,
96: entries_list);
1.65 joris 97: xfree(line->buf);
98: xfree(line);
99: }
1.57 joris 100: cvs_ent_free(ent);
101: }
102: }
1.4 jfb 103:
1.52 joris 104: (void)fclose(fp);
105: }
1.12 jfb 106:
1.1 jfb 107: return (ep);
108: }
109:
1.57 joris 110: struct cvs_ent *
111: cvs_ent_parse(const char *entry)
1.1 jfb 112: {
1.57 joris 113: int i;
1.5 jfb 114: struct cvs_ent *ent;
1.57 joris 115: char *fields[CVS_ENTRIES_NFIELDS], *buf, *sp, *dp;
1.5 jfb 116:
1.57 joris 117: buf = xstrdup(entry);
118: sp = buf;
119: i = 0;
120: do {
121: dp = strchr(sp, CVS_ENTRIES_DELIM);
122: if (dp != NULL)
123: *(dp++) = '\0';
124: fields[i++] = sp;
125: sp = dp;
126: } while (dp != NULL && i < CVS_ENTRIES_NFIELDS);
1.8 jfb 127:
1.57 joris 128: if (i < CVS_ENTRIES_NFIELDS)
129: fatal("missing fields in entry line '%s'", entry);
1.5 jfb 130:
1.57 joris 131: ent = (struct cvs_ent *)xmalloc(sizeof(*ent));
132: ent->ce_buf = buf;
1.52 joris 133:
1.57 joris 134: if (*fields[0] == '\0')
135: ent->ce_type = CVS_ENT_FILE;
136: else if (*fields[0] == 'D')
137: ent->ce_type = CVS_ENT_DIR;
138: else
139: ent->ce_type = CVS_ENT_NONE;
140:
141: ent->ce_status = CVS_ENT_REG;
142: ent->ce_name = fields[1];
143: ent->ce_rev = NULL;
1.5 jfb 144:
1.57 joris 145: if (ent->ce_type == CVS_ENT_FILE) {
146: if (*fields[2] == '-') {
147: ent->ce_status = CVS_ENT_REMOVED;
148: sp = fields[2] + 1;
149: } else {
150: sp = fields[2];
151: if (fields[2][0] == '0' && fields[2][1] == '\0')
152: ent->ce_status = CVS_ENT_ADDED;
153: }
1.1 jfb 154:
1.57 joris 155: if ((ent->ce_rev = rcsnum_parse(sp)) == NULL)
156: fatal("failed to parse entry revision '%s'", entry);
1.1 jfb 157:
1.57 joris 158: if (strcmp(fields[3], CVS_DATE_DUMMY) == 0 ||
159: strncmp(fields[3], "Initial ", 8) == 0 ||
160: strncmp(fields[3], "Result of merge", 15) == 0)
161: ent->ce_mtime = CVS_DATE_DMSEC;
162: else
163: ent->ce_mtime = cvs_date_parse(fields[3]);
1.3 jfb 164: }
1.1 jfb 165:
1.57 joris 166: ent->ce_conflict = fields[3];
167: if ((dp = strchr(ent->ce_conflict, '+')) != NULL)
168: *dp = '\0';
169: else
170: ent->ce_conflict = NULL;
1.1 jfb 171:
1.57 joris 172: if (strcmp(fields[4], ""))
173: ent->ce_opts = fields[4];
174: else
175: ent->ce_opts = NULL;
1.8 jfb 176:
1.57 joris 177: if (strcmp(fields[5], ""))
1.58 joris 178: ent->ce_tag = fields[5] + 1;
1.57 joris 179: else
180: ent->ce_tag = NULL;
1.3 jfb 181:
1.57 joris 182: return (ent);
1.3 jfb 183: }
184:
1.57 joris 185: struct cvs_ent *
186: cvs_ent_get(CVSENTRIES *ep, const char *name)
1.3 jfb 187: {
188: struct cvs_ent *ent;
1.57 joris 189: struct cvs_ent_line *l;
1.3 jfb 190:
1.57 joris 191: l = ent_get_line(ep, name);
192: if (l == NULL)
193: return (NULL);
1.3 jfb 194:
1.57 joris 195: ent = cvs_ent_parse(l->buf);
196: return (ent);
197: }
1.3 jfb 198:
1.57 joris 199: int
200: cvs_ent_exists(CVSENTRIES *ep, const char *name)
201: {
202: struct cvs_ent_line *l;
1.1 jfb 203:
1.57 joris 204: l = ent_get_line(ep, name);
205: if (l == NULL)
206: return (0);
1.8 jfb 207:
1.57 joris 208: return (1);
1.1 jfb 209: }
210:
1.57 joris 211: void
212: cvs_ent_close(CVSENTRIES *ep, int writefile)
1.9 jfb 213: {
1.57 joris 214: FILE *fp;
215: struct cvs_ent_line *l;
1.44 xsa 216:
1.57 joris 217: if (writefile) {
218: if ((fp = fopen(ep->cef_bpath, "w")) == NULL)
1.63 xsa 219: fatal("cvs_ent_close: fopen: `%s': %s",
220: ep->cef_path, strerror(errno));
1.57 joris 221: }
1.16 jfb 222:
1.57 joris 223: while ((l = TAILQ_FIRST(&(ep->cef_ent))) != NULL) {
224: if (writefile) {
225: fputs(l->buf, fp);
226: fputc('\n', fp);
1.51 joris 227: }
1.57 joris 228:
229: TAILQ_REMOVE(&(ep->cef_ent), l, entries_list);
230: xfree(l->buf);
231: xfree(l);
1.22 jfb 232: }
1.9 jfb 233:
1.57 joris 234: if (writefile) {
235: fputc('D', fp);
236: (void)fclose(fp);
237:
238: if (rename(ep->cef_bpath, ep->cef_path) == -1)
1.63 xsa 239: fatal("cvs_ent_close: rename: `%s'->`%s': %s",
240: ep->cef_bpath, ep->cef_path, strerror(errno));
1.57 joris 241:
242: (void)unlink(ep->cef_lpath);
243: }
1.9 jfb 244:
1.57 joris 245: xfree(ep->cef_path);
246: xfree(ep->cef_bpath);
247: xfree(ep->cef_lpath);
248: xfree(ep);
1.9 jfb 249: }
250:
1.57 joris 251: void
252: cvs_ent_add(CVSENTRIES *ep, const char *line)
1.1 jfb 253: {
1.57 joris 254: FILE *fp;
255: struct cvs_ent_line *l;
1.39 xsa 256: struct cvs_ent *ent;
1.1 jfb 257:
1.57 joris 258: if ((ent = cvs_ent_parse(line)) == NULL)
259: fatal("cvs_ent_add: parsing failed '%s'", line);
1.1 jfb 260:
1.57 joris 261: l = ent_get_line(ep, ent->ce_name);
262: if (l != NULL)
263: cvs_ent_remove(ep, ent->ce_name);
1.1 jfb 264:
1.57 joris 265: cvs_ent_free(ent);
1.1 jfb 266:
1.61 joris 267: if (cvs_server_active == 0)
268: cvs_log(LP_TRACE, "cvs_ent_add(%s, %s)", ep->cef_path, line);
1.1 jfb 269:
1.57 joris 270: if ((fp = fopen(ep->cef_lpath, "a")) == NULL)
1.63 xsa 271: fatal("cvs_ent_add: fopen: `%s': %s",
272: ep->cef_lpath, strerror(errno));
1.1 jfb 273:
1.57 joris 274: fputc('A', fp);
275: fputs(line, fp);
276: fputc('\n', fp);
1.1 jfb 277:
1.57 joris 278: (void)fclose(fp);
1.10 jfb 279:
1.57 joris 280: l = (struct cvs_ent_line *)xmalloc(sizeof(*l));
281: l->buf = xstrdup(line);
282: TAILQ_INSERT_TAIL(&(ep->cef_ent), l, entries_list);
283: }
1.10 jfb 284:
1.57 joris 285: void
286: cvs_ent_remove(CVSENTRIES *ep, const char *name)
287: {
288: FILE *fp;
289: struct cvs_ent_line *l;
1.1 jfb 290:
1.61 joris 291: if (cvs_server_active == 0)
292: cvs_log(LP_TRACE, "cvs_ent_remove(%s, %s)", ep->cef_path, name);
1.1 jfb 293:
1.57 joris 294: l = ent_get_line(ep, name);
295: if (l == NULL)
296: return;
1.1 jfb 297:
1.57 joris 298: if ((fp = fopen(ep->cef_lpath, "a")) == NULL)
1.63 xsa 299: fatal("cvs_ent_remove: fopen: `%s': %s", ep->cef_lpath,
300: strerror(errno));
1.38 joris 301:
1.57 joris 302: fputc('R', fp);
303: fputs(l->buf, fp);
304: fputc('\n', fp);
1.24 jfb 305:
1.57 joris 306: (void)fclose(fp);
1.24 jfb 307:
1.57 joris 308: TAILQ_REMOVE(&(ep->cef_ent), l, entries_list);
309: xfree(l->buf);
310: xfree(l);
1.5 jfb 311: }
312:
313: void
314: cvs_ent_free(struct cvs_ent *ent)
315: {
316: if (ent->ce_rev != NULL)
317: rcsnum_free(ent->ce_rev);
1.57 joris 318: xfree(ent->ce_buf);
1.53 joris 319: xfree(ent);
1.5 jfb 320: }
1.8 jfb 321:
1.57 joris 322: static struct cvs_ent_line *
323: ent_get_line(CVSENTRIES *ep, const char *name)
1.8 jfb 324: {
1.57 joris 325: char *p, *s;
326: struct cvs_ent_line *l;
1.8 jfb 327:
1.57 joris 328: TAILQ_FOREACH(l, &(ep->cef_ent), entries_list) {
329: if (l->buf[0] == 'D')
330: p = &(l->buf[2]);
331: else
332: p = &(l->buf[1]);
333:
334: if ((s = strchr(p, '/')) == NULL)
335: fatal("ent_get_line: bad entry line '%s'", l->buf);
336:
337: *s = '\0';
338:
339: if (!strcmp(p, name)) {
340: *s = '/';
341: return (l);
1.15 jfb 342: }
1.8 jfb 343:
1.57 joris 344: *s = '/';
1.8 jfb 345: }
346:
1.57 joris 347: return (NULL);
1.59 xsa 348: }
349:
350: void
351: cvs_parse_tagfile(char *dir, char **tagp, char **datep, int *nbp)
352: {
353: FILE *fp;
354: int linenum;
355: size_t len;
356: char linebuf[128], *tagpath;
357:
358: if (tagp != NULL)
1.60 joris 359: *tagp = NULL;
1.59 xsa 360:
361: if (datep != NULL)
1.60 joris 362: *datep = NULL;
1.59 xsa 363:
364: if (nbp != NULL)
365: *nbp = 0;
366:
367: tagpath = xmalloc(MAXPATHLEN);
368:
369: if (cvs_path_cat(dir, CVS_PATH_TAG, tagpath, MAXPATHLEN) >= MAXPATHLEN)
370: goto out;
371:
372: if ((fp = fopen(tagpath, "r")) == NULL) {
373: if (errno != ENOENT)
374: cvs_log(LP_NOTICE, "failed to open `%s' : %s", tagpath,
375: strerror(errno));
376: goto out;
377: }
378:
379: linenum = 0;
380:
381: while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
382: linenum++;
383: if ((len = strlen(linebuf)) == 0)
384: continue;
385: if (linebuf[len - 1] != '\n') {
386: cvs_log(LP_NOTICE, "line too long in `%s:%d'",
387: tagpath, linenum);
388: break;
389: }
390: linebuf[--len] = '\0';
391:
392: switch (*linebuf) {
393: case 'T':
394: if (tagp != NULL)
395: *tagp = xstrdup(linebuf + 1);
396: break;
397: case 'D':
398: if (datep != NULL)
399: *datep = xstrdup(linebuf + 1);
400: break;
401: case 'N':
402: if (tagp != NULL)
403: *tagp = xstrdup(linebuf + 1);
404: if (nbp != NULL)
405: *nbp = 1;
406: break;
407: default:
408: break;
409: }
410: }
411: if (ferror(fp))
412: cvs_log(LP_NOTICE, "failed to read line from `%s'", tagpath);
413:
414: (void)fclose(fp);
415: out:
416: xfree(tagpath);
417: }
418:
419: void
420: cvs_write_tagfile(char *dir, char *tag, char *date, int nb)
421: {
422: FILE *fp;
423: char *tagpath;
424:
425: if (cvs_noexec == 1)
426: return;
427:
428: tagpath = xmalloc(MAXPATHLEN);
429:
430: if (cvs_path_cat(dir, CVS_PATH_TAG, tagpath, MAXPATHLEN) >= MAXPATHLEN)
431: goto out;
432:
433: if ((tag != NULL) || (date != NULL)) {
434: if ((fp = fopen(tagpath, "w+")) == NULL) {
435: if (errno != ENOENT) {
436: cvs_log(LP_NOTICE, "failed to open `%s' : %s",
437: tagpath, strerror(errno));
438: }
439: goto out;
440: }
441: if (tag != NULL) {
442: if (nb != 0)
443: (void)fprintf(fp, "N%s\n", tag);
444: else
445: (void)fprintf(fp, "T%s\n", tag);
446: } else
447: (void)fprintf(fp, "D%s\n", date);
448:
449: (void)fclose(fp);
450: } else {
451: (void)cvs_unlink(tagpath);
452: goto out;
453: }
454: out:
455: xfree(tagpath);
1.1 jfb 456: }