Annotation of src/usr.bin/cvs/import.c, Revision 1.6
1.6 ! joris 1: /* $OpenBSD: import.c,v 1.5 2005/02/28 20:18:02 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>
28: #include <sys/queue.h>
29:
30: #include <err.h>
31: #include <dirent.h>
32: #include <errno.h>
33: #include <stdio.h>
34: #include <stdlib.h>
35: #include <unistd.h>
36: #include <string.h>
37: #include <sysexits.h>
38:
39: #include "log.h"
40: #include "file.h"
1.3 jfb 41: #include "cvs.h"
1.1 krapht 42: #include "proto.h"
43:
1.3 jfb 44:
45: #define CVS_IMPORT_DEFBRANCH "1.1.1"
46:
47:
48:
49: int cvs_import_file(CVSFILE *, void *);
50: char repo[MAXPATHLEN];
1.1 krapht 51:
52: /*
53: * cvs_import()
54: *
55: * Handler for the `cvs import' command.
56: */
57: int
58: cvs_import(int argc, char **argv)
59: {
60: int ch, flags;
1.6 ! joris 61: char *branch;
1.1 krapht 62: struct cvsroot *root;
1.3 jfb 63: RCSNUM *bnum;
1.1 krapht 64:
1.3 jfb 65: branch = CVS_IMPORT_DEFBRANCH;
66: flags = CF_RECURSE | CF_IGNORE | CF_NOSYMS;
1.1 krapht 67:
68: while ((ch = getopt(argc, argv, "b:dI:k:m:")) != -1) {
69: switch (ch) {
70: case 'b':
1.3 jfb 71: branch = optarg;
1.6 ! joris 72: if ((bnum = rcsnum_parse(branch)) == NULL) {
1.3 jfb 73: cvs_log(LP_ERR, "%s is not a numeric branch",
74: branch);
75: return (EX_USAGE);
76: }
1.6 ! joris 77: rcsnum_free(bnum);
1.3 jfb 78: break;
1.1 krapht 79: case 'd':
80: break;
81: case 'I':
82: if (cvs_file_ignore(optarg) < 0) {
83: cvs_log(LP_ERR, "failed to add `%s' to list "
84: "of ignore patterns", optarg);
85: return (EX_USAGE);
86: }
87: break;
88: case 'k':
89: break;
90: case 'm':
1.4 jfb 91: cvs_msg = strdup(optarg);
92: if (cvs_msg == NULL) {
93: cvs_log(LP_ERRNO, "failed to copy message");
94: return (EX_DATAERR);
95: }
1.1 krapht 96: break;
97: default:
98: return (EX_USAGE);
99: }
100: }
101:
102: argc -= optind;
103: argv += optind;
104: if (argc > 4)
105: return (EX_USAGE);
106:
107: cvs_files = cvs_file_get(".", flags);
108: if (cvs_files == NULL)
109: return (EX_DATAERR);
110:
111: root = CVS_DIR_ROOT(cvs_files);
1.3 jfb 112: if (root == NULL) {
113: cvs_log(LP_ERR,
114: "No CVSROOT specified! Please use the `-d' option");
115: cvs_log(LP_ERR,
116: "or set the CVSROOT environment variable.");
117: return (EX_USAGE);
1.1 krapht 118: }
119:
1.3 jfb 120: if ((cvs_msg == NULL) &&
121: (cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL)) == NULL)
122: return (-1);
1.1 krapht 123:
1.3 jfb 124: if (root->cr_method != CVS_METHOD_LOCAL) {
125: if ((cvs_connect(root) < 0) ||
126: (cvs_sendarg(root, "-b", 0) < 0) ||
127: (cvs_sendarg(root, branch, 0) < 0) ||
128: (cvs_logmsg_send(root, cvs_msg) < 0) ||
129: (cvs_sendarg(root, argv[0], 0) < 0) ||
130: (cvs_sendarg(root, argv[1], 0) < 0) ||
131: (cvs_sendarg(root, argv[2], 0) < 0))
132: return (EX_PROTOCOL);
1.1 krapht 133: }
134:
1.3 jfb 135: snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, argv[0]);
136: cvs_file_examine(cvs_files, cvs_import_file, NULL);
1.1 krapht 137:
1.3 jfb 138: if (root->cr_method != CVS_METHOD_LOCAL) {
139: if (cvs_senddir(root, cvs_files) < 0 ||
140: cvs_sendreq(root, CVS_REQ_IMPORT, NULL) < 0)
141: return (EX_PROTOCOL);
1.1 krapht 142: }
143:
144: return (0);
145: }
146:
1.3 jfb 147: /*
148: * cvs_import_file()
149: *
150: * Perform the import of a single file or directory.
151: */
152: int
153: cvs_import_file(CVSFILE *cfp, void *arg)
1.1 krapht 154: {
1.3 jfb 155: int ret;
156: struct cvsroot *root;
157: char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.1 krapht 158:
1.3 jfb 159: root = CVS_DIR_ROOT(cfp);
1.1 krapht 160:
1.3 jfb 161: cvs_file_getpath(cfp, fpath, sizeof(fpath));
162: printf("Importing %s\n", fpath);
1.1 krapht 163:
1.3 jfb 164: if (cfp->cf_type == DT_DIR) {
165: if (!strcmp(CVS_FILE_NAME(cfp), "."))
166: strlcpy(repodir, repo, sizeof(repodir));
167: else
168: snprintf(repodir, sizeof(repodir), "%s/%s", repo, fpath);
169: if (root->cr_method != CVS_METHOD_LOCAL) {
170: ret = cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath);
171: if (ret == 0)
172: ret = cvs_sendln(root, repodir);
173: } else {
174: /* create the directory */
175: }
1.1 krapht 176:
1.3 jfb 177: return (0);
1.1 krapht 178: }
179:
1.3 jfb 180: if (root->cr_method != CVS_METHOD_LOCAL) {
181: if (cvs_sendreq(root, CVS_REQ_MODIFIED, CVS_FILE_NAME(cfp)) < 0)
182: return (-1);
183: if (cvs_sendfile(root, fpath) < 0)
184: return (-1);
185: } else {
186: /* local import */
1.1 krapht 187: }
188:
189: return (0);
190: }