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

Annotation of src/usr.bin/cvs/add.c, Revision 1.94

1.94    ! xsa         1: /*     $OpenBSD: add.c,v 1.93 2008/02/06 18:12:28 tobias Exp $ */
1.1       jfb         2: /*
1.43      joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.55      xsa         4:  * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.1       jfb         5:  *
1.43      joris       6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       jfb         9:  *
1.43      joris      10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       jfb        17:  */
                     18:
1.77      otto       19: #include <sys/stat.h>
                     20:
                     21: #include <errno.h>
                     22: #include <string.h>
                     23: #include <unistd.h>
1.1       jfb        24:
                     25: #include "cvs.h"
1.61      xsa        26: #include "remote.h"
1.1       jfb        27:
1.48      xsa        28: extern char *__progname;
                     29:
1.43      joris      30: void   cvs_add_local(struct cvs_file *);
1.63      joris      31: void   cvs_add_entry(struct cvs_file *);
1.85      tobias     32: void   cvs_add_remote(struct cvs_file *);
1.1       jfb        33:
1.45      joris      34: static void add_directory(struct cvs_file *);
                     35: static void add_file(struct cvs_file *);
1.55      xsa        36: static void add_entry(struct cvs_file *);
1.45      joris      37:
1.90      tobias     38: int            kflag = 0;
                     39: static char    kbuf[8];
1.72      xsa        40:
1.43      joris      41: char   *logmsg;
1.21      jfb        42:
                     43: struct cvs_cmd cvs_cmd_add = {
1.88      tobias     44:        CVS_OP_ADD, CVS_USE_WDIR, "add",
1.21      jfb        45:        { "ad", "new" },
1.43      joris      46:        "Add a new file or directory to the repository",
1.72      xsa        47:        "[-k mode] [-m message] ...",
                     48:        "k:m:",
1.21      jfb        49:        NULL,
1.43      joris      50:        cvs_add
1.15      joris      51: };
1.3       jfb        52:
1.43      joris      53: int
                     54: cvs_add(int argc, char **argv)
1.1       jfb        55: {
1.15      joris      56:        int ch;
1.43      joris      57:        int flags;
                     58:        struct cvs_recursion cr;
1.1       jfb        59:
1.45      joris      60:        flags = CR_REPO;
1.1       jfb        61:
1.43      joris      62:        while ((ch = getopt(argc, argv, cvs_cmd_add.cmd_opts)) != -1) {
1.1       jfb        63:                switch (ch) {
1.72      xsa        64:                case 'k':
1.90      tobias     65:                        kflag = rcs_kflag_get(optarg);
1.72      xsa        66:                        if (RCS_KWEXP_INVAL(kflag)) {
                     67:                                cvs_log(LP_ERR,
                     68:                                    "invalid RCS keyword expension mode");
                     69:                                fatal("%s", cvs_cmd_add.cmd_synopsis);
                     70:                        }
1.90      tobias     71:                        (void)xsnprintf(kbuf, sizeof(kbuf), "-k%s", optarg);
1.72      xsa        72:                        break;
1.1       jfb        73:                case 'm':
1.87      tobias     74:                        logmsg = optarg;
1.1       jfb        75:                        break;
                     76:                default:
1.43      joris      77:                        fatal("%s", cvs_cmd_add.cmd_synopsis);
1.1       jfb        78:                }
                     79:        }
                     80:
1.43      joris      81:        argc -= optind;
                     82:        argv += optind;
1.28      xsa        83:
1.46      joris      84:        if (argc == 0)
                     85:                fatal("%s", cvs_cmd_add.cmd_synopsis);
                     86:
1.43      joris      87:        cr.enterdir = NULL;
                     88:        cr.leavedir = NULL;
1.61      xsa        89:
                     90:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.66      joris      91:                cvs_client_connect_to_server();
1.85      tobias     92:                cr.fileproc = cvs_add_remote;
1.61      xsa        93:
1.92      joris      94:                if (kflag)
1.72      xsa        95:                        cvs_client_send_request("Argument %s", kbuf);
                     96:
1.61      xsa        97:                if (logmsg != NULL)
1.80      joris      98:                        cvs_client_send_logmsg(logmsg);
1.61      xsa        99:        } else {
                    100:                cr.fileproc = cvs_add_local;
                    101:        }
                    102:
1.43      joris     103:        cr.flags = flags;
                    104:
1.46      joris     105:        cvs_file_run(argc, argv, &cr);
1.61      xsa       106:
                    107:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.85      tobias    108:                cvs_client_senddir(".");
1.61      xsa       109:                cvs_client_send_files(argv, argc);
                    110:                cvs_client_send_request("add");
                    111:                cvs_client_get_responses();
1.63      joris     112:
                    113:                if (server_response == SERVER_OK) {
                    114:                        cr.fileproc = cvs_add_entry;
                    115:                        cvs_file_run(argc, argv, &cr);
                    116:                }
1.61      xsa       117:        }
                    118:
1.3       jfb       119:        return (0);
1.63      joris     120: }
                    121:
                    122: void
                    123: cvs_add_entry(struct cvs_file *cf)
                    124: {
1.94    ! xsa       125:        char *entry;
1.63      joris     126:        CVSENTRIES *entlist;
                    127:
                    128:        if (cf->file_type == CVS_DIR) {
1.94    ! xsa       129:                entry = xmalloc(CVS_ENT_MAXLINELEN);
        !           130:                cvs_ent_line_str(cf->file_name, NULL, NULL, NULL, NULL, 1, 0,
        !           131:                    entry, CVS_ENT_MAXLINELEN);
1.64      joris     132:
1.63      joris     133:                entlist = cvs_ent_open(cf->file_wd);
                    134:                cvs_ent_add(entlist, entry);
                    135:                cvs_ent_close(entlist, ENT_SYNC);
1.94    ! xsa       136:
        !           137:                xfree(entry);
1.63      joris     138:        } else {
                    139:                add_entry(cf);
                    140:        }
1.3       jfb       141: }
                    142:
1.43      joris     143: void
                    144: cvs_add_local(struct cvs_file *cf)
1.3       jfb       145: {
1.45      joris     146:        cvs_log(LP_TRACE, "cvs_add_local(%s)", cf->file_path);
                    147:
1.81      joris     148:        cvs_file_classify(cf, cvs_directory_tag);
1.49      xsa       149:
                    150:        /* dont use `cvs add *' */
                    151:        if (strcmp(cf->file_name, ".") == 0 ||
                    152:            strcmp(cf->file_name, "..") == 0 ||
                    153:            strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
                    154:                if (verbosity > 1)
                    155:                        cvs_log(LP_ERR,
                    156:                            "cannot add special file `%s'; skipping",
                    157:                            cf->file_name);
                    158:                return;
                    159:        }
1.45      joris     160:
                    161:        if (cf->file_type == CVS_DIR)
                    162:                add_directory(cf);
                    163:        else
                    164:                add_file(cf);
                    165: }
                    166:
1.85      tobias    167: void
                    168: cvs_add_remote(struct cvs_file *cf)
                    169: {
                    170:        char path[MAXPATHLEN];
                    171:
                    172:        cvs_log(LP_TRACE, "cvs_add_remote(%s)", cf->file_path);
                    173:
                    174:        cvs_file_classify(cf, cvs_directory_tag);
                    175:
                    176:        if (cf->file_type == CVS_DIR) {
                    177:                cvs_get_repository_path(cf->file_wd, path, MAXPATHLEN);
                    178:                if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
                    179:                        fatal("cvs_add_remote: truncation");
                    180:                if (strlcat(path, cf->file_path, sizeof(path)) >= sizeof(path))
                    181:                        fatal("cvs_add_remote: truncation");
                    182:                cvs_client_send_request("Directory %s\n%s", cf->file_path,
                    183:                    path);
                    184:
                    185:                add_directory(cf);
                    186:        } else {
                    187:                cvs_client_sendfile(cf);
                    188:        }
                    189: }
                    190:
1.45      joris     191: static void
                    192: add_directory(struct cvs_file *cf)
                    193: {
1.74      xsa       194:        int added, nb;
1.45      joris     195:        struct stat st;
1.44      joris     196:        CVSENTRIES *entlist;
1.70      otto      197:        char *date, entry[MAXPATHLEN], msg[1024], repo[MAXPATHLEN], *tag, *p;
1.45      joris     198:
                    199:        cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44      joris     200:
1.74      xsa       201:        (void)xsnprintf(entry, MAXPATHLEN, "%s%s",
                    202:            cf->file_rpath, RCS_FILE_EXT);
1.45      joris     203:
1.53      joris     204:        added = 1;
1.45      joris     205:        if (stat(entry, &st) != -1) {
                    206:                cvs_log(LP_NOTICE, "cannot add directory %s: "
                    207:                    "a file with that name already exists",
                    208:                    cf->file_path);
1.53      joris     209:                added = 0;
1.45      joris     210:        } else {
1.56      xsa       211:                /* Let's see if we have any per-directory tags first. */
                    212:                cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
                    213:
1.76      xsa       214:                (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
                    215:                    cf->file_path, CVS_PATH_CVSDIR);
1.45      joris     216:
1.85      tobias    217:                if (cvs_server_active) {
                    218:                        if (mkdir(cf->file_rpath, 0755) == -1 &&
                    219:                            errno != EEXIST)
                    220:                                fatal("add_directory: %s: %s", cf->file_rpath,
                    221:                                    strerror(errno));
                    222:                } else if (stat(entry, &st) != -1) {
1.45      joris     223:                        if (!S_ISDIR(st.st_mode)) {
                    224:                                cvs_log(LP_ERR, "%s exists but is not "
                    225:                                    "directory", entry);
                    226:                        } else {
                    227:                                cvs_log(LP_NOTICE, "%s already exists",
                    228:                                    entry);
                    229:                        }
1.53      joris     230:                        added = 0;
                    231:                } else if (cvs_noexec != 1) {
1.45      joris     232:                        if (mkdir(cf->file_rpath, 0755) == -1 &&
                    233:                            errno != EEXIST)
1.85      tobias    234:                                fatal("add_directory: %s: %s", cf->file_rpath,
1.45      joris     235:                                    strerror(errno));
                    236:
                    237:                        cvs_get_repository_name(cf->file_wd, repo,
                    238:                            MAXPATHLEN);
                    239:
1.76      xsa       240:                        (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
                    241:                            repo, cf->file_name);
1.45      joris     242:
                    243:                        cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
1.91      tobias    244:                            entry, tag, date);
1.45      joris     245:
1.70      otto      246:                        p = xmalloc(CVS_ENT_MAXLINELEN);
1.94    ! xsa       247:                        cvs_ent_line_str(cf->file_name, NULL, NULL, NULL,
        !           248:                            NULL, 1, 0, p, CVS_ENT_MAXLINELEN);
        !           249:
1.45      joris     250:                        entlist = cvs_ent_open(cf->file_wd);
1.70      otto      251:                        cvs_ent_add(entlist, p);
1.45      joris     252:                        cvs_ent_close(entlist, ENT_SYNC);
1.79      joris     253:                        xfree(p);
1.53      joris     254:                }
                    255:        }
1.45      joris     256:
1.85      tobias    257:        if (added == 1 && current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.74      xsa       258:                (void)xsnprintf(msg, sizeof(msg),
1.56      xsa       259:                    "Directory %s added to the repository", cf->file_rpath);
                    260:
                    261:                if (tag != NULL) {
                    262:                        (void)strlcat(msg,
                    263:                            "\n--> Using per-directory sticky tag ",
                    264:                            sizeof(msg));
                    265:                        (void)strlcat(msg, tag, sizeof(msg));
                    266:                }
                    267:                if (date != NULL) {
                    268:                        (void)strlcat(msg,
                    269:                            "\n--> Using per-directory sticky date ",
                    270:                            sizeof(msg));
                    271:                        (void)strlcat(msg, date, sizeof(msg));
                    272:                }
                    273:                cvs_printf("%s\n", msg);
                    274:
                    275:                if (tag != NULL)
                    276:                        xfree(tag);
                    277:                if (date != NULL)
                    278:                        xfree(date);
1.45      joris     279:        }
                    280:
                    281:        cf->file_status = FILE_SKIP;
                    282: }
1.3       jfb       283:
1.45      joris     284: static void
                    285: add_file(struct cvs_file *cf)
                    286: {
1.93      tobias    287:        int added, nb, stop;
1.78      xsa       288:        char revbuf[CVS_REV_BUFSZ];
1.52      joris     289:        RCSNUM *head;
1.93      tobias    290:        char *tag;
                    291:
                    292:        cvs_parse_tagfile(cf->file_wd, &tag, NULL, &nb);
                    293:        if (nb) {
                    294:                cvs_log(LP_ERR, "cannot add file on non-branch tag %s", tag);
                    295:                return;
                    296:        }
1.10      xsa       297:
1.68      joris     298:        if (cf->file_rcs != NULL) {
                    299:                head = rcs_head_get(cf->file_rcs);
1.83      tobias    300:                if (head == NULL)
                    301:                        fatal("RCS head empty or missing in %s\n",
                    302:                            cf->file_rcs->rf_path);
1.68      joris     303:                rcsnum_tostr(head, revbuf, sizeof(revbuf));
                    304:                rcsnum_free(head);
                    305:        }
1.44      joris     306:
1.48      xsa       307:        added = stop = 0;
1.43      joris     308:        switch (cf->file_status) {
                    309:        case FILE_ADDED:
1.48      xsa       310:                if (verbosity > 1)
                    311:                        cvs_log(LP_NOTICE, "%s has already been entered",
                    312:                            cf->file_path);
                    313:                stop = 1;
                    314:                break;
                    315:        case FILE_REMOVED:
                    316:                if (cf->file_rcs == NULL) {
                    317:                        cvs_log(LP_NOTICE, "cannot resurrect %s; "
                    318:                            "RCS file removed by second party", cf->file_name);
1.73      joris     319:                } else if (cf->fd == -1) {
1.55      xsa       320:                        add_entry(cf);
1.48      xsa       321:
1.55      xsa       322:                        /* Restore the file. */
1.52      joris     323:                        head = rcs_head_get(cf->file_rcs);
1.83      tobias    324:                        if (head == NULL)
                    325:                                fatal("RCS head empty or missing in %s\n",
                    326:                                    cf->file_rcs->rf_path);
1.82      joris     327:                        cvs_checkout_file(cf, head, NULL, 0);
1.68      joris     328:                        rcsnum_free(head);
                    329:
1.48      xsa       330:                        cvs_printf("U %s\n", cf->file_path);
                    331:
                    332:                        cvs_log(LP_NOTICE, "%s, version %s, resurrected",
                    333:                            cf->file_name, revbuf);
                    334:
                    335:                        cf->file_status = FILE_UPTODATE;
                    336:                }
1.44      joris     337:                stop = 1;
                    338:                break;
1.50      xsa       339:        case FILE_CONFLICT:
                    340:        case FILE_LOST:
                    341:        case FILE_MODIFIED:
1.44      joris     342:        case FILE_UPTODATE:
                    343:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
                    344:                        cvs_log(LP_NOTICE, "%s already exists, with version "
                    345:                             "number %s", cf->file_path, revbuf);
                    346:                        stop = 1;
                    347:                }
1.43      joris     348:                break;
                    349:        case FILE_UNKNOWN:
1.44      joris     350:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
                    351:                        cvs_log(LP_NOTICE, "re-adding file %s "
                    352:                            "(instead of dead revision %s)",
                    353:                            cf->file_path, revbuf);
1.73      joris     354:                        added++;
                    355:                } else if (cf->fd != -1) {
1.44      joris     356:                        cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
                    357:                            cf->file_path);
1.73      joris     358:                        added++;
                    359:                } else {
                    360:                        stop = 1;
1.44      joris     361:                }
1.43      joris     362:                break;
                    363:        default:
                    364:                break;
1.1       jfb       365:        }
1.44      joris     366:
                    367:        if (stop == 1)
                    368:                return;
                    369:
1.55      xsa       370:        add_entry(cf);
1.44      joris     371:
1.48      xsa       372:        if (added != 0) {
1.67      joris     373:                cvs_log(LP_NOTICE, "use '%s commit' to add %s "
                    374:                    "permanently", __progname,
                    375:                    (added == 1) ? "this file" : "these files");
1.48      xsa       376:        }
1.55      xsa       377: }
                    378:
                    379: static void
                    380: add_entry(struct cvs_file *cf)
                    381: {
                    382:        FILE *fp;
1.94    ! xsa       383:        char *entry, path[MAXPATHLEN];
1.78      xsa       384:        char revbuf[CVS_REV_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.93      tobias    385:        char sticky[CVS_ENT_MAXLINELEN];
1.55      xsa       386:        CVSENTRIES *entlist;
                    387:
                    388:        if (cvs_noexec == 1)
                    389:                return;
                    390:
1.93      tobias    391:        sticky[0] = '\0';
1.94    ! xsa       392:        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.93      tobias    393:
1.55      xsa       394:        if (cf->file_status == FILE_REMOVED) {
                    395:                rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
                    396:
                    397:                ctime_r(&cf->file_ent->ce_mtime, tbuf);
1.86      tobias    398:                tbuf[strcspn(tbuf, "\n")] = '\0';
1.55      xsa       399:
1.93      tobias    400:                if (cf->file_ent->ce_tag != NULL)
                    401:                        (void)xsnprintf(sticky, sizeof(sticky), "T%s",
                    402:                            cf->file_ent->ce_tag);
                    403:
1.55      xsa       404:                /* Remove the '-' prefixing the version number. */
1.94    ! xsa       405:                cvs_ent_line_str(cf->file_name, revbuf, tbuf,
        !           406:                    cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "", sticky,
        !           407:                    0, 0, entry, CVS_ENT_MAXLINELEN);
1.55      xsa       408:        } else {
                    409:                if (logmsg != NULL) {
1.74      xsa       410:                        (void)xsnprintf(path, MAXPATHLEN, "%s/%s%s",
1.55      xsa       411:                            CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
                    412:
                    413:                        if ((fp = fopen(path, "w+")) == NULL)
                    414:                                fatal("add_entry: fopen `%s': %s",
                    415:                                    path, strerror(errno));
                    416:
                    417:                        if (fputs(logmsg, fp) == EOF) {
                    418:                                (void)unlink(path);
                    419:                                fatal("add_entry: fputs `%s': %s",
                    420:                                    path, strerror(errno));
                    421:                        }
                    422:                        (void)fclose(fp);
                    423:                }
                    424:
1.93      tobias    425:                if (cvs_directory_tag != NULL)
                    426:                        (void)xsnprintf(sticky, sizeof(sticky), "T%s",
                    427:                            cvs_directory_tag);
                    428:
                    429:                tbuf[0] = '\0';
                    430:                if (!cvs_server_active)
                    431:                        (void)xsnprintf(tbuf, sizeof(tbuf), "Initial %s",
                    432:                            cf->file_name);
                    433:
1.94    ! xsa       434:
        !           435:                cvs_ent_line_str(cf->file_name, "0", tbuf, kflag ? kbuf : "",
        !           436:                    sticky, 0, 0, entry, CVS_ENT_MAXLINELEN);
1.55      xsa       437:        }
                    438:
1.84      tobias    439:        if (cvs_server_active) {
                    440:                cvs_server_send_response("Checked-in %s/", cf->file_wd);
                    441:                cvs_server_send_response(cf->file_path);
                    442:                cvs_server_send_response(entry);
                    443:        } else {
                    444:                entlist = cvs_ent_open(cf->file_wd);
                    445:                cvs_ent_add(entlist, entry);
                    446:                cvs_ent_close(entlist, ENT_SYNC);
                    447:        }
1.94    ! xsa       448:        xfree(entry);
1.1       jfb       449: }