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

1.74    ! xsa         1: /*     $OpenBSD: add.c,v 1.73 2007/01/27 21:18:17 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.37      xsa        19: #include "includes.h"
1.1       jfb        20:
                     21: #include "cvs.h"
1.43      joris      22: #include "diff.h"
1.1       jfb        23: #include "log.h"
1.61      xsa        24: #include "remote.h"
1.1       jfb        25:
1.48      xsa        26: extern char *__progname;
                     27:
1.43      joris      28: void   cvs_add_local(struct cvs_file *);
1.63      joris      29: void   cvs_add_entry(struct cvs_file *);
1.1       jfb        30:
1.45      joris      31: static void add_directory(struct cvs_file *);
                     32: static void add_file(struct cvs_file *);
1.55      xsa        33: static void add_entry(struct cvs_file *);
1.45      joris      34:
1.72      xsa        35: static int      kflag = RCS_KWEXP_DEFAULT;
                     36: static char     kbuf[8], *koptstr;
                     37:
1.43      joris      38: char   *logmsg;
1.21      jfb        39:
                     40: struct cvs_cmd cvs_cmd_add = {
1.57      joris      41:        CVS_OP_ADD, 0, "add",
1.21      jfb        42:        { "ad", "new" },
1.43      joris      43:        "Add a new file or directory to the repository",
1.72      xsa        44:        "[-k mode] [-m message] ...",
                     45:        "k:m:",
1.21      jfb        46:        NULL,
1.43      joris      47:        cvs_add
1.15      joris      48: };
1.3       jfb        49:
1.43      joris      50: int
                     51: cvs_add(int argc, char **argv)
1.1       jfb        52: {
1.15      joris      53:        int ch;
1.43      joris      54:        int flags;
                     55:        struct cvs_recursion cr;
1.1       jfb        56:
1.45      joris      57:        flags = CR_REPO;
1.1       jfb        58:
1.43      joris      59:        while ((ch = getopt(argc, argv, cvs_cmd_add.cmd_opts)) != -1) {
1.1       jfb        60:                switch (ch) {
1.72      xsa        61:                case 'k':
                     62:                        koptstr = optarg;
                     63:                        kflag = rcs_kflag_get(koptstr);
                     64:                        if (RCS_KWEXP_INVAL(kflag)) {
                     65:                                cvs_log(LP_ERR,
                     66:                                    "invalid RCS keyword expension mode");
                     67:                                fatal("%s", cvs_cmd_add.cmd_synopsis);
                     68:                        }
1.74    ! xsa        69:                        (void)xsnprintf(kbuf, sizeof(kbuf), "-k%s", koptstr);
1.72      xsa        70:                        break;
1.1       jfb        71:                case 'm':
1.43      joris      72:                        logmsg = xstrdup(optarg);
1.1       jfb        73:                        break;
                     74:                default:
1.43      joris      75:                        fatal("%s", cvs_cmd_add.cmd_synopsis);
1.1       jfb        76:                }
                     77:        }
                     78:
1.43      joris      79:        argc -= optind;
                     80:        argv += optind;
1.28      xsa        81:
1.46      joris      82:        if (argc == 0)
                     83:                fatal("%s", cvs_cmd_add.cmd_synopsis);
                     84:
1.43      joris      85:        cr.enterdir = NULL;
                     86:        cr.leavedir = NULL;
1.61      xsa        87:
                     88:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.66      joris      89:                cvs_client_connect_to_server();
1.61      xsa        90:                cr.fileproc = cvs_client_sendfile;
                     91:
1.72      xsa        92:                if (kflag != RCS_KWEXP_DEFAULT)
                     93:                        cvs_client_send_request("Argument %s", kbuf);
                     94:
1.61      xsa        95:                if (logmsg != NULL)
                     96:                        cvs_client_send_request("Argument -m%s", logmsg);
                     97:        } else {
                     98:                cr.fileproc = cvs_add_local;
                     99:        }
                    100:
1.43      joris     101:        cr.flags = flags;
                    102:
1.46      joris     103:        cvs_file_run(argc, argv, &cr);
1.61      xsa       104:
                    105:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    106:                cvs_client_send_files(argv, argc);
                    107:                cvs_client_senddir(".");
                    108:                cvs_client_send_request("add");
                    109:                cvs_client_get_responses();
1.63      joris     110:
                    111:                if (server_response == SERVER_OK) {
                    112:                        cr.fileproc = cvs_add_entry;
                    113:                        cvs_file_run(argc, argv, &cr);
                    114:                }
1.61      xsa       115:        }
                    116:
1.3       jfb       117:        return (0);
1.63      joris     118: }
                    119:
                    120: void
                    121: cvs_add_entry(struct cvs_file *cf)
                    122: {
1.70      otto      123:        char entry[CVS_ENT_MAXLINELEN];
1.63      joris     124:        CVSENTRIES *entlist;
                    125:
                    126:        if (cf->file_type == CVS_DIR) {
1.74    ! xsa       127:                (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
        !           128:                    "D/%s/////", cf->file_name);
1.64      joris     129:
1.63      joris     130:                entlist = cvs_ent_open(cf->file_wd);
                    131:                cvs_ent_add(entlist, entry);
                    132:                cvs_ent_close(entlist, ENT_SYNC);
                    133:        } else {
                    134:                add_entry(cf);
                    135:        }
1.3       jfb       136: }
                    137:
1.43      joris     138: void
                    139: cvs_add_local(struct cvs_file *cf)
1.3       jfb       140: {
1.45      joris     141:        cvs_log(LP_TRACE, "cvs_add_local(%s)", cf->file_path);
                    142:
1.73      joris     143:        cvs_file_classify(cf, NULL, 1);
1.49      xsa       144:
                    145:        /* dont use `cvs add *' */
                    146:        if (strcmp(cf->file_name, ".") == 0 ||
                    147:            strcmp(cf->file_name, "..") == 0 ||
                    148:            strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
                    149:                if (verbosity > 1)
                    150:                        cvs_log(LP_ERR,
                    151:                            "cannot add special file `%s'; skipping",
                    152:                            cf->file_name);
                    153:                return;
                    154:        }
1.45      joris     155:
                    156:        if (cf->file_type == CVS_DIR)
                    157:                add_directory(cf);
                    158:        else
                    159:                add_file(cf);
                    160: }
                    161:
                    162: static void
                    163: add_directory(struct cvs_file *cf)
                    164: {
1.74    ! xsa       165:        int added, nb;
1.45      joris     166:        struct stat st;
1.44      joris     167:        CVSENTRIES *entlist;
1.70      otto      168:        char *date, entry[MAXPATHLEN], msg[1024], repo[MAXPATHLEN], *tag, *p;
1.45      joris     169:
                    170:        cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44      joris     171:
1.74    ! xsa       172:        (void)xsnprintf(entry, MAXPATHLEN, "%s%s",
        !           173:            cf->file_rpath, RCS_FILE_EXT);
1.45      joris     174:
1.53      joris     175:        added = 1;
1.45      joris     176:        if (stat(entry, &st) != -1) {
                    177:                cvs_log(LP_NOTICE, "cannot add directory %s: "
                    178:                    "a file with that name already exists",
                    179:                    cf->file_path);
1.53      joris     180:                added = 0;
1.45      joris     181:        } else {
1.56      xsa       182:                /* Let's see if we have any per-directory tags first. */
                    183:                cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
                    184:
1.62      xsa       185:                if (cvs_path_cat(cf->file_path, CVS_PATH_CVSDIR,
                    186:                    entry, MAXPATHLEN) >= MAXPATHLEN)
                    187:                        fatal("add_directory: truncation");
1.45      joris     188:
                    189:                if (stat(entry, &st) != -1) {
                    190:                        if (!S_ISDIR(st.st_mode)) {
                    191:                                cvs_log(LP_ERR, "%s exists but is not "
                    192:                                    "directory", entry);
                    193:                        } else {
                    194:                                cvs_log(LP_NOTICE, "%s already exists",
                    195:                                    entry);
                    196:                        }
1.53      joris     197:                        added = 0;
                    198:                } else if (cvs_noexec != 1) {
1.45      joris     199:                        if (mkdir(cf->file_rpath, 0755) == -1 &&
                    200:                            errno != EEXIST)
                    201:                                fatal("add_directory: %s: %s", cf->file_path,
                    202:                                    strerror(errno));
                    203:
                    204:                        cvs_get_repository_name(cf->file_wd, repo,
                    205:                            MAXPATHLEN);
                    206:
1.62      xsa       207:                        if (cvs_path_cat(repo, cf->file_name, entry,
                    208:                            MAXPATHLEN) >= MAXPATHLEN)
                    209:                                fatal("add_directory: truncation");
1.45      joris     210:
                    211:                        cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
1.56      xsa       212:                            entry, tag, date, nb);
1.45      joris     213:
1.70      otto      214:                        p = xmalloc(CVS_ENT_MAXLINELEN);
1.74    ! xsa       215:                        (void)xsnprintf(p, CVS_ENT_MAXLINELEN,
1.45      joris     216:                            "D/%s/////", cf->file_name);
                    217:                        entlist = cvs_ent_open(cf->file_wd);
1.70      otto      218:                        cvs_ent_add(entlist, p);
1.45      joris     219:                        cvs_ent_close(entlist, ENT_SYNC);
1.53      joris     220:                }
                    221:        }
1.45      joris     222:
1.53      joris     223:        if (added == 1) {
1.74    ! xsa       224:                (void)xsnprintf(msg, sizeof(msg),
1.56      xsa       225:                    "Directory %s added to the repository", cf->file_rpath);
                    226:
                    227:                if (tag != NULL) {
                    228:                        (void)strlcat(msg,
                    229:                            "\n--> Using per-directory sticky tag ",
                    230:                            sizeof(msg));
                    231:                        (void)strlcat(msg, tag, sizeof(msg));
                    232:                }
                    233:                if (date != NULL) {
                    234:                        (void)strlcat(msg,
                    235:                            "\n--> Using per-directory sticky date ",
                    236:                            sizeof(msg));
                    237:                        (void)strlcat(msg, date, sizeof(msg));
                    238:                }
                    239:                cvs_printf("%s\n", msg);
                    240:
                    241:                if (tag != NULL)
                    242:                        xfree(tag);
                    243:                if (date != NULL)
                    244:                        xfree(date);
1.45      joris     245:        }
                    246:
                    247:        cf->file_status = FILE_SKIP;
                    248: }
1.3       jfb       249:
1.45      joris     250: static void
                    251: add_file(struct cvs_file *cf)
                    252: {
1.55      xsa       253:        int added, stop;
                    254:        char revbuf[16];
1.52      joris     255:        RCSNUM *head;
1.10      xsa       256:
1.68      joris     257:        if (cf->file_rcs != NULL) {
                    258:                head = rcs_head_get(cf->file_rcs);
                    259:                rcsnum_tostr(head, revbuf, sizeof(revbuf));
                    260:                rcsnum_free(head);
                    261:        }
1.44      joris     262:
1.48      xsa       263:        added = stop = 0;
1.43      joris     264:        switch (cf->file_status) {
                    265:        case FILE_ADDED:
1.48      xsa       266:                if (verbosity > 1)
                    267:                        cvs_log(LP_NOTICE, "%s has already been entered",
                    268:                            cf->file_path);
                    269:                stop = 1;
                    270:                break;
                    271:        case FILE_REMOVED:
                    272:                if (cf->file_rcs == NULL) {
                    273:                        cvs_log(LP_NOTICE, "cannot resurrect %s; "
                    274:                            "RCS file removed by second party", cf->file_name);
1.73      joris     275:                } else if (cf->fd == -1) {
1.55      xsa       276:                        add_entry(cf);
1.48      xsa       277:
1.55      xsa       278:                        /* Restore the file. */
1.52      joris     279:                        head = rcs_head_get(cf->file_rcs);
1.69      joris     280:                        cvs_checkout_file(cf, head, 0);
1.68      joris     281:                        rcsnum_free(head);
                    282:
1.48      xsa       283:                        cvs_printf("U %s\n", cf->file_path);
                    284:
                    285:                        cvs_log(LP_NOTICE, "%s, version %s, resurrected",
                    286:                            cf->file_name, revbuf);
                    287:
                    288:                        cf->file_status = FILE_UPTODATE;
                    289:                }
1.44      joris     290:                stop = 1;
                    291:                break;
1.50      xsa       292:        case FILE_CONFLICT:
                    293:        case FILE_LOST:
                    294:        case FILE_MODIFIED:
1.44      joris     295:        case FILE_UPTODATE:
                    296:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
                    297:                        cvs_log(LP_NOTICE, "%s already exists, with version "
                    298:                             "number %s", cf->file_path, revbuf);
                    299:                        stop = 1;
                    300:                }
1.43      joris     301:                break;
                    302:        case FILE_UNKNOWN:
1.44      joris     303:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
                    304:                        cvs_log(LP_NOTICE, "re-adding file %s "
                    305:                            "(instead of dead revision %s)",
                    306:                            cf->file_path, revbuf);
1.73      joris     307:                        added++;
                    308:                } else if (cf->fd != -1) {
1.44      joris     309:                        cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
                    310:                            cf->file_path);
1.73      joris     311:                        added++;
                    312:                } else {
                    313:                        stop = 1;
1.44      joris     314:                }
1.43      joris     315:                break;
                    316:        default:
                    317:                break;
1.1       jfb       318:        }
1.44      joris     319:
                    320:        if (stop == 1)
                    321:                return;
                    322:
1.55      xsa       323:        add_entry(cf);
1.44      joris     324:
1.48      xsa       325:        if (added != 0) {
1.67      joris     326:                cvs_log(LP_NOTICE, "use '%s commit' to add %s "
                    327:                    "permanently", __progname,
                    328:                    (added == 1) ? "this file" : "these files");
1.48      xsa       329:        }
1.55      xsa       330: }
                    331:
                    332: static void
                    333: add_entry(struct cvs_file *cf)
                    334: {
                    335:        FILE *fp;
1.70      otto      336:        char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN], revbuf[16], tbuf[32];
1.55      xsa       337:        CVSENTRIES *entlist;
                    338:
                    339:        if (cvs_noexec == 1)
                    340:                return;
                    341:
                    342:        if (cf->file_status == FILE_REMOVED) {
                    343:                rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
                    344:
                    345:                ctime_r(&cf->file_ent->ce_mtime, tbuf);
                    346:                if (tbuf[strlen(tbuf) - 1] == '\n')
                    347:                        tbuf[strlen(tbuf) - 1] = '\0';
                    348:
                    349:                /* Remove the '-' prefixing the version number. */
1.74    ! xsa       350:                (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.72      xsa       351:                    "/%s/%s/%s/%s/", cf->file_name, revbuf, tbuf,
                    352:                    cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "");
1.55      xsa       353:        } else {
                    354:                if (logmsg != NULL) {
1.74    ! xsa       355:                        (void)xsnprintf(path, MAXPATHLEN, "%s/%s%s",
1.55      xsa       356:                            CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
                    357:
                    358:                        if ((fp = fopen(path, "w+")) == NULL)
                    359:                                fatal("add_entry: fopen `%s': %s",
                    360:                                    path, strerror(errno));
                    361:
                    362:                        if (fputs(logmsg, fp) == EOF) {
                    363:                                (void)unlink(path);
                    364:                                fatal("add_entry: fputs `%s': %s",
                    365:                                    path, strerror(errno));
                    366:                        }
                    367:                        (void)fclose(fp);
                    368:                }
                    369:
1.74    ! xsa       370:                (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.72      xsa       371:                    "/%s/0/Initial %s/%s/", cf->file_name, cf->file_name,
                    372:                    (kflag != RCS_KWEXP_DEFAULT) ? kbuf : "");
1.55      xsa       373:        }
                    374:
                    375:        entlist = cvs_ent_open(cf->file_wd);
                    376:        cvs_ent_add(entlist, entry);
                    377:        cvs_ent_close(entlist, ENT_SYNC);
1.1       jfb       378: }