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

1.27    ! joris       1: /*     $OpenBSD: edit.c,v 1.26 2007/01/10 21:32:19 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) {
1.27    ! joris     119:                cvs_client_connect_to_server();
1.18      xsa       120:                cr.fileproc = cvs_client_sendfile;
                    121:
                    122:                if (!(flags & CR_RECURSE_DIRS))
                    123:                        cvs_client_send_request("Argument -l");
                    124:        } else {
                    125:                cr.fileproc = cvs_edit_local;
                    126:        }
                    127:
                    128:        cr.flags = flags;
                    129:
                    130:        cvs_file_run(argc, argv, &cr);
                    131:
                    132:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    133:                cvs_client_send_files(argv, argc);
                    134:                cvs_client_senddir(".");
                    135:                cvs_client_send_request("edit");
                    136:                cvs_client_get_responses();
                    137:        }
                    138:
                    139:        return (0);
                    140: }
                    141:
                    142: int
1.14      xsa       143: cvs_editors(int argc, char **argv)
1.1       jfb       144: {
1.12      xsa       145:        int ch;
1.14      xsa       146:        int flags;
                    147:        struct cvs_recursion cr;
                    148:
                    149:        flags = CR_RECURSE_DIRS;
1.1       jfb       150:
1.14      xsa       151:        while ((ch = getopt(argc, argv, cvs_cmd_editors.cmd_opts)) != -1) {
1.1       jfb       152:                switch (ch) {
                    153:                case 'l':
1.14      xsa       154:                        flags &= ~CR_RECURSE_DIRS;
1.1       jfb       155:                        break;
                    156:                case 'R':
                    157:                        break;
                    158:                default:
1.14      xsa       159:                        fatal("%s", cvs_cmd_editors.cmd_synopsis);
1.1       jfb       160:                }
                    161:        }
                    162:
1.14      xsa       163:        argc -= optind;
                    164:        argv += optind;
1.1       jfb       165:
1.14      xsa       166:        if (argc == 0)
                    167:                fatal("%s", cvs_cmd_editors.cmd_synopsis);
1.1       jfb       168:
1.14      xsa       169:        cr.enterdir = NULL;
                    170:        cr.leavedir = NULL;
1.1       jfb       171:
1.14      xsa       172:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.27    ! joris     173:                cvs_client_connect_to_server();
1.14      xsa       174:                cr.fileproc = cvs_client_sendfile;
                    175:
                    176:                if (!(flags & CR_RECURSE_DIRS))
                    177:                        cvs_client_send_request("Argument -l");
                    178:        } else {
                    179:                cr.fileproc = cvs_editors_local;
                    180:        }
1.1       jfb       181:
1.14      xsa       182:        cr.flags = flags;
1.9       xsa       183:
1.14      xsa       184:        cvs_file_run(argc, argv, &cr);
1.9       xsa       185:
1.14      xsa       186:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    187:                cvs_client_send_files(argv, argc);
                    188:                cvs_client_senddir(".");
                    189:                cvs_client_send_request("editors");
                    190:                cvs_client_get_responses();
1.9       xsa       191:        }
                    192:
1.14      xsa       193:        return (0);
                    194: }
1.9       xsa       195:
1.15      xsa       196: int
                    197: cvs_unedit(int argc, char **argv)
                    198: {
                    199:        int ch;
                    200:        int flags;
                    201:        struct cvs_recursion cr;
                    202:
                    203:        flags = CR_RECURSE_DIRS;
                    204:
                    205:        while ((ch = getopt(argc, argv, cvs_cmd_unedit.cmd_opts)) != -1) {
                    206:                switch (ch) {
                    207:                case 'l':
                    208:                        flags &= ~CR_RECURSE_DIRS;
                    209:                        break;
                    210:                case 'R':
                    211:                        break;
                    212:                default:
                    213:                        fatal("%s", cvs_cmd_unedit.cmd_synopsis);
                    214:                }
                    215:        }
                    216:
                    217:        argc -= optind;
                    218:        argv += optind;
                    219:
                    220:        if (argc == 0)
                    221:                fatal("%s", cvs_cmd_unedit.cmd_synopsis);
                    222:
                    223:        cr.enterdir = NULL;
                    224:        cr.leavedir = NULL;
                    225:
                    226:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    227:                cr.fileproc = cvs_client_sendfile;
                    228:
                    229:                if (!(flags & CR_RECURSE_DIRS))
                    230:                        cvs_client_send_request("Argument -l");
                    231:        } else {
                    232:                cr.fileproc = cvs_unedit_local;
                    233:        }
                    234:
                    235:        cr.flags = flags;
                    236:
                    237:        cvs_file_run(argc, argv, &cr);
                    238:
                    239:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    240:                cvs_client_send_files(argv, argc);
                    241:                cvs_client_senddir(".");
                    242:                cvs_client_send_request("unedit");
                    243:                cvs_client_get_responses();
                    244:        }
                    245:
                    246:        return (0);
1.18      xsa       247: }
                    248:
                    249: static void
                    250: cvs_edit_local(struct cvs_file *cf)
                    251: {
                    252:        FILE *fp;
                    253:        struct tm *t;
                    254:        time_t now;
1.21      xsa       255:        char *bfpath, timebuf[64], thishost[MAXHOSTNAMELEN], wdir[MAXPATHLEN];
1.18      xsa       256:
                    257:        if (cvs_noexec == 1)
                    258:                return;
                    259:
1.24      xsa       260:        cvs_log(LP_TRACE, "cvs_edit_local(%s)", cf->file_path);
                    261:
1.23      xsa       262:        cvs_file_classify(cf, NULL, 0);
                    263:
1.18      xsa       264:        if ((fp = fopen(CVS_PATH_NOTIFY, "a")) == NULL)
                    265:                fatal("cvs_edit_local: fopen: `%s': %s",
                    266:                    CVS_PATH_NOTIFY, strerror(errno));
                    267:
                    268:        (void)time(&now);
                    269:        if ((t = gmtime(&now)) == NULL)
                    270:                fatal("gmtime failed");
                    271:
1.20      xsa       272:        asctime_r(t, timebuf);
                    273:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    274:                 timebuf[strlen(timebuf) - 1] = '\0';
1.18      xsa       275:
1.19      xsa       276:        if (gethostname(thishost, sizeof(thishost)) == -1)
                    277:                fatal("gethostname failed");
                    278:
1.21      xsa       279:        if (getcwd(wdir, sizeof(wdir)) == NULL)
                    280:                fatal("getcwd failed");
                    281:
1.25      xsa       282:        (void)fprintf(fp, "E%s\t%s GMT\t%s\t%s\t",
1.21      xsa       283:            cf->file_name, timebuf, thishost, wdir);
1.18      xsa       284:
                    285:        if (edit_aflags & E_EDIT)
                    286:                (void)fprintf(fp, "E");
                    287:        if (edit_aflags & E_UNEDIT)
                    288:                (void)fprintf(fp, "U");
                    289:        if (edit_aflags & E_COMMIT)
                    290:                (void)fprintf(fp, "C");
                    291:
                    292:        (void)fprintf(fp, "\n");
                    293:
                    294:        (void)fclose(fp);
                    295:
                    296:        if (fchmod(cf->fd, 0644) == -1)
                    297:                fatal("cvs_edit_local: fchmod %s", strerror(errno));
                    298:
                    299:        bfpath = xmalloc(MAXPATHLEN);
                    300:        if (cvs_path_cat(CVS_PATH_BASEDIR, cf->file_name, bfpath,
                    301:            MAXPATHLEN) >= MAXPATHLEN)
                    302:                fatal("cvs_edit_local: truncation");
1.22      xsa       303:
                    304:        if (mkdir(CVS_PATH_BASEDIR, 0755) == -1 && errno != EEXIST)
                    305:                fatal("cvs_edit_local: `%s': %s", CVS_PATH_BASEDIR,
                    306:                    strerror(errno));
1.18      xsa       307:
1.26      xsa       308:        if (cvs_file_copy(cf->file_path, bfpath) == -1)
                    309:                fatal("cvs_edit_local: cvs_file_copy failed");
1.18      xsa       310:
                    311:        xfree(bfpath);
                    312:
1.23      xsa       313:        (void)cvs_base_handle(cf, BASE_ADD);
1.15      xsa       314: }
                    315:
1.14      xsa       316: static void
                    317: cvs_editors_local(struct cvs_file *cf)
                    318: {
1.15      xsa       319: }
                    320:
                    321: static void
                    322: cvs_unedit_local(struct cvs_file *cf)
                    323: {
                    324:        FILE *fp;
                    325:        struct stat st;
                    326:        struct tm *t;
                    327:        time_t now;
1.21      xsa       328:        char *bfpath, timebuf[64], thishost[MAXHOSTNAMELEN], wdir[MAXPATHLEN];
1.24      xsa       329:        RCSNUM *ba_rev;
1.15      xsa       330:
                    331:        if (cvs_noexec == 1)
                    332:                return;
                    333:
1.24      xsa       334:        cvs_log(LP_TRACE, "cvs_unedit_local(%s)", cf->file_path);
                    335:
1.23      xsa       336:        cvs_file_classify(cf, NULL, 0);
                    337:
1.15      xsa       338:        bfpath = xmalloc(MAXPATHLEN);
                    339:        if (cvs_path_cat(CVS_PATH_BASEDIR, cf->file_name, bfpath,
                    340:            MAXPATHLEN) >= MAXPATHLEN)
                    341:                fatal("cvs_unedit_local: truncation");
                    342:
                    343:        if (stat(bfpath, &st) == -1) {
                    344:                xfree(bfpath);
                    345:                return;
                    346:        }
                    347:
1.17      xsa       348:        if (cvs_file_cmp(cf->file_path, bfpath) != 0) {
                    349:                cvs_printf("%s has been modified; revert changes? ",
                    350:                    cf->file_name);
                    351:
                    352:                if (cvs_yesno() == -1) {
                    353:                        xfree(bfpath);
                    354:                        return;
                    355:                }
                    356:        }
1.15      xsa       357:
                    358:        cvs_rename(bfpath, cf->file_path);
                    359:        xfree(bfpath);
                    360:
                    361:        if ((fp = fopen(CVS_PATH_NOTIFY, "a")) == NULL)
                    362:                fatal("cvs_unedit_local: fopen: `%s': %s",
                    363:                    CVS_PATH_NOTIFY, strerror(errno));
                    364:
                    365:        (void)time(&now);
                    366:        if ((t = gmtime(&now)) == NULL)
                    367:                fatal("gmtime failed");
                    368:
1.20      xsa       369:        asctime_r(t, timebuf);
                    370:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    371:                 timebuf[strlen(timebuf) - 1] = '\0';
1.15      xsa       372:
1.19      xsa       373:        if (gethostname(thishost, sizeof(thishost)) == -1)
                    374:                fatal("gethostname failed");
                    375:
1.21      xsa       376:        if (getcwd(wdir, sizeof(wdir)) == NULL)
1.23      xsa       377:                fatal("getcwd failed");
1.21      xsa       378:
1.15      xsa       379:        (void)fprintf(fp, "U%s\t%s GMT\t%s\t%s\t\n",
1.21      xsa       380:            cf->file_name, timebuf, thishost, wdir);
1.15      xsa       381:
                    382:        (void)fclose(fp);
                    383:
1.23      xsa       384:        /* XXX: Update the revision number in CVS/Entries from CVS/Baserev */
1.24      xsa       385:        if (cf->file_ent != NULL) {
                    386:                if ((ba_rev = cvs_base_handle(cf, BASE_GET)) == NULL) {
                    387:                        cvs_log(LP_ERR, "%s not mentioned in %s",
                    388:                            cf->file_name, CVS_PATH_BASEREV);
                    389:                        return;
                    390:                }
                    391:                rcsnum_free(ba_rev);
                    392:        }
                    393:
                    394:        (void)cvs_base_handle(cf, BASE_REMOVE);
1.16      xsa       395:
                    396:        if (fchmod(cf->fd, 0644) == -1)
                    397:                fatal("cvs_unedit_local: fchmod %s", strerror(errno));
1.23      xsa       398: }
                    399:
                    400: static RCSNUM *
                    401: cvs_base_handle(struct cvs_file *cf, int flags)
                    402: {
                    403:        FILE *fp, *tfp;
                    404:        RCSNUM *ba_rev;
                    405:        size_t len;
1.24      xsa       406:        int i;
                    407:        char *dp, *sp;
                    408:        char buf[MAXPATHLEN], *fields[2], rbuf[16];
1.23      xsa       409:
                    410:        cvs_log(LP_TRACE, "cvs_base_handle(%s)", cf->file_path);
                    411:
                    412:        tfp = NULL;
                    413:        ba_rev = NULL;
                    414:
1.24      xsa       415:        if (((fp = fopen(CVS_PATH_BASEREV, "r")) == NULL) && errno != ENOENT) {
1.23      xsa       416:                cvs_log(LP_ERRNO, "%s", CVS_PATH_BASEREV);
                    417:                goto out;
                    418:        }
                    419:
                    420:        if (flags & (BASE_ADD|BASE_REMOVE)) {
                    421:                if ((tfp = fopen(CVS_PATH_BASEREVTMP, "w")) == NULL) {
                    422:                        cvs_log(LP_ERRNO, "%s", CVS_PATH_BASEREVTMP);
                    423:                        goto out;
                    424:                }
                    425:        }
                    426:
1.24      xsa       427:        if (fp != NULL) {
                    428:                while(fgets(buf, sizeof(buf), fp)) {
                    429:                        len = strlen(buf);
                    430:                        if (len > 0 && buf[len - 1] == '\n')
                    431:                                buf[len - 1] = '\0';
                    432:
                    433:                        if (buf[0] != 'B')
                    434:                                continue;
                    435:
                    436:                        sp = buf + 1;
                    437:                        i = 0;
                    438:                        do {
                    439:                                if ((dp = strchr(sp, '/')) != NULL)
                    440:                                        *(dp++) = '\0';
                    441:                                fields[i++] = sp;
                    442:                                sp = dp;
                    443:                        } while (dp != NULL && i < 2);
                    444:
                    445:                        if (cvs_file_cmpname(fields[0], cf->file_path) == 0) {
                    446:                                if (flags & BASE_GET) {
                    447:                                        ba_rev = rcsnum_parse(fields[1]);
                    448:                                        if (ba_rev == NULL)
                    449:                                                fatal("cvs_base_handle: "
                    450:                                                    "rcsnum_parse");
                    451:                                        goto got_rev;
                    452:                                }
                    453:                        } else {
                    454:                                if (flags & (BASE_ADD|BASE_REMOVE))
                    455:                                        (void)fprintf(tfp, "%s\n", buf);
1.23      xsa       456:                        }
                    457:                }
                    458:        }
                    459:
                    460: got_rev:
                    461:        if (flags & (BASE_ADD)) {
                    462:                (void)rcsnum_tostr(cf->file_ent->ce_rev, rbuf, sizeof(rbuf));
                    463:                (void)fprintf(tfp, "B%s/%s/\n", cf->file_path, rbuf);
                    464:        }
                    465:
                    466: out:
                    467:        if (fp != NULL)
                    468:                (void)fclose(fp);
                    469:
                    470:        if (tfp != NULL) {
                    471:                (void)fclose(tfp);
                    472:                (void)cvs_rename(CVS_PATH_BASEREVTMP, CVS_PATH_BASEREV);
                    473:        }
                    474:
                    475:        return (ba_rev);
1.1       jfb       476: }