[BACK]Return to import.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/import.c, Revision 1.22

1.22    ! jfb         1: /*     $OpenBSD: import.c,v 1.21 2005/05/26 03:10:01 jfb Exp $ */
1.1       krapht      2: /*
1.5       joris       3:  * Copyright (c) 2004 Joris Vink <joris@openbsd.org>
1.1       krapht      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include <sys/types.h>
1.15      jfb        28: #include <sys/stat.h>
1.1       krapht     29:
                     30: #include <errno.h>
                     31: #include <stdio.h>
                     32: #include <stdlib.h>
                     33: #include <unistd.h>
                     34: #include <string.h>
                     35:
                     36: #include "log.h"
                     37: #include "file.h"
1.3       jfb        38: #include "cvs.h"
1.1       krapht     39: #include "proto.h"
                     40:
1.3       jfb        41:
                     42: #define CVS_IMPORT_DEFBRANCH    "1.1.1"
                     43:
1.19      jfb        44:
1.15      jfb        45: static int cvs_import_init     (struct cvs_cmd *, int, char **, int *);
                     46: static int cvs_import_pre_exec (struct cvsroot *);
1.19      jfb        47: static int cvs_import_pre_exec (struct cvsroot *);
                     48: static int cvs_import_post_exec(struct cvsroot *);
1.15      jfb        49: static int cvs_import_remote   (CVSFILE *, void *);
                     50: static int cvs_import_local    (CVSFILE *, void *);
1.19      jfb        51: static int cvs_import_cleanup  (void);
1.3       jfb        52:
1.18      jfb        53: static int dflag = 0;
1.19      jfb        54: static int conflicts = 0;
                     55: static RCSNUM *imp_brnum;
                     56:
                     57: static char *module, *vendor, *release;
1.3       jfb        58:
1.15      jfb        59: struct cvs_cmd cvs_cmd_import = {
                     60:        CVS_OP_IMPORT, CVS_REQ_IMPORT, "import",
                     61:        { "im", "imp" },
                     62:        "Import sources into CVS, using vendor branches",
                     63:        "[-d] [-b branch] [-I ign] [-k subst] [-m msg] repository "
                     64:        "vendor-tag release-tags ...",
                     65:        "b:dI:k:m:",
                     66:        NULL,
1.7       joris      67:        CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.15      jfb        68:        cvs_import_init,
                     69:        cvs_import_pre_exec,
                     70:        cvs_import_remote,
                     71:        cvs_import_local,
1.19      jfb        72:        cvs_import_post_exec,
                     73:        cvs_import_cleanup,
1.7       joris      74:        CVS_CMD_SENDDIR
                     75: };
1.1       krapht     76:
1.15      jfb        77: static int
                     78: cvs_import_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       krapht     79: {
1.7       joris      80:        int ch;
1.1       krapht     81:
1.15      jfb        82:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       krapht     83:                switch (ch) {
                     84:                case 'b':
1.19      jfb        85:                        if ((imp_brnum = rcsnum_parse(optarg)) == NULL) {
1.3       jfb        86:                                cvs_log(LP_ERR, "%s is not a numeric branch",
1.19      jfb        87:                                    optarg);
1.11      joris      88:                                return (CVS_EX_USAGE);
1.3       jfb        89:                        }
                     90:                        break;
1.1       krapht     91:                case 'd':
1.18      jfb        92:                        dflag = 1;
1.1       krapht     93:                        break;
                     94:                case 'I':
                     95:                        if (cvs_file_ignore(optarg) < 0) {
                     96:                                cvs_log(LP_ERR, "failed to add `%s' to list "
                     97:                                    "of ignore patterns", optarg);
1.11      joris      98:                                return (CVS_EX_USAGE);
1.1       krapht     99:                        }
                    100:                        break;
                    101:                case 'k':
                    102:                        break;
                    103:                case 'm':
1.4       jfb       104:                        cvs_msg = strdup(optarg);
                    105:                        if (cvs_msg == NULL) {
                    106:                                cvs_log(LP_ERRNO, "failed to copy message");
1.11      joris     107:                                return (CVS_EX_DATA);
1.4       jfb       108:                        }
1.1       krapht    109:                        break;
                    110:                default:
1.11      joris     111:                        return (CVS_EX_USAGE);
1.1       krapht    112:                }
                    113:        }
                    114:
                    115:        argc -= optind;
                    116:        argv += optind;
1.15      jfb       117:        if (argc != 3)
1.11      joris     118:                return (CVS_EX_USAGE);
1.1       krapht    119:
1.19      jfb       120:        if ((imp_brnum == NULL) &&
                    121:            ((imp_brnum = rcsnum_parse(CVS_IMPORT_DEFBRANCH)) == NULL)) {
                    122:                cvs_log(LP_ERR, "failed to parse default import branch");
                    123:                return (CVS_EX_DATA);
                    124:        }
                    125:
1.7       joris     126:        module = argv[0];
                    127:        vendor = argv[1];
                    128:        release = argv[2];
1.1       krapht    129:
1.15      jfb       130:        *arg = optind + 3;
                    131:
1.3       jfb       132:        if ((cvs_msg == NULL) &&
                    133:            (cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL)) == NULL)
1.11      joris     134:                return (CVS_EX_DATA);
1.1       krapht    135:
1.7       joris     136:        return (0);
                    137: }
1.1       krapht    138:
1.15      jfb       139: static int
                    140: cvs_import_pre_exec(struct cvsroot *root)
1.7       joris     141: {
1.19      jfb       142:        char numbuf[64], repodir[MAXPATHLEN];
1.15      jfb       143:
                    144:        if (root->cr_method == CVS_METHOD_LOCAL) {
                    145:                snprintf(repodir, sizeof(repodir), "%s/%s", root->cr_dir,
                    146:                    module);
1.18      jfb       147:                if (mkdir(repodir, 0700) == -1) {
                    148:                        cvs_log(LP_ERRNO, "failed to create %s", repodir);
                    149:                        return (CVS_EX_DATA);
                    150:                }
1.15      jfb       151:        } else {
1.19      jfb       152:                rcsnum_tostr(imp_brnum, numbuf, sizeof(numbuf));
                    153:
1.15      jfb       154:                if ((cvs_sendarg(root, "-b", 0) < 0) ||
1.19      jfb       155:                    (cvs_sendarg(root, numbuf, 0) < 0) ||
1.15      jfb       156:                    (cvs_logmsg_send(root, cvs_msg) < 0) ||
                    157:                    (cvs_sendarg(root, module, 0) < 0) ||
                    158:                    (cvs_sendarg(root, vendor, 0) < 0) ||
                    159:                    (cvs_sendarg(root, release, 0) < 0))
                    160:                        return (CVS_EX_PROTO);
                    161:        }
1.1       krapht    162:
                    163:        return (0);
                    164: }
                    165:
1.19      jfb       166: static int
                    167: cvs_import_post_exec(struct cvsroot *root)
                    168: {
                    169:        char buf[8];
                    170:
                    171:        if (root->cr_method == CVS_METHOD_LOCAL) {
                    172:                if (conflicts > 0)
                    173:                        snprintf(buf, sizeof(buf), "%d", conflicts);
                    174:
                    175:                cvs_printf("\n%s conflicts created by this import\n\n",
                    176:                    conflicts == 0 ? "No" : buf);
                    177:        }
                    178:
                    179:        return (CVS_EX_OK);
                    180: }
                    181:
1.3       jfb       182: /*
1.15      jfb       183:  * cvs_import_remote()
1.3       jfb       184:  *
                    185:  * Perform the import of a single file or directory.
                    186:  */
1.15      jfb       187: static int
                    188: cvs_import_remote(CVSFILE *cf, void *arg)
1.1       krapht    189: {
1.15      jfb       190:        int len;
1.22    ! jfb       191:        size_t sz;
1.3       jfb       192:        struct cvsroot *root;
                    193:        char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.21      jfb       194:        char repo[MAXPATHLEN], date[32];
1.1       krapht    195:
1.15      jfb       196:        root = CVS_DIR_ROOT(cf);
                    197:        len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
                    198:        if (len == -1 || len >= (int)sizeof(repo)) {
1.12      xsa       199:                errno = ENAMETOOLONG;
                    200:                cvs_log(LP_ERRNO, "%s", repo);
1.14      joris     201:                return (CVS_EX_DATA);
1.12      xsa       202:        }
1.1       krapht    203:
1.15      jfb       204:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       krapht    205:
1.15      jfb       206:        if (cf->cf_type == DT_DIR) {
                    207:                if (!strcmp(cf->cf_name, "."))
1.3       jfb       208:                        strlcpy(repodir, repo, sizeof(repodir));
1.12      xsa       209:                else {
1.15      jfb       210:                        len = snprintf(repodir, sizeof(repodir), "%s/%s",
1.12      xsa       211:                            repo, fpath);
1.15      jfb       212:                        if (len == -1 || len >= (int)sizeof(repodir)) {
1.12      xsa       213:                                errno = ENAMETOOLONG;
                    214:                                cvs_log(LP_ERRNO, "%s", repodir);
1.14      joris     215:                                return (CVS_EX_DATA);
1.12      xsa       216:                        }
                    217:                }
1.15      jfb       218:
                    219:                if (cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath) < 0)
                    220:                        return (CVS_EX_PROTO);
                    221:                if (cvs_sendln(root, repodir) < 0)
                    222:                        return (CVS_EX_PROTO);
                    223:                return (0);
                    224:        }
                    225:
1.21      jfb       226:        if (dflag) {
                    227:                ctime_r(&(cf->cf_mtime), date);
1.22    ! jfb       228:                sz = strlen(date);
        !           229:                if ((sz > 0) && (date[sz - 1] == '\n'))
        !           230:                        date[--sz] = '\0';
1.21      jfb       231:                if (cvs_sendreq(root, CVS_REQ_CHECKINTIME, date) < 0)
                    232:                        return (CVS_EX_PROTO);
                    233:        }
1.15      jfb       234:        if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0)
                    235:                return (CVS_EX_PROTO);
                    236:        if (cvs_sendfile(root, fpath) < 0)
                    237:                return (CVS_EX_PROTO);
                    238:
                    239:        return (0);
                    240: }
                    241:
                    242: static int
                    243: cvs_import_local(CVSFILE *cf, void *arg)
                    244: {
                    245:        int len;
1.18      jfb       246:        time_t stamp;
1.15      jfb       247:        char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.17      jfb       248:        const char *comment;
1.18      jfb       249:        struct stat fst;
                    250:        struct timeval ts[2];
                    251:        struct cvsroot *root;
1.15      jfb       252:        RCSFILE *rf;
1.17      jfb       253:        RCSNUM *rev;
1.15      jfb       254:
                    255:        root = CVS_DIR_ROOT(cf);
                    256:        len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
                    257:        if (len == -1 || len >= (int)sizeof(repo)) {
                    258:                errno = ENAMETOOLONG;
                    259:                cvs_log(LP_ERRNO, "%s", repo);
                    260:                return (CVS_EX_DATA);
                    261:        }
                    262:
                    263:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    264:
                    265:        if (cf->cf_type == DT_DIR) {
                    266:                if (!strcmp(cf->cf_name, "."))
                    267:                        strlcpy(rpath, repo, sizeof(rpath));
                    268:                else {
                    269:                        len = snprintf(rpath, sizeof(rpath), "%s/%s",
                    270:                            repo, fpath);
                    271:                        if (len == -1 || len >= (int)sizeof(rpath)) {
                    272:                                errno = ENAMETOOLONG;
                    273:                                cvs_log(LP_ERRNO, "%s", rpath);
                    274:                                return (CVS_EX_DATA);
                    275:                        }
                    276:
                    277:                        cvs_printf("Importing %s\n", rpath);
                    278:                        if (mkdir(rpath, 0700) == -1) {
                    279:                                cvs_log(LP_ERRNO, "failed to create %s",
                    280:                                    rpath);
                    281:                        }
1.3       jfb       282:                }
1.1       krapht    283:
1.3       jfb       284:                return (0);
1.1       krapht    285:        }
                    286:
1.18      jfb       287:        /*
                    288:         * If -d was given, use the file's last modification time as the
                    289:         * timestamps for the initial revisions.
                    290:         */
                    291:        if (dflag) {
                    292:                if (stat(fpath, &fst) == -1) {
                    293:                        cvs_log(LP_ERRNO, "failed to stat %s", fpath);
                    294:                        return (CVS_EX_DATA);
                    295:                }
                    296:                stamp = (time_t)fst.st_mtime;
                    297:
                    298:                ts[0].tv_sec = stamp;
                    299:                ts[0].tv_usec = 0;
                    300:                ts[1].tv_sec = stamp;
                    301:                ts[1].tv_usec = 0;
                    302:        } else
                    303:                stamp = -1;
                    304:
1.15      jfb       305:        snprintf(rpath, sizeof(rpath), "%s/%s%s",
                    306:            repo, fpath, RCS_FILE_EXT);
                    307:
                    308:        cvs_printf("N %s\n", fpath);
                    309:
                    310:        rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE);
                    311:        if (rf == NULL) {
1.17      jfb       312:                cvs_log(LP_ERR, "failed to create RCS file: %s",
                    313:                    strerror(rcs_errno));
                    314:                return (CVS_EX_DATA);
                    315:        }
                    316:
                    317:        comment = rcs_comment_lookup(cf->cf_name);
                    318:        if ((comment != NULL) && (rcs_comment_set(rf, comment) < 0)) {
1.18      jfb       319:                cvs_log(LP_WARN, "failed to set RCS comment leader: %s",
1.17      jfb       320:                    rcs_errstr(rcs_errno));
1.18      jfb       321:                /* don't error out, no big deal */
1.17      jfb       322:        }
                    323:
1.20      jfb       324:        rev = rcsnum_brtorev(imp_brnum);
1.18      jfb       325:        if (rcs_rev_add(rf, rev, cvs_msg, stamp) < 0) {
1.17      jfb       326:                cvs_log(LP_ERR, "failed to add revision: %s",
                    327:                    rcs_errstr(rcs_errno));
                    328:                rcs_close(rf);
                    329:                (void)unlink(rpath);
                    330:                return (CVS_EX_DATA);
                    331:        }
1.18      jfb       332:
                    333:        if (rcs_sym_add(rf, release, rev) < 0) {
                    334:                cvs_log(LP_ERR, "failed to set RCS symbol: %s",
                    335:                    strerror(rcs_errno));
                    336:                rcs_close(rf);
                    337:                (void)unlink(rpath);
                    338:                return (CVS_EX_DATA);
                    339:        }
                    340:
1.20      jfb       341:        rcsnum_cpy(imp_brnum, rev, 2);
1.18      jfb       342:        if (rcs_rev_add(rf, rev, cvs_msg, stamp) < 0) {
1.17      jfb       343:                cvs_log(LP_ERR, "failed to add revision: %s",
                    344:                    rcs_errstr(rcs_errno));
                    345:                rcs_close(rf);
                    346:                (void)unlink(rpath);
                    347:                return (CVS_EX_DATA);
                    348:        }
                    349:
                    350:        if (rcs_head_set(rf, rev) < 0) {
                    351:                cvs_log(LP_ERR, "failed to set RCS head: %s",
                    352:                    rcs_errstr(rcs_errno));
                    353:                rcs_close(rf);
                    354:                (void)unlink(rpath);
                    355:                return (CVS_EX_DATA);
1.1       krapht    356:        }
                    357:
1.19      jfb       358:        if (rcs_branch_set(rf, imp_brnum) < 0) {
1.18      jfb       359:                cvs_log(LP_ERR, "failed to set RCS default branch: %s",
                    360:                    strerror(rcs_errno));
1.20      jfb       361:                return (CVS_EX_DATA);
                    362:        }
                    363:
                    364:        if (rcs_sym_add(rf, vendor, imp_brnum) < 0) {
                    365:                cvs_log(LP_ERR, "failed to set RCS symbol: %s",
                    366:                    strerror(rcs_errno));
                    367:                rcs_close(rf);
                    368:                (void)unlink(rpath);
1.18      jfb       369:                return (CVS_EX_DATA);
                    370:        }
                    371:
                    372:        /* add the vendor tag and release tag as symbols */
1.15      jfb       373:        rcs_close(rf);
1.18      jfb       374:
                    375:        if (dflag && (utimes(rpath, ts) == -1))
                    376:                cvs_log(LP_ERRNO, "failed to timestamp RCS file");
1.15      jfb       377:
                    378:        return (CVS_EX_OK);
1.19      jfb       379: }
                    380:
                    381: static int
                    382: cvs_import_cleanup(void)
                    383: {
                    384:        if (imp_brnum != NULL)
                    385:                rcsnum_free(imp_brnum);
                    386:        return (0);
1.1       krapht    387: }