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