Annotation of src/usr.bin/cvs/import.c, Revision 1.4
1.4 ! jfb 1: /* $OpenBSD: import.c,v 1.3 2005/01/06 19:56:38 jfb Exp $ */
1.1 krapht 2: /*
3: * Copyright (c) 2004 Joris Vink <amni@pandora.be>
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.3 jfb 61: char *branch, *ep;
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;
72: if ((bnum = rcsnum_alloc()) == NULL)
73: return (-1);
74: if ((rcsnum_aton(branch, &ep, bnum) < 0) ||
75: (*ep != '\0')) {
76: cvs_log(LP_ERR, "%s is not a numeric branch",
77: branch);
78: return (EX_USAGE);
79: }
80: break;
1.1 krapht 81: case 'd':
82: break;
83: case 'I':
84: if (cvs_file_ignore(optarg) < 0) {
85: cvs_log(LP_ERR, "failed to add `%s' to list "
86: "of ignore patterns", optarg);
87: return (EX_USAGE);
88: }
89: break;
90: case 'k':
91: break;
92: case 'm':
1.4 ! jfb 93: cvs_msg = strdup(optarg);
! 94: if (cvs_msg == NULL) {
! 95: cvs_log(LP_ERRNO, "failed to copy message");
! 96: return (EX_DATAERR);
! 97: }
1.1 krapht 98: break;
99: default:
100: return (EX_USAGE);
101: }
102: }
103:
104: argc -= optind;
105: argv += optind;
106: if (argc > 4)
107: return (EX_USAGE);
108:
109: cvs_files = cvs_file_get(".", flags);
110: if (cvs_files == NULL)
111: return (EX_DATAERR);
112:
113: root = CVS_DIR_ROOT(cvs_files);
1.3 jfb 114: if (root == NULL) {
115: cvs_log(LP_ERR,
116: "No CVSROOT specified! Please use the `-d' option");
117: cvs_log(LP_ERR,
118: "or set the CVSROOT environment variable.");
119: return (EX_USAGE);
1.1 krapht 120: }
121:
1.3 jfb 122: if ((cvs_msg == NULL) &&
123: (cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL)) == NULL)
124: return (-1);
1.1 krapht 125:
1.3 jfb 126: if (root->cr_method != CVS_METHOD_LOCAL) {
127: if ((cvs_connect(root) < 0) ||
128: (cvs_sendarg(root, "-b", 0) < 0) ||
129: (cvs_sendarg(root, branch, 0) < 0) ||
130: (cvs_logmsg_send(root, cvs_msg) < 0) ||
131: (cvs_sendarg(root, argv[0], 0) < 0) ||
132: (cvs_sendarg(root, argv[1], 0) < 0) ||
133: (cvs_sendarg(root, argv[2], 0) < 0))
134: return (EX_PROTOCOL);
1.1 krapht 135: }
136:
1.3 jfb 137: snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, argv[0]);
138: cvs_file_examine(cvs_files, cvs_import_file, NULL);
1.1 krapht 139:
1.3 jfb 140: if (root->cr_method != CVS_METHOD_LOCAL) {
141: if (cvs_senddir(root, cvs_files) < 0 ||
142: cvs_sendreq(root, CVS_REQ_IMPORT, NULL) < 0)
143: return (EX_PROTOCOL);
1.1 krapht 144: }
145:
146: return (0);
147: }
148:
1.3 jfb 149: /*
150: * cvs_import_file()
151: *
152: * Perform the import of a single file or directory.
153: */
154: int
155: cvs_import_file(CVSFILE *cfp, void *arg)
1.1 krapht 156: {
1.3 jfb 157: int ret;
158: struct cvsroot *root;
159: char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.1 krapht 160:
1.3 jfb 161: root = CVS_DIR_ROOT(cfp);
1.1 krapht 162:
1.3 jfb 163: cvs_file_getpath(cfp, fpath, sizeof(fpath));
164: printf("Importing %s\n", fpath);
1.1 krapht 165:
1.3 jfb 166: if (cfp->cf_type == DT_DIR) {
167: if (!strcmp(CVS_FILE_NAME(cfp), "."))
168: strlcpy(repodir, repo, sizeof(repodir));
169: else
170: snprintf(repodir, sizeof(repodir), "%s/%s", repo, fpath);
171: if (root->cr_method != CVS_METHOD_LOCAL) {
172: ret = cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath);
173: if (ret == 0)
174: ret = cvs_sendln(root, repodir);
175: } else {
176: /* create the directory */
177: }
1.1 krapht 178:
1.3 jfb 179: return (0);
1.1 krapht 180: }
181:
1.3 jfb 182: if (root->cr_method != CVS_METHOD_LOCAL) {
183: if (cvs_sendreq(root, CVS_REQ_MODIFIED, CVS_FILE_NAME(cfp)) < 0)
184: return (-1);
185: if (cvs_sendfile(root, fpath) < 0)
186: return (-1);
187: } else {
188: /* local import */
1.1 krapht 189: }
190:
191: return (0);
192: }