Annotation of src/usr.bin/cvs/import.c, Revision 1.14
1.14 ! joris 1: /* $OpenBSD: import.c,v 1.13 2005/04/25 16:29:41 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>
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:
38: #include "log.h"
39: #include "file.h"
1.3 jfb 40: #include "cvs.h"
1.1 krapht 41: #include "proto.h"
42:
1.3 jfb 43:
44: #define CVS_IMPORT_DEFBRANCH "1.1.1"
45:
1.7 joris 46: int cvs_import_options(char *, int, char **, int *);
47: int cvs_import_sendflags(struct cvsroot *);
48: int cvs_import_file(CVSFILE *, void *);
1.3 jfb 49:
1.7 joris 50: static RCSNUM *bnum;
51: static char *branch, *module, *vendor, *release;
1.3 jfb 52:
1.7 joris 53: struct cvs_cmd_info cvs_import = {
54: cvs_import_options,
55: cvs_import_sendflags,
56: cvs_import_file,
57: NULL, NULL,
58: CF_RECURSE | CF_IGNORE | CF_NOSYMS,
59: CVS_REQ_IMPORT,
60: CVS_CMD_SENDDIR
61: };
1.1 krapht 62:
63: int
1.7 joris 64: cvs_import_options(char *opt, int argc, char **argv, int *arg)
1.1 krapht 65: {
1.7 joris 66: int ch;
1.1 krapht 67:
1.13 jfb 68: branch = CVS_IMPORT_DEFBRANCH;
69:
1.7 joris 70: while ((ch = getopt(argc, argv, opt)) != -1) {
1.1 krapht 71: switch (ch) {
72: case 'b':
1.3 jfb 73: branch = optarg;
1.6 joris 74: if ((bnum = rcsnum_parse(branch)) == NULL) {
1.3 jfb 75: cvs_log(LP_ERR, "%s is not a numeric branch",
76: branch);
1.11 joris 77: return (CVS_EX_USAGE);
1.3 jfb 78: }
1.6 joris 79: rcsnum_free(bnum);
1.3 jfb 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);
1.11 joris 87: return (CVS_EX_USAGE);
1.1 krapht 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");
1.11 joris 96: return (CVS_EX_DATA);
1.4 jfb 97: }
1.1 krapht 98: break;
99: default:
1.11 joris 100: return (CVS_EX_USAGE);
1.1 krapht 101: }
102: }
103:
104: argc -= optind;
105: argv += optind;
1.7 joris 106: *arg = optind;
107:
1.1 krapht 108: if (argc > 4)
1.11 joris 109: return (CVS_EX_USAGE);
1.1 krapht 110:
1.7 joris 111: module = argv[0];
112: vendor = argv[1];
113: release = argv[2];
1.1 krapht 114:
1.3 jfb 115: if ((cvs_msg == NULL) &&
116: (cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL)) == NULL)
1.11 joris 117: return (CVS_EX_DATA);
1.1 krapht 118:
1.7 joris 119: return (0);
120: }
1.1 krapht 121:
1.7 joris 122: int
123: cvs_import_sendflags(struct cvsroot *root)
124: {
1.13 jfb 125: if ((cvs_sendarg(root, "-b", 0) < 0) ||
1.7 joris 126: (cvs_sendarg(root, branch, 0) < 0) ||
127: (cvs_logmsg_send(root, cvs_msg) < 0) ||
128: (cvs_sendarg(root, module, 0) < 0) ||
129: (cvs_sendarg(root, vendor, 0) < 0) ||
130: (cvs_sendarg(root, release, 0) < 0))
1.11 joris 131: return (CVS_EX_PROTO);
1.1 krapht 132:
133: return (0);
134: }
135:
1.3 jfb 136: /*
137: * cvs_import_file()
138: *
139: * Perform the import of a single file or directory.
140: */
141: int
142: cvs_import_file(CVSFILE *cfp, void *arg)
1.1 krapht 143: {
1.14 ! joris 144: int l;
1.3 jfb 145: struct cvsroot *root;
146: char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.7 joris 147: char repo[MAXPATHLEN];
1.1 krapht 148:
1.3 jfb 149: root = CVS_DIR_ROOT(cfp);
1.12 xsa 150: l = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
151: if (l == -1 || l >= (int)sizeof(repo)) {
152: errno = ENAMETOOLONG;
153: cvs_log(LP_ERRNO, "%s", repo);
1.14 ! joris 154: return (CVS_EX_DATA);
1.12 xsa 155: }
1.1 krapht 156:
1.3 jfb 157: cvs_file_getpath(cfp, fpath, sizeof(fpath));
158: printf("Importing %s\n", fpath);
1.1 krapht 159:
1.3 jfb 160: if (cfp->cf_type == DT_DIR) {
161: if (!strcmp(CVS_FILE_NAME(cfp), "."))
162: strlcpy(repodir, repo, sizeof(repodir));
1.12 xsa 163: else {
164: l = snprintf(repodir, sizeof(repodir), "%s/%s",
165: repo, fpath);
166: if (l == -1 || l >= (int)sizeof(repodir)) {
167: errno = ENAMETOOLONG;
168: cvs_log(LP_ERRNO, "%s", repodir);
1.14 ! joris 169: return (CVS_EX_DATA);
1.12 xsa 170: }
171: }
1.3 jfb 172: if (root->cr_method != CVS_METHOD_LOCAL) {
1.14 ! joris 173: if (cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath) < 0)
! 174: return (CVS_EX_PROTO);
! 175: if (cvs_sendln(root, repodir) < 0)
! 176: return (CVS_EX_PROTO);
1.3 jfb 177: } else {
178: /* create the directory */
179: }
1.1 krapht 180:
1.3 jfb 181: return (0);
1.1 krapht 182: }
183:
1.3 jfb 184: if (root->cr_method != CVS_METHOD_LOCAL) {
185: if (cvs_sendreq(root, CVS_REQ_MODIFIED, CVS_FILE_NAME(cfp)) < 0)
1.11 joris 186: return (CVS_EX_PROTO);
1.3 jfb 187: if (cvs_sendfile(root, fpath) < 0)
1.11 joris 188: return (CVS_EX_PROTO);
1.3 jfb 189: } else {
190: /* local import */
1.1 krapht 191: }
192:
193: return (0);
194: }