[BACK]Return to entries.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/entries.c, Revision 1.78

1.78    ! niallo      1: /*     $OpenBSD: entries.c,v 1.77 2007/05/26 02:30:28 ray 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>
                     20: #include <unistd.h>
1.1       jfb        21:
1.34      xsa        22: #include "cvs.h"
1.1       jfb        23:
1.42      xsa        24: #define CVS_ENTRIES_NFIELDS    6
                     25: #define CVS_ENTRIES_DELIM      '/'
1.1       jfb        26:
1.57      joris      27: static struct cvs_ent_line *ent_get_line(CVSENTRIES *, const char *);
1.1       jfb        28:
1.42      xsa        29: CVSENTRIES *
1.57      joris      30: cvs_ent_open(const char *dir)
1.1       jfb        31: {
1.57      joris      32:        FILE *fp;
1.1       jfb        33:        size_t len;
1.57      joris      34:        CVSENTRIES *ep;
                     35:        char *p, buf[MAXPATHLEN];
1.1       jfb        36:        struct cvs_ent *ent;
1.57      joris      37:        struct cvs_ent_line *line;
1.36      xsa        38:
1.57      joris      39:        ep = (CVSENTRIES *)xmalloc(sizeof(*ep));
                     40:        memset(ep, 0, 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)) {
                     59:                        len = strlen(buf);
                     60:                        if (len > 0 && buf[len - 1] == '\n')
                     61:                                buf[len - 1] = '\0';
                     62:
                     63:                        if (buf[0] == 'D' && buf[1] == '\0')
                     64:                                break;
                     65:
                     66:                        line = (struct cvs_ent_line *)xmalloc(sizeof(*line));
                     67:                        line->buf = xstrdup(buf);
                     68:                        TAILQ_INSERT_TAIL(&(ep->cef_ent), line, entries_list);
                     69:                }
1.52      joris      70:
1.25      jfb        71:                (void)fclose(fp);
1.12      jfb        72:        }
1.1       jfb        73:
1.57      joris      74:        if ((fp = fopen(ep->cef_lpath, "r")) != NULL) {
                     75:                while (fgets(buf, sizeof(buf), fp)) {
                     76:                        len = strlen(buf);
1.62      ray        77:                        if (len > 0 && buf[len - 1] == '\n')
                     78:                                buf[len - 1] = '\0';
1.57      joris      79:
                     80:                        p = &buf[1];
                     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.70      joris     110:        struct tm t;
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.5       jfb       140:
1.57      joris     141:        if (ent->ce_type == CVS_ENT_FILE) {
                    142:                if (*fields[2] == '-') {
                    143:                        ent->ce_status = CVS_ENT_REMOVED;
                    144:                        sp = fields[2] + 1;
                    145:                } else {
                    146:                        sp = fields[2];
                    147:                        if (fields[2][0] == '0' && fields[2][1] == '\0')
                    148:                                ent->ce_status = CVS_ENT_ADDED;
                    149:                }
1.1       jfb       150:
1.57      joris     151:                if ((ent->ce_rev = rcsnum_parse(sp)) == NULL)
                    152:                        fatal("failed to parse entry revision '%s'", entry);
1.1       jfb       153:
1.72      joris     154:                if (fields[3][0] == '\0' ||
1.77      ray       155:                    strncmp(fields[3], CVS_DATE_DUMMY, sizeof(CVS_DATE_DUMMY) - 1) == 0 ||
1.57      joris     156:                    strncmp(fields[3], "Initial ", 8) == 0 ||
                    157:                    strncmp(fields[3], "Result of merge", 15) == 0)
                    158:                        ent->ce_mtime = CVS_DATE_DMSEC;
1.70      joris     159:                else {
1.78    ! niallo    160:                        /* Date field can be a '+=' with remote to indicate
        !           161:                         * conflict.  In this case do nothing. */
        !           162:                        if (strptime(fields[3], "%a %b %d %T %Y", &t) != NULL) {
        !           163:
        !           164:                                t.tm_isdst = -1;        /* Figure out DST. */
        !           165:                                t.tm_gmtoff = 0;
        !           166:                                ent->ce_mtime = mktime(&t);
        !           167:                                ent->ce_mtime += t.tm_gmtoff;
        !           168:                        }
1.70      joris     169:                }
1.3       jfb       170:        }
1.1       jfb       171:
1.57      joris     172:        ent->ce_conflict = fields[3];
                    173:        if ((dp = strchr(ent->ce_conflict, '+')) != NULL)
                    174:                *dp = '\0';
                    175:        else
                    176:                ent->ce_conflict = NULL;
1.1       jfb       177:
1.57      joris     178:        if (strcmp(fields[4], ""))
                    179:                ent->ce_opts = fields[4];
                    180:        else
                    181:                ent->ce_opts = NULL;
1.8       jfb       182:
1.57      joris     183:        if (strcmp(fields[5], ""))
1.58      joris     184:                ent->ce_tag = fields[5] + 1;
1.57      joris     185:        else
                    186:                ent->ce_tag = NULL;
1.3       jfb       187:
1.57      joris     188:        return (ent);
1.3       jfb       189: }
                    190:
1.57      joris     191: struct cvs_ent *
                    192: cvs_ent_get(CVSENTRIES *ep, const char *name)
1.3       jfb       193: {
                    194:        struct cvs_ent *ent;
1.57      joris     195:        struct cvs_ent_line *l;
1.3       jfb       196:
1.57      joris     197:        l = ent_get_line(ep, name);
                    198:        if (l == NULL)
                    199:                return (NULL);
1.3       jfb       200:
1.57      joris     201:        ent = cvs_ent_parse(l->buf);
                    202:        return (ent);
                    203: }
1.3       jfb       204:
1.57      joris     205: int
                    206: cvs_ent_exists(CVSENTRIES *ep, const char *name)
                    207: {
                    208:        struct cvs_ent_line *l;
1.1       jfb       209:
1.57      joris     210:        l = ent_get_line(ep, name);
                    211:        if (l == NULL)
                    212:                return (0);
1.8       jfb       213:
1.57      joris     214:        return (1);
1.1       jfb       215: }
                    216:
1.57      joris     217: void
                    218: cvs_ent_close(CVSENTRIES *ep, int writefile)
1.9       jfb       219: {
1.57      joris     220:        FILE *fp;
                    221:        struct cvs_ent_line *l;
1.44      xsa       222:
1.57      joris     223:        if (writefile) {
                    224:                if ((fp = fopen(ep->cef_bpath, "w")) == NULL)
1.63      xsa       225:                        fatal("cvs_ent_close: fopen: `%s': %s",
                    226:                            ep->cef_path, strerror(errno));
1.57      joris     227:        }
1.16      jfb       228:
1.57      joris     229:        while ((l = TAILQ_FIRST(&(ep->cef_ent))) != NULL) {
                    230:                if (writefile) {
                    231:                        fputs(l->buf, fp);
                    232:                        fputc('\n', fp);
1.51      joris     233:                }
1.57      joris     234:
                    235:                TAILQ_REMOVE(&(ep->cef_ent), l, entries_list);
                    236:                xfree(l->buf);
                    237:                xfree(l);
1.22      jfb       238:        }
1.9       jfb       239:
1.57      joris     240:        if (writefile) {
                    241:                fputc('D', fp);
1.68      pyr       242:                fputc('\n', fp);
1.57      joris     243:                (void)fclose(fp);
                    244:
                    245:                if (rename(ep->cef_bpath, ep->cef_path) == -1)
1.63      xsa       246:                        fatal("cvs_ent_close: rename: `%s'->`%s': %s",
                    247:                            ep->cef_bpath, ep->cef_path, strerror(errno));
1.57      joris     248:
                    249:                (void)unlink(ep->cef_lpath);
                    250:        }
1.9       jfb       251:
1.57      joris     252:        xfree(ep->cef_path);
                    253:        xfree(ep->cef_bpath);
                    254:        xfree(ep->cef_lpath);
                    255:        xfree(ep);
1.9       jfb       256: }
                    257:
1.57      joris     258: void
                    259: cvs_ent_add(CVSENTRIES *ep, const char *line)
1.1       jfb       260: {
1.57      joris     261:        FILE *fp;
                    262:        struct cvs_ent_line *l;
1.39      xsa       263:        struct cvs_ent *ent;
1.1       jfb       264:
1.57      joris     265:        if ((ent = cvs_ent_parse(line)) == NULL)
                    266:                fatal("cvs_ent_add: parsing failed '%s'", line);
1.1       jfb       267:
1.57      joris     268:        l = ent_get_line(ep, ent->ce_name);
                    269:        if (l != NULL)
                    270:                cvs_ent_remove(ep, ent->ce_name);
1.1       jfb       271:
1.57      joris     272:        cvs_ent_free(ent);
1.1       jfb       273:
1.61      joris     274:        if (cvs_server_active == 0)
                    275:                cvs_log(LP_TRACE, "cvs_ent_add(%s, %s)", ep->cef_path, line);
1.1       jfb       276:
1.57      joris     277:        if ((fp = fopen(ep->cef_lpath, "a")) == NULL)
1.63      xsa       278:                fatal("cvs_ent_add: fopen: `%s': %s",
                    279:                    ep->cef_lpath, strerror(errno));
1.1       jfb       280:
1.57      joris     281:        fputc('A', fp);
                    282:        fputs(line, fp);
                    283:        fputc('\n', fp);
1.1       jfb       284:
1.57      joris     285:        (void)fclose(fp);
1.10      jfb       286:
1.57      joris     287:        l = (struct cvs_ent_line *)xmalloc(sizeof(*l));
                    288:        l->buf = xstrdup(line);
                    289:        TAILQ_INSERT_TAIL(&(ep->cef_ent), l, entries_list);
                    290: }
1.10      jfb       291:
1.57      joris     292: void
                    293: cvs_ent_remove(CVSENTRIES *ep, const char *name)
                    294: {
                    295:        FILE *fp;
                    296:        struct cvs_ent_line *l;
1.1       jfb       297:
1.61      joris     298:        if (cvs_server_active == 0)
                    299:                cvs_log(LP_TRACE, "cvs_ent_remove(%s, %s)", ep->cef_path, name);
1.1       jfb       300:
1.57      joris     301:        l = ent_get_line(ep, name);
                    302:        if (l == NULL)
                    303:                return;
1.1       jfb       304:
1.57      joris     305:        if ((fp = fopen(ep->cef_lpath, "a")) == NULL)
1.63      xsa       306:                fatal("cvs_ent_remove: fopen: `%s': %s", ep->cef_lpath,
                    307:                    strerror(errno));
1.38      joris     308:
1.57      joris     309:        fputc('R', fp);
                    310:        fputs(l->buf, fp);
                    311:        fputc('\n', fp);
1.24      jfb       312:
1.57      joris     313:        (void)fclose(fp);
1.24      jfb       314:
1.57      joris     315:        TAILQ_REMOVE(&(ep->cef_ent), l, entries_list);
                    316:        xfree(l->buf);
                    317:        xfree(l);
1.5       jfb       318: }
                    319:
                    320: void
                    321: cvs_ent_free(struct cvs_ent *ent)
                    322: {
                    323:        if (ent->ce_rev != NULL)
                    324:                rcsnum_free(ent->ce_rev);
1.57      joris     325:        xfree(ent->ce_buf);
1.53      joris     326:        xfree(ent);
1.5       jfb       327: }
1.8       jfb       328:
1.57      joris     329: static struct cvs_ent_line *
                    330: ent_get_line(CVSENTRIES *ep, const char *name)
1.8       jfb       331: {
1.57      joris     332:        char *p, *s;
                    333:        struct cvs_ent_line *l;
1.8       jfb       334:
1.57      joris     335:        TAILQ_FOREACH(l, &(ep->cef_ent), entries_list) {
                    336:                if (l->buf[0] == 'D')
                    337:                        p = &(l->buf[2]);
                    338:                else
                    339:                        p = &(l->buf[1]);
                    340:
                    341:                if ((s = strchr(p, '/')) == NULL)
                    342:                        fatal("ent_get_line: bad entry line '%s'", l->buf);
                    343:
                    344:                *s = '\0';
                    345:
                    346:                if (!strcmp(p, name)) {
                    347:                        *s = '/';
                    348:                        return (l);
1.15      jfb       349:                }
1.8       jfb       350:
1.57      joris     351:                *s = '/';
1.8       jfb       352:        }
                    353:
1.57      joris     354:        return (NULL);
1.59      xsa       355: }
                    356:
                    357: void
                    358: cvs_parse_tagfile(char *dir, char **tagp, char **datep, int *nbp)
                    359: {
                    360:        FILE *fp;
1.73      xsa       361:        int i, linenum;
1.59      xsa       362:        size_t len;
1.69      otto      363:        char linebuf[128], tagpath[MAXPATHLEN];
1.59      xsa       364:
                    365:        if (tagp != NULL)
1.60      joris     366:                *tagp = NULL;
1.59      xsa       367:
                    368:        if (datep != NULL)
1.60      joris     369:                *datep = NULL;
1.59      xsa       370:
                    371:        if (nbp != NULL)
                    372:                *nbp = 0;
                    373:
1.73      xsa       374:        i = snprintf(tagpath, MAXPATHLEN, "%s/%s", dir, CVS_PATH_TAG);
                    375:        if (i < 0 || i >= MAXPATHLEN)
1.69      otto      376:                return;
1.59      xsa       377:
                    378:        if ((fp = fopen(tagpath, "r")) == NULL) {
                    379:                if (errno != ENOENT)
                    380:                        cvs_log(LP_NOTICE, "failed to open `%s' : %s", tagpath,
                    381:                            strerror(errno));
1.69      otto      382:                return;
1.59      xsa       383:         }
                    384:
                    385:        linenum = 0;
                    386:
                    387:        while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
                    388:                linenum++;
                    389:                if ((len = strlen(linebuf)) == 0)
                    390:                        continue;
                    391:                if (linebuf[len - 1] != '\n') {
                    392:                        cvs_log(LP_NOTICE, "line too long in `%s:%d'",
                    393:                            tagpath, linenum);
                    394:                        break;
                    395:                }
                    396:                linebuf[--len] = '\0';
                    397:
                    398:                switch (*linebuf) {
                    399:                case 'T':
                    400:                        if (tagp != NULL)
                    401:                                *tagp = xstrdup(linebuf + 1);
                    402:                        break;
                    403:                case 'D':
                    404:                        if (datep != NULL)
                    405:                                *datep = xstrdup(linebuf + 1);
                    406:                        break;
                    407:                case 'N':
                    408:                        if (tagp != NULL)
                    409:                                *tagp = xstrdup(linebuf + 1);
                    410:                        if (nbp != NULL)
                    411:                                *nbp = 1;
                    412:                        break;
                    413:                default:
                    414:                        break;
                    415:                }
                    416:        }
                    417:        if (ferror(fp))
                    418:                cvs_log(LP_NOTICE, "failed to read line from `%s'", tagpath);
                    419:
                    420:        (void)fclose(fp);
                    421: }
                    422:
                    423: void
1.74      otto      424: cvs_write_tagfile(const char *dir, char *tag, char *date, int nb)
1.59      xsa       425: {
                    426:        FILE *fp;
1.69      otto      427:        char tagpath[MAXPATHLEN];
1.73      xsa       428:        int i;
1.59      xsa       429:
                    430:        if (cvs_noexec == 1)
                    431:                return;
                    432:
1.73      xsa       433:        i = snprintf(tagpath, MAXPATHLEN, "%s/%s", dir, CVS_PATH_TAG);
                    434:        if (i < 0 || i >= MAXPATHLEN)
1.69      otto      435:                return;
1.59      xsa       436:
                    437:        if ((tag != NULL) || (date != NULL)) {
                    438:                if ((fp = fopen(tagpath, "w+")) == NULL) {
                    439:                        if (errno != ENOENT) {
                    440:                                cvs_log(LP_NOTICE, "failed to open `%s' : %s",
                    441:                                    tagpath, strerror(errno));
                    442:                        }
1.69      otto      443:                        return;
1.59      xsa       444:                }
                    445:                if (tag != NULL) {
                    446:                        if (nb != 0)
                    447:                                (void)fprintf(fp, "N%s\n", tag);
                    448:                        else
                    449:                                (void)fprintf(fp, "T%s\n", tag);
                    450:                } else
                    451:                        (void)fprintf(fp, "D%s\n", date);
                    452:
                    453:                (void)fclose(fp);
                    454:        } else {
                    455:                (void)cvs_unlink(tagpath);
                    456:        }
1.1       jfb       457: }