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