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

1.81    ! joris       1: /*     $OpenBSD: add.c,v 1.80 2007/08/30 11:07:18 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.43      joris      30: void   cvs_add_local(struct cvs_file *);
1.63      joris      31: void   cvs_add_entry(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.72      xsa        37: static int      kflag = RCS_KWEXP_DEFAULT;
                     38: static char     kbuf[8], *koptstr;
                     39:
1.43      joris      40: char   *logmsg;
1.21      jfb        41:
                     42: struct cvs_cmd cvs_cmd_add = {
1.57      joris      43:        CVS_OP_ADD, 0, "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':
                     64:                        koptstr = optarg;
                     65:                        kflag = rcs_kflag_get(koptstr);
                     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.74      xsa        71:                        (void)xsnprintf(kbuf, sizeof(kbuf), "-k%s", koptstr);
1.72      xsa        72:                        break;
1.1       jfb        73:                case 'm':
1.43      joris      74:                        logmsg = xstrdup(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.61      xsa        92:                cr.fileproc = cvs_client_sendfile;
                     93:
1.72      xsa        94:                if (kflag != RCS_KWEXP_DEFAULT)
                     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) {
                    108:                cvs_client_send_files(argv, argc);
                    109:                cvs_client_senddir(".");
                    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,
                    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:
                    164: static void
                    165: add_directory(struct cvs_file *cf)
                    166: {
1.74      xsa       167:        int added, nb;
1.45      joris     168:        struct stat st;
1.44      joris     169:        CVSENTRIES *entlist;
1.70      otto      170:        char *date, entry[MAXPATHLEN], msg[1024], repo[MAXPATHLEN], *tag, *p;
1.45      joris     171:
                    172:        cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44      joris     173:
1.74      xsa       174:        (void)xsnprintf(entry, MAXPATHLEN, "%s%s",
                    175:            cf->file_rpath, RCS_FILE_EXT);
1.45      joris     176:
1.53      joris     177:        added = 1;
1.45      joris     178:        if (stat(entry, &st) != -1) {
                    179:                cvs_log(LP_NOTICE, "cannot add directory %s: "
                    180:                    "a file with that name already exists",
                    181:                    cf->file_path);
1.53      joris     182:                added = 0;
1.45      joris     183:        } else {
1.56      xsa       184:                /* Let's see if we have any per-directory tags first. */
                    185:                cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
                    186:
1.76      xsa       187:                (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
                    188:                    cf->file_path, CVS_PATH_CVSDIR);
1.45      joris     189:
                    190:                if (stat(entry, &st) != -1) {
                    191:                        if (!S_ISDIR(st.st_mode)) {
                    192:                                cvs_log(LP_ERR, "%s exists but is not "
                    193:                                    "directory", entry);
                    194:                        } else {
                    195:                                cvs_log(LP_NOTICE, "%s already exists",
                    196:                                    entry);
                    197:                        }
1.53      joris     198:                        added = 0;
                    199:                } else if (cvs_noexec != 1) {
1.45      joris     200:                        if (mkdir(cf->file_rpath, 0755) == -1 &&
                    201:                            errno != EEXIST)
                    202:                                fatal("add_directory: %s: %s", cf->file_path,
                    203:                                    strerror(errno));
                    204:
                    205:                        cvs_get_repository_name(cf->file_wd, repo,
                    206:                            MAXPATHLEN);
                    207:
1.76      xsa       208:                        (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
                    209:                            repo, cf->file_name);
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.79      joris     220:                        xfree(p);
1.53      joris     221:                }
                    222:        }
1.45      joris     223:
1.53      joris     224:        if (added == 1) {
1.74      xsa       225:                (void)xsnprintf(msg, sizeof(msg),
1.56      xsa       226:                    "Directory %s added to the repository", cf->file_rpath);
                    227:
                    228:                if (tag != NULL) {
                    229:                        (void)strlcat(msg,
                    230:                            "\n--> Using per-directory sticky tag ",
                    231:                            sizeof(msg));
                    232:                        (void)strlcat(msg, tag, sizeof(msg));
                    233:                }
                    234:                if (date != NULL) {
                    235:                        (void)strlcat(msg,
                    236:                            "\n--> Using per-directory sticky date ",
                    237:                            sizeof(msg));
                    238:                        (void)strlcat(msg, date, sizeof(msg));
                    239:                }
                    240:                cvs_printf("%s\n", msg);
                    241:
                    242:                if (tag != NULL)
                    243:                        xfree(tag);
                    244:                if (date != NULL)
                    245:                        xfree(date);
1.45      joris     246:        }
                    247:
                    248:        cf->file_status = FILE_SKIP;
                    249: }
1.3       jfb       250:
1.45      joris     251: static void
                    252: add_file(struct cvs_file *cf)
                    253: {
1.55      xsa       254:        int added, stop;
1.78      xsa       255:        char revbuf[CVS_REV_BUFSZ];
1.52      joris     256:        RCSNUM *head;
1.10      xsa       257:
1.68      joris     258:        if (cf->file_rcs != NULL) {
                    259:                head = rcs_head_get(cf->file_rcs);
                    260:                rcsnum_tostr(head, revbuf, sizeof(revbuf));
                    261:                rcsnum_free(head);
                    262:        }
1.44      joris     263:
1.48      xsa       264:        added = stop = 0;
1.43      joris     265:        switch (cf->file_status) {
                    266:        case FILE_ADDED:
1.48      xsa       267:                if (verbosity > 1)
                    268:                        cvs_log(LP_NOTICE, "%s has already been entered",
                    269:                            cf->file_path);
                    270:                stop = 1;
                    271:                break;
                    272:        case FILE_REMOVED:
                    273:                if (cf->file_rcs == NULL) {
                    274:                        cvs_log(LP_NOTICE, "cannot resurrect %s; "
                    275:                            "RCS file removed by second party", cf->file_name);
1.73      joris     276:                } else if (cf->fd == -1) {
1.55      xsa       277:                        add_entry(cf);
1.48      xsa       278:
1.55      xsa       279:                        /* Restore the file. */
1.52      joris     280:                        head = rcs_head_get(cf->file_rcs);
1.69      joris     281:                        cvs_checkout_file(cf, head, 0);
1.68      joris     282:                        rcsnum_free(head);
                    283:
1.48      xsa       284:                        cvs_printf("U %s\n", cf->file_path);
                    285:
                    286:                        cvs_log(LP_NOTICE, "%s, version %s, resurrected",
                    287:                            cf->file_name, revbuf);
                    288:
                    289:                        cf->file_status = FILE_UPTODATE;
                    290:                }
1.44      joris     291:                stop = 1;
                    292:                break;
1.50      xsa       293:        case FILE_CONFLICT:
                    294:        case FILE_LOST:
                    295:        case FILE_MODIFIED:
1.44      joris     296:        case FILE_UPTODATE:
                    297:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
                    298:                        cvs_log(LP_NOTICE, "%s already exists, with version "
                    299:                             "number %s", cf->file_path, revbuf);
                    300:                        stop = 1;
                    301:                }
1.43      joris     302:                break;
                    303:        case FILE_UNKNOWN:
1.44      joris     304:                if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
                    305:                        cvs_log(LP_NOTICE, "re-adding file %s "
                    306:                            "(instead of dead revision %s)",
                    307:                            cf->file_path, revbuf);
1.73      joris     308:                        added++;
                    309:                } else if (cf->fd != -1) {
1.44      joris     310:                        cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
                    311:                            cf->file_path);
1.73      joris     312:                        added++;
                    313:                } else {
                    314:                        stop = 1;
1.44      joris     315:                }
1.43      joris     316:                break;
                    317:        default:
                    318:                break;
1.1       jfb       319:        }
1.44      joris     320:
                    321:        if (stop == 1)
                    322:                return;
                    323:
1.55      xsa       324:        add_entry(cf);
1.44      joris     325:
1.48      xsa       326:        if (added != 0) {
1.67      joris     327:                cvs_log(LP_NOTICE, "use '%s commit' to add %s "
                    328:                    "permanently", __progname,
                    329:                    (added == 1) ? "this file" : "these files");
1.48      xsa       330:        }
1.55      xsa       331: }
                    332:
                    333: static void
                    334: add_entry(struct cvs_file *cf)
                    335: {
                    336:        FILE *fp;
1.78      xsa       337:        char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
                    338:        char revbuf[CVS_REV_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.55      xsa       339:        CVSENTRIES *entlist;
                    340:
                    341:        if (cvs_noexec == 1)
                    342:                return;
                    343:
                    344:        if (cf->file_status == FILE_REMOVED) {
                    345:                rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
                    346:
                    347:                ctime_r(&cf->file_ent->ce_mtime, tbuf);
                    348:                if (tbuf[strlen(tbuf) - 1] == '\n')
                    349:                        tbuf[strlen(tbuf) - 1] = '\0';
                    350:
                    351:                /* Remove the '-' prefixing the version number. */
1.74      xsa       352:                (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.72      xsa       353:                    "/%s/%s/%s/%s/", cf->file_name, revbuf, tbuf,
                    354:                    cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "");
1.55      xsa       355:        } else {
                    356:                if (logmsg != NULL) {
1.74      xsa       357:                        (void)xsnprintf(path, MAXPATHLEN, "%s/%s%s",
1.55      xsa       358:                            CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
                    359:
                    360:                        if ((fp = fopen(path, "w+")) == NULL)
                    361:                                fatal("add_entry: fopen `%s': %s",
                    362:                                    path, strerror(errno));
                    363:
                    364:                        if (fputs(logmsg, fp) == EOF) {
                    365:                                (void)unlink(path);
                    366:                                fatal("add_entry: fputs `%s': %s",
                    367:                                    path, strerror(errno));
                    368:                        }
                    369:                        (void)fclose(fp);
                    370:                }
                    371:
1.74      xsa       372:                (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.72      xsa       373:                    "/%s/0/Initial %s/%s/", cf->file_name, cf->file_name,
                    374:                    (kflag != RCS_KWEXP_DEFAULT) ? kbuf : "");
1.55      xsa       375:        }
                    376:
                    377:        entlist = cvs_ent_open(cf->file_wd);
                    378:        cvs_ent_add(entlist, entry);
                    379:        cvs_ent_close(entlist, ENT_SYNC);
1.1       jfb       380: }