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

Annotation of src/usr.bin/cvs/edit.c, Revision 1.24

1.24    ! xsa         1: /*     $OpenBSD: edit.c,v 1.23 2007/01/06 17:09:08 xsa Exp $   */
1.1       jfb         2: /*
1.15      xsa         3:  * Copyright (c) 2006, 2007 Xavier Santolaria <xsa@openbsd.org>
1.1       jfb         4:  *
1.14      xsa         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.11      xsa        18: #include "includes.h"
1.1       jfb        19:
                     20: #include "cvs.h"
                     21: #include "log.h"
1.14      xsa        22: #include "remote.h"
1.1       jfb        23:
1.18      xsa        24: #define E_COMMIT       0x01
                     25: #define E_EDIT         0x02
                     26: #define E_UNEDIT       0x04
                     27: #define E_ALL          (E_EDIT|E_COMMIT|E_UNEDIT)
                     28:
1.23      xsa        29: #define BASE_ADD       0x01
                     30: #define BASE_GET       0x02
                     31: #define BASE_REMOVE    0x04
                     32:
1.18      xsa        33: static void    cvs_edit_local(struct cvs_file *);
1.14      xsa        34: static void    cvs_editors_local(struct cvs_file *);
1.15      xsa        35: static void    cvs_unedit_local(struct cvs_file *);
1.1       jfb        36:
1.23      xsa        37: static RCSNUM  *cvs_base_handle(struct cvs_file *, int);
                     38:
1.18      xsa        39: static int     edit_aflags = 0;
                     40:
                     41: struct cvs_cmd cvs_cmd_edit = {
                     42:        CVS_OP_EDIT, 0, "edit",
                     43:        { },
                     44:        "Get ready to edit a watched file",
                     45:        "[-lR] [-a action] [file ...]",
                     46:        "a:lR",
                     47:        NULL,
                     48:        cvs_edit
                     49: };
                     50:
1.1       jfb        51: struct cvs_cmd cvs_cmd_editors = {
1.14      xsa        52:        CVS_OP_EDITORS, 0, "editors",
1.1       jfb        53:        { },
1.14      xsa        54:        "See who is editing a watched file",
1.3       xsa        55:        "[-lR] [file ...]",
1.1       jfb        56:        "lR",
                     57:        NULL,
1.14      xsa        58:        cvs_editors
1.1       jfb        59: };
                     60:
1.15      xsa        61: struct cvs_cmd cvs_cmd_unedit = {
                     62:        CVS_OP_UNEDIT, 0, "unedit",
                     63:        { },
                     64:        "Undo an edit command",
                     65:        "[-lR] [file ...]",
                     66:        "lR",
                     67:        NULL,
                     68:        cvs_unedit
                     69: };
                     70:
1.14      xsa        71: int
1.18      xsa        72: cvs_edit(int argc, char **argv)
                     73: {
                     74:        int ch;
                     75:        int flags;
                     76:        struct cvs_recursion cr;
                     77:
                     78:        flags = CR_RECURSE_DIRS;
                     79:
                     80:        while ((ch = getopt(argc, argv, cvs_cmd_edit.cmd_opts)) != -1) {
                     81:                switch (ch) {
                     82:                case 'a':
                     83:                        if (strcmp(optarg, "edit") == 0)
                     84:                                edit_aflags |= E_EDIT;
                     85:                        else if (strcmp(optarg, "unedit") == 0)
                     86:                                edit_aflags |= E_UNEDIT;
                     87:                        else if (strcmp(optarg, "commit") == 0)
                     88:                                edit_aflags |= E_COMMIT;
                     89:                        else if (strcmp(optarg, "all") == 0)
                     90:                                edit_aflags |= E_ALL;
                     91:                        else if (strcmp(optarg, "none") == 0)
                     92:                                edit_aflags &= ~E_ALL;
                     93:                        else
                     94:                                fatal("%s", cvs_cmd_edit.cmd_synopsis);
                     95:                        break;
                     96:                case 'l':
                     97:                        flags &= ~CR_RECURSE_DIRS;
                     98:                        break;
                     99:                case 'R':
                    100:                        break;
                    101:                default:
                    102:                        fatal("%s", cvs_cmd_edit.cmd_synopsis);
                    103:                }
                    104:        }
                    105:
                    106:        argc -= optind;
                    107:        argv += optind;
                    108:
                    109:        if (argc == 0)
                    110:                fatal("%s", cvs_cmd_edit.cmd_synopsis);
                    111:
                    112:        if (edit_aflags == 0)
                    113:                edit_aflags |= E_ALL;
                    114:
                    115:        cr.enterdir = NULL;
                    116:        cr.leavedir = NULL;
                    117:
                    118:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    119:                cr.fileproc = cvs_client_sendfile;
                    120:
                    121:                if (!(flags & CR_RECURSE_DIRS))
                    122:                        cvs_client_send_request("Argument -l");
                    123:        } else {
                    124:                cr.fileproc = cvs_edit_local;
                    125:        }
                    126:
                    127:        cr.flags = flags;
                    128:
                    129:        cvs_file_run(argc, argv, &cr);
                    130:
                    131:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    132:                cvs_client_send_files(argv, argc);
                    133:                cvs_client_senddir(".");
                    134:                cvs_client_send_request("edit");
                    135:                cvs_client_get_responses();
                    136:        }
                    137:
                    138:        return (0);
                    139: }
                    140:
                    141: int
1.14      xsa       142: cvs_editors(int argc, char **argv)
1.1       jfb       143: {
1.12      xsa       144:        int ch;
1.14      xsa       145:        int flags;
                    146:        struct cvs_recursion cr;
                    147:
                    148:        flags = CR_RECURSE_DIRS;
1.1       jfb       149:
1.14      xsa       150:        while ((ch = getopt(argc, argv, cvs_cmd_editors.cmd_opts)) != -1) {
1.1       jfb       151:                switch (ch) {
                    152:                case 'l':
1.14      xsa       153:                        flags &= ~CR_RECURSE_DIRS;
1.1       jfb       154:                        break;
                    155:                case 'R':
                    156:                        break;
                    157:                default:
1.14      xsa       158:                        fatal("%s", cvs_cmd_editors.cmd_synopsis);
1.1       jfb       159:                }
                    160:        }
                    161:
1.14      xsa       162:        argc -= optind;
                    163:        argv += optind;
1.1       jfb       164:
1.14      xsa       165:        if (argc == 0)
                    166:                fatal("%s", cvs_cmd_editors.cmd_synopsis);
1.1       jfb       167:
1.14      xsa       168:        cr.enterdir = NULL;
                    169:        cr.leavedir = NULL;
1.1       jfb       170:
1.14      xsa       171:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    172:                cr.fileproc = cvs_client_sendfile;
                    173:
                    174:                if (!(flags & CR_RECURSE_DIRS))
                    175:                        cvs_client_send_request("Argument -l");
                    176:        } else {
                    177:                cr.fileproc = cvs_editors_local;
                    178:        }
1.1       jfb       179:
1.14      xsa       180:        cr.flags = flags;
1.9       xsa       181:
1.14      xsa       182:        cvs_file_run(argc, argv, &cr);
1.9       xsa       183:
1.14      xsa       184:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    185:                cvs_client_send_files(argv, argc);
                    186:                cvs_client_senddir(".");
                    187:                cvs_client_send_request("editors");
                    188:                cvs_client_get_responses();
1.9       xsa       189:        }
                    190:
1.14      xsa       191:        return (0);
                    192: }
1.9       xsa       193:
1.15      xsa       194: int
                    195: cvs_unedit(int argc, char **argv)
                    196: {
                    197:        int ch;
                    198:        int flags;
                    199:        struct cvs_recursion cr;
                    200:
                    201:        flags = CR_RECURSE_DIRS;
                    202:
                    203:        while ((ch = getopt(argc, argv, cvs_cmd_unedit.cmd_opts)) != -1) {
                    204:                switch (ch) {
                    205:                case 'l':
                    206:                        flags &= ~CR_RECURSE_DIRS;
                    207:                        break;
                    208:                case 'R':
                    209:                        break;
                    210:                default:
                    211:                        fatal("%s", cvs_cmd_unedit.cmd_synopsis);
                    212:                }
                    213:        }
                    214:
                    215:        argc -= optind;
                    216:        argv += optind;
                    217:
                    218:        if (argc == 0)
                    219:                fatal("%s", cvs_cmd_unedit.cmd_synopsis);
                    220:
                    221:        cr.enterdir = NULL;
                    222:        cr.leavedir = NULL;
                    223:
                    224:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    225:                cr.fileproc = cvs_client_sendfile;
                    226:
                    227:                if (!(flags & CR_RECURSE_DIRS))
                    228:                        cvs_client_send_request("Argument -l");
                    229:        } else {
                    230:                cr.fileproc = cvs_unedit_local;
                    231:        }
                    232:
                    233:        cr.flags = flags;
                    234:
                    235:        cvs_file_run(argc, argv, &cr);
                    236:
                    237:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    238:                cvs_client_send_files(argv, argc);
                    239:                cvs_client_senddir(".");
                    240:                cvs_client_send_request("unedit");
                    241:                cvs_client_get_responses();
                    242:        }
                    243:
                    244:        return (0);
1.18      xsa       245: }
                    246:
                    247: static void
                    248: cvs_edit_local(struct cvs_file *cf)
                    249: {
                    250:        FILE *fp;
                    251:        struct tm *t;
                    252:        time_t now;
1.21      xsa       253:        char *bfpath, timebuf[64], thishost[MAXHOSTNAMELEN], wdir[MAXPATHLEN];
1.18      xsa       254:
                    255:        if (cvs_noexec == 1)
                    256:                return;
                    257:
1.24    ! xsa       258:        cvs_log(LP_TRACE, "cvs_edit_local(%s)", cf->file_path);
        !           259:
1.23      xsa       260:        cvs_file_classify(cf, NULL, 0);
                    261:
1.18      xsa       262:        if ((fp = fopen(CVS_PATH_NOTIFY, "a")) == NULL)
                    263:                fatal("cvs_edit_local: fopen: `%s': %s",
                    264:                    CVS_PATH_NOTIFY, strerror(errno));
                    265:
                    266:        (void)time(&now);
                    267:        if ((t = gmtime(&now)) == NULL)
                    268:                fatal("gmtime failed");
                    269:
1.20      xsa       270:        asctime_r(t, timebuf);
                    271:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    272:                 timebuf[strlen(timebuf) - 1] = '\0';
1.18      xsa       273:
1.19      xsa       274:        if (gethostname(thishost, sizeof(thishost)) == -1)
                    275:                fatal("gethostname failed");
                    276:
1.21      xsa       277:        if (getcwd(wdir, sizeof(wdir)) == NULL)
                    278:                fatal("getcwd failed");
                    279:
1.18      xsa       280:        (void)fprintf(fp, "E%s\t%s GMT\t%s\t%s\t\n",
1.21      xsa       281:            cf->file_name, timebuf, thishost, wdir);
1.18      xsa       282:
                    283:        if (edit_aflags & E_EDIT)
                    284:                (void)fprintf(fp, "E");
                    285:        if (edit_aflags & E_UNEDIT)
                    286:                (void)fprintf(fp, "U");
                    287:        if (edit_aflags & E_COMMIT)
                    288:                (void)fprintf(fp, "C");
                    289:
                    290:        (void)fprintf(fp, "\n");
                    291:
                    292:        (void)fclose(fp);
                    293:
                    294:        if (fchmod(cf->fd, 0644) == -1)
                    295:                fatal("cvs_edit_local: fchmod %s", strerror(errno));
                    296:
                    297:        bfpath = xmalloc(MAXPATHLEN);
                    298:        if (cvs_path_cat(CVS_PATH_BASEDIR, cf->file_name, bfpath,
                    299:            MAXPATHLEN) >= MAXPATHLEN)
                    300:                fatal("cvs_edit_local: truncation");
1.22      xsa       301:
                    302:        if (mkdir(CVS_PATH_BASEDIR, 0755) == -1 && errno != EEXIST)
                    303:                fatal("cvs_edit_local: `%s': %s", CVS_PATH_BASEDIR,
                    304:                    strerror(errno));
1.18      xsa       305:
                    306:        /* XXX: copy cf->file_path to bfpath */
                    307:
                    308:        xfree(bfpath);
                    309:
1.23      xsa       310:        (void)cvs_base_handle(cf, BASE_ADD);
1.15      xsa       311: }
                    312:
1.14      xsa       313: static void
                    314: cvs_editors_local(struct cvs_file *cf)
                    315: {
1.15      xsa       316: }
                    317:
                    318: static void
                    319: cvs_unedit_local(struct cvs_file *cf)
                    320: {
                    321:        FILE *fp;
                    322:        struct stat st;
                    323:        struct tm *t;
                    324:        time_t now;
1.21      xsa       325:        char *bfpath, timebuf[64], thishost[MAXHOSTNAMELEN], wdir[MAXPATHLEN];
1.24    ! xsa       326:        RCSNUM *ba_rev;
1.15      xsa       327:
                    328:        if (cvs_noexec == 1)
                    329:                return;
                    330:
1.24    ! xsa       331:        cvs_log(LP_TRACE, "cvs_unedit_local(%s)", cf->file_path);
        !           332:
1.23      xsa       333:        cvs_file_classify(cf, NULL, 0);
                    334:
1.15      xsa       335:        bfpath = xmalloc(MAXPATHLEN);
                    336:        if (cvs_path_cat(CVS_PATH_BASEDIR, cf->file_name, bfpath,
                    337:            MAXPATHLEN) >= MAXPATHLEN)
                    338:                fatal("cvs_unedit_local: truncation");
                    339:
                    340:        if (stat(bfpath, &st) == -1) {
                    341:                xfree(bfpath);
                    342:                return;
                    343:        }
                    344:
1.17      xsa       345:        if (cvs_file_cmp(cf->file_path, bfpath) != 0) {
                    346:                cvs_printf("%s has been modified; revert changes? ",
                    347:                    cf->file_name);
                    348:
                    349:                if (cvs_yesno() == -1) {
                    350:                        xfree(bfpath);
                    351:                        return;
                    352:                }
                    353:        }
1.15      xsa       354:
                    355:        cvs_rename(bfpath, cf->file_path);
                    356:        xfree(bfpath);
                    357:
                    358:        if ((fp = fopen(CVS_PATH_NOTIFY, "a")) == NULL)
                    359:                fatal("cvs_unedit_local: fopen: `%s': %s",
                    360:                    CVS_PATH_NOTIFY, strerror(errno));
                    361:
                    362:        (void)time(&now);
                    363:        if ((t = gmtime(&now)) == NULL)
                    364:                fatal("gmtime failed");
                    365:
1.20      xsa       366:        asctime_r(t, timebuf);
                    367:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    368:                 timebuf[strlen(timebuf) - 1] = '\0';
1.15      xsa       369:
1.19      xsa       370:        if (gethostname(thishost, sizeof(thishost)) == -1)
                    371:                fatal("gethostname failed");
                    372:
1.21      xsa       373:        if (getcwd(wdir, sizeof(wdir)) == NULL)
1.23      xsa       374:                fatal("getcwd failed");
1.21      xsa       375:
1.15      xsa       376:        (void)fprintf(fp, "U%s\t%s GMT\t%s\t%s\t\n",
1.21      xsa       377:            cf->file_name, timebuf, thishost, wdir);
1.15      xsa       378:
                    379:        (void)fclose(fp);
                    380:
1.23      xsa       381:        /* XXX: Update the revision number in CVS/Entries from CVS/Baserev */
1.24    ! xsa       382:        if (cf->file_ent != NULL) {
        !           383:                if ((ba_rev = cvs_base_handle(cf, BASE_GET)) == NULL) {
        !           384:                        cvs_log(LP_ERR, "%s not mentioned in %s",
        !           385:                            cf->file_name, CVS_PATH_BASEREV);
        !           386:                        return;
        !           387:                }
        !           388:                rcsnum_free(ba_rev);
        !           389:        }
        !           390:
        !           391:        (void)cvs_base_handle(cf, BASE_REMOVE);
1.16      xsa       392:
                    393:        if (fchmod(cf->fd, 0644) == -1)
                    394:                fatal("cvs_unedit_local: fchmod %s", strerror(errno));
1.23      xsa       395: }
                    396:
                    397: static RCSNUM *
                    398: cvs_base_handle(struct cvs_file *cf, int flags)
                    399: {
                    400:        FILE *fp, *tfp;
                    401:        RCSNUM *ba_rev;
                    402:        size_t len;
1.24    ! xsa       403:        int i;
        !           404:        char *dp, *sp;
        !           405:        char buf[MAXPATHLEN], *fields[2], rbuf[16];
1.23      xsa       406:
                    407:        cvs_log(LP_TRACE, "cvs_base_handle(%s)", cf->file_path);
                    408:
                    409:        tfp = NULL;
                    410:        ba_rev = NULL;
                    411:
1.24    ! xsa       412:        if (((fp = fopen(CVS_PATH_BASEREV, "r")) == NULL) && errno != ENOENT) {
1.23      xsa       413:                cvs_log(LP_ERRNO, "%s", CVS_PATH_BASEREV);
                    414:                goto out;
                    415:        }
                    416:
                    417:        if (flags & (BASE_ADD|BASE_REMOVE)) {
                    418:                if ((tfp = fopen(CVS_PATH_BASEREVTMP, "w")) == NULL) {
                    419:                        cvs_log(LP_ERRNO, "%s", CVS_PATH_BASEREVTMP);
                    420:                        goto out;
                    421:                }
                    422:        }
                    423:
1.24    ! xsa       424:        if (fp != NULL) {
        !           425:                while(fgets(buf, sizeof(buf), fp)) {
        !           426:                        len = strlen(buf);
        !           427:                        if (len > 0 && buf[len - 1] == '\n')
        !           428:                                buf[len - 1] = '\0';
        !           429:
        !           430:                        if (buf[0] != 'B')
        !           431:                                continue;
        !           432:
        !           433:                        sp = buf + 1;
        !           434:                        i = 0;
        !           435:                        do {
        !           436:                                if ((dp = strchr(sp, '/')) != NULL)
        !           437:                                        *(dp++) = '\0';
        !           438:                                fields[i++] = sp;
        !           439:                                sp = dp;
        !           440:                        } while (dp != NULL && i < 2);
        !           441:
        !           442:                        if (cvs_file_cmpname(fields[0], cf->file_path) == 0) {
        !           443:                                if (flags & BASE_GET) {
        !           444:                                        ba_rev = rcsnum_parse(fields[1]);
        !           445:                                        if (ba_rev == NULL)
        !           446:                                                fatal("cvs_base_handle: "
        !           447:                                                    "rcsnum_parse");
        !           448:                                        goto got_rev;
        !           449:                                }
        !           450:                        } else {
        !           451:                                if (flags & (BASE_ADD|BASE_REMOVE))
        !           452:                                        (void)fprintf(tfp, "%s\n", buf);
1.23      xsa       453:                        }
                    454:                }
                    455:        }
                    456:
                    457: got_rev:
                    458:        if (flags & (BASE_ADD)) {
                    459:                (void)rcsnum_tostr(cf->file_ent->ce_rev, rbuf, sizeof(rbuf));
                    460:                (void)fprintf(tfp, "B%s/%s/\n", cf->file_path, rbuf);
                    461:        }
                    462:
                    463: out:
                    464:        if (fp != NULL)
                    465:                (void)fclose(fp);
                    466:
                    467:        if (tfp != NULL) {
                    468:                (void)fclose(tfp);
                    469:                (void)cvs_rename(CVS_PATH_BASEREVTMP, CVS_PATH_BASEREV);
                    470:        }
                    471:
                    472:        return (ba_rev);
1.1       jfb       473: }