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

1.33    ! xsa         1: /*     $OpenBSD: import.c,v 1.32 2005/12/20 18:17:01 xsa 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>
1.23      xsa        33: #include <string.h>
1.1       krapht     34: #include <unistd.h>
                     35:
1.23      xsa        36: #include "cvs.h"
1.1       krapht     37: #include "log.h"
                     38: #include "proto.h"
                     39:
1.3       jfb        40:
1.25      xsa        41: #define CVS_IMPORT_DEFBRANCH   "1.1.1"
1.3       jfb        42:
1.19      jfb        43:
1.25      xsa        44: static int     cvs_import_init(struct cvs_cmd *, int, char **, int *);
                     45: static int     cvs_import_pre_exec(struct cvsroot *);
                     46: static int     cvs_import_pre_exec(struct cvsroot *);
                     47: static int     cvs_import_post_exec(struct cvsroot *);
                     48: static int     cvs_import_remote(CVSFILE *, void *);
                     49: static int     cvs_import_local(CVSFILE *, void *);
                     50: static int     cvs_import_cleanup(void);
1.3       jfb        51:
1.18      jfb        52: static int dflag = 0;
1.19      jfb        53: static int conflicts = 0;
                     54: static RCSNUM *imp_brnum;
                     55:
                     56: static char *module, *vendor, *release;
1.3       jfb        57:
1.15      jfb        58: struct cvs_cmd cvs_cmd_import = {
                     59:        CVS_OP_IMPORT, CVS_REQ_IMPORT, "import",
                     60:        { "im", "imp" },
                     61:        "Import sources into CVS, using vendor branches",
1.26      xsa        62:        "[-d] [-b branch] [-I ign] [-k mode] [-m msg] [-W spec] module "
                     63:        "vendortag releasetag ...",
                     64:        "b:dI:k:m:W:",
1.15      jfb        65:        NULL,
1.7       joris      66:        CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.15      jfb        67:        cvs_import_init,
                     68:        cvs_import_pre_exec,
                     69:        cvs_import_remote,
                     70:        cvs_import_local,
1.19      jfb        71:        cvs_import_post_exec,
                     72:        cvs_import_cleanup,
1.7       joris      73:        CVS_CMD_SENDDIR
                     74: };
1.1       krapht     75:
1.15      jfb        76: static int
                     77: cvs_import_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       krapht     78: {
1.7       joris      79:        int ch;
1.1       krapht     80:
1.15      jfb        81:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       krapht     82:                switch (ch) {
                     83:                case 'b':
1.19      jfb        84:                        if ((imp_brnum = rcsnum_parse(optarg)) == NULL) {
1.3       jfb        85:                                cvs_log(LP_ERR, "%s is not a numeric branch",
1.19      jfb        86:                                    optarg);
1.11      joris      87:                                return (CVS_EX_USAGE);
1.3       jfb        88:                        }
                     89:                        break;
1.1       krapht     90:                case 'd':
1.18      jfb        91:                        dflag = 1;
1.1       krapht     92:                        break;
                     93:                case 'I':
                     94:                        if (cvs_file_ignore(optarg) < 0) {
                     95:                                cvs_log(LP_ERR, "failed to add `%s' to list "
                     96:                                    "of ignore patterns", optarg);
1.11      joris      97:                                return (CVS_EX_USAGE);
1.1       krapht     98:                        }
                     99:                        break;
                    100:                case 'k':
                    101:                        break;
                    102:                case 'm':
1.31      joris     103:                        cvs_msg = xstrdup(optarg);
1.1       krapht    104:                        break;
                    105:                default:
1.11      joris     106:                        return (CVS_EX_USAGE);
1.1       krapht    107:                }
                    108:        }
                    109:
                    110:        argc -= optind;
                    111:        argv += optind;
1.15      jfb       112:        if (argc != 3)
1.11      joris     113:                return (CVS_EX_USAGE);
1.1       krapht    114:
1.19      jfb       115:        if ((imp_brnum == NULL) &&
                    116:            ((imp_brnum = rcsnum_parse(CVS_IMPORT_DEFBRANCH)) == NULL)) {
                    117:                cvs_log(LP_ERR, "failed to parse default import branch");
                    118:                return (CVS_EX_DATA);
                    119:        }
                    120:
1.7       joris     121:        module = argv[0];
                    122:        vendor = argv[1];
                    123:        release = argv[2];
1.1       krapht    124:
1.15      jfb       125:        *arg = optind + 3;
                    126:
1.33    ! xsa       127:        if (cvs_msg == NULL)
        !           128:                cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL);
1.1       krapht    129:
1.7       joris     130:        return (0);
                    131: }
1.1       krapht    132:
1.15      jfb       133: static int
                    134: cvs_import_pre_exec(struct cvsroot *root)
1.7       joris     135: {
1.24      xsa       136:        size_t len;
1.19      jfb       137:        char numbuf[64], repodir[MAXPATHLEN];
1.15      jfb       138:
                    139:        if (root->cr_method == CVS_METHOD_LOCAL) {
1.24      xsa       140:                len = cvs_path_cat(root->cr_dir, module, repodir,
                    141:                    sizeof(repodir));
                    142:                if (len >= sizeof(repodir))
                    143:                        return (CVS_EX_DATA);
                    144:
1.18      jfb       145:                if (mkdir(repodir, 0700) == -1) {
                    146:                        cvs_log(LP_ERRNO, "failed to create %s", repodir);
                    147:                        return (CVS_EX_DATA);
                    148:                }
1.15      jfb       149:        } else {
1.19      jfb       150:                rcsnum_tostr(imp_brnum, numbuf, sizeof(numbuf));
                    151:
1.15      jfb       152:                if ((cvs_sendarg(root, "-b", 0) < 0) ||
1.19      jfb       153:                    (cvs_sendarg(root, numbuf, 0) < 0) ||
1.15      jfb       154:                    (cvs_logmsg_send(root, cvs_msg) < 0) ||
                    155:                    (cvs_sendarg(root, module, 0) < 0) ||
                    156:                    (cvs_sendarg(root, vendor, 0) < 0) ||
                    157:                    (cvs_sendarg(root, release, 0) < 0))
                    158:                        return (CVS_EX_PROTO);
                    159:        }
1.1       krapht    160:
                    161:        return (0);
                    162: }
                    163:
1.19      jfb       164: static int
                    165: cvs_import_post_exec(struct cvsroot *root)
                    166: {
                    167:        char buf[8];
                    168:
                    169:        if (root->cr_method == CVS_METHOD_LOCAL) {
                    170:                if (conflicts > 0)
                    171:                        snprintf(buf, sizeof(buf), "%d", conflicts);
                    172:
1.24      xsa       173:                if (verbosity > 0)
                    174:                        cvs_printf("\n%s conflicts created by this import\n\n",
                    175:                            conflicts == 0 ? "No" : buf);
1.19      jfb       176:        }
                    177:
                    178:        return (CVS_EX_OK);
                    179: }
                    180:
1.3       jfb       181: /*
1.15      jfb       182:  * cvs_import_remote()
1.3       jfb       183:  *
                    184:  * Perform the import of a single file or directory.
                    185:  */
1.15      jfb       186: static int
                    187: cvs_import_remote(CVSFILE *cf, void *arg)
1.1       krapht    188: {
1.24      xsa       189:        size_t len, sz;
1.3       jfb       190:        struct cvsroot *root;
                    191:        char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.21      jfb       192:        char repo[MAXPATHLEN], date[32];
1.1       krapht    193:
1.15      jfb       194:        root = CVS_DIR_ROOT(cf);
1.24      xsa       195:
                    196:        len = cvs_path_cat(root->cr_dir, module, repo, sizeof(repo));
                    197:        if (len >= sizeof(repo))
1.14      joris     198:                return (CVS_EX_DATA);
1.1       krapht    199:
1.15      jfb       200:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       krapht    201:
1.15      jfb       202:        if (cf->cf_type == DT_DIR) {
                    203:                if (!strcmp(cf->cf_name, "."))
1.3       jfb       204:                        strlcpy(repodir, repo, sizeof(repodir));
1.12      xsa       205:                else {
1.24      xsa       206:                        len = cvs_path_cat(repo, fpath, repodir,
                    207:                            sizeof(repodir));
                    208:                        if (len >= sizeof(repodir))
1.14      joris     209:                                return (CVS_EX_DATA);
1.12      xsa       210:                }
1.15      jfb       211:
                    212:                if (cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath) < 0)
                    213:                        return (CVS_EX_PROTO);
                    214:                if (cvs_sendln(root, repodir) < 0)
                    215:                        return (CVS_EX_PROTO);
                    216:                return (0);
                    217:        }
                    218:
1.28      xsa       219:        if (dflag == 1) {
1.21      jfb       220:                ctime_r(&(cf->cf_mtime), date);
1.22      jfb       221:                sz = strlen(date);
                    222:                if ((sz > 0) && (date[sz - 1] == '\n'))
                    223:                        date[--sz] = '\0';
1.21      jfb       224:                if (cvs_sendreq(root, CVS_REQ_CHECKINTIME, date) < 0)
                    225:                        return (CVS_EX_PROTO);
                    226:        }
1.15      jfb       227:        if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0)
                    228:                return (CVS_EX_PROTO);
                    229:        if (cvs_sendfile(root, fpath) < 0)
                    230:                return (CVS_EX_PROTO);
                    231:
                    232:        return (0);
                    233: }
                    234:
                    235: static int
                    236: cvs_import_local(CVSFILE *cf, void *arg)
                    237: {
1.24      xsa       238:        size_t len;
1.28      xsa       239:        int l;
1.18      jfb       240:        time_t stamp;
1.30      joris     241:        char *fcont;
1.15      jfb       242:        char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.17      jfb       243:        const char *comment;
1.18      jfb       244:        struct stat fst;
                    245:        struct timeval ts[2];
                    246:        struct cvsroot *root;
1.15      jfb       247:        RCSFILE *rf;
1.17      jfb       248:        RCSNUM *rev;
1.30      joris     249:        BUF *bp;
1.15      jfb       250:
                    251:        root = CVS_DIR_ROOT(cf);
1.24      xsa       252:
                    253:        len = cvs_path_cat(root->cr_dir, module, repo, sizeof(repo));
                    254:        if (len >= sizeof(repo))
1.15      jfb       255:                return (CVS_EX_DATA);
                    256:
                    257:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    258:
                    259:        if (cf->cf_type == DT_DIR) {
                    260:                if (!strcmp(cf->cf_name, "."))
                    261:                        strlcpy(rpath, repo, sizeof(rpath));
                    262:                else {
1.24      xsa       263:                        len = cvs_path_cat(repo, fpath, rpath, sizeof(rpath));
                    264:                        if (len >= sizeof(rpath))
1.15      jfb       265:                                return (CVS_EX_DATA);
                    266:
                    267:                        cvs_printf("Importing %s\n", rpath);
                    268:                        if (mkdir(rpath, 0700) == -1) {
                    269:                                cvs_log(LP_ERRNO, "failed to create %s",
                    270:                                    rpath);
                    271:                        }
1.3       jfb       272:                }
1.1       krapht    273:
1.3       jfb       274:                return (0);
1.1       krapht    275:        }
                    276:
1.18      jfb       277:        /*
                    278:         * If -d was given, use the file's last modification time as the
                    279:         * timestamps for the initial revisions.
                    280:         */
1.28      xsa       281:        if (dflag == 1) {
1.18      jfb       282:                if (stat(fpath, &fst) == -1) {
1.29      xsa       283:                        cvs_log(LP_ERRNO, "failed to stat `%s'", fpath);
1.18      jfb       284:                        return (CVS_EX_DATA);
                    285:                }
                    286:                stamp = (time_t)fst.st_mtime;
                    287:
                    288:                ts[0].tv_sec = stamp;
                    289:                ts[0].tv_usec = 0;
                    290:                ts[1].tv_sec = stamp;
                    291:                ts[1].tv_usec = 0;
                    292:        } else
                    293:                stamp = -1;
                    294:
1.28      xsa       295:        l = snprintf(rpath, sizeof(rpath), "%s/%s%s",
1.15      jfb       296:            repo, fpath, RCS_FILE_EXT);
1.28      xsa       297:        if (l == -1 || l >= (int)sizeof(rpath)) {
                    298:                errno = ENAMETOOLONG;
                    299:                cvs_log(LP_ERRNO, "%s", rpath);
                    300:                return (CVS_EX_DATA);
                    301:        }
1.15      jfb       302:
                    303:        cvs_printf("N %s\n", fpath);
                    304:
                    305:        rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE);
                    306:        if (rf == NULL) {
1.17      jfb       307:                cvs_log(LP_ERR, "failed to create RCS file: %s",
                    308:                    strerror(rcs_errno));
                    309:                return (CVS_EX_DATA);
                    310:        }
                    311:
                    312:        comment = rcs_comment_lookup(cf->cf_name);
                    313:        if ((comment != NULL) && (rcs_comment_set(rf, comment) < 0)) {
1.18      jfb       314:                cvs_log(LP_WARN, "failed to set RCS comment leader: %s",
1.17      jfb       315:                    rcs_errstr(rcs_errno));
1.18      jfb       316:                /* don't error out, no big deal */
1.17      jfb       317:        }
                    318:
1.20      jfb       319:        rev = rcsnum_brtorev(imp_brnum);
1.27      niallo    320:        if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
1.17      jfb       321:                cvs_log(LP_ERR, "failed to add revision: %s",
                    322:                    rcs_errstr(rcs_errno));
                    323:                rcs_close(rf);
                    324:                (void)unlink(rpath);
                    325:                return (CVS_EX_DATA);
                    326:        }
1.18      jfb       327:
                    328:        if (rcs_sym_add(rf, release, rev) < 0) {
                    329:                cvs_log(LP_ERR, "failed to set RCS symbol: %s",
                    330:                    strerror(rcs_errno));
                    331:                rcs_close(rf);
                    332:                (void)unlink(rpath);
                    333:                return (CVS_EX_DATA);
                    334:        }
                    335:
1.20      jfb       336:        rcsnum_cpy(imp_brnum, rev, 2);
1.27      niallo    337:        if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
1.17      jfb       338:                cvs_log(LP_ERR, "failed to add revision: %s",
                    339:                    rcs_errstr(rcs_errno));
                    340:                rcs_close(rf);
                    341:                (void)unlink(rpath);
                    342:                return (CVS_EX_DATA);
                    343:        }
                    344:
                    345:        if (rcs_head_set(rf, rev) < 0) {
                    346:                cvs_log(LP_ERR, "failed to set RCS head: %s",
                    347:                    rcs_errstr(rcs_errno));
                    348:                rcs_close(rf);
                    349:                (void)unlink(rpath);
                    350:                return (CVS_EX_DATA);
1.1       krapht    351:        }
                    352:
1.19      jfb       353:        if (rcs_branch_set(rf, imp_brnum) < 0) {
1.18      jfb       354:                cvs_log(LP_ERR, "failed to set RCS default branch: %s",
                    355:                    strerror(rcs_errno));
1.20      jfb       356:                return (CVS_EX_DATA);
                    357:        }
                    358:
                    359:        if (rcs_sym_add(rf, vendor, imp_brnum) < 0) {
                    360:                cvs_log(LP_ERR, "failed to set RCS symbol: %s",
                    361:                    strerror(rcs_errno));
1.30      joris     362:                rcs_close(rf);
                    363:                (void)unlink(rpath);
                    364:                return (CVS_EX_DATA);
                    365:        }
                    366:
                    367:        if ((bp = cvs_buf_load(fpath, BUF_AUTOEXT)) == NULL) {
                    368:                rcs_close(rf);
                    369:                (void)unlink(rpath);
                    370:                return (CVS_EX_DATA);
                    371:        }
                    372:
1.32      xsa       373:        cvs_buf_putc(bp, '\0');
1.30      joris     374:
                    375:        fcont = cvs_buf_release(bp);
                    376:
                    377:        if (rcs_deltatext_set(rf, rev, fcont) < 0) {
1.20      jfb       378:                rcs_close(rf);
                    379:                (void)unlink(rpath);
1.18      jfb       380:                return (CVS_EX_DATA);
                    381:        }
                    382:
                    383:        /* add the vendor tag and release tag as symbols */
1.15      jfb       384:        rcs_close(rf);
1.18      jfb       385:
1.28      xsa       386:        if ((dflag ==1) && (utimes(rpath, ts) == -1))
1.18      jfb       387:                cvs_log(LP_ERRNO, "failed to timestamp RCS file");
1.15      jfb       388:
                    389:        return (CVS_EX_OK);
1.19      jfb       390: }
                    391:
                    392: static int
                    393: cvs_import_cleanup(void)
                    394: {
                    395:        if (imp_brnum != NULL)
                    396:                rcsnum_free(imp_brnum);
                    397:        return (0);
1.1       krapht    398: }