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

1.34    ! joris       1: /*     $OpenBSD: import.c,v 1.33 2005/12/21 20:06:25 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.34    ! joris     152:                cvs_sendarg(root, "-b", 0);
        !           153:                cvs_sendarg(root, numbuf, 0);
        !           154:                cvs_logmsg_send(root, cvs_msg);
        !           155:                cvs_sendarg(root, module, 0);
        !           156:                cvs_sendarg(root, vendor, 0);
        !           157:                cvs_sendarg(root, release, 0);
1.15      jfb       158:        }
1.1       krapht    159:
                    160:        return (0);
                    161: }
                    162:
1.19      jfb       163: static int
                    164: cvs_import_post_exec(struct cvsroot *root)
                    165: {
                    166:        char buf[8];
                    167:
                    168:        if (root->cr_method == CVS_METHOD_LOCAL) {
                    169:                if (conflicts > 0)
                    170:                        snprintf(buf, sizeof(buf), "%d", conflicts);
                    171:
1.24      xsa       172:                if (verbosity > 0)
                    173:                        cvs_printf("\n%s conflicts created by this import\n\n",
                    174:                            conflicts == 0 ? "No" : buf);
1.19      jfb       175:        }
                    176:
                    177:        return (CVS_EX_OK);
                    178: }
                    179:
1.3       jfb       180: /*
1.15      jfb       181:  * cvs_import_remote()
1.3       jfb       182:  *
                    183:  * Perform the import of a single file or directory.
                    184:  */
1.15      jfb       185: static int
                    186: cvs_import_remote(CVSFILE *cf, void *arg)
1.1       krapht    187: {
1.24      xsa       188:        size_t len, sz;
1.3       jfb       189:        struct cvsroot *root;
                    190:        char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.21      jfb       191:        char repo[MAXPATHLEN], date[32];
1.1       krapht    192:
1.15      jfb       193:        root = CVS_DIR_ROOT(cf);
1.24      xsa       194:
                    195:        len = cvs_path_cat(root->cr_dir, module, repo, sizeof(repo));
                    196:        if (len >= sizeof(repo))
1.14      joris     197:                return (CVS_EX_DATA);
1.1       krapht    198:
1.15      jfb       199:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       krapht    200:
1.15      jfb       201:        if (cf->cf_type == DT_DIR) {
                    202:                if (!strcmp(cf->cf_name, "."))
1.3       jfb       203:                        strlcpy(repodir, repo, sizeof(repodir));
1.12      xsa       204:                else {
1.24      xsa       205:                        len = cvs_path_cat(repo, fpath, repodir,
                    206:                            sizeof(repodir));
                    207:                        if (len >= sizeof(repodir))
1.14      joris     208:                                return (CVS_EX_DATA);
1.12      xsa       209:                }
1.15      jfb       210:
1.34    ! joris     211:                cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath);
        !           212:                cvs_sendln(root, repodir);
1.15      jfb       213:                return (0);
                    214:        }
                    215:
1.28      xsa       216:        if (dflag == 1) {
1.21      jfb       217:                ctime_r(&(cf->cf_mtime), date);
1.22      jfb       218:                sz = strlen(date);
                    219:                if ((sz > 0) && (date[sz - 1] == '\n'))
                    220:                        date[--sz] = '\0';
1.34    ! joris     221:                cvs_sendreq(root, CVS_REQ_CHECKINTIME, date);
1.21      jfb       222:        }
1.34    ! joris     223:
        !           224:        cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
        !           225:        cvs_sendfile(root, fpath);
1.15      jfb       226:
                    227:        return (0);
                    228: }
                    229:
                    230: static int
                    231: cvs_import_local(CVSFILE *cf, void *arg)
                    232: {
1.24      xsa       233:        size_t len;
1.28      xsa       234:        int l;
1.18      jfb       235:        time_t stamp;
1.30      joris     236:        char *fcont;
1.15      jfb       237:        char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.17      jfb       238:        const char *comment;
1.18      jfb       239:        struct stat fst;
                    240:        struct timeval ts[2];
                    241:        struct cvsroot *root;
1.15      jfb       242:        RCSFILE *rf;
1.17      jfb       243:        RCSNUM *rev;
1.30      joris     244:        BUF *bp;
1.15      jfb       245:
                    246:        root = CVS_DIR_ROOT(cf);
1.24      xsa       247:
                    248:        len = cvs_path_cat(root->cr_dir, module, repo, sizeof(repo));
                    249:        if (len >= sizeof(repo))
1.15      jfb       250:                return (CVS_EX_DATA);
                    251:
                    252:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    253:
                    254:        if (cf->cf_type == DT_DIR) {
                    255:                if (!strcmp(cf->cf_name, "."))
                    256:                        strlcpy(rpath, repo, sizeof(rpath));
                    257:                else {
1.24      xsa       258:                        len = cvs_path_cat(repo, fpath, rpath, sizeof(rpath));
                    259:                        if (len >= sizeof(rpath))
1.15      jfb       260:                                return (CVS_EX_DATA);
                    261:
                    262:                        cvs_printf("Importing %s\n", rpath);
                    263:                        if (mkdir(rpath, 0700) == -1) {
                    264:                                cvs_log(LP_ERRNO, "failed to create %s",
                    265:                                    rpath);
                    266:                        }
1.3       jfb       267:                }
1.1       krapht    268:
1.3       jfb       269:                return (0);
1.1       krapht    270:        }
                    271:
1.18      jfb       272:        /*
                    273:         * If -d was given, use the file's last modification time as the
                    274:         * timestamps for the initial revisions.
                    275:         */
1.28      xsa       276:        if (dflag == 1) {
1.18      jfb       277:                if (stat(fpath, &fst) == -1) {
1.29      xsa       278:                        cvs_log(LP_ERRNO, "failed to stat `%s'", fpath);
1.18      jfb       279:                        return (CVS_EX_DATA);
                    280:                }
                    281:                stamp = (time_t)fst.st_mtime;
                    282:
                    283:                ts[0].tv_sec = stamp;
                    284:                ts[0].tv_usec = 0;
                    285:                ts[1].tv_sec = stamp;
                    286:                ts[1].tv_usec = 0;
                    287:        } else
                    288:                stamp = -1;
                    289:
1.28      xsa       290:        l = snprintf(rpath, sizeof(rpath), "%s/%s%s",
1.15      jfb       291:            repo, fpath, RCS_FILE_EXT);
1.28      xsa       292:        if (l == -1 || l >= (int)sizeof(rpath)) {
                    293:                errno = ENAMETOOLONG;
                    294:                cvs_log(LP_ERRNO, "%s", rpath);
                    295:                return (CVS_EX_DATA);
                    296:        }
1.15      jfb       297:
                    298:        cvs_printf("N %s\n", fpath);
                    299:
                    300:        rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE);
                    301:        if (rf == NULL) {
1.17      jfb       302:                cvs_log(LP_ERR, "failed to create RCS file: %s",
                    303:                    strerror(rcs_errno));
                    304:                return (CVS_EX_DATA);
                    305:        }
                    306:
                    307:        comment = rcs_comment_lookup(cf->cf_name);
                    308:        if ((comment != NULL) && (rcs_comment_set(rf, comment) < 0)) {
1.18      jfb       309:                cvs_log(LP_WARN, "failed to set RCS comment leader: %s",
1.17      jfb       310:                    rcs_errstr(rcs_errno));
1.18      jfb       311:                /* don't error out, no big deal */
1.17      jfb       312:        }
                    313:
1.20      jfb       314:        rev = rcsnum_brtorev(imp_brnum);
1.27      niallo    315:        if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
1.17      jfb       316:                cvs_log(LP_ERR, "failed to add revision: %s",
                    317:                    rcs_errstr(rcs_errno));
                    318:                rcs_close(rf);
                    319:                (void)unlink(rpath);
                    320:                return (CVS_EX_DATA);
                    321:        }
1.18      jfb       322:
                    323:        if (rcs_sym_add(rf, release, rev) < 0) {
                    324:                cvs_log(LP_ERR, "failed to set RCS symbol: %s",
                    325:                    strerror(rcs_errno));
                    326:                rcs_close(rf);
                    327:                (void)unlink(rpath);
                    328:                return (CVS_EX_DATA);
                    329:        }
                    330:
1.20      jfb       331:        rcsnum_cpy(imp_brnum, rev, 2);
1.27      niallo    332:        if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
1.17      jfb       333:                cvs_log(LP_ERR, "failed to add revision: %s",
                    334:                    rcs_errstr(rcs_errno));
                    335:                rcs_close(rf);
                    336:                (void)unlink(rpath);
                    337:                return (CVS_EX_DATA);
                    338:        }
                    339:
                    340:        if (rcs_head_set(rf, rev) < 0) {
                    341:                cvs_log(LP_ERR, "failed to set RCS head: %s",
                    342:                    rcs_errstr(rcs_errno));
                    343:                rcs_close(rf);
                    344:                (void)unlink(rpath);
                    345:                return (CVS_EX_DATA);
1.1       krapht    346:        }
                    347:
1.19      jfb       348:        if (rcs_branch_set(rf, imp_brnum) < 0) {
1.18      jfb       349:                cvs_log(LP_ERR, "failed to set RCS default branch: %s",
                    350:                    strerror(rcs_errno));
1.20      jfb       351:                return (CVS_EX_DATA);
                    352:        }
                    353:
                    354:        if (rcs_sym_add(rf, vendor, imp_brnum) < 0) {
                    355:                cvs_log(LP_ERR, "failed to set RCS symbol: %s",
                    356:                    strerror(rcs_errno));
1.30      joris     357:                rcs_close(rf);
                    358:                (void)unlink(rpath);
                    359:                return (CVS_EX_DATA);
                    360:        }
                    361:
                    362:        if ((bp = cvs_buf_load(fpath, BUF_AUTOEXT)) == NULL) {
                    363:                rcs_close(rf);
                    364:                (void)unlink(rpath);
                    365:                return (CVS_EX_DATA);
                    366:        }
                    367:
1.32      xsa       368:        cvs_buf_putc(bp, '\0');
1.30      joris     369:
                    370:        fcont = cvs_buf_release(bp);
                    371:
                    372:        if (rcs_deltatext_set(rf, rev, fcont) < 0) {
1.20      jfb       373:                rcs_close(rf);
                    374:                (void)unlink(rpath);
1.18      jfb       375:                return (CVS_EX_DATA);
                    376:        }
                    377:
                    378:        /* add the vendor tag and release tag as symbols */
1.15      jfb       379:        rcs_close(rf);
1.18      jfb       380:
1.28      xsa       381:        if ((dflag ==1) && (utimes(rpath, ts) == -1))
1.18      jfb       382:                cvs_log(LP_ERRNO, "failed to timestamp RCS file");
1.15      jfb       383:
                    384:        return (CVS_EX_OK);
1.19      jfb       385: }
                    386:
                    387: static int
                    388: cvs_import_cleanup(void)
                    389: {
                    390:        if (imp_brnum != NULL)
                    391:                rcsnum_free(imp_brnum);
                    392:        return (0);
1.1       krapht    393: }