Annotation of src/usr.bin/cvs/import.c, Revision 1.19
1.19 ! jfb 1: /* $OpenBSD: import.c,v 1.18 2005/05/25 21:47:19 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.7 joris 193: char repo[MAXPATHLEN];
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:
225: if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0)
226: return (CVS_EX_PROTO);
227: if (cvs_sendfile(root, fpath) < 0)
228: return (CVS_EX_PROTO);
229:
230: return (0);
231: }
232:
233: static int
234: cvs_import_local(CVSFILE *cf, void *arg)
235: {
236: int len;
1.18 jfb 237: time_t stamp;
1.15 jfb 238: char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.17 jfb 239: const char *comment;
1.18 jfb 240: struct stat fst;
241: struct timeval ts[2];
242: struct cvsroot *root;
1.15 jfb 243: RCSFILE *rf;
1.17 jfb 244: RCSNUM *rev;
1.15 jfb 245:
246: root = CVS_DIR_ROOT(cf);
247: len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
248: if (len == -1 || len >= (int)sizeof(repo)) {
249: errno = ENAMETOOLONG;
250: cvs_log(LP_ERRNO, "%s", repo);
251: return (CVS_EX_DATA);
252: }
253:
254: cvs_file_getpath(cf, fpath, sizeof(fpath));
255:
256: if (cf->cf_type == DT_DIR) {
257: if (!strcmp(cf->cf_name, "."))
258: strlcpy(rpath, repo, sizeof(rpath));
259: else {
260: len = snprintf(rpath, sizeof(rpath), "%s/%s",
261: repo, fpath);
262: if (len == -1 || len >= (int)sizeof(rpath)) {
263: errno = ENAMETOOLONG;
264: cvs_log(LP_ERRNO, "%s", rpath);
265: return (CVS_EX_DATA);
266: }
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: */
282: if (dflag) {
283: if (stat(fpath, &fst) == -1) {
284: cvs_log(LP_ERRNO, "failed to stat %s", fpath);
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.15 jfb 296: snprintf(rpath, sizeof(rpath), "%s/%s%s",
297: repo, fpath, RCS_FILE_EXT);
298:
299: cvs_printf("N %s\n", fpath);
300:
301: rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE);
302: if (rf == NULL) {
1.17 jfb 303: cvs_log(LP_ERR, "failed to create RCS file: %s",
304: strerror(rcs_errno));
305: return (CVS_EX_DATA);
306: }
307:
308: comment = rcs_comment_lookup(cf->cf_name);
309: if ((comment != NULL) && (rcs_comment_set(rf, comment) < 0)) {
1.18 jfb 310: cvs_log(LP_WARN, "failed to set RCS comment leader: %s",
1.17 jfb 311: rcs_errstr(rcs_errno));
1.18 jfb 312: /* don't error out, no big deal */
1.17 jfb 313: }
314:
315: /* first add the magic 1.1.1.1 revision */
316: rev = rcsnum_parse("1.1.1.1");
1.18 jfb 317: if (rcs_rev_add(rf, rev, cvs_msg, stamp) < 0) {
1.17 jfb 318: cvs_log(LP_ERR, "failed to add revision: %s",
319: rcs_errstr(rcs_errno));
320: rcs_close(rf);
321: (void)unlink(rpath);
322: return (CVS_EX_DATA);
323: }
1.18 jfb 324:
325: if (rcs_sym_add(rf, release, rev) < 0) {
326: cvs_log(LP_ERR, "failed to set RCS symbol: %s",
327: strerror(rcs_errno));
328: rcs_close(rf);
329: (void)unlink(rpath);
330: return (CVS_EX_DATA);
331: }
332:
1.17 jfb 333: rcsnum_free(rev);
334:
335: rev = rcsnum_parse(RCS_HEAD_INIT);
1.18 jfb 336: if (rcs_rev_add(rf, rev, cvs_msg, stamp) < 0) {
1.17 jfb 337: cvs_log(LP_ERR, "failed to add revision: %s",
338: rcs_errstr(rcs_errno));
339: rcs_close(rf);
340: (void)unlink(rpath);
341: return (CVS_EX_DATA);
342: }
343:
344: if (rcs_head_set(rf, rev) < 0) {
345: cvs_log(LP_ERR, "failed to set RCS head: %s",
346: rcs_errstr(rcs_errno));
347: rcs_close(rf);
348: (void)unlink(rpath);
349: return (CVS_EX_DATA);
1.1 krapht 350: }
351:
1.19 ! jfb 352: if (rcs_branch_set(rf, imp_brnum) < 0) {
1.18 jfb 353: cvs_log(LP_ERR, "failed to set RCS default branch: %s",
354: strerror(rcs_errno));
355: return (CVS_EX_DATA);
356: }
357:
358: /* add the vendor tag and release tag as symbols */
1.15 jfb 359: rcs_close(rf);
1.18 jfb 360:
361: if (dflag && (utimes(rpath, ts) == -1))
362: cvs_log(LP_ERRNO, "failed to timestamp RCS file");
1.15 jfb 363:
364: return (CVS_EX_OK);
1.19 ! jfb 365: }
! 366:
! 367: static int
! 368: cvs_import_cleanup(void)
! 369: {
! 370: if (imp_brnum != NULL)
! 371: rcsnum_free(imp_brnum);
! 372: return (0);
1.1 krapht 373: }