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

1.98    ! tobias      1: /*     $OpenBSD: add.c,v 1.97 2008/03/09 03:41:55 joris 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.63      joris      30: void   cvs_add_entry(struct cvs_file *);
1.85      tobias     31: void   cvs_add_remote(struct cvs_file *);
1.1       jfb        32:
1.45      joris      33: static void add_directory(struct cvs_file *);
                     34: static void add_file(struct cvs_file *);
1.55      xsa        35: static void add_entry(struct cvs_file *);
1.45      joris      36:
1.90      tobias     37: int            kflag = 0;
                     38: static char    kbuf[8];
1.72      xsa        39:
1.43      joris      40: char   *logmsg;
1.21      jfb        41:
                     42: struct cvs_cmd cvs_cmd_add = {
1.88      tobias     43:        CVS_OP_ADD, CVS_USE_WDIR, "add",
1.21      jfb        44:        { "ad", "new" },
1.43      joris      45:        "Add a new file or directory to the repository",
1.72      xsa        46:        "[-k mode] [-m message] ...",
                     47:        "k:m:",
1.21      jfb        48:        NULL,
1.43      joris      49:        cvs_add
1.15      joris      50: };
1.3       jfb        51:
1.43      joris      52: int
                     53: cvs_add(int argc, char **argv)
1.1       jfb        54: {
1.15      joris      55:        int ch;
1.43      joris      56:        int flags;
                     57:        struct cvs_recursion cr;
1.1       jfb        58:
1.45      joris      59:        flags = CR_REPO;
1.1       jfb        60:
1.43      joris      61:        while ((ch = getopt(argc, argv, cvs_cmd_add.cmd_opts)) != -1) {
1.1       jfb        62:                switch (ch) {
1.72      xsa        63:                case 'k':
1.90      tobias     64:                        kflag = rcs_kflag_get(optarg);
1.72      xsa        65:                        if (RCS_KWEXP_INVAL(kflag)) {
                     66:                                cvs_log(LP_ERR,
1.98    ! tobias     67:                                    "invalid RCS keyword expansion mode");
1.72      xsa        68:                                fatal("%s", cvs_cmd_add.cmd_synopsis);
                     69:                        }
1.90      tobias     70:                        (void)xsnprintf(kbuf, sizeof(kbuf), "-k%s", optarg);
1.72      xsa        71:                        break;
1.1       jfb        72:                case 'm':
1.87      tobias     73:                        logmsg = optarg;
1.1       jfb        74:                        break;
                     75:                default:
1.43      joris      76:                        fatal("%s", cvs_cmd_add.cmd_synopsis);
1.1       jfb        77:                }
                     78:        }
                     79:
1.43      joris      80:        argc -= optind;
                     81:        argv += optind;
1.28      xsa        82:
1.46      joris      83:        if (argc == 0)
                     84:                fatal("%s", cvs_cmd_add.cmd_synopsis);
                     85:
1.43      joris      86:        cr.enterdir = NULL;
                     87:        cr.leavedir = NULL;
1.61      xsa        88:
                     89:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.66      joris      90:                cvs_client_connect_to_server();
1.85      tobias     91:                cr.fileproc = cvs_add_remote;
1.97      joris      92:                flags = 0;
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.96      joris     148:        if (cvs_cmdop != CVS_OP_CHECKOUT && cvs_cmdop != CVS_OP_UPDATE)
                    149:                cvs_file_classify(cf, cvs_directory_tag);
1.49      xsa       150:
                    151:        /* dont use `cvs add *' */
                    152:        if (strcmp(cf->file_name, ".") == 0 ||
                    153:            strcmp(cf->file_name, "..") == 0 ||
                    154:            strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
                    155:                if (verbosity > 1)
                    156:                        cvs_log(LP_ERR,
                    157:                            "cannot add special file `%s'; skipping",
                    158:                            cf->file_name);
                    159:                return;
                    160:        }
1.45      joris     161:
                    162:        if (cf->file_type == CVS_DIR)
                    163:                add_directory(cf);
                    164:        else
                    165:                add_file(cf);
                    166: }
                    167:
1.85      tobias    168: void
                    169: cvs_add_remote(struct cvs_file *cf)
                    170: {
                    171:        char path[MAXPATHLEN];
                    172:
                    173:        cvs_log(LP_TRACE, "cvs_add_remote(%s)", cf->file_path);
                    174:
                    175:        cvs_file_classify(cf, cvs_directory_tag);
                    176:
                    177:        if (cf->file_type == CVS_DIR) {
                    178:                cvs_get_repository_path(cf->file_wd, path, MAXPATHLEN);
                    179:                if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
                    180:                        fatal("cvs_add_remote: truncation");
                    181:                if (strlcat(path, cf->file_path, sizeof(path)) >= sizeof(path))
                    182:                        fatal("cvs_add_remote: truncation");
                    183:                cvs_client_send_request("Directory %s\n%s", cf->file_path,
                    184:                    path);
                    185:
                    186:                add_directory(cf);
                    187:        } else {
                    188:                cvs_client_sendfile(cf);
                    189:        }
                    190: }
                    191:
1.45      joris     192: static void
                    193: add_directory(struct cvs_file *cf)
                    194: {
1.74      xsa       195:        int added, nb;
1.45      joris     196:        struct stat st;
1.44      joris     197:        CVSENTRIES *entlist;
1.70      otto      198:        char *date, entry[MAXPATHLEN], msg[1024], repo[MAXPATHLEN], *tag, *p;
1.45      joris     199:
                    200:        cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44      joris     201:
1.74      xsa       202:        (void)xsnprintf(entry, MAXPATHLEN, "%s%s",
                    203:            cf->file_rpath, RCS_FILE_EXT);
1.45      joris     204:
1.53      joris     205:        added = 1;
1.45      joris     206:        if (stat(entry, &st) != -1) {
                    207:                cvs_log(LP_NOTICE, "cannot add directory %s: "
                    208:                    "a file with that name already exists",
                    209:                    cf->file_path);
1.53      joris     210:                added = 0;
1.45      joris     211:        } else {
1.56      xsa       212:                /* Let's see if we have any per-directory tags first. */
                    213:                cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
                    214:
1.76      xsa       215:                (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
                    216:                    cf->file_path, CVS_PATH_CVSDIR);
1.45      joris     217:
1.85      tobias    218:                if (cvs_server_active) {
                    219:                        if (mkdir(cf->file_rpath, 0755) == -1 &&
                    220:                            errno != EEXIST)
                    221:                                fatal("add_directory: %s: %s", cf->file_rpath,
                    222:                                    strerror(errno));
                    223:                } else if (stat(entry, &st) != -1) {
1.45      joris     224:                        if (!S_ISDIR(st.st_mode)) {
                    225:                                cvs_log(LP_ERR, "%s exists but is not "
                    226:                                    "directory", entry);
                    227:                        } else {
                    228:                                cvs_log(LP_NOTICE, "%s already exists",
                    229:                                    entry);
                    230:                        }
1.53      joris     231:                        added = 0;
                    232:                } else if (cvs_noexec != 1) {
1.45      joris     233:                        if (mkdir(cf->file_rpath, 0755) == -1 &&
                    234:                            errno != EEXIST)
1.85      tobias    235:                                fatal("add_directory: %s: %s", cf->file_rpath,
1.45      joris     236:                                    strerror(errno));
                    237:
                    238:                        cvs_get_repository_name(cf->file_wd, repo,
                    239:                            MAXPATHLEN);
                    240:
1.76      xsa       241:                        (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
                    242:                            repo, cf->file_name);
1.45      joris     243:
                    244:                        cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
1.91      tobias    245:                            entry, tag, date);
1.45      joris     246:
1.70      otto      247:                        p = xmalloc(CVS_ENT_MAXLINELEN);
1.94      xsa       248:                        cvs_ent_line_str(cf->file_name, NULL, NULL, NULL,
                    249:                            NULL, 1, 0, p, CVS_ENT_MAXLINELEN);
                    250:
1.45      joris     251:                        entlist = cvs_ent_open(cf->file_wd);
1.70      otto      252:                        cvs_ent_add(entlist, p);
1.45      joris     253:                        cvs_ent_close(entlist, ENT_SYNC);
1.79      joris     254:                        xfree(p);
1.53      joris     255:                }
                    256:        }
1.45      joris     257:
1.85      tobias    258:        if (added == 1 && current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.74      xsa       259:                (void)xsnprintf(msg, sizeof(msg),
1.56      xsa       260:                    "Directory %s added to the repository", cf->file_rpath);
                    261:
                    262:                if (tag != NULL) {
                    263:                        (void)strlcat(msg,
                    264:                            "\n--> Using per-directory sticky tag ",
                    265:                            sizeof(msg));
                    266:                        (void)strlcat(msg, tag, sizeof(msg));
                    267:                }
                    268:                if (date != NULL) {
                    269:                        (void)strlcat(msg,
                    270:                            "\n--> Using per-directory sticky date ",
                    271:                            sizeof(msg));
                    272:                        (void)strlcat(msg, date, sizeof(msg));
                    273:                }
                    274:                cvs_printf("%s\n", msg);
                    275:
                    276:                if (tag != NULL)
                    277:                        xfree(tag);
                    278:                if (date != NULL)
                    279:                        xfree(date);
1.45      joris     280:        }
                    281:
                    282:        cf->file_status = FILE_SKIP;
                    283: }
1.3       jfb       284:
1.45      joris     285: static void
                    286: add_file(struct cvs_file *cf)
                    287: {
1.93      tobias    288:        int added, nb, stop;
1.78      xsa       289:        char revbuf[CVS_REV_BUFSZ];
1.52      joris     290:        RCSNUM *head;
1.93      tobias    291:        char *tag;
                    292:
                    293:        cvs_parse_tagfile(cf->file_wd, &tag, NULL, &nb);
                    294:        if (nb) {
                    295:                cvs_log(LP_ERR, "cannot add file on non-branch tag %s", tag);
                    296:                return;
                    297:        }
1.10      xsa       298:
1.68      joris     299:        if (cf->file_rcs != NULL) {
                    300:                head = rcs_head_get(cf->file_rcs);
1.83      tobias    301:                if (head == NULL)
                    302:                        fatal("RCS head empty or missing in %s\n",
                    303:                            cf->file_rcs->rf_path);
1.68      joris     304:                rcsnum_tostr(head, revbuf, sizeof(revbuf));
                    305:                rcsnum_free(head);
                    306:        }
1.44      joris     307:
1.48      xsa       308:        added = stop = 0;
1.43      joris     309:        switch (cf->file_status) {
                    310:        case FILE_ADDED:
1.48      xsa       311:                if (verbosity > 1)
                    312:                        cvs_log(LP_NOTICE, "%s has already been entered",
                    313:                            cf->file_path);
                    314:                stop = 1;
                    315:                break;
                    316:        case FILE_REMOVED:
                    317:                if (cf->file_rcs == NULL) {
                    318:                        cvs_log(LP_NOTICE, "cannot resurrect %s; "
                    319:                            "RCS file removed by second party", cf->file_name);
1.73      joris     320:                } else if (cf->fd == -1) {
1.55      xsa       321:                        add_entry(cf);
1.48      xsa       322:
1.55      xsa       323:                        /* Restore the file. */
1.52      joris     324:                        head = rcs_head_get(cf->file_rcs);
1.83      tobias    325:                        if (head == NULL)
                    326:                                fatal("RCS head empty or missing in %s\n",
                    327:                                    cf->file_rcs->rf_path);
1.82      joris     328:                        cvs_checkout_file(cf, head, NULL, 0);
1.68      joris     329:                        rcsnum_free(head);
                    330:
1.48      xsa       331:                        cvs_printf("U %s\n", cf->file_path);
                    332:
                    333:                        cvs_log(LP_NOTICE, "%s, version %s, resurrected",
                    334:                            cf->file_name, revbuf);
                    335:
                    336:                        cf->file_status = FILE_UPTODATE;
                    337:                }
1.44      joris     338:                stop = 1;
                    339:                break;
1.50      xsa       340:        case FILE_CONFLICT:
                    341:        case FILE_LOST:
                    342:        case FILE_MODIFIED:
1.44      joris     343:        case FILE_UPTODATE:
                    344:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
                    345:                        cvs_log(LP_NOTICE, "%s already exists, with version "
                    346:                             "number %s", cf->file_path, revbuf);
                    347:                        stop = 1;
                    348:                }
1.43      joris     349:                break;
                    350:        case FILE_UNKNOWN:
1.44      joris     351:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
                    352:                        cvs_log(LP_NOTICE, "re-adding file %s "
                    353:                            "(instead of dead revision %s)",
                    354:                            cf->file_path, revbuf);
1.73      joris     355:                        added++;
                    356:                } else if (cf->fd != -1) {
1.44      joris     357:                        cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
                    358:                            cf->file_path);
1.73      joris     359:                        added++;
                    360:                } else {
                    361:                        stop = 1;
1.44      joris     362:                }
1.43      joris     363:                break;
                    364:        default:
                    365:                break;
1.1       jfb       366:        }
1.44      joris     367:
                    368:        if (stop == 1)
                    369:                return;
                    370:
1.55      xsa       371:        add_entry(cf);
1.44      joris     372:
1.48      xsa       373:        if (added != 0) {
1.67      joris     374:                cvs_log(LP_NOTICE, "use '%s commit' to add %s "
                    375:                    "permanently", __progname,
                    376:                    (added == 1) ? "this file" : "these files");
1.48      xsa       377:        }
1.55      xsa       378: }
                    379:
                    380: static void
                    381: add_entry(struct cvs_file *cf)
                    382: {
                    383:        FILE *fp;
1.94      xsa       384:        char *entry, path[MAXPATHLEN];
1.78      xsa       385:        char revbuf[CVS_REV_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.93      tobias    386:        char sticky[CVS_ENT_MAXLINELEN];
1.55      xsa       387:        CVSENTRIES *entlist;
                    388:
                    389:        if (cvs_noexec == 1)
                    390:                return;
                    391:
1.93      tobias    392:        sticky[0] = '\0';
1.94      xsa       393:        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.93      tobias    394:
1.55      xsa       395:        if (cf->file_status == FILE_REMOVED) {
                    396:                rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
                    397:
                    398:                ctime_r(&cf->file_ent->ce_mtime, tbuf);
1.86      tobias    399:                tbuf[strcspn(tbuf, "\n")] = '\0';
1.55      xsa       400:
1.93      tobias    401:                if (cf->file_ent->ce_tag != NULL)
                    402:                        (void)xsnprintf(sticky, sizeof(sticky), "T%s",
                    403:                            cf->file_ent->ce_tag);
                    404:
1.55      xsa       405:                /* Remove the '-' prefixing the version number. */
1.94      xsa       406:                cvs_ent_line_str(cf->file_name, revbuf, tbuf,
                    407:                    cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "", sticky,
                    408:                    0, 0, entry, CVS_ENT_MAXLINELEN);
1.55      xsa       409:        } else {
                    410:                if (logmsg != NULL) {
1.74      xsa       411:                        (void)xsnprintf(path, MAXPATHLEN, "%s/%s%s",
1.55      xsa       412:                            CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
                    413:
                    414:                        if ((fp = fopen(path, "w+")) == NULL)
                    415:                                fatal("add_entry: fopen `%s': %s",
                    416:                                    path, strerror(errno));
                    417:
                    418:                        if (fputs(logmsg, fp) == EOF) {
                    419:                                (void)unlink(path);
                    420:                                fatal("add_entry: fputs `%s': %s",
                    421:                                    path, strerror(errno));
                    422:                        }
                    423:                        (void)fclose(fp);
                    424:                }
                    425:
1.93      tobias    426:                if (cvs_directory_tag != NULL)
                    427:                        (void)xsnprintf(sticky, sizeof(sticky), "T%s",
                    428:                            cvs_directory_tag);
                    429:
                    430:                tbuf[0] = '\0';
                    431:                if (!cvs_server_active)
                    432:                        (void)xsnprintf(tbuf, sizeof(tbuf), "Initial %s",
                    433:                            cf->file_name);
                    434:
1.94      xsa       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: }