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

1.27    ! niallo      1: /*     $OpenBSD: import.c,v 1.26 2005/08/04 12:20:42 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.4       jfb       103:                        cvs_msg = strdup(optarg);
                    104:                        if (cvs_msg == NULL) {
                    105:                                cvs_log(LP_ERRNO, "failed to copy message");
1.11      joris     106:                                return (CVS_EX_DATA);
1.4       jfb       107:                        }
1.1       krapht    108:                        break;
                    109:                default:
1.11      joris     110:                        return (CVS_EX_USAGE);
1.1       krapht    111:                }
                    112:        }
                    113:
                    114:        argc -= optind;
                    115:        argv += optind;
1.15      jfb       116:        if (argc != 3)
1.11      joris     117:                return (CVS_EX_USAGE);
1.1       krapht    118:
1.19      jfb       119:        if ((imp_brnum == NULL) &&
                    120:            ((imp_brnum = rcsnum_parse(CVS_IMPORT_DEFBRANCH)) == NULL)) {
                    121:                cvs_log(LP_ERR, "failed to parse default import branch");
                    122:                return (CVS_EX_DATA);
                    123:        }
                    124:
1.7       joris     125:        module = argv[0];
                    126:        vendor = argv[1];
                    127:        release = argv[2];
1.1       krapht    128:
1.15      jfb       129:        *arg = optind + 3;
                    130:
1.3       jfb       131:        if ((cvs_msg == NULL) &&
                    132:            (cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL)) == NULL)
1.11      joris     133:                return (CVS_EX_DATA);
1.1       krapht    134:
1.7       joris     135:        return (0);
                    136: }
1.1       krapht    137:
1.15      jfb       138: static int
                    139: cvs_import_pre_exec(struct cvsroot *root)
1.7       joris     140: {
1.24      xsa       141:        size_t len;
1.19      jfb       142:        char numbuf[64], repodir[MAXPATHLEN];
1.15      jfb       143:
                    144:        if (root->cr_method == CVS_METHOD_LOCAL) {
1.24      xsa       145:                len = cvs_path_cat(root->cr_dir, module, repodir,
                    146:                    sizeof(repodir));
                    147:                if (len >= sizeof(repodir))
                    148:                        return (CVS_EX_DATA);
                    149:
1.18      jfb       150:                if (mkdir(repodir, 0700) == -1) {
                    151:                        cvs_log(LP_ERRNO, "failed to create %s", repodir);
                    152:                        return (CVS_EX_DATA);
                    153:                }
1.15      jfb       154:        } else {
1.19      jfb       155:                rcsnum_tostr(imp_brnum, numbuf, sizeof(numbuf));
                    156:
1.15      jfb       157:                if ((cvs_sendarg(root, "-b", 0) < 0) ||
1.19      jfb       158:                    (cvs_sendarg(root, numbuf, 0) < 0) ||
1.15      jfb       159:                    (cvs_logmsg_send(root, cvs_msg) < 0) ||
                    160:                    (cvs_sendarg(root, module, 0) < 0) ||
                    161:                    (cvs_sendarg(root, vendor, 0) < 0) ||
                    162:                    (cvs_sendarg(root, release, 0) < 0))
                    163:                        return (CVS_EX_PROTO);
                    164:        }
1.1       krapht    165:
                    166:        return (0);
                    167: }
                    168:
1.19      jfb       169: static int
                    170: cvs_import_post_exec(struct cvsroot *root)
                    171: {
                    172:        char buf[8];
                    173:
                    174:        if (root->cr_method == CVS_METHOD_LOCAL) {
                    175:                if (conflicts > 0)
                    176:                        snprintf(buf, sizeof(buf), "%d", conflicts);
                    177:
1.24      xsa       178:                if (verbosity > 0)
                    179:                        cvs_printf("\n%s conflicts created by this import\n\n",
                    180:                            conflicts == 0 ? "No" : buf);
1.19      jfb       181:        }
                    182:
                    183:        return (CVS_EX_OK);
                    184: }
                    185:
1.3       jfb       186: /*
1.15      jfb       187:  * cvs_import_remote()
1.3       jfb       188:  *
                    189:  * Perform the import of a single file or directory.
                    190:  */
1.15      jfb       191: static int
                    192: cvs_import_remote(CVSFILE *cf, void *arg)
1.1       krapht    193: {
1.24      xsa       194:        size_t len, sz;
1.3       jfb       195:        struct cvsroot *root;
                    196:        char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.21      jfb       197:        char repo[MAXPATHLEN], date[32];
1.1       krapht    198:
1.15      jfb       199:        root = CVS_DIR_ROOT(cf);
1.24      xsa       200:
                    201:        len = cvs_path_cat(root->cr_dir, module, repo, sizeof(repo));
                    202:        if (len >= sizeof(repo))
1.14      joris     203:                return (CVS_EX_DATA);
1.1       krapht    204:
1.15      jfb       205:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       krapht    206:
1.15      jfb       207:        if (cf->cf_type == DT_DIR) {
                    208:                if (!strcmp(cf->cf_name, "."))
1.3       jfb       209:                        strlcpy(repodir, repo, sizeof(repodir));
1.12      xsa       210:                else {
1.24      xsa       211:                        len = cvs_path_cat(repo, fpath, repodir,
                    212:                            sizeof(repodir));
                    213:                        if (len >= sizeof(repodir))
1.14      joris     214:                                return (CVS_EX_DATA);
1.12      xsa       215:                }
1.15      jfb       216:
                    217:                if (cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath) < 0)
                    218:                        return (CVS_EX_PROTO);
                    219:                if (cvs_sendln(root, repodir) < 0)
                    220:                        return (CVS_EX_PROTO);
                    221:                return (0);
                    222:        }
                    223:
1.21      jfb       224:        if (dflag) {
                    225:                ctime_r(&(cf->cf_mtime), date);
1.22      jfb       226:                sz = strlen(date);
                    227:                if ((sz > 0) && (date[sz - 1] == '\n'))
                    228:                        date[--sz] = '\0';
1.21      jfb       229:                if (cvs_sendreq(root, CVS_REQ_CHECKINTIME, date) < 0)
                    230:                        return (CVS_EX_PROTO);
                    231:        }
1.15      jfb       232:        if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0)
                    233:                return (CVS_EX_PROTO);
                    234:        if (cvs_sendfile(root, fpath) < 0)
                    235:                return (CVS_EX_PROTO);
                    236:
                    237:        return (0);
                    238: }
                    239:
                    240: static int
                    241: cvs_import_local(CVSFILE *cf, void *arg)
                    242: {
1.24      xsa       243:        size_t len;
1.18      jfb       244:        time_t stamp;
1.15      jfb       245:        char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.17      jfb       246:        const char *comment;
1.18      jfb       247:        struct stat fst;
                    248:        struct timeval ts[2];
                    249:        struct cvsroot *root;
1.15      jfb       250:        RCSFILE *rf;
1.17      jfb       251:        RCSNUM *rev;
1.15      jfb       252:
                    253:        root = CVS_DIR_ROOT(cf);
1.24      xsa       254:
                    255:        len = cvs_path_cat(root->cr_dir, module, repo, sizeof(repo));
                    256:        if (len >= sizeof(repo))
1.15      jfb       257:                return (CVS_EX_DATA);
                    258:
                    259:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    260:
                    261:        if (cf->cf_type == DT_DIR) {
                    262:                if (!strcmp(cf->cf_name, "."))
                    263:                        strlcpy(rpath, repo, sizeof(rpath));
                    264:                else {
1.24      xsa       265:                        len = cvs_path_cat(repo, fpath, rpath, sizeof(rpath));
                    266:                        if (len >= sizeof(rpath))
1.15      jfb       267:                                return (CVS_EX_DATA);
                    268:
                    269:                        cvs_printf("Importing %s\n", rpath);
                    270:                        if (mkdir(rpath, 0700) == -1) {
                    271:                                cvs_log(LP_ERRNO, "failed to create %s",
                    272:                                    rpath);
                    273:                        }
1.3       jfb       274:                }
1.1       krapht    275:
1.3       jfb       276:                return (0);
1.1       krapht    277:        }
                    278:
1.18      jfb       279:        /*
                    280:         * If -d was given, use the file's last modification time as the
                    281:         * timestamps for the initial revisions.
                    282:         */
                    283:        if (dflag) {
                    284:                if (stat(fpath, &fst) == -1) {
                    285:                        cvs_log(LP_ERRNO, "failed to stat %s", fpath);
                    286:                        return (CVS_EX_DATA);
                    287:                }
                    288:                stamp = (time_t)fst.st_mtime;
                    289:
                    290:                ts[0].tv_sec = stamp;
                    291:                ts[0].tv_usec = 0;
                    292:                ts[1].tv_sec = stamp;
                    293:                ts[1].tv_usec = 0;
                    294:        } else
                    295:                stamp = -1;
                    296:
1.15      jfb       297:        snprintf(rpath, sizeof(rpath), "%s/%s%s",
                    298:            repo, fpath, RCS_FILE_EXT);
                    299:
                    300:        cvs_printf("N %s\n", fpath);
                    301:
                    302:        rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE);
                    303:        if (rf == NULL) {
1.17      jfb       304:                cvs_log(LP_ERR, "failed to create RCS file: %s",
                    305:                    strerror(rcs_errno));
                    306:                return (CVS_EX_DATA);
                    307:        }
                    308:
                    309:        comment = rcs_comment_lookup(cf->cf_name);
                    310:        if ((comment != NULL) && (rcs_comment_set(rf, comment) < 0)) {
1.18      jfb       311:                cvs_log(LP_WARN, "failed to set RCS comment leader: %s",
1.17      jfb       312:                    rcs_errstr(rcs_errno));
1.18      jfb       313:                /* don't error out, no big deal */
1.17      jfb       314:        }
                    315:
1.20      jfb       316:        rev = rcsnum_brtorev(imp_brnum);
1.27    ! niallo    317:        if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
1.17      jfb       318:                cvs_log(LP_ERR, "failed to add revision: %s",
                    319:                    rcs_errstr(rcs_errno));
                    320:                rcs_close(rf);
                    321:                (void)unlink(rpath);
                    322:                return (CVS_EX_DATA);
                    323:        }
1.18      jfb       324:
                    325:        if (rcs_sym_add(rf, release, rev) < 0) {
                    326:                cvs_log(LP_ERR, "failed to set RCS symbol: %s",
                    327:                    strerror(rcs_errno));
                    328:                rcs_close(rf);
                    329:                (void)unlink(rpath);
                    330:                return (CVS_EX_DATA);
                    331:        }
                    332:
1.20      jfb       333:        rcsnum_cpy(imp_brnum, rev, 2);
1.27    ! niallo    334:        if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
1.17      jfb       335:                cvs_log(LP_ERR, "failed to add revision: %s",
                    336:                    rcs_errstr(rcs_errno));
                    337:                rcs_close(rf);
                    338:                (void)unlink(rpath);
                    339:                return (CVS_EX_DATA);
                    340:        }
                    341:
                    342:        if (rcs_head_set(rf, rev) < 0) {
                    343:                cvs_log(LP_ERR, "failed to set RCS head: %s",
                    344:                    rcs_errstr(rcs_errno));
                    345:                rcs_close(rf);
                    346:                (void)unlink(rpath);
                    347:                return (CVS_EX_DATA);
1.1       krapht    348:        }
                    349:
1.19      jfb       350:        if (rcs_branch_set(rf, imp_brnum) < 0) {
1.18      jfb       351:                cvs_log(LP_ERR, "failed to set RCS default branch: %s",
                    352:                    strerror(rcs_errno));
1.20      jfb       353:                return (CVS_EX_DATA);
                    354:        }
                    355:
                    356:        if (rcs_sym_add(rf, vendor, imp_brnum) < 0) {
                    357:                cvs_log(LP_ERR, "failed to set RCS symbol: %s",
                    358:                    strerror(rcs_errno));
                    359:                rcs_close(rf);
                    360:                (void)unlink(rpath);
1.18      jfb       361:                return (CVS_EX_DATA);
                    362:        }
                    363:
                    364:        /* add the vendor tag and release tag as symbols */
1.15      jfb       365:        rcs_close(rf);
1.18      jfb       366:
                    367:        if (dflag && (utimes(rpath, ts) == -1))
                    368:                cvs_log(LP_ERRNO, "failed to timestamp RCS file");
1.15      jfb       369:
                    370:        return (CVS_EX_OK);
1.19      jfb       371: }
                    372:
                    373: static int
                    374: cvs_import_cleanup(void)
                    375: {
                    376:        if (imp_brnum != NULL)
                    377:                rcsnum_free(imp_brnum);
                    378:        return (0);
1.1       krapht    379: }