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

1.92    ! joris       1: /*     $OpenBSD: add.c,v 1.91 2008/02/04 18:23:58 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.70      otto      125:        char entry[CVS_ENT_MAXLINELEN];
1.63      joris     126:        CVSENTRIES *entlist;
                    127:
                    128:        if (cf->file_type == CVS_DIR) {
1.74      xsa       129:                (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.89      tobias    130:                    "D/%s////", cf->file_name);
1.64      joris     131:
1.63      joris     132:                entlist = cvs_ent_open(cf->file_wd);
                    133:                cvs_ent_add(entlist, entry);
                    134:                cvs_ent_close(entlist, ENT_SYNC);
                    135:        } else {
                    136:                add_entry(cf);
                    137:        }
1.3       jfb       138: }
                    139:
1.43      joris     140: void
                    141: cvs_add_local(struct cvs_file *cf)
1.3       jfb       142: {
1.45      joris     143:        cvs_log(LP_TRACE, "cvs_add_local(%s)", cf->file_path);
                    144:
1.81      joris     145:        cvs_file_classify(cf, cvs_directory_tag);
1.49      xsa       146:
                    147:        /* dont use `cvs add *' */
                    148:        if (strcmp(cf->file_name, ".") == 0 ||
                    149:            strcmp(cf->file_name, "..") == 0 ||
                    150:            strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
                    151:                if (verbosity > 1)
                    152:                        cvs_log(LP_ERR,
                    153:                            "cannot add special file `%s'; skipping",
                    154:                            cf->file_name);
                    155:                return;
                    156:        }
1.45      joris     157:
                    158:        if (cf->file_type == CVS_DIR)
                    159:                add_directory(cf);
                    160:        else
                    161:                add_file(cf);
                    162: }
                    163:
1.85      tobias    164: void
                    165: cvs_add_remote(struct cvs_file *cf)
                    166: {
                    167:        char path[MAXPATHLEN];
                    168:
                    169:        cvs_log(LP_TRACE, "cvs_add_remote(%s)", cf->file_path);
                    170:
                    171:        cvs_file_classify(cf, cvs_directory_tag);
                    172:
                    173:        if (cf->file_type == CVS_DIR) {
                    174:                cvs_get_repository_path(cf->file_wd, path, MAXPATHLEN);
                    175:                if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
                    176:                        fatal("cvs_add_remote: truncation");
                    177:                if (strlcat(path, cf->file_path, sizeof(path)) >= sizeof(path))
                    178:                        fatal("cvs_add_remote: truncation");
                    179:                cvs_client_send_request("Directory %s\n%s", cf->file_path,
                    180:                    path);
                    181:
                    182:                add_directory(cf);
                    183:        } else {
                    184:                cvs_client_sendfile(cf);
                    185:        }
                    186: }
                    187:
1.45      joris     188: static void
                    189: add_directory(struct cvs_file *cf)
                    190: {
1.74      xsa       191:        int added, nb;
1.45      joris     192:        struct stat st;
1.44      joris     193:        CVSENTRIES *entlist;
1.70      otto      194:        char *date, entry[MAXPATHLEN], msg[1024], repo[MAXPATHLEN], *tag, *p;
1.45      joris     195:
                    196:        cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44      joris     197:
1.74      xsa       198:        (void)xsnprintf(entry, MAXPATHLEN, "%s%s",
                    199:            cf->file_rpath, RCS_FILE_EXT);
1.45      joris     200:
1.53      joris     201:        added = 1;
1.45      joris     202:        if (stat(entry, &st) != -1) {
                    203:                cvs_log(LP_NOTICE, "cannot add directory %s: "
                    204:                    "a file with that name already exists",
                    205:                    cf->file_path);
1.53      joris     206:                added = 0;
1.45      joris     207:        } else {
1.56      xsa       208:                /* Let's see if we have any per-directory tags first. */
                    209:                cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
                    210:
1.76      xsa       211:                (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
                    212:                    cf->file_path, CVS_PATH_CVSDIR);
1.45      joris     213:
1.85      tobias    214:                if (cvs_server_active) {
                    215:                        if (mkdir(cf->file_rpath, 0755) == -1 &&
                    216:                            errno != EEXIST)
                    217:                                fatal("add_directory: %s: %s", cf->file_rpath,
                    218:                                    strerror(errno));
                    219:                } else if (stat(entry, &st) != -1) {
1.45      joris     220:                        if (!S_ISDIR(st.st_mode)) {
                    221:                                cvs_log(LP_ERR, "%s exists but is not "
                    222:                                    "directory", entry);
                    223:                        } else {
                    224:                                cvs_log(LP_NOTICE, "%s already exists",
                    225:                                    entry);
                    226:                        }
1.53      joris     227:                        added = 0;
                    228:                } else if (cvs_noexec != 1) {
1.45      joris     229:                        if (mkdir(cf->file_rpath, 0755) == -1 &&
                    230:                            errno != EEXIST)
1.85      tobias    231:                                fatal("add_directory: %s: %s", cf->file_rpath,
1.45      joris     232:                                    strerror(errno));
                    233:
                    234:                        cvs_get_repository_name(cf->file_wd, repo,
                    235:                            MAXPATHLEN);
                    236:
1.76      xsa       237:                        (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
                    238:                            repo, cf->file_name);
1.45      joris     239:
                    240:                        cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
1.91      tobias    241:                            entry, tag, date);
1.45      joris     242:
1.70      otto      243:                        p = xmalloc(CVS_ENT_MAXLINELEN);
1.74      xsa       244:                        (void)xsnprintf(p, CVS_ENT_MAXLINELEN,
1.89      tobias    245:                            "D/%s////", cf->file_name);
1.45      joris     246:                        entlist = cvs_ent_open(cf->file_wd);
1.70      otto      247:                        cvs_ent_add(entlist, p);
1.45      joris     248:                        cvs_ent_close(entlist, ENT_SYNC);
1.79      joris     249:                        xfree(p);
1.53      joris     250:                }
                    251:        }
1.45      joris     252:
1.85      tobias    253:        if (added == 1 && current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.74      xsa       254:                (void)xsnprintf(msg, sizeof(msg),
1.56      xsa       255:                    "Directory %s added to the repository", cf->file_rpath);
                    256:
                    257:                if (tag != NULL) {
                    258:                        (void)strlcat(msg,
                    259:                            "\n--> Using per-directory sticky tag ",
                    260:                            sizeof(msg));
                    261:                        (void)strlcat(msg, tag, sizeof(msg));
                    262:                }
                    263:                if (date != NULL) {
                    264:                        (void)strlcat(msg,
                    265:                            "\n--> Using per-directory sticky date ",
                    266:                            sizeof(msg));
                    267:                        (void)strlcat(msg, date, sizeof(msg));
                    268:                }
                    269:                cvs_printf("%s\n", msg);
                    270:
                    271:                if (tag != NULL)
                    272:                        xfree(tag);
                    273:                if (date != NULL)
                    274:                        xfree(date);
1.45      joris     275:        }
                    276:
                    277:        cf->file_status = FILE_SKIP;
                    278: }
1.3       jfb       279:
1.45      joris     280: static void
                    281: add_file(struct cvs_file *cf)
                    282: {
1.55      xsa       283:        int added, stop;
1.78      xsa       284:        char revbuf[CVS_REV_BUFSZ];
1.52      joris     285:        RCSNUM *head;
1.10      xsa       286:
1.68      joris     287:        if (cf->file_rcs != NULL) {
                    288:                head = rcs_head_get(cf->file_rcs);
1.83      tobias    289:                if (head == NULL)
                    290:                        fatal("RCS head empty or missing in %s\n",
                    291:                            cf->file_rcs->rf_path);
1.68      joris     292:                rcsnum_tostr(head, revbuf, sizeof(revbuf));
                    293:                rcsnum_free(head);
                    294:        }
1.44      joris     295:
1.48      xsa       296:        added = stop = 0;
1.43      joris     297:        switch (cf->file_status) {
                    298:        case FILE_ADDED:
1.48      xsa       299:                if (verbosity > 1)
                    300:                        cvs_log(LP_NOTICE, "%s has already been entered",
                    301:                            cf->file_path);
                    302:                stop = 1;
                    303:                break;
                    304:        case FILE_REMOVED:
                    305:                if (cf->file_rcs == NULL) {
                    306:                        cvs_log(LP_NOTICE, "cannot resurrect %s; "
                    307:                            "RCS file removed by second party", cf->file_name);
1.73      joris     308:                } else if (cf->fd == -1) {
1.55      xsa       309:                        add_entry(cf);
1.48      xsa       310:
1.55      xsa       311:                        /* Restore the file. */
1.52      joris     312:                        head = rcs_head_get(cf->file_rcs);
1.83      tobias    313:                        if (head == NULL)
                    314:                                fatal("RCS head empty or missing in %s\n",
                    315:                                    cf->file_rcs->rf_path);
1.82      joris     316:                        cvs_checkout_file(cf, head, NULL, 0);
1.68      joris     317:                        rcsnum_free(head);
                    318:
1.48      xsa       319:                        cvs_printf("U %s\n", cf->file_path);
                    320:
                    321:                        cvs_log(LP_NOTICE, "%s, version %s, resurrected",
                    322:                            cf->file_name, revbuf);
                    323:
                    324:                        cf->file_status = FILE_UPTODATE;
                    325:                }
1.44      joris     326:                stop = 1;
                    327:                break;
1.50      xsa       328:        case FILE_CONFLICT:
                    329:        case FILE_LOST:
                    330:        case FILE_MODIFIED:
1.44      joris     331:        case FILE_UPTODATE:
                    332:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
                    333:                        cvs_log(LP_NOTICE, "%s already exists, with version "
                    334:                             "number %s", cf->file_path, revbuf);
                    335:                        stop = 1;
                    336:                }
1.43      joris     337:                break;
                    338:        case FILE_UNKNOWN:
1.44      joris     339:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
                    340:                        cvs_log(LP_NOTICE, "re-adding file %s "
                    341:                            "(instead of dead revision %s)",
                    342:                            cf->file_path, revbuf);
1.73      joris     343:                        added++;
                    344:                } else if (cf->fd != -1) {
1.44      joris     345:                        cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
                    346:                            cf->file_path);
1.73      joris     347:                        added++;
                    348:                } else {
                    349:                        stop = 1;
1.44      joris     350:                }
1.43      joris     351:                break;
                    352:        default:
                    353:                break;
1.1       jfb       354:        }
1.44      joris     355:
                    356:        if (stop == 1)
                    357:                return;
                    358:
1.55      xsa       359:        add_entry(cf);
1.44      joris     360:
1.48      xsa       361:        if (added != 0) {
1.67      joris     362:                cvs_log(LP_NOTICE, "use '%s commit' to add %s "
                    363:                    "permanently", __progname,
                    364:                    (added == 1) ? "this file" : "these files");
1.48      xsa       365:        }
1.55      xsa       366: }
                    367:
                    368: static void
                    369: add_entry(struct cvs_file *cf)
                    370: {
                    371:        FILE *fp;
1.78      xsa       372:        char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
                    373:        char revbuf[CVS_REV_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.55      xsa       374:        CVSENTRIES *entlist;
                    375:
                    376:        if (cvs_noexec == 1)
                    377:                return;
                    378:
                    379:        if (cf->file_status == FILE_REMOVED) {
                    380:                rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
                    381:
                    382:                ctime_r(&cf->file_ent->ce_mtime, tbuf);
1.86      tobias    383:                tbuf[strcspn(tbuf, "\n")] = '\0';
1.55      xsa       384:
                    385:                /* Remove the '-' prefixing the version number. */
1.74      xsa       386:                (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.72      xsa       387:                    "/%s/%s/%s/%s/", cf->file_name, revbuf, tbuf,
                    388:                    cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "");
1.55      xsa       389:        } else {
                    390:                if (logmsg != NULL) {
1.74      xsa       391:                        (void)xsnprintf(path, MAXPATHLEN, "%s/%s%s",
1.55      xsa       392:                            CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
                    393:
                    394:                        if ((fp = fopen(path, "w+")) == NULL)
                    395:                                fatal("add_entry: fopen `%s': %s",
                    396:                                    path, strerror(errno));
                    397:
                    398:                        if (fputs(logmsg, fp) == EOF) {
                    399:                                (void)unlink(path);
                    400:                                fatal("add_entry: fputs `%s': %s",
                    401:                                    path, strerror(errno));
                    402:                        }
                    403:                        (void)fclose(fp);
                    404:                }
                    405:
1.74      xsa       406:                (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.72      xsa       407:                    "/%s/0/Initial %s/%s/", cf->file_name, cf->file_name,
1.90      tobias    408:                    kflag ? kbuf : "");
1.55      xsa       409:        }
                    410:
1.84      tobias    411:        if (cvs_server_active) {
                    412:                cvs_server_send_response("Checked-in %s/", cf->file_wd);
                    413:                cvs_server_send_response(cf->file_path);
                    414:                cvs_server_send_response(entry);
                    415:        } else {
                    416:                entlist = cvs_ent_open(cf->file_wd);
                    417:                cvs_ent_add(entlist, entry);
                    418:                cvs_ent_close(entlist, ENT_SYNC);
                    419:        }
1.1       jfb       420: }