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

1.33    ! xsa         1: /*     $OpenBSD: add.c,v 1.32 2005/09/25 17:38:44 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:
                     28: #include <sys/types.h>
1.29      xsa        29: #include <sys/stat.h>
1.1       jfb        30:
                     31: #include <errno.h>
                     32: #include <stdio.h>
                     33: #include <stdlib.h>
                     34: #include <string.h>
1.10      xsa        35: #include <unistd.h>
1.1       jfb        36:
                     37: #include "cvs.h"
                     38: #include "log.h"
1.2       jfb        39: #include "proto.h"
1.1       jfb        40:
                     41:
1.3       jfb        42: extern char *__progname;
                     43:
                     44:
1.25      xsa        45: static int     cvs_add_remote(CVSFILE *, void *);
                     46: static int     cvs_add_local(CVSFILE *, void *);
                     47: static int     cvs_add_init(struct cvs_cmd *, int, char **, int *);
                     48: static int     cvs_add_pre_exec(struct cvsroot *);
1.29      xsa        49: static int     cvs_add_directory(CVSFILE *);
1.28      xsa        50: static int     cvs_add_build_entry(CVSFILE *);
1.21      jfb        51:
                     52: struct cvs_cmd cvs_cmd_add = {
                     53:        CVS_OP_ADD, CVS_REQ_ADD, "add",
                     54:        { "ad", "new" },
                     55:        "Add a new file/directory to the repository",
                     56:        "[-k mode] [-m msg] file ...",
                     57:        "k:m:",
                     58:        NULL,
                     59:        0,
                     60:        cvs_add_init,
                     61:        cvs_add_pre_exec,
1.22      joris      62:        cvs_add_remote,
                     63:        cvs_add_local,
1.21      jfb        64:        NULL,
                     65:        NULL,
1.15      joris      66:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
                     67: };
1.3       jfb        68:
1.15      joris      69: static int kflag = RCS_KWEXP_DEFAULT;
                     70: static char *koptstr;
1.28      xsa        71: static char kbuf[16];
1.3       jfb        72:
1.21      jfb        73: static int
                     74: cvs_add_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        75: {
1.15      joris      76:        int ch;
1.1       jfb        77:
1.13      jfb        78:        cvs_msg = NULL;
1.1       jfb        79:
1.21      jfb        80:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        81:                switch (ch) {
                     82:                case 'k':
1.13      jfb        83:                        koptstr = optarg;
                     84:                        kflag = rcs_kflag_get(koptstr);
                     85:                        if (RCS_KWEXP_INVAL(kflag)) {
                     86:                                cvs_log(LP_ERR,
                     87:                                    "invalid RCS keyword expansion mode");
                     88:                                rcs_kflag_usage();
1.19      joris      89:                                return (CVS_EX_USAGE);
1.13      jfb        90:                        }
1.1       jfb        91:                        break;
                     92:                case 'm':
1.13      jfb        93:                        if ((cvs_msg = strdup(optarg)) == NULL) {
                     94:                                cvs_log(LP_ERRNO, "failed to copy message");
1.19      joris      95:                                return (CVS_EX_DATA);
1.13      jfb        96:                        }
1.1       jfb        97:                        break;
                     98:                default:
1.19      joris      99:                        return (CVS_EX_USAGE);
1.1       jfb       100:                }
                    101:        }
                    102:
1.15      joris     103:        *arg = optind;
                    104:        return (0);
                    105: }
1.10      xsa       106:
1.21      jfb       107: static int
                    108: cvs_add_pre_exec(struct cvsroot *root)
1.15      joris     109: {
1.28      xsa       110:        kbuf[0] = '\0';
1.3       jfb       111:
1.28      xsa       112:        if (kflag != RCS_KWEXP_DEFAULT) {
                    113:                strlcpy(kbuf, "-k", sizeof(kbuf));
                    114:                strlcat(kbuf, koptstr, sizeof(kbuf));
                    115:
                    116:                if (root->cr_method != CVS_METHOD_LOCAL) {
                    117:                        if (cvs_sendarg(root, kbuf, 0) < 0)
                    118:                                return (CVS_EX_PROTO);
                    119:                }
1.10      xsa       120:        }
1.3       jfb       121:
                    122:        return (0);
                    123: }
                    124:
1.21      jfb       125: static int
1.22      joris     126: cvs_add_remote(CVSFILE *cf, void *arg)
1.3       jfb       127: {
1.10      xsa       128:        int ret;
1.3       jfb       129:        struct cvsroot *root;
                    130:
1.10      xsa       131:        ret = 0;
                    132:        root = CVS_DIR_ROOT(cf);
                    133:
1.3       jfb       134:        if (cf->cf_type == DT_DIR) {
1.22      joris     135:                ret = cvs_senddir(root, cf);
1.20      joris     136:                if (ret == -1)
                    137:                        ret = CVS_EX_PROTO;
1.10      xsa       138:                return (ret);
1.1       jfb       139:        }
                    140:
1.27      joris     141:        if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.22      joris     142:                ret = cvs_sendreq(root, CVS_REQ_ISMODIFIED,
1.21      jfb       143:                    cf->cf_name);
1.23      joris     144:
                    145:        if (ret == -1)
                    146:                ret = CVS_EX_PROTO;
1.1       jfb       147:
1.10      xsa       148:        return (ret);
1.22      joris     149: }
                    150:
1.28      xsa       151: static int
                    152: cvs_add_local(CVSFILE *cf, void *arg)
1.22      joris     153: {
1.32      xsa       154:        int added, ret;
1.28      xsa       155:        char numbuf[64];
                    156:
                    157:        added = 0;
                    158:
1.33    ! xsa       159:        /* dont use `cvs add *' */
        !           160:        if ((strcmp(cf->cf_name, ".") == 0) ||
        !           161:            (strcmp(cf->cf_name, "..") == 0) ||
        !           162:            (strcmp(cf->cf_name, CVS_PATH_CVSDIR) == 0)) {
        !           163:                if (verbosity > 1)
        !           164:                        cvs_log(LP_ERR,
        !           165:                            "cannot add special file `%s'.", cf->cf_name);
        !           166:                return (CVS_EX_FILE);
        !           167:        }
        !           168:
1.31      xsa       169:        if (cf->cf_type == DT_DIR)
                    170:                return cvs_add_directory(cf);
1.29      xsa       171:
1.28      xsa       172:        if ((!(cf->cf_flags & CVS_FILE_ONDISK)) &&
                    173:            (cf->cf_cvstat != CVS_FST_LOST) &&
                    174:            (cf->cf_cvstat != CVS_FST_REMOVED)) {
                    175:                if (verbosity > 1)
                    176:                        cvs_log(LP_WARN, "nothing known about `%s'",
                    177:                            cf->cf_name);
                    178:                return (0);
                    179:        } else if (cf->cf_cvstat == CVS_FST_ADDED) {
                    180:                if (verbosity > 1)
                    181:                        cvs_log(LP_WARN, "`%s' has already been entered",
                    182:                            cf->cf_name);
                    183:                return (0);
                    184:        } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
                    185:
                    186:                /* XXX remove '-' from CVS/Entries */
                    187:
                    188:                /* XXX check the file out */
                    189:
                    190:                rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
                    191:                cvs_log(LP_WARN, "%s, version %s, resurrected",
                    192:                    cf->cf_name, numbuf);
                    193:
                    194:                return (0);
                    195:        } else if ((cf->cf_cvstat == CVS_FST_CONFLICT) ||
                    196:            (cf->cf_cvstat == CVS_FST_LOST) ||
                    197:            (cf->cf_cvstat == CVS_FST_MODIFIED) ||
                    198:            (cf->cf_cvstat == CVS_FST_UPTODATE)) {
                    199:                if (verbosity > 1) {
                    200:                        rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
                    201:                        cvs_log(LP_WARN,
                    202:                            "%s already exists, with version number %s",
                    203:                            cf->cf_name, numbuf);
                    204:                }
                    205:                return (0);
                    206:        }
                    207:
1.32      xsa       208:        if ((ret = cvs_add_build_entry(cf)) != 0)
                    209:                return (ret);
                    210:        else {
1.28      xsa       211:                added++;
1.32      xsa       212:                if (verbosity > 1)
                    213:                        cvs_log(LP_NOTICE, "scheduling file `%s' for addition",
                    214:                            cf->cf_name);
1.28      xsa       215:        }
                    216:
                    217:        if (added != 0) {
                    218:                if (verbosity > 0)
                    219:                        cvs_log(LP_NOTICE, "use '%s commit' to add %s "
                    220:                            "permanently", __progname,
                    221:                            (added == 1) ? "this file" : "these files");
                    222:                return (0);
                    223:        }
1.29      xsa       224:
                    225:        return (0);
                    226: }
                    227:
                    228: /*
                    229:  * cvs_add_directory()
                    230:  *
                    231:  * Add a directory to the repository.
                    232:  *
                    233:  * Returns 0 on success, -1 on failure.
                    234:  */
                    235: static int
                    236: cvs_add_directory(CVSFILE *cf)
                    237: {
                    238:        int l, nb;
                    239:        char *date, *repo, *tag;
                    240:        char entry[CVS_ENT_MAXLINELEN], fpath[MAXPATHLEN], rcsdir[MAXPATHLEN];
                    241:        char msg[1024];
                    242:        CVSENTRIES *entf;
                    243:        struct cvsroot *root;
                    244:        struct stat st;
                    245:        struct cvs_ent *ent;
                    246:
                    247:        entf = (CVSENTRIES *)cf->cf_entry;
                    248:
                    249:        root = CVS_DIR_ROOT(cf);
                    250:        repo = CVS_DIR_REPO(cf);
                    251:
                    252:        if (strlcpy(fpath, cf->cf_name, sizeof(fpath)) >= sizeof(fpath))
1.31      xsa       253:                return (CVS_EX_DATA);
1.29      xsa       254:
                    255:        if (strchr(fpath, '/') != NULL) {
                    256:                cvs_log(LP_ERR,
                    257:                    "directory %s not added; must be a direct sub-directory",
                    258:                    fpath);
1.31      xsa       259:                return (CVS_EX_FILE);
1.29      xsa       260:        }
                    261:
                    262:        /* Let's see if we have any per-directory tags first */
                    263:        cvs_parse_tagfile(&tag, &date, &nb);
                    264:
1.30      xsa       265:        /* XXX check for <dir>/CVS */
                    266:
1.29      xsa       267:        l = snprintf(rcsdir, sizeof(rcsdir), "%s/%s",
                    268:            root->cr_dir, repo);
                    269:        if (l == -1 || l >= (int)sizeof(rcsdir)) {
                    270:                errno = ENAMETOOLONG;
                    271:                cvs_log(LP_ERRNO, "%s", rcsdir);
1.31      xsa       272:                return (CVS_EX_DATA);
1.29      xsa       273:        }
                    274:
                    275:        if ((stat(rcsdir, &st) == 0) && !(S_ISDIR(st.st_mode))) {
                    276:                cvs_log(LP_ERRNO,
                    277:                    "%s is not a directory; %s not added", rcsdir, fpath);
1.31      xsa       278:                return (CVS_EX_FILE);
1.29      xsa       279:        }
                    280:
                    281:        snprintf(msg, sizeof(msg),
                    282:            "Directory %s added to the repository", rcsdir);
                    283:
                    284:        if (tag != NULL) {
                    285:                strlcat(msg, "\n--> Using per-directory sticky tag ",
                    286:                    sizeof(msg));
                    287:                strlcat(msg, tag, sizeof(msg));
                    288:        }
                    289:        if (date != NULL) {
                    290:                strlcat(msg, "\n--> Using per-directory sticky date ",
                    291:                    sizeof(msg));
                    292:                strlcat(msg, date, sizeof(msg));
                    293:        }
                    294:        strlcat(msg, "\n", sizeof(msg));
                    295:
                    296:        if (cvs_noexec == 0) {
                    297:                if (mkdir(rcsdir, 0777) == -1) {
                    298:                        cvs_log(LP_ERRNO, "failed to create %s", rcsdir);
1.31      xsa       299:                        return (CVS_EX_FILE);
1.29      xsa       300:                }
                    301:        }
                    302:
                    303:        /* create CVS/ admin files */
                    304:        if (cvs_noexec == 0)
1.30      xsa       305:                if (cvs_mkadmin(fpath, root->cr_str, repo, tag, date, nb) == -1)
1.31      xsa       306:                        return (CVS_EX_FILE);
1.29      xsa       307:
                    308:        /* XXX Build the Entries line. */
                    309:        l = snprintf(entry, sizeof(entry), "D/%s////", fpath);
                    310:        if (l == -1 || l >= (int)sizeof(entry)) {
                    311:                errno = ENAMETOOLONG;
                    312:                cvs_log(LP_ERRNO, "%s", entry);
1.31      xsa       313:                return (CVS_EX_DATA);
1.33    ! xsa       314:        }
1.29      xsa       315:
                    316:        if ((ent = cvs_ent_parse(entry)) == NULL) {
                    317:                cvs_log(LP_ERR, "failed to parse entry");
1.31      xsa       318:                return (CVS_EX_DATA);
1.29      xsa       319:        }
                    320:
                    321:        if (cvs_ent_add(entf, ent) < 0) {
                    322:                cvs_log(LP_ERR, "failed to add entry");
1.31      xsa       323:                return (CVS_EX_DATA);
1.29      xsa       324:        }
                    325:
                    326:        cvs_printf("%s", msg);
1.28      xsa       327:
                    328:        return (0);
                    329: }
                    330:
                    331: static int
                    332: cvs_add_build_entry(CVSFILE *cf)
                    333: {
                    334:        int l;
                    335:        char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
                    336:        FILE *fp;
                    337:        CVSENTRIES *entf;
                    338:        struct cvs_ent *ent;
                    339:
                    340:        entf = (CVSENTRIES *)cf->cf_entry;
                    341:
                    342:        if (cvs_noexec == 1)
                    343:                return (0);
                    344:
                    345:        /* Build the path to the <file>,t file. */
                    346:        l = snprintf(path, sizeof(path), "%s/%s%s",
                    347:            CVS_PATH_CVSDIR, cf->cf_name, CVS_DESCR_FILE_EXT);
                    348:        if (l == -1 || l >= (int)sizeof(path)) {
                    349:                errno = ENAMETOOLONG;
                    350:                cvs_log(LP_ERRNO, "%s", path);
                    351:                return (CVS_EX_DATA);
                    352:        }
                    353:
                    354:        fp = fopen(path, "w+");
                    355:        if (fp == NULL) {
                    356:                cvs_log(LP_ERRNO, "failed to open `%s'", path);
                    357:                return (CVS_EX_FILE);
                    358:        }
                    359:
                    360:        if (cvs_msg != NULL) {
                    361:                if (fputs(cvs_msg, fp) == EOF) {
                    362:                        cvs_log(LP_ERRNO, "cannot write to `%s'", path);
                    363:                        (void)fclose(fp);
                    364:                        return (CVS_EX_FILE);
                    365:                }
                    366:        }
                    367:        (void)fclose(fp);
                    368:
                    369:        /* XXX Build the Entries line. */
                    370:        l = snprintf(entry, sizeof(entry), "/%s/0/Initial %s/%s/",
                    371:            cf->cf_name, cf->cf_name, kbuf);
                    372:        if (l == -1 || l >= (int)sizeof(entry)) {
                    373:                errno = ENAMETOOLONG;
                    374:                cvs_log(LP_ERRNO, "%s", entry);
                    375:                (void)cvs_unlink(path);
                    376:                return (CVS_EX_DATA);
                    377:        }
                    378:
                    379:        if ((ent = cvs_ent_parse(entry)) == NULL) {
                    380:                cvs_log(LP_ERR, "failed to parse entry");
                    381:                (void)cvs_unlink(path);
                    382:                return (CVS_EX_DATA);
                    383:        }
                    384:
                    385:        if (cvs_ent_add(entf, ent) < 0) {
                    386:                cvs_log(LP_ERR, "failed to add entry");
                    387:                (void)cvs_unlink(path);
                    388:                return (CVS_EX_DATA);
                    389:        }
1.22      joris     390:
                    391:        return (0);
1.1       jfb       392: }