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