Annotation of src/usr.bin/cvs/import.c, Revision 1.21
1.21 ! jfb 1: /* $OpenBSD: import.c,v 1.20 2005/05/26 02:35:13 jfb 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>
33: #include <unistd.h>
34: #include <string.h>
35:
36: #include "log.h"
37: #include "file.h"
1.3 jfb 38: #include "cvs.h"
1.1 krapht 39: #include "proto.h"
40:
1.3 jfb 41:
42: #define CVS_IMPORT_DEFBRANCH "1.1.1"
43:
1.19 jfb 44:
1.15 jfb 45: static int cvs_import_init (struct cvs_cmd *, int, char **, int *);
46: static int cvs_import_pre_exec (struct cvsroot *);
1.19 jfb 47: static int cvs_import_pre_exec (struct cvsroot *);
48: static int cvs_import_post_exec(struct cvsroot *);
1.15 jfb 49: static int cvs_import_remote (CVSFILE *, void *);
50: static int cvs_import_local (CVSFILE *, void *);
1.19 jfb 51: static int cvs_import_cleanup (void);
1.3 jfb 52:
1.18 jfb 53: static int dflag = 0;
1.19 jfb 54: static int conflicts = 0;
55: static RCSNUM *imp_brnum;
56:
57: static char *module, *vendor, *release;
1.3 jfb 58:
1.15 jfb 59: struct cvs_cmd cvs_cmd_import = {
60: CVS_OP_IMPORT, CVS_REQ_IMPORT, "import",
61: { "im", "imp" },
62: "Import sources into CVS, using vendor branches",
63: "[-d] [-b branch] [-I ign] [-k subst] [-m msg] repository "
64: "vendor-tag release-tags ...",
65: "b:dI:k:m:",
66: NULL,
1.7 joris 67: CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.15 jfb 68: cvs_import_init,
69: cvs_import_pre_exec,
70: cvs_import_remote,
71: cvs_import_local,
1.19 jfb 72: cvs_import_post_exec,
73: cvs_import_cleanup,
1.7 joris 74: CVS_CMD_SENDDIR
75: };
1.1 krapht 76:
1.15 jfb 77: static int
78: cvs_import_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 krapht 79: {
1.7 joris 80: int ch;
1.1 krapht 81:
1.15 jfb 82: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 krapht 83: switch (ch) {
84: case 'b':
1.19 jfb 85: if ((imp_brnum = rcsnum_parse(optarg)) == NULL) {
1.3 jfb 86: cvs_log(LP_ERR, "%s is not a numeric branch",
1.19 jfb 87: optarg);
1.11 joris 88: return (CVS_EX_USAGE);
1.3 jfb 89: }
90: break;
1.1 krapht 91: case 'd':
1.18 jfb 92: dflag = 1;
1.1 krapht 93: break;
94: case 'I':
95: if (cvs_file_ignore(optarg) < 0) {
96: cvs_log(LP_ERR, "failed to add `%s' to list "
97: "of ignore patterns", optarg);
1.11 joris 98: return (CVS_EX_USAGE);
1.1 krapht 99: }
100: break;
101: case 'k':
102: break;
103: case 'm':
1.4 jfb 104: cvs_msg = strdup(optarg);
105: if (cvs_msg == NULL) {
106: cvs_log(LP_ERRNO, "failed to copy message");
1.11 joris 107: return (CVS_EX_DATA);
1.4 jfb 108: }
1.1 krapht 109: break;
110: default:
1.11 joris 111: return (CVS_EX_USAGE);
1.1 krapht 112: }
113: }
114:
115: argc -= optind;
116: argv += optind;
1.15 jfb 117: if (argc != 3)
1.11 joris 118: return (CVS_EX_USAGE);
1.1 krapht 119:
1.19 jfb 120: if ((imp_brnum == NULL) &&
121: ((imp_brnum = rcsnum_parse(CVS_IMPORT_DEFBRANCH)) == NULL)) {
122: cvs_log(LP_ERR, "failed to parse default import branch");
123: return (CVS_EX_DATA);
124: }
125:
1.7 joris 126: module = argv[0];
127: vendor = argv[1];
128: release = argv[2];
1.1 krapht 129:
1.15 jfb 130: *arg = optind + 3;
131:
1.3 jfb 132: if ((cvs_msg == NULL) &&
133: (cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL)) == NULL)
1.11 joris 134: return (CVS_EX_DATA);
1.1 krapht 135:
1.7 joris 136: return (0);
137: }
1.1 krapht 138:
1.15 jfb 139: static int
140: cvs_import_pre_exec(struct cvsroot *root)
1.7 joris 141: {
1.19 jfb 142: char numbuf[64], repodir[MAXPATHLEN];
1.15 jfb 143:
144: if (root->cr_method == CVS_METHOD_LOCAL) {
145: snprintf(repodir, sizeof(repodir), "%s/%s", root->cr_dir,
146: module);
1.18 jfb 147: if (mkdir(repodir, 0700) == -1) {
148: cvs_log(LP_ERRNO, "failed to create %s", repodir);
149: return (CVS_EX_DATA);
150: }
1.15 jfb 151: } else {
1.19 jfb 152: rcsnum_tostr(imp_brnum, numbuf, sizeof(numbuf));
153:
1.15 jfb 154: if ((cvs_sendarg(root, "-b", 0) < 0) ||
1.19 jfb 155: (cvs_sendarg(root, numbuf, 0) < 0) ||
1.15 jfb 156: (cvs_logmsg_send(root, cvs_msg) < 0) ||
157: (cvs_sendarg(root, module, 0) < 0) ||
158: (cvs_sendarg(root, vendor, 0) < 0) ||
159: (cvs_sendarg(root, release, 0) < 0))
160: return (CVS_EX_PROTO);
161: }
1.1 krapht 162:
163: return (0);
164: }
165:
1.19 jfb 166: static int
167: cvs_import_post_exec(struct cvsroot *root)
168: {
169: char buf[8];
170:
171: if (root->cr_method == CVS_METHOD_LOCAL) {
172: if (conflicts > 0)
173: snprintf(buf, sizeof(buf), "%d", conflicts);
174:
175: cvs_printf("\n%s conflicts created by this import\n\n",
176: conflicts == 0 ? "No" : buf);
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.15 jfb 190: int len;
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);
196: len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
197: if (len == -1 || len >= (int)sizeof(repo)) {
1.12 xsa 198: errno = ENAMETOOLONG;
199: cvs_log(LP_ERRNO, "%s", repo);
1.14 joris 200: return (CVS_EX_DATA);
1.12 xsa 201: }
1.1 krapht 202:
1.15 jfb 203: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1 krapht 204:
1.15 jfb 205: if (cf->cf_type == DT_DIR) {
206: if (!strcmp(cf->cf_name, "."))
1.3 jfb 207: strlcpy(repodir, repo, sizeof(repodir));
1.12 xsa 208: else {
1.15 jfb 209: len = snprintf(repodir, sizeof(repodir), "%s/%s",
1.12 xsa 210: repo, fpath);
1.15 jfb 211: if (len == -1 || len >= (int)sizeof(repodir)) {
1.12 xsa 212: errno = ENAMETOOLONG;
213: cvs_log(LP_ERRNO, "%s", repodir);
1.14 joris 214: return (CVS_EX_DATA);
1.12 xsa 215: }
216: }
1.15 jfb 217:
218: if (cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath) < 0)
219: return (CVS_EX_PROTO);
220: if (cvs_sendln(root, repodir) < 0)
221: return (CVS_EX_PROTO);
222: return (0);
223: }
224:
1.21 ! jfb 225: if (dflag) {
! 226: ctime_r(&(cf->cf_mtime), date);
! 227: if (cvs_sendreq(root, CVS_REQ_CHECKINTIME, date) < 0)
! 228: return (CVS_EX_PROTO);
! 229: }
1.15 jfb 230: if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0)
231: return (CVS_EX_PROTO);
232: if (cvs_sendfile(root, fpath) < 0)
233: return (CVS_EX_PROTO);
234:
235: return (0);
236: }
237:
238: static int
239: cvs_import_local(CVSFILE *cf, void *arg)
240: {
241: int len;
1.18 jfb 242: time_t stamp;
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.15 jfb 250:
251: root = CVS_DIR_ROOT(cf);
252: len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
253: if (len == -1 || len >= (int)sizeof(repo)) {
254: errno = ENAMETOOLONG;
255: cvs_log(LP_ERRNO, "%s", repo);
256: return (CVS_EX_DATA);
257: }
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 {
265: len = snprintf(rpath, sizeof(rpath), "%s/%s",
266: repo, fpath);
267: if (len == -1 || len >= (int)sizeof(rpath)) {
268: errno = ENAMETOOLONG;
269: cvs_log(LP_ERRNO, "%s", rpath);
270: return (CVS_EX_DATA);
271: }
272:
273: cvs_printf("Importing %s\n", rpath);
274: if (mkdir(rpath, 0700) == -1) {
275: cvs_log(LP_ERRNO, "failed to create %s",
276: rpath);
277: }
1.3 jfb 278: }
1.1 krapht 279:
1.3 jfb 280: return (0);
1.1 krapht 281: }
282:
1.18 jfb 283: /*
284: * If -d was given, use the file's last modification time as the
285: * timestamps for the initial revisions.
286: */
287: if (dflag) {
288: if (stat(fpath, &fst) == -1) {
289: cvs_log(LP_ERRNO, "failed to stat %s", fpath);
290: return (CVS_EX_DATA);
291: }
292: stamp = (time_t)fst.st_mtime;
293:
294: ts[0].tv_sec = stamp;
295: ts[0].tv_usec = 0;
296: ts[1].tv_sec = stamp;
297: ts[1].tv_usec = 0;
298: } else
299: stamp = -1;
300:
1.15 jfb 301: snprintf(rpath, sizeof(rpath), "%s/%s%s",
302: repo, fpath, RCS_FILE_EXT);
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.18 jfb 321: if (rcs_rev_add(rf, rev, cvs_msg, stamp) < 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.18 jfb 338: if (rcs_rev_add(rf, rev, cvs_msg, stamp) < 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));
363: rcs_close(rf);
364: (void)unlink(rpath);
1.18 jfb 365: return (CVS_EX_DATA);
366: }
367:
368: /* add the vendor tag and release tag as symbols */
1.15 jfb 369: rcs_close(rf);
1.18 jfb 370:
371: if (dflag && (utimes(rpath, ts) == -1))
372: cvs_log(LP_ERRNO, "failed to timestamp RCS file");
1.15 jfb 373:
374: return (CVS_EX_OK);
1.19 jfb 375: }
376:
377: static int
378: cvs_import_cleanup(void)
379: {
380: if (imp_brnum != NULL)
381: rcsnum_free(imp_brnum);
382: return (0);
1.1 krapht 383: }