Annotation of src/usr.bin/cvs/import.c, Revision 1.15
1.15 ! jfb 1: /* $OpenBSD: import.c,v 1.14 2005/05/20 20:00:53 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>
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:
! 123: cvs_msg = "test\n";
! 124: #if 0
1.3 jfb 125: if ((cvs_msg == NULL) &&
126: (cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL)) == NULL)
1.11 joris 127: return (CVS_EX_DATA);
1.15 ! jfb 128: #endif
1.1 krapht 129:
1.7 joris 130: return (0);
131: }
1.1 krapht 132:
1.15 ! jfb 133: static int
! 134: cvs_import_pre_exec(struct cvsroot *root)
1.7 joris 135: {
1.15 ! jfb 136: char repodir[MAXPATHLEN];
! 137:
! 138: if (root->cr_method == CVS_METHOD_LOCAL) {
! 139: snprintf(repodir, sizeof(repodir), "%s/%s", root->cr_dir,
! 140: module);
! 141: mkdir(repodir, 0700);
! 142: } else {
! 143: if ((cvs_sendarg(root, "-b", 0) < 0) ||
! 144: (cvs_sendarg(root, branch, 0) < 0) ||
! 145: (cvs_logmsg_send(root, cvs_msg) < 0) ||
! 146: (cvs_sendarg(root, module, 0) < 0) ||
! 147: (cvs_sendarg(root, vendor, 0) < 0) ||
! 148: (cvs_sendarg(root, release, 0) < 0))
! 149: return (CVS_EX_PROTO);
! 150: }
1.1 krapht 151:
152: return (0);
153: }
154:
1.3 jfb 155: /*
1.15 ! jfb 156: * cvs_import_remote()
1.3 jfb 157: *
158: * Perform the import of a single file or directory.
159: */
1.15 ! jfb 160: static int
! 161: cvs_import_remote(CVSFILE *cf, void *arg)
1.1 krapht 162: {
1.15 ! jfb 163: int len;
1.3 jfb 164: struct cvsroot *root;
165: char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
1.7 joris 166: char repo[MAXPATHLEN];
1.1 krapht 167:
1.15 ! jfb 168: root = CVS_DIR_ROOT(cf);
! 169: len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
! 170: if (len == -1 || len >= (int)sizeof(repo)) {
1.12 xsa 171: errno = ENAMETOOLONG;
172: cvs_log(LP_ERRNO, "%s", repo);
1.14 joris 173: return (CVS_EX_DATA);
1.12 xsa 174: }
1.1 krapht 175:
1.15 ! jfb 176: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1 krapht 177:
1.15 ! jfb 178: if (cf->cf_type == DT_DIR) {
! 179: if (!strcmp(cf->cf_name, "."))
1.3 jfb 180: strlcpy(repodir, repo, sizeof(repodir));
1.12 xsa 181: else {
1.15 ! jfb 182: len = snprintf(repodir, sizeof(repodir), "%s/%s",
1.12 xsa 183: repo, fpath);
1.15 ! jfb 184: if (len == -1 || len >= (int)sizeof(repodir)) {
1.12 xsa 185: errno = ENAMETOOLONG;
186: cvs_log(LP_ERRNO, "%s", repodir);
1.14 joris 187: return (CVS_EX_DATA);
1.12 xsa 188: }
189: }
1.15 ! jfb 190:
! 191: if (cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath) < 0)
! 192: return (CVS_EX_PROTO);
! 193: if (cvs_sendln(root, repodir) < 0)
! 194: return (CVS_EX_PROTO);
! 195: return (0);
! 196: }
! 197:
! 198: if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0)
! 199: return (CVS_EX_PROTO);
! 200: if (cvs_sendfile(root, fpath) < 0)
! 201: return (CVS_EX_PROTO);
! 202:
! 203: return (0);
! 204: }
! 205:
! 206:
! 207: /*
! 208: * cvs_import_local()
! 209: *
! 210: */
! 211: static int
! 212: cvs_import_local(CVSFILE *cf, void *arg)
! 213: {
! 214: int len;
! 215: struct cvsroot *root;
! 216: char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
! 217: RCSFILE *rf;
! 218:
! 219: root = CVS_DIR_ROOT(cf);
! 220: len = snprintf(repo, sizeof(repo), "%s/%s", root->cr_dir, module);
! 221: if (len == -1 || len >= (int)sizeof(repo)) {
! 222: errno = ENAMETOOLONG;
! 223: cvs_log(LP_ERRNO, "%s", repo);
! 224: return (CVS_EX_DATA);
! 225: }
! 226:
! 227: cvs_file_getpath(cf, fpath, sizeof(fpath));
! 228:
! 229: if (cf->cf_type == DT_DIR) {
! 230: if (!strcmp(cf->cf_name, "."))
! 231: strlcpy(rpath, repo, sizeof(rpath));
! 232: else {
! 233: len = snprintf(rpath, sizeof(rpath), "%s/%s",
! 234: repo, fpath);
! 235: if (len == -1 || len >= (int)sizeof(rpath)) {
! 236: errno = ENAMETOOLONG;
! 237: cvs_log(LP_ERRNO, "%s", rpath);
! 238: return (CVS_EX_DATA);
! 239: }
! 240:
! 241: cvs_printf("Importing %s\n", rpath);
! 242: if (mkdir(rpath, 0700) == -1) {
! 243: cvs_log(LP_ERRNO, "failed to create %s",
! 244: rpath);
! 245: }
1.3 jfb 246: }
1.1 krapht 247:
1.3 jfb 248: return (0);
1.1 krapht 249: }
250:
1.15 ! jfb 251: snprintf(rpath, sizeof(rpath), "%s/%s%s",
! 252: repo, fpath, RCS_FILE_EXT);
! 253:
! 254: printf("importing file in %s\n", rpath);
! 255: cvs_printf("N %s\n", fpath);
! 256:
! 257: rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE);
! 258: if (rf == NULL) {
1.1 krapht 259: }
260:
1.15 ! jfb 261: rcs_close(rf);
! 262:
! 263: return (CVS_EX_OK);
1.1 krapht 264: }