Annotation of src/usr.bin/cvs/entries.c, Revision 1.95
1.95 ! deraadt 1: /* $OpenBSD: entries.c,v 1.94 2008/02/10 14:08:52 xsa 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.75 otto 18: #include <errno.h>
19: #include <string.h>
1.83 chl 20: #include <time.h>
1.75 otto 21: #include <unistd.h>
1.1 jfb 22:
1.34 xsa 23: #include "cvs.h"
1.81 joris 24: #include "remote.h"
1.1 jfb 25:
1.42 xsa 26: #define CVS_ENTRIES_NFIELDS 6
27: #define CVS_ENTRIES_DELIM '/'
1.1 jfb 28:
1.57 joris 29: static struct cvs_ent_line *ent_get_line(CVSENTRIES *, const char *);
1.1 jfb 30:
1.42 xsa 31: CVSENTRIES *
1.57 joris 32: cvs_ent_open(const char *dir)
1.1 jfb 33: {
1.57 joris 34: FILE *fp;
35: CVSENTRIES *ep;
36: char *p, buf[MAXPATHLEN];
1.1 jfb 37: struct cvs_ent *ent;
1.57 joris 38: struct cvs_ent_line *line;
1.36 xsa 39:
1.86 tobias 40: ep = (CVSENTRIES *)xcalloc(1, sizeof(*ep));
1.52 joris 41:
1.73 xsa 42: (void)xsnprintf(buf, sizeof(buf), "%s/%s", dir, CVS_PATH_ENTRIES);
1.64 xsa 43:
1.57 joris 44: ep->cef_path = xstrdup(buf);
1.8 jfb 45:
1.73 xsa 46: (void)xsnprintf(buf, sizeof(buf), "%s/%s",
47: dir, CVS_PATH_BACKUPENTRIES);
1.64 xsa 48:
1.57 joris 49: ep->cef_bpath = xstrdup(buf);
1.3 jfb 50:
1.73 xsa 51: (void)xsnprintf(buf, sizeof(buf), "%s/%s", dir, CVS_PATH_LOGENTRIES);
1.64 xsa 52:
1.57 joris 53: ep->cef_lpath = xstrdup(buf);
1.1 jfb 54:
1.4 jfb 55: TAILQ_INIT(&(ep->cef_ent));
1.3 jfb 56:
1.57 joris 57: if ((fp = fopen(ep->cef_path, "r")) != NULL) {
58: while (fgets(buf, sizeof(buf), fp)) {
1.84 gilles 59: buf[strcspn(buf, "\n")] = '\0';
1.57 joris 60:
61: if (buf[0] == 'D' && buf[1] == '\0')
62: break;
63:
64: line = (struct cvs_ent_line *)xmalloc(sizeof(*line));
65: line->buf = xstrdup(buf);
66: TAILQ_INSERT_TAIL(&(ep->cef_ent), line, entries_list);
67: }
1.52 joris 68:
1.25 jfb 69: (void)fclose(fp);
1.12 jfb 70: }
1.1 jfb 71:
1.57 joris 72: if ((fp = fopen(ep->cef_lpath, "r")) != NULL) {
73: while (fgets(buf, sizeof(buf), fp)) {
1.84 gilles 74: buf[strcspn(buf, "\n")] = '\0';
1.57 joris 75:
1.87 tobias 76: if (strlen(buf) < 2)
77: fatal("cvs_ent_open: %s: malformed line %s",
78: ep->cef_lpath, buf);
79:
80: p = &buf[2];
1.57 joris 81:
82: if (buf[0] == 'A') {
83: line = xmalloc(sizeof(*line));
84: line->buf = xstrdup(p);
85: TAILQ_INSERT_TAIL(&(ep->cef_ent), line,
86: entries_list);
87: } else if (buf[0] == 'R') {
88: ent = cvs_ent_parse(p);
89: line = ent_get_line(ep, ent->ce_name);
1.65 joris 90: if (line != NULL) {
1.57 joris 91: TAILQ_REMOVE(&(ep->cef_ent), line,
92: entries_list);
1.65 joris 93: xfree(line->buf);
94: xfree(line);
95: }
1.57 joris 96: cvs_ent_free(ent);
97: }
98: }
1.4 jfb 99:
1.52 joris 100: (void)fclose(fp);
101: }
1.12 jfb 102:
1.1 jfb 103: return (ep);
104: }
105:
1.57 joris 106: struct cvs_ent *
107: cvs_ent_parse(const char *entry)
1.1 jfb 108: {
1.57 joris 109: int i;
1.91 joris 110: struct tm t, dt;
1.5 jfb 111: struct cvs_ent *ent;
1.57 joris 112: char *fields[CVS_ENTRIES_NFIELDS], *buf, *sp, *dp;
1.5 jfb 113:
1.72 joris 114: buf = sp = xstrdup(entry);
1.57 joris 115: i = 0;
116: do {
117: dp = strchr(sp, CVS_ENTRIES_DELIM);
118: if (dp != NULL)
119: *(dp++) = '\0';
120: fields[i++] = sp;
121: sp = dp;
122: } while (dp != NULL && i < CVS_ENTRIES_NFIELDS);
1.8 jfb 123:
1.57 joris 124: if (i < CVS_ENTRIES_NFIELDS)
125: fatal("missing fields in entry line '%s'", entry);
1.5 jfb 126:
1.72 joris 127: ent = xmalloc(sizeof(*ent));
1.57 joris 128: ent->ce_buf = buf;
1.52 joris 129:
1.57 joris 130: if (*fields[0] == '\0')
131: ent->ce_type = CVS_ENT_FILE;
132: else if (*fields[0] == 'D')
133: ent->ce_type = CVS_ENT_DIR;
134: else
135: ent->ce_type = CVS_ENT_NONE;
136:
137: ent->ce_status = CVS_ENT_REG;
138: ent->ce_name = fields[1];
139: ent->ce_rev = NULL;
1.92 joris 140: ent->ce_date = -1;
141: ent->ce_tag = NULL;
1.5 jfb 142:
1.57 joris 143: if (ent->ce_type == CVS_ENT_FILE) {
144: if (*fields[2] == '-') {
145: ent->ce_status = CVS_ENT_REMOVED;
146: sp = fields[2] + 1;
147: } else {
148: sp = fields[2];
149: if (fields[2][0] == '0' && fields[2][1] == '\0')
150: ent->ce_status = CVS_ENT_ADDED;
151: }
1.1 jfb 152:
1.57 joris 153: if ((ent->ce_rev = rcsnum_parse(sp)) == NULL)
154: fatal("failed to parse entry revision '%s'", entry);
1.1 jfb 155:
1.72 joris 156: if (fields[3][0] == '\0' ||
1.77 ray 157: strncmp(fields[3], CVS_DATE_DUMMY, sizeof(CVS_DATE_DUMMY) - 1) == 0 ||
1.57 joris 158: strncmp(fields[3], "Initial ", 8) == 0 ||
1.81 joris 159: strncmp(fields[3], "Result of merge", 15) == 0) {
1.57 joris 160: ent->ce_mtime = CVS_DATE_DMSEC;
1.81 joris 161: } else if (cvs_server_active == 1 &&
162: strncmp(fields[3], CVS_SERVER_UNCHANGED,
163: strlen(CVS_SERVER_UNCHANGED)) == 0) {
164: ent->ce_mtime = CVS_SERVER_UPTODATE;
165: } else {
1.78 niallo 166: /* Date field can be a '+=' with remote to indicate
167: * conflict. In this case do nothing. */
168: if (strptime(fields[3], "%a %b %d %T %Y", &t) != NULL) {
169:
170: t.tm_isdst = -1; /* Figure out DST. */
171: t.tm_gmtoff = 0;
172: ent->ce_mtime = mktime(&t);
173: ent->ce_mtime += t.tm_gmtoff;
174: }
1.70 joris 175: }
1.3 jfb 176: }
1.1 jfb 177:
1.57 joris 178: ent->ce_conflict = fields[3];
179: if ((dp = strchr(ent->ce_conflict, '+')) != NULL)
180: *dp = '\0';
181: else
182: ent->ce_conflict = NULL;
1.1 jfb 183:
1.57 joris 184: if (strcmp(fields[4], ""))
185: ent->ce_opts = fields[4];
186: else
187: ent->ce_opts = NULL;
1.8 jfb 188:
1.91 joris 189: if (strcmp(fields[5], "")) {
1.95 ! deraadt 190: switch (*fields[5]) {
1.91 joris 191: case 'D':
192: if (sscanf(fields[5] + 1, "%d.%d.%d.%d.%d.%d",
193: &dt.tm_year, &dt.tm_mon, &dt.tm_mday,
194: &dt.tm_hour, &dt.tm_min, &dt.tm_sec) != 6)
195: fatal("wrong date specification");
196: dt.tm_year -= 1900;
197: dt.tm_mon -= 1;
198: ent->ce_date = timegm(&dt);
199: ent->ce_tag = NULL;
200: break;
201: case 'T':
202: ent->ce_tag = fields[5] + 1;
203: break;
204: default:
205: fatal("invalid sticky entry");
206: }
1.92 joris 207: }
1.3 jfb 208:
1.57 joris 209: return (ent);
1.3 jfb 210: }
211:
1.57 joris 212: struct cvs_ent *
213: cvs_ent_get(CVSENTRIES *ep, const char *name)
1.3 jfb 214: {
215: struct cvs_ent *ent;
1.57 joris 216: struct cvs_ent_line *l;
1.3 jfb 217:
1.57 joris 218: l = ent_get_line(ep, name);
219: if (l == NULL)
220: return (NULL);
1.3 jfb 221:
1.57 joris 222: ent = cvs_ent_parse(l->buf);
223: return (ent);
1.1 jfb 224: }
225:
1.57 joris 226: void
227: cvs_ent_close(CVSENTRIES *ep, int writefile)
1.9 jfb 228: {
1.57 joris 229: FILE *fp;
230: struct cvs_ent_line *l;
1.85 tobias 231: int dflag;
232:
233: dflag = 1;
1.44 xsa 234:
1.57 joris 235: if (writefile) {
236: if ((fp = fopen(ep->cef_bpath, "w")) == NULL)
1.63 xsa 237: fatal("cvs_ent_close: fopen: `%s': %s",
238: ep->cef_path, strerror(errno));
1.57 joris 239: }
1.16 jfb 240:
1.57 joris 241: while ((l = TAILQ_FIRST(&(ep->cef_ent))) != NULL) {
242: if (writefile) {
1.85 tobias 243: if (l->buf[0] == 'D')
244: dflag = 0;
245:
1.57 joris 246: fputs(l->buf, fp);
247: fputc('\n', fp);
1.51 joris 248: }
1.57 joris 249:
250: TAILQ_REMOVE(&(ep->cef_ent), l, entries_list);
251: xfree(l->buf);
252: xfree(l);
1.22 jfb 253: }
1.9 jfb 254:
1.57 joris 255: if (writefile) {
1.85 tobias 256: if (dflag) {
257: fputc('D', fp);
258: fputc('\n', fp);
259: }
1.57 joris 260: (void)fclose(fp);
261:
262: if (rename(ep->cef_bpath, ep->cef_path) == -1)
1.63 xsa 263: fatal("cvs_ent_close: rename: `%s'->`%s': %s",
264: ep->cef_bpath, ep->cef_path, strerror(errno));
1.57 joris 265:
266: (void)unlink(ep->cef_lpath);
267: }
1.9 jfb 268:
1.57 joris 269: xfree(ep->cef_path);
270: xfree(ep->cef_bpath);
271: xfree(ep->cef_lpath);
272: xfree(ep);
1.9 jfb 273: }
274:
1.57 joris 275: void
276: cvs_ent_add(CVSENTRIES *ep, const char *line)
1.1 jfb 277: {
1.57 joris 278: FILE *fp;
279: struct cvs_ent_line *l;
1.39 xsa 280: struct cvs_ent *ent;
1.1 jfb 281:
1.57 joris 282: if ((ent = cvs_ent_parse(line)) == NULL)
283: fatal("cvs_ent_add: parsing failed '%s'", line);
1.1 jfb 284:
1.57 joris 285: l = ent_get_line(ep, ent->ce_name);
286: if (l != NULL)
287: cvs_ent_remove(ep, ent->ce_name);
1.1 jfb 288:
1.57 joris 289: cvs_ent_free(ent);
1.1 jfb 290:
1.61 joris 291: if (cvs_server_active == 0)
292: cvs_log(LP_TRACE, "cvs_ent_add(%s, %s)", ep->cef_path, line);
1.1 jfb 293:
1.57 joris 294: if ((fp = fopen(ep->cef_lpath, "a")) == NULL)
1.63 xsa 295: fatal("cvs_ent_add: fopen: `%s': %s",
296: ep->cef_lpath, strerror(errno));
1.1 jfb 297:
1.87 tobias 298: fputs("A ", fp);
1.57 joris 299: fputs(line, fp);
300: fputc('\n', fp);
1.1 jfb 301:
1.57 joris 302: (void)fclose(fp);
1.10 jfb 303:
1.57 joris 304: l = (struct cvs_ent_line *)xmalloc(sizeof(*l));
305: l->buf = xstrdup(line);
306: TAILQ_INSERT_TAIL(&(ep->cef_ent), l, entries_list);
307: }
1.10 jfb 308:
1.57 joris 309: void
310: cvs_ent_remove(CVSENTRIES *ep, const char *name)
311: {
312: FILE *fp;
313: struct cvs_ent_line *l;
1.1 jfb 314:
1.61 joris 315: if (cvs_server_active == 0)
316: cvs_log(LP_TRACE, "cvs_ent_remove(%s, %s)", ep->cef_path, name);
1.1 jfb 317:
1.57 joris 318: l = ent_get_line(ep, name);
319: if (l == NULL)
320: return;
1.1 jfb 321:
1.57 joris 322: if ((fp = fopen(ep->cef_lpath, "a")) == NULL)
1.63 xsa 323: fatal("cvs_ent_remove: fopen: `%s': %s", ep->cef_lpath,
324: strerror(errno));
1.38 joris 325:
1.87 tobias 326: fputs("R ", fp);
1.57 joris 327: fputs(l->buf, fp);
328: fputc('\n', fp);
1.24 jfb 329:
1.57 joris 330: (void)fclose(fp);
1.24 jfb 331:
1.57 joris 332: TAILQ_REMOVE(&(ep->cef_ent), l, entries_list);
333: xfree(l->buf);
334: xfree(l);
1.90 xsa 335: }
336:
337: /*
338: * cvs_ent_line_str()
339: *
340: * Build CVS/Entries line.
341: *
342: */
343: void
344: cvs_ent_line_str(const char *name, char *rev, char *tstamp, char *opts,
345: char *sticky, int isdir, int isremoved, char *buf, size_t len)
346: {
347: if (isdir == 1) {
348: (void)xsnprintf(buf, len, "D/%s////", name);
349: return;
350: }
351:
352: (void)xsnprintf(buf, len, "/%s/%s%s/%s/%s/%s",
353: name, isremoved == 1 ? "-" : "", rev, tstamp, opts, sticky);
1.5 jfb 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);
1.57 joris 361: xfree(ent->ce_buf);
1.53 joris 362: xfree(ent);
1.5 jfb 363: }
1.8 jfb 364:
1.57 joris 365: static struct cvs_ent_line *
366: ent_get_line(CVSENTRIES *ep, const char *name)
1.8 jfb 367: {
1.57 joris 368: char *p, *s;
369: struct cvs_ent_line *l;
1.8 jfb 370:
1.57 joris 371: TAILQ_FOREACH(l, &(ep->cef_ent), entries_list) {
372: if (l->buf[0] == 'D')
373: p = &(l->buf[2]);
374: else
375: p = &(l->buf[1]);
376:
377: if ((s = strchr(p, '/')) == NULL)
378: fatal("ent_get_line: bad entry line '%s'", l->buf);
379:
380: *s = '\0';
381:
382: if (!strcmp(p, name)) {
383: *s = '/';
384: return (l);
1.15 jfb 385: }
1.8 jfb 386:
1.57 joris 387: *s = '/';
1.8 jfb 388: }
389:
1.57 joris 390: return (NULL);
1.59 xsa 391: }
392:
393: void
394: cvs_parse_tagfile(char *dir, char **tagp, char **datep, int *nbp)
395: {
396: FILE *fp;
1.73 xsa 397: int i, linenum;
1.59 xsa 398: size_t len;
1.91 joris 399: struct tm datetm;
1.69 otto 400: char linebuf[128], tagpath[MAXPATHLEN];
1.59 xsa 401:
402: if (tagp != NULL)
1.60 joris 403: *tagp = NULL;
1.59 xsa 404:
405: if (datep != NULL)
1.60 joris 406: *datep = NULL;
1.59 xsa 407:
408: if (nbp != NULL)
409: *nbp = 0;
410:
1.73 xsa 411: i = snprintf(tagpath, MAXPATHLEN, "%s/%s", dir, CVS_PATH_TAG);
412: if (i < 0 || i >= MAXPATHLEN)
1.69 otto 413: return;
1.59 xsa 414:
415: if ((fp = fopen(tagpath, "r")) == NULL) {
416: if (errno != ENOENT)
417: cvs_log(LP_NOTICE, "failed to open `%s' : %s", tagpath,
418: strerror(errno));
1.69 otto 419: return;
1.59 xsa 420: }
421:
422: linenum = 0;
423:
424: while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
425: linenum++;
426: if ((len = strlen(linebuf)) == 0)
427: continue;
428: if (linebuf[len - 1] != '\n') {
429: cvs_log(LP_NOTICE, "line too long in `%s:%d'",
430: tagpath, linenum);
431: break;
432: }
433: linebuf[--len] = '\0';
434:
435: switch (*linebuf) {
436: case 'T':
437: if (tagp != NULL)
438: *tagp = xstrdup(linebuf + 1);
439: break;
440: case 'D':
1.91 joris 441: if (sscanf(linebuf + 1, "%d.%d.%d.%d.%d.%d",
442: &datetm.tm_year, &datetm.tm_mon, &datetm.tm_mday,
443: &datetm.tm_hour, &datetm.tm_min, &datetm.tm_sec) !=
444: 6)
445: fatal("wrong date specification");
446: datetm.tm_year -= 1900;
447: datetm.tm_mon -= 1;
448:
1.92 joris 449: if (cvs_specified_date == -1)
1.91 joris 450: cvs_specified_date = timegm(&datetm);
451:
1.59 xsa 452: if (datep != NULL)
453: *datep = xstrdup(linebuf + 1);
454: break;
455: case 'N':
456: if (tagp != NULL)
457: *tagp = xstrdup(linebuf + 1);
458: if (nbp != NULL)
459: *nbp = 1;
460: break;
461: default:
462: break;
463: }
464: }
465: if (ferror(fp))
466: cvs_log(LP_NOTICE, "failed to read line from `%s'", tagpath);
467:
468: (void)fclose(fp);
469: }
470:
471: void
1.88 tobias 472: cvs_write_tagfile(const char *dir, char *tag, char *date)
1.59 xsa 473: {
474: FILE *fp;
1.88 tobias 475: RCSNUM *rev;
1.69 otto 476: char tagpath[MAXPATHLEN];
1.82 joris 477: char sticky[CVS_REV_BUFSZ];
1.89 tobias 478: struct tm *datetm;
1.73 xsa 479: int i;
1.59 xsa 480:
1.88 tobias 481: cvs_log(LP_TRACE, "cvs_write_tagfile(%s, %s, %s)", dir,
482: tag != NULL ? tag : "", date != NULL ? date : "");
483:
1.59 xsa 484: if (cvs_noexec == 1)
485: return;
486:
1.73 xsa 487: i = snprintf(tagpath, MAXPATHLEN, "%s/%s", dir, CVS_PATH_TAG);
488: if (i < 0 || i >= MAXPATHLEN)
1.69 otto 489: return;
1.59 xsa 490:
1.92 joris 491: if (tag != NULL || cvs_specified_date != -1) {
1.59 xsa 492: if ((fp = fopen(tagpath, "w+")) == NULL) {
493: if (errno != ENOENT) {
494: cvs_log(LP_NOTICE, "failed to open `%s' : %s",
495: tagpath, strerror(errno));
496: }
1.69 otto 497: return;
1.59 xsa 498: }
1.79 joris 499:
1.59 xsa 500: if (tag != NULL) {
1.88 tobias 501: if ((rev = rcsnum_parse(tag)) != NULL) {
1.82 joris 502: (void)xsnprintf(sticky, sizeof(sticky),
503: "N%s", tag);
1.88 tobias 504: rcsnum_free(rev);
1.82 joris 505: } else {
506: (void)xsnprintf(sticky, sizeof(sticky),
507: "T%s", tag);
508: }
509: } else {
1.89 tobias 510: datetm = gmtime(&cvs_specified_date);
1.94 xsa 511: (void)strftime(sticky, sizeof(sticky),
512: "D"CVS_DATE_FMT, datetm);
1.82 joris 513: }
514:
515: if (cvs_server_active == 1)
516: cvs_server_set_sticky(dir, sticky);
1.59 xsa 517:
1.82 joris 518: (void)fprintf(fp, "%s\n", sticky);
1.59 xsa 519: (void)fclose(fp);
520: }
1.1 jfb 521: }