Annotation of src/usr.bin/cvs/import.c, Revision 1.17
1.17 ! jfb 1: /* $OpenBSD: import.c,v 1.16 2005/05/24 04:21:54 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.15 jfb 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_remote (CVSFILE *, void *);
47: static int cvs_import_local (CVSFILE *, void *);
1.3 jfb 48:
1.7 joris 49: static RCSNUM *bnum;
50: static char *branch, *module, *vendor, *release;
1.3 jfb 51:
1.15 jfb 52: struct cvs_cmd cvs_cmd_import = {
53: CVS_OP_IMPORT, CVS_REQ_IMPORT, "import",
54: { "im", "imp" },
55: "Import sources into CVS, using vendor branches",
56: "[-d] [-b branch] [-I ign] [-k subst] [-m msg] repository "
57: "vendor-tag release-tags ...",
58: "b:dI:k:m:",
59: NULL,
1.7 joris 60: CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.15 jfb 61: cvs_import_init,
62: cvs_import_pre_exec,
63: cvs_import_remote,
64: cvs_import_local,
65: NULL,
66: NULL,
1.7 joris 67: CVS_CMD_SENDDIR
68: };
1.1 krapht 69:
1.15 jfb 70: static int
71: cvs_import_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 krapht 72: {
1.7 joris 73: int ch;
1.1 krapht 74:
1.13 jfb 75: branch = CVS_IMPORT_DEFBRANCH;
76:
1.15 jfb 77: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 krapht 78: switch (ch) {
79: case 'b':
1.3 jfb 80: branch = optarg;
1.6 joris 81: if ((bnum = rcsnum_parse(branch)) == NULL) {
1.3 jfb 82: cvs_log(LP_ERR, "%s is not a numeric branch",
83: branch);
1.11 joris 84: return (CVS_EX_USAGE);
1.3 jfb 85: }
1.6 joris 86: rcsnum_free(bnum);
1.3 jfb 87: break;
1.1 krapht 88: case 'd':
89: break;
90: case 'I':
91: if (cvs_file_ignore(optarg) < 0) {
92: cvs_log(LP_ERR, "failed to add `%s' to list "
93: "of ignore patterns", optarg);
1.11 joris 94: return (CVS_EX_USAGE);
1.1 krapht 95: }
96: break;
97: case 'k':
98: break;
99: case 'm':
1.4 jfb 100: cvs_msg = strdup(optarg);
101: if (cvs_msg == NULL) {
102: cvs_log(LP_ERRNO, "failed to copy message");
1.11 joris 103: return (CVS_EX_DATA);
1.4 jfb 104: }
1.1 krapht 105: break;
106: default:
1.11 joris 107: return (CVS_EX_USAGE);
1.1 krapht 108: }
109: }
110:
111: argc -= optind;
112: argv += optind;
1.7 joris 113:
1.15 jfb 114: if (argc != 3)
1.11 joris 115: return (CVS_EX_USAGE);
1.1 krapht 116:
1.7 joris 117: module = argv[0];
118: vendor = argv[1];
119: release = argv[2];
1.1 krapht 120:
1.15 jfb 121: *arg = optind + 3;
122:
1.3 jfb 123: if ((cvs_msg == NULL) &&
124: (cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL)) == NULL)
1.11 joris 125: return (CVS_EX_DATA);
1.1 krapht 126:
1.7 joris 127: return (0);
128: }
1.1 krapht 129:
1.15 jfb 130: static int
131: cvs_import_pre_exec(struct cvsroot *root)
1.7 joris 132: {
1.15 jfb 133: char repodir[MAXPATHLEN];
134:
135: if (root->cr_method == CVS_METHOD_LOCAL) {
136: snprintf(repodir, sizeof(repodir), "%s/%s", root->cr_dir,
137: module);
138: mkdir(repodir, 0700);
139: } else {
140: if ((cvs_sendarg(root, "-b", 0) < 0) ||
141: (cvs_sendarg(root, branch, 0) < 0) ||
142: (cvs_logmsg_send(root, cvs_msg) < 0) ||
143: (cvs_sendarg(root, module, 0) < 0) ||
144: (cvs_sendarg(root, vendor, 0) < 0) ||
145: (cvs_sendarg(root, release, 0) < 0))
146: return (CVS_EX_PROTO);
147: }
1.1 krapht 148:
149: return (0);
150: }
151:
1.3 jfb 152: /*
1.15 jfb 153: * cvs_import_remote()
1.3 jfb 154: *
155: * Perform the import of a single file or directory.
156: */
1.15 jfb 157: static int
158: cvs_import_remote(CVSFILE *cf, void *arg)
1.1 krapht 159: {
1.15 jfb 160: int len;
1.3 jfb 161: struct cvsroot *root;
162: char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.7 joris 163: char repo[MAXPATHLEN];
1.1 krapht 164:
1.15 jfb 165: root = CVS_DIR_ROOT(cf);
166: len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
167: if (len == -1 || len >= (int)sizeof(repo)) {
1.12 xsa 168: errno = ENAMETOOLONG;
169: cvs_log(LP_ERRNO, "%s", repo);
1.14 joris 170: return (CVS_EX_DATA);
1.12 xsa 171: }
1.1 krapht 172:
1.15 jfb 173: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1 krapht 174:
1.15 jfb 175: if (cf->cf_type == DT_DIR) {
176: if (!strcmp(cf->cf_name, "."))
1.3 jfb 177: strlcpy(repodir, repo, sizeof(repodir));
1.12 xsa 178: else {
1.15 jfb 179: len = snprintf(repodir, sizeof(repodir), "%s/%s",
1.12 xsa 180: repo, fpath);
1.15 jfb 181: if (len == -1 || len >= (int)sizeof(repodir)) {
1.12 xsa 182: errno = ENAMETOOLONG;
183: cvs_log(LP_ERRNO, "%s", repodir);
1.14 joris 184: return (CVS_EX_DATA);
1.12 xsa 185: }
186: }
1.15 jfb 187:
188: if (cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath) < 0)
189: return (CVS_EX_PROTO);
190: if (cvs_sendln(root, repodir) < 0)
191: return (CVS_EX_PROTO);
192: return (0);
193: }
194:
195: if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0)
196: return (CVS_EX_PROTO);
197: if (cvs_sendfile(root, fpath) < 0)
198: return (CVS_EX_PROTO);
199:
200: return (0);
201: }
202:
203:
204: /*
205: * cvs_import_local()
206: *
207: */
208: static int
209: cvs_import_local(CVSFILE *cf, void *arg)
210: {
211: int len;
212: struct cvsroot *root;
213: char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.17 ! jfb 214: const char *comment;
1.15 jfb 215: RCSFILE *rf;
1.17 ! jfb 216: RCSNUM *rev;
1.15 jfb 217:
218: root = CVS_DIR_ROOT(cf);
219: len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
220: if (len == -1 || len >= (int)sizeof(repo)) {
221: errno = ENAMETOOLONG;
222: cvs_log(LP_ERRNO, "%s", repo);
223: return (CVS_EX_DATA);
224: }
225:
226: cvs_file_getpath(cf, fpath, sizeof(fpath));
227:
228: if (cf->cf_type == DT_DIR) {
229: if (!strcmp(cf->cf_name, "."))
230: strlcpy(rpath, repo, sizeof(rpath));
231: else {
232: len = snprintf(rpath, sizeof(rpath), "%s/%s",
233: repo, fpath);
234: if (len == -1 || len >= (int)sizeof(rpath)) {
235: errno = ENAMETOOLONG;
236: cvs_log(LP_ERRNO, "%s", rpath);
237: return (CVS_EX_DATA);
238: }
239:
240: cvs_printf("Importing %s\n", rpath);
241: if (mkdir(rpath, 0700) == -1) {
242: cvs_log(LP_ERRNO, "failed to create %s",
243: rpath);
244: }
1.3 jfb 245: }
1.1 krapht 246:
1.3 jfb 247: return (0);
1.1 krapht 248: }
249:
1.15 jfb 250: snprintf(rpath, sizeof(rpath), "%s/%s%s",
251: repo, fpath, RCS_FILE_EXT);
252:
253: cvs_printf("N %s\n", fpath);
254:
255: rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE);
256: if (rf == NULL) {
1.17 ! jfb 257: cvs_log(LP_ERR, "failed to create RCS file: %s",
! 258: strerror(rcs_errno));
! 259: return (CVS_EX_DATA);
! 260: }
! 261:
! 262: comment = rcs_comment_lookup(cf->cf_name);
! 263: if ((comment != NULL) && (rcs_comment_set(rf, comment) < 0)) {
! 264: cvs_log(LP_ERR, "failed to set RCS comment leader: %s",
! 265: rcs_errstr(rcs_errno));
! 266: }
! 267:
! 268: /* first add the magic 1.1.1.1 revision */
! 269: rev = rcsnum_parse("1.1.1.1");
! 270: if (rcs_rev_add(rf, rev, cvs_msg) < 0) {
! 271: cvs_log(LP_ERR, "failed to add revision: %s",
! 272: rcs_errstr(rcs_errno));
! 273: rcs_close(rf);
! 274: (void)unlink(rpath);
! 275: return (CVS_EX_DATA);
! 276: }
! 277: rcsnum_free(rev);
! 278:
! 279: rev = rcsnum_parse(RCS_HEAD_INIT);
! 280: if (rcs_rev_add(rf, rev, cvs_msg) < 0) {
! 281: cvs_log(LP_ERR, "failed to add revision: %s",
! 282: rcs_errstr(rcs_errno));
! 283: rcs_close(rf);
! 284: (void)unlink(rpath);
! 285: return (CVS_EX_DATA);
! 286: }
! 287:
! 288: if (rcs_head_set(rf, rev) < 0) {
! 289: cvs_log(LP_ERR, "failed to set RCS head: %s",
! 290: rcs_errstr(rcs_errno));
! 291: rcs_close(rf);
! 292: (void)unlink(rpath);
! 293: return (CVS_EX_DATA);
1.1 krapht 294: }
295:
1.15 jfb 296: rcs_close(rf);
297:
298: return (CVS_EX_OK);
1.1 krapht 299: }