[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.90

1.90    ! xsa         1: /*     $OpenBSD: entries.c,v 1.89 2008/02/09 17:01:43 tobias 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.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 ||
1.81      joris     157:                    strncmp(fields[3], "Result of merge", 15) == 0) {
1.57      joris     158:                        ent->ce_mtime = CVS_DATE_DMSEC;
1.81      joris     159:                } else if (cvs_server_active == 1 &&
                    160:                    strncmp(fields[3], CVS_SERVER_UNCHANGED,
                    161:                    strlen(CVS_SERVER_UNCHANGED)) == 0) {
                    162:                        ent->ce_mtime = CVS_SERVER_UPTODATE;
                    163:                } else {
1.78      niallo    164:                        /* Date field can be a '+=' with remote to indicate
                    165:                         * conflict.  In this case do nothing. */
                    166:                        if (strptime(fields[3], "%a %b %d %T %Y", &t) != NULL) {
                    167:
                    168:                                t.tm_isdst = -1;        /* Figure out DST. */
                    169:                                t.tm_gmtoff = 0;
                    170:                                ent->ce_mtime = mktime(&t);
                    171:                                ent->ce_mtime += t.tm_gmtoff;
                    172:                        }
1.70      joris     173:                }
1.3       jfb       174:        }
1.1       jfb       175:
1.57      joris     176:        ent->ce_conflict = fields[3];
                    177:        if ((dp = strchr(ent->ce_conflict, '+')) != NULL)
                    178:                *dp = '\0';
                    179:        else
                    180:                ent->ce_conflict = NULL;
1.1       jfb       181:
1.57      joris     182:        if (strcmp(fields[4], ""))
                    183:                ent->ce_opts = fields[4];
                    184:        else
                    185:                ent->ce_opts = NULL;
1.8       jfb       186:
1.57      joris     187:        if (strcmp(fields[5], ""))
1.58      joris     188:                ent->ce_tag = fields[5] + 1;
1.57      joris     189:        else
                    190:                ent->ce_tag = NULL;
1.3       jfb       191:
1.57      joris     192:        return (ent);
1.3       jfb       193: }
                    194:
1.57      joris     195: struct cvs_ent *
                    196: cvs_ent_get(CVSENTRIES *ep, const char *name)
1.3       jfb       197: {
                    198:        struct cvs_ent *ent;
1.57      joris     199:        struct cvs_ent_line *l;
1.3       jfb       200:
1.57      joris     201:        l = ent_get_line(ep, name);
                    202:        if (l == NULL)
                    203:                return (NULL);
1.3       jfb       204:
1.57      joris     205:        ent = cvs_ent_parse(l->buf);
                    206:        return (ent);
1.1       jfb       207: }
                    208:
1.57      joris     209: void
                    210: cvs_ent_close(CVSENTRIES *ep, int writefile)
1.9       jfb       211: {
1.57      joris     212:        FILE *fp;
                    213:        struct cvs_ent_line *l;
1.85      tobias    214:        int dflag;
                    215:
                    216:        dflag = 1;
1.44      xsa       217:
1.57      joris     218:        if (writefile) {
                    219:                if ((fp = fopen(ep->cef_bpath, "w")) == NULL)
1.63      xsa       220:                        fatal("cvs_ent_close: fopen: `%s': %s",
                    221:                            ep->cef_path, strerror(errno));
1.57      joris     222:        }
1.16      jfb       223:
1.57      joris     224:        while ((l = TAILQ_FIRST(&(ep->cef_ent))) != NULL) {
                    225:                if (writefile) {
1.85      tobias    226:                        if (l->buf[0] == 'D')
                    227:                                dflag = 0;
                    228:
1.57      joris     229:                        fputs(l->buf, fp);
                    230:                        fputc('\n', fp);
1.51      joris     231:                }
1.57      joris     232:
                    233:                TAILQ_REMOVE(&(ep->cef_ent), l, entries_list);
                    234:                xfree(l->buf);
                    235:                xfree(l);
1.22      jfb       236:        }
1.9       jfb       237:
1.57      joris     238:        if (writefile) {
1.85      tobias    239:                if (dflag) {
                    240:                        fputc('D', fp);
                    241:                        fputc('\n', fp);
                    242:                }
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.87      tobias    281:        fputs("A ", fp);
1.57      joris     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.87      tobias    309:        fputs("R ", fp);
1.57      joris     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.90    ! xsa       318: }
        !           319:
        !           320: /*
        !           321:  * cvs_ent_line_str()
        !           322:  *
        !           323:  * Build CVS/Entries line.
        !           324:  *
        !           325:  */
        !           326: void
        !           327: cvs_ent_line_str(const char *name, char *rev, char *tstamp, char *opts,
        !           328:     char *sticky, int isdir, int isremoved, char *buf, size_t len)
        !           329: {
        !           330:        if (isdir == 1) {
        !           331:                (void)xsnprintf(buf, len, "D/%s////", name);
        !           332:                return;
        !           333:        }
        !           334:
        !           335:        (void)xsnprintf(buf, len, "/%s/%s%s/%s/%s/%s",
        !           336:            name, isremoved == 1 ? "-" : "", rev, tstamp, opts, sticky);
1.5       jfb       337: }
                    338:
                    339: void
                    340: cvs_ent_free(struct cvs_ent *ent)
                    341: {
                    342:        if (ent->ce_rev != NULL)
                    343:                rcsnum_free(ent->ce_rev);
1.57      joris     344:        xfree(ent->ce_buf);
1.53      joris     345:        xfree(ent);
1.5       jfb       346: }
1.8       jfb       347:
1.57      joris     348: static struct cvs_ent_line *
                    349: ent_get_line(CVSENTRIES *ep, const char *name)
1.8       jfb       350: {
1.57      joris     351:        char *p, *s;
                    352:        struct cvs_ent_line *l;
1.8       jfb       353:
1.57      joris     354:        TAILQ_FOREACH(l, &(ep->cef_ent), entries_list) {
                    355:                if (l->buf[0] == 'D')
                    356:                        p = &(l->buf[2]);
                    357:                else
                    358:                        p = &(l->buf[1]);
                    359:
                    360:                if ((s = strchr(p, '/')) == NULL)
                    361:                        fatal("ent_get_line: bad entry line '%s'", l->buf);
                    362:
                    363:                *s = '\0';
                    364:
                    365:                if (!strcmp(p, name)) {
                    366:                        *s = '/';
                    367:                        return (l);
1.15      jfb       368:                }
1.8       jfb       369:
1.57      joris     370:                *s = '/';
1.8       jfb       371:        }
                    372:
1.57      joris     373:        return (NULL);
1.59      xsa       374: }
                    375:
                    376: void
                    377: cvs_parse_tagfile(char *dir, char **tagp, char **datep, int *nbp)
                    378: {
                    379:        FILE *fp;
1.73      xsa       380:        int i, linenum;
1.59      xsa       381:        size_t len;
1.69      otto      382:        char linebuf[128], tagpath[MAXPATHLEN];
1.59      xsa       383:
                    384:        if (tagp != NULL)
1.60      joris     385:                *tagp = NULL;
1.59      xsa       386:
                    387:        if (datep != NULL)
1.60      joris     388:                *datep = NULL;
1.59      xsa       389:
                    390:        if (nbp != NULL)
                    391:                *nbp = 0;
                    392:
1.73      xsa       393:        i = snprintf(tagpath, MAXPATHLEN, "%s/%s", dir, CVS_PATH_TAG);
                    394:        if (i < 0 || i >= MAXPATHLEN)
1.69      otto      395:                return;
1.59      xsa       396:
                    397:        if ((fp = fopen(tagpath, "r")) == NULL) {
                    398:                if (errno != ENOENT)
                    399:                        cvs_log(LP_NOTICE, "failed to open `%s' : %s", tagpath,
                    400:                            strerror(errno));
1.69      otto      401:                return;
1.59      xsa       402:         }
                    403:
                    404:        linenum = 0;
                    405:
                    406:        while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
                    407:                linenum++;
                    408:                if ((len = strlen(linebuf)) == 0)
                    409:                        continue;
                    410:                if (linebuf[len - 1] != '\n') {
                    411:                        cvs_log(LP_NOTICE, "line too long in `%s:%d'",
                    412:                            tagpath, linenum);
                    413:                        break;
                    414:                }
                    415:                linebuf[--len] = '\0';
                    416:
                    417:                switch (*linebuf) {
                    418:                case 'T':
                    419:                        if (tagp != NULL)
                    420:                                *tagp = xstrdup(linebuf + 1);
                    421:                        break;
                    422:                case 'D':
                    423:                        if (datep != NULL)
                    424:                                *datep = xstrdup(linebuf + 1);
                    425:                        break;
                    426:                case 'N':
                    427:                        if (tagp != NULL)
                    428:                                *tagp = xstrdup(linebuf + 1);
                    429:                        if (nbp != NULL)
                    430:                                *nbp = 1;
                    431:                        break;
                    432:                default:
                    433:                        break;
                    434:                }
                    435:        }
                    436:        if (ferror(fp))
                    437:                cvs_log(LP_NOTICE, "failed to read line from `%s'", tagpath);
                    438:
                    439:        (void)fclose(fp);
                    440: }
                    441:
                    442: void
1.88      tobias    443: cvs_write_tagfile(const char *dir, char *tag, char *date)
1.59      xsa       444: {
                    445:        FILE *fp;
1.88      tobias    446:        RCSNUM *rev;
1.69      otto      447:        char tagpath[MAXPATHLEN];
1.82      joris     448:        char sticky[CVS_REV_BUFSZ];
1.89      tobias    449:        struct tm *datetm;
1.73      xsa       450:        int i;
1.59      xsa       451:
1.88      tobias    452:        cvs_log(LP_TRACE, "cvs_write_tagfile(%s, %s, %s)", dir,
                    453:            tag != NULL ? tag : "", date != NULL ? date : "");
                    454:
1.59      xsa       455:        if (cvs_noexec == 1)
                    456:                return;
                    457:
1.73      xsa       458:        i = snprintf(tagpath, MAXPATHLEN, "%s/%s", dir, CVS_PATH_TAG);
                    459:        if (i < 0 || i >= MAXPATHLEN)
1.69      otto      460:                return;
1.59      xsa       461:
1.89      tobias    462:        if (tag != NULL || cvs_specified_date != 0) {
1.59      xsa       463:                if ((fp = fopen(tagpath, "w+")) == NULL) {
                    464:                        if (errno != ENOENT) {
                    465:                                cvs_log(LP_NOTICE, "failed to open `%s' : %s",
                    466:                                    tagpath, strerror(errno));
                    467:                        }
1.69      otto      468:                        return;
1.59      xsa       469:                }
1.79      joris     470:
1.59      xsa       471:                if (tag != NULL) {
1.88      tobias    472:                        if ((rev = rcsnum_parse(tag)) != NULL) {
1.82      joris     473:                                (void)xsnprintf(sticky, sizeof(sticky),
                    474:                                    "N%s", tag);
1.88      tobias    475:                                rcsnum_free(rev);
1.82      joris     476:                        } else {
                    477:                                (void)xsnprintf(sticky, sizeof(sticky),
                    478:                                    "T%s", tag);
                    479:                        }
                    480:                } else {
1.89      tobias    481:                        datetm = gmtime(&cvs_specified_date);
                    482:                        strftime(sticky, sizeof(sticky), "D%Y.%m.%d.%H.%M.%S",
                    483:                            datetm);
1.82      joris     484:                }
                    485:
                    486:                if (cvs_server_active == 1)
                    487:                        cvs_server_set_sticky(dir, sticky);
1.59      xsa       488:
1.82      joris     489:                (void)fprintf(fp, "%s\n", sticky);
1.59      xsa       490:                (void)fclose(fp);
                    491:        } else {
                    492:                (void)cvs_unlink(tagpath);
                    493:        }
1.1       jfb       494: }