Annotation of src/usr.bin/cvs/import.c, Revision 1.28
1.28 ! xsa 1: /* $OpenBSD: import.c,v 1.27 2005/10/15 22:56:02 niallo 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.28 ! xsa 224: if (dflag == 1) {
1.21 jfb 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.28 ! xsa 244: int l;
1.18 jfb 245: time_t stamp;
1.15 jfb 246: char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.17 jfb 247: const char *comment;
1.18 jfb 248: struct stat fst;
249: struct timeval ts[2];
250: struct cvsroot *root;
1.15 jfb 251: RCSFILE *rf;
1.17 jfb 252: RCSNUM *rev;
1.15 jfb 253:
254: root = CVS_DIR_ROOT(cf);
1.24 xsa 255:
256: len = cvs_path_cat(root->cr_dir, module, repo, sizeof(repo));
257: if (len >= sizeof(repo))
1.15 jfb 258: return (CVS_EX_DATA);
259:
260: cvs_file_getpath(cf, fpath, sizeof(fpath));
261:
262: if (cf->cf_type == DT_DIR) {
263: if (!strcmp(cf->cf_name, "."))
264: strlcpy(rpath, repo, sizeof(rpath));
265: else {
1.24 xsa 266: len = cvs_path_cat(repo, fpath, rpath, sizeof(rpath));
267: if (len >= sizeof(rpath))
1.15 jfb 268: return (CVS_EX_DATA);
269:
270: cvs_printf("Importing %s\n", rpath);
271: if (mkdir(rpath, 0700) == -1) {
272: cvs_log(LP_ERRNO, "failed to create %s",
273: rpath);
274: }
1.3 jfb 275: }
1.1 krapht 276:
1.3 jfb 277: return (0);
1.1 krapht 278: }
279:
1.18 jfb 280: /*
281: * If -d was given, use the file's last modification time as the
282: * timestamps for the initial revisions.
283: */
1.28 ! xsa 284: if (dflag == 1) {
1.18 jfb 285: if (stat(fpath, &fst) == -1) {
286: cvs_log(LP_ERRNO, "failed to stat %s", fpath);
287: return (CVS_EX_DATA);
288: }
289: stamp = (time_t)fst.st_mtime;
290:
291: ts[0].tv_sec = stamp;
292: ts[0].tv_usec = 0;
293: ts[1].tv_sec = stamp;
294: ts[1].tv_usec = 0;
295: } else
296: stamp = -1;
297:
1.28 ! xsa 298: l = snprintf(rpath, sizeof(rpath), "%s/%s%s",
1.15 jfb 299: repo, fpath, RCS_FILE_EXT);
1.28 ! xsa 300: if (l == -1 || l >= (int)sizeof(rpath)) {
! 301: errno = ENAMETOOLONG;
! 302: cvs_log(LP_ERRNO, "%s", rpath);
! 303: return (CVS_EX_DATA);
! 304: }
1.15 jfb 305:
306: cvs_printf("N %s\n", fpath);
307:
308: rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE);
309: if (rf == NULL) {
1.17 jfb 310: cvs_log(LP_ERR, "failed to create RCS file: %s",
311: strerror(rcs_errno));
312: return (CVS_EX_DATA);
313: }
314:
315: comment = rcs_comment_lookup(cf->cf_name);
316: if ((comment != NULL) && (rcs_comment_set(rf, comment) < 0)) {
1.18 jfb 317: cvs_log(LP_WARN, "failed to set RCS comment leader: %s",
1.17 jfb 318: rcs_errstr(rcs_errno));
1.18 jfb 319: /* don't error out, no big deal */
1.17 jfb 320: }
321:
1.20 jfb 322: rev = rcsnum_brtorev(imp_brnum);
1.27 niallo 323: if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
1.17 jfb 324: cvs_log(LP_ERR, "failed to add revision: %s",
325: rcs_errstr(rcs_errno));
326: rcs_close(rf);
327: (void)unlink(rpath);
328: return (CVS_EX_DATA);
329: }
1.18 jfb 330:
331: if (rcs_sym_add(rf, release, rev) < 0) {
332: cvs_log(LP_ERR, "failed to set RCS symbol: %s",
333: strerror(rcs_errno));
334: rcs_close(rf);
335: (void)unlink(rpath);
336: return (CVS_EX_DATA);
337: }
338:
1.20 jfb 339: rcsnum_cpy(imp_brnum, rev, 2);
1.27 niallo 340: if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
1.17 jfb 341: cvs_log(LP_ERR, "failed to add revision: %s",
342: rcs_errstr(rcs_errno));
343: rcs_close(rf);
344: (void)unlink(rpath);
345: return (CVS_EX_DATA);
346: }
347:
348: if (rcs_head_set(rf, rev) < 0) {
349: cvs_log(LP_ERR, "failed to set RCS head: %s",
350: rcs_errstr(rcs_errno));
351: rcs_close(rf);
352: (void)unlink(rpath);
353: return (CVS_EX_DATA);
1.1 krapht 354: }
355:
1.19 jfb 356: if (rcs_branch_set(rf, imp_brnum) < 0) {
1.18 jfb 357: cvs_log(LP_ERR, "failed to set RCS default branch: %s",
358: strerror(rcs_errno));
1.20 jfb 359: return (CVS_EX_DATA);
360: }
361:
362: if (rcs_sym_add(rf, vendor, imp_brnum) < 0) {
363: cvs_log(LP_ERR, "failed to set RCS symbol: %s",
364: strerror(rcs_errno));
365: rcs_close(rf);
366: (void)unlink(rpath);
1.18 jfb 367: return (CVS_EX_DATA);
368: }
369:
370: /* add the vendor tag and release tag as symbols */
1.15 jfb 371: rcs_close(rf);
1.18 jfb 372:
1.28 ! xsa 373: if ((dflag ==1) && (utimes(rpath, ts) == -1))
1.18 jfb 374: cvs_log(LP_ERRNO, "failed to timestamp RCS file");
1.15 jfb 375:
376: return (CVS_EX_OK);
1.19 jfb 377: }
378:
379: static int
380: cvs_import_cleanup(void)
381: {
382: if (imp_brnum != NULL)
383: rcsnum_free(imp_brnum);
384: return (0);
1.1 krapht 385: }