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

1.38    ! xsa         1: /*     $OpenBSD: add.c,v 1.37 2006/01/02 08:11:56 xsa Exp $    */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.28      xsa         4:  * Copyright (c) 2005 Xavier Santolaria <xsa@openbsd.org>
1.8       tedu        5:  * All rights reserved.
1.1       jfb         6:  *
1.8       tedu        7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
1.1       jfb        10:  *
1.8       tedu       11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        13:  * 2. The name of the author may not be used to endorse or promote products
1.8       tedu       14:  *    derived from this software without specific prior written permission.
1.1       jfb        15:  *
                     16:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     17:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     18:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     19:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     20:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     21:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     22:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     23:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     24:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.8       tedu       25:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        26:  */
                     27:
1.37      xsa        28: #include "includes.h"
1.1       jfb        29:
                     30: #include "cvs.h"
                     31: #include "log.h"
1.2       jfb        32: #include "proto.h"
1.1       jfb        33:
                     34:
1.3       jfb        35: extern char *__progname;
                     36:
                     37:
1.25      xsa        38: static int     cvs_add_remote(CVSFILE *, void *);
                     39: static int     cvs_add_local(CVSFILE *, void *);
                     40: static int     cvs_add_init(struct cvs_cmd *, int, char **, int *);
                     41: static int     cvs_add_pre_exec(struct cvsroot *);
1.29      xsa        42: static int     cvs_add_directory(CVSFILE *);
1.28      xsa        43: static int     cvs_add_build_entry(CVSFILE *);
1.21      jfb        44:
                     45: struct cvs_cmd cvs_cmd_add = {
                     46:        CVS_OP_ADD, CVS_REQ_ADD, "add",
                     47:        { "ad", "new" },
                     48:        "Add a new file/directory to the repository",
                     49:        "[-k mode] [-m msg] file ...",
                     50:        "k:m:",
                     51:        NULL,
                     52:        0,
                     53:        cvs_add_init,
                     54:        cvs_add_pre_exec,
1.22      joris      55:        cvs_add_remote,
                     56:        cvs_add_local,
1.21      jfb        57:        NULL,
                     58:        NULL,
1.15      joris      59:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
                     60: };
1.3       jfb        61:
1.15      joris      62: static int kflag = RCS_KWEXP_DEFAULT;
                     63: static char *koptstr;
1.28      xsa        64: static char kbuf[16];
1.3       jfb        65:
1.21      jfb        66: static int
                     67: cvs_add_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        68: {
1.15      joris      69:        int ch;
1.1       jfb        70:
1.13      jfb        71:        cvs_msg = NULL;
1.1       jfb        72:
1.21      jfb        73:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        74:                switch (ch) {
                     75:                case 'k':
1.13      jfb        76:                        koptstr = optarg;
                     77:                        kflag = rcs_kflag_get(koptstr);
                     78:                        if (RCS_KWEXP_INVAL(kflag)) {
                     79:                                cvs_log(LP_ERR,
                     80:                                    "invalid RCS keyword expansion mode");
                     81:                                rcs_kflag_usage();
1.19      joris      82:                                return (CVS_EX_USAGE);
1.13      jfb        83:                        }
1.1       jfb        84:                        break;
                     85:                case 'm':
1.35      joris      86:                        cvs_msg = xstrdup(optarg);
1.1       jfb        87:                        break;
                     88:                default:
1.19      joris      89:                        return (CVS_EX_USAGE);
1.1       jfb        90:                }
                     91:        }
                     92:
1.15      joris      93:        *arg = optind;
                     94:        return (0);
                     95: }
1.10      xsa        96:
1.21      jfb        97: static int
                     98: cvs_add_pre_exec(struct cvsroot *root)
1.15      joris      99: {
1.28      xsa       100:        kbuf[0] = '\0';
1.3       jfb       101:
1.28      xsa       102:        if (kflag != RCS_KWEXP_DEFAULT) {
                    103:                strlcpy(kbuf, "-k", sizeof(kbuf));
                    104:                strlcat(kbuf, koptstr, sizeof(kbuf));
                    105:
1.36      joris     106:                if (root->cr_method != CVS_METHOD_LOCAL)
                    107:                        cvs_sendarg(root, kbuf, 0);
1.10      xsa       108:        }
1.3       jfb       109:
                    110:        return (0);
                    111: }
                    112:
1.21      jfb       113: static int
1.22      joris     114: cvs_add_remote(CVSFILE *cf, void *arg)
1.3       jfb       115: {
                    116:        struct cvsroot *root;
                    117:
1.10      xsa       118:        root = CVS_DIR_ROOT(cf);
                    119:
1.3       jfb       120:        if (cf->cf_type == DT_DIR) {
1.36      joris     121:                cvs_senddir(root, cf);
                    122:                return (0);
1.1       jfb       123:        }
                    124:
1.27      joris     125:        if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.36      joris     126:                cvs_sendreq(root, CVS_REQ_ISMODIFIED, cf->cf_name);
1.23      joris     127:
1.36      joris     128:        return (0);
1.22      joris     129: }
                    130:
1.28      xsa       131: static int
                    132: cvs_add_local(CVSFILE *cf, void *arg)
1.22      joris     133: {
1.32      xsa       134:        int added, ret;
1.28      xsa       135:        char numbuf[64];
                    136:
                    137:        added = 0;
                    138:
1.33      xsa       139:        /* dont use `cvs add *' */
                    140:        if ((strcmp(cf->cf_name, ".") == 0) ||
                    141:            (strcmp(cf->cf_name, "..") == 0) ||
                    142:            (strcmp(cf->cf_name, CVS_PATH_CVSDIR) == 0)) {
                    143:                if (verbosity > 1)
                    144:                        cvs_log(LP_ERR,
                    145:                            "cannot add special file `%s'.", cf->cf_name);
                    146:                return (CVS_EX_FILE);
                    147:        }
                    148:
1.31      xsa       149:        if (cf->cf_type == DT_DIR)
                    150:                return cvs_add_directory(cf);
1.29      xsa       151:
1.28      xsa       152:        if ((!(cf->cf_flags & CVS_FILE_ONDISK)) &&
                    153:            (cf->cf_cvstat != CVS_FST_LOST) &&
                    154:            (cf->cf_cvstat != CVS_FST_REMOVED)) {
                    155:                if (verbosity > 1)
                    156:                        cvs_log(LP_WARN, "nothing known about `%s'",
                    157:                            cf->cf_name);
                    158:                return (0);
                    159:        } else if (cf->cf_cvstat == CVS_FST_ADDED) {
                    160:                if (verbosity > 1)
                    161:                        cvs_log(LP_WARN, "`%s' has already been entered",
                    162:                            cf->cf_name);
                    163:                return (0);
                    164:        } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
                    165:
                    166:                /* XXX remove '-' from CVS/Entries */
                    167:
                    168:                /* XXX check the file out */
                    169:
                    170:                rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
                    171:                cvs_log(LP_WARN, "%s, version %s, resurrected",
                    172:                    cf->cf_name, numbuf);
                    173:
                    174:                return (0);
                    175:        } else if ((cf->cf_cvstat == CVS_FST_CONFLICT) ||
1.34      reyk      176:            (cf->cf_cvstat == CVS_FST_LOST) ||
1.28      xsa       177:            (cf->cf_cvstat == CVS_FST_MODIFIED) ||
                    178:            (cf->cf_cvstat == CVS_FST_UPTODATE)) {
                    179:                if (verbosity > 1) {
                    180:                        rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
                    181:                        cvs_log(LP_WARN,
                    182:                            "%s already exists, with version number %s",
                    183:                            cf->cf_name, numbuf);
                    184:                }
                    185:                return (0);
                    186:        }
                    187:
1.32      xsa       188:        if ((ret = cvs_add_build_entry(cf)) != 0)
                    189:                return (ret);
                    190:        else {
1.28      xsa       191:                added++;
1.32      xsa       192:                if (verbosity > 1)
                    193:                        cvs_log(LP_NOTICE, "scheduling file `%s' for addition",
                    194:                            cf->cf_name);
1.28      xsa       195:        }
                    196:
                    197:        if (added != 0) {
                    198:                if (verbosity > 0)
                    199:                        cvs_log(LP_NOTICE, "use '%s commit' to add %s "
                    200:                            "permanently", __progname,
                    201:                            (added == 1) ? "this file" : "these files");
                    202:                return (0);
                    203:        }
1.29      xsa       204:
                    205:        return (0);
                    206: }
                    207:
                    208: /*
                    209:  * cvs_add_directory()
                    210:  *
                    211:  * Add a directory to the repository.
                    212:  *
                    213:  * Returns 0 on success, -1 on failure.
                    214:  */
                    215: static int
                    216: cvs_add_directory(CVSFILE *cf)
                    217: {
1.38    ! xsa       218:        int nb;
1.29      xsa       219:        char *date, *repo, *tag;
                    220:        char entry[CVS_ENT_MAXLINELEN], fpath[MAXPATHLEN], rcsdir[MAXPATHLEN];
                    221:        char msg[1024];
                    222:        CVSENTRIES *entf;
                    223:        struct cvsroot *root;
                    224:        struct stat st;
                    225:        struct cvs_ent *ent;
                    226:
                    227:        entf = (CVSENTRIES *)cf->cf_entry;
                    228:
                    229:        root = CVS_DIR_ROOT(cf);
                    230:        repo = CVS_DIR_REPO(cf);
                    231:
                    232:        if (strlcpy(fpath, cf->cf_name, sizeof(fpath)) >= sizeof(fpath))
1.31      xsa       233:                return (CVS_EX_DATA);
1.29      xsa       234:
                    235:        if (strchr(fpath, '/') != NULL) {
                    236:                cvs_log(LP_ERR,
                    237:                    "directory %s not added; must be a direct sub-directory",
                    238:                    fpath);
1.31      xsa       239:                return (CVS_EX_FILE);
1.29      xsa       240:        }
                    241:
                    242:        /* Let's see if we have any per-directory tags first */
                    243:        cvs_parse_tagfile(&tag, &date, &nb);
                    244:
1.30      xsa       245:        /* XXX check for <dir>/CVS */
                    246:
1.38    ! xsa       247:        if (strlcpy(rcsdir, root->cr_dir, sizeof(rcsdir)) >= sizeof(rcsdir) ||
        !           248:            strlcat(rcsdir, "/", sizeof(rcsdir)) >= sizeof(rcsdir) ||
        !           249:            strlcat(rcsdir, repo, sizeof(rcsdir)) >= sizeof(rcsdir))
        !           250:                fatal("cvs_add_directory: path truncation");
1.29      xsa       251:
                    252:        if ((stat(rcsdir, &st) == 0) && !(S_ISDIR(st.st_mode))) {
                    253:                cvs_log(LP_ERRNO,
                    254:                    "%s is not a directory; %s not added", rcsdir, fpath);
1.31      xsa       255:                return (CVS_EX_FILE);
1.29      xsa       256:        }
                    257:
                    258:        snprintf(msg, sizeof(msg),
                    259:            "Directory %s added to the repository", rcsdir);
                    260:
                    261:        if (tag != NULL) {
                    262:                strlcat(msg, "\n--> Using per-directory sticky tag ",
                    263:                    sizeof(msg));
                    264:                strlcat(msg, tag, sizeof(msg));
                    265:        }
                    266:        if (date != NULL) {
                    267:                strlcat(msg, "\n--> Using per-directory sticky date ",
                    268:                    sizeof(msg));
                    269:                strlcat(msg, date, sizeof(msg));
                    270:        }
                    271:        strlcat(msg, "\n", sizeof(msg));
                    272:
                    273:        if (cvs_noexec == 0) {
                    274:                if (mkdir(rcsdir, 0777) == -1) {
                    275:                        cvs_log(LP_ERRNO, "failed to create %s", rcsdir);
1.31      xsa       276:                        return (CVS_EX_FILE);
1.29      xsa       277:                }
                    278:        }
                    279:
                    280:        /* create CVS/ admin files */
                    281:        if (cvs_noexec == 0)
1.30      xsa       282:                if (cvs_mkadmin(fpath, root->cr_str, repo, tag, date, nb) == -1)
1.31      xsa       283:                        return (CVS_EX_FILE);
1.29      xsa       284:
                    285:        /* XXX Build the Entries line. */
1.38    ! xsa       286:        if (strlcpy(entry, "D/", sizeof(entry)) >= sizeof(entry) ||
        !           287:            strlcat(entry, fpath, sizeof(entry)) >= sizeof(entry) ||
        !           288:            strlcat(entry, "////", sizeof(entry)) >= sizeof(entry))
        !           289:                fatal("cvs_add_directory: path truncation");
1.29      xsa       290:
                    291:        if ((ent = cvs_ent_parse(entry)) == NULL) {
                    292:                cvs_log(LP_ERR, "failed to parse entry");
1.31      xsa       293:                return (CVS_EX_DATA);
1.34      reyk      294:        }
1.29      xsa       295:
                    296:        if (cvs_ent_add(entf, ent) < 0) {
                    297:                cvs_log(LP_ERR, "failed to add entry");
1.31      xsa       298:                return (CVS_EX_DATA);
1.29      xsa       299:        }
                    300:
                    301:        cvs_printf("%s", msg);
1.28      xsa       302:
                    303:        return (0);
                    304: }
                    305:
                    306: static int
                    307: cvs_add_build_entry(CVSFILE *cf)
                    308: {
                    309:        char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
                    310:        FILE *fp;
                    311:        CVSENTRIES *entf;
                    312:        struct cvs_ent *ent;
                    313:
                    314:        entf = (CVSENTRIES *)cf->cf_entry;
                    315:
                    316:        if (cvs_noexec == 1)
                    317:                return (0);
                    318:
                    319:        /* Build the path to the <file>,t file. */
1.38    ! xsa       320:        if (strlcpy(path, CVS_PATH_CVSDIR, sizeof(path)) >= sizeof(path) ||
        !           321:            strlcat(path, "/", sizeof(path)) >= sizeof(path) ||
        !           322:            strlcat(path, cf->cf_name, sizeof(path)) >= sizeof(path) ||
        !           323:            strlcat(path, CVS_DESCR_FILE_EXT, sizeof(path)) >= sizeof(path))
        !           324:                fatal("cvs_add_build_entry: path truncation");
1.28      xsa       325:
                    326:        fp = fopen(path, "w+");
                    327:        if (fp == NULL) {
                    328:                cvs_log(LP_ERRNO, "failed to open `%s'", path);
                    329:                return (CVS_EX_FILE);
                    330:        }
                    331:
                    332:        if (cvs_msg != NULL) {
                    333:                if (fputs(cvs_msg, fp) == EOF) {
                    334:                        cvs_log(LP_ERRNO, "cannot write to `%s'", path);
                    335:                        (void)fclose(fp);
                    336:                        return (CVS_EX_FILE);
                    337:                }
                    338:        }
                    339:        (void)fclose(fp);
                    340:
                    341:        /* XXX Build the Entries line. */
1.38    ! xsa       342:        if (strlcpy(entry, "/", sizeof(entry)) >= sizeof(entry) ||
        !           343:            strlcat(entry, cf->cf_name, sizeof(entry)) >= sizeof(entry) ||
        !           344:            strlcat(entry, "/0/Initial ", sizeof(entry)) >= sizeof(entry) ||
        !           345:            strlcat(entry, cf->cf_name, sizeof(entry)) >= sizeof(entry) ||
        !           346:            strlcat(entry, "/", sizeof(entry)) >= sizeof(entry) ||
        !           347:            strlcat(entry, kbuf, sizeof(entry)) >= sizeof(entry) ||
        !           348:            strlcat(entry, "/", sizeof(entry)) >= sizeof(entry)) {
1.28      xsa       349:                (void)cvs_unlink(path);
1.38    ! xsa       350:                fatal("cvs_add_build_entry: path truncation");
1.28      xsa       351:        }
                    352:
                    353:        if ((ent = cvs_ent_parse(entry)) == NULL) {
                    354:                cvs_log(LP_ERR, "failed to parse entry");
                    355:                (void)cvs_unlink(path);
                    356:                return (CVS_EX_DATA);
1.34      reyk      357:        }
1.28      xsa       358:
                    359:        if (cvs_ent_add(entf, ent) < 0) {
                    360:                cvs_log(LP_ERR, "failed to add entry");
                    361:                (void)cvs_unlink(path);
                    362:                return (CVS_EX_DATA);
                    363:        }
1.22      joris     364:
                    365:        return (0);
1.1       jfb       366: }