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

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