Annotation of src/usr.bin/cvs/checkout.c, Revision 1.38
1.38 ! xsa 1: /* $OpenBSD: checkout.c,v 1.37 2005/07/08 08:54:43 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.11 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.11 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.11 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.11 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 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
1.11 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
1.23 joris 28: #include <sys/stat.h>
1.1 jfb 29:
30: #include <errno.h>
31: #include <stdio.h>
32: #include <stdlib.h>
1.28 xsa 33: #include <string.h>
1.1 jfb 34: #include <unistd.h>
35:
36: #include "cvs.h"
37: #include "log.h"
1.5 jfb 38: #include "proto.h"
1.1 jfb 39:
40:
1.13 jfb 41: #define CVS_LISTMOD 1
42: #define CVS_STATMOD 2
43:
1.22 jfb 44: static int cvs_checkout_init (struct cvs_cmd *, int, char **, int *);
45: static int cvs_checkout_pre_exec (struct cvsroot *);
1.14 joris 46:
1.22 jfb 47: struct cvs_cmd cvs_cmd_checkout = {
48: CVS_OP_CHECKOUT, CVS_REQ_CO, "checkout",
49: { "co", "get" },
50: "Checkout sources for editing",
1.33 xsa 51: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 52: "[-t id] module ...",
53: "AcD:d:fj:k:lNnPRr:st:",
54: NULL,
1.14 joris 55: 0,
1.22 jfb 56: cvs_checkout_init,
57: cvs_checkout_pre_exec,
58: NULL,
59: NULL,
60: NULL,
61: NULL,
1.23 joris 62: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR
1.14 joris 63: };
64:
1.33 xsa 65: struct cvs_cmd cvs_cmd_export = {
66: CVS_OP_EXPORT, CVS_REQ_EXPORT, "export",
67: { "ex", "exp" },
68: "Extract copy of a module without management directories",
69: "[-flNnR] [-d dir] [-k mode] -D date | -r tag module ...",
70: "D:d:fk:lNnRr:",
71: NULL,
72: 0,
73: cvs_checkout_init,
74: cvs_checkout_pre_exec,
75: NULL,
76: NULL,
77: NULL,
78: NULL,
79: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR
80: };
81:
82: static char *date, *tag, *koptstr, *tgtdir, *rcsid;
1.14 joris 83: static int statmod = 0;
1.37 joris 84: static int shorten = 0;
1.22 jfb 85: static int usehead = 0;
1.14 joris 86: static int kflag = RCS_KWEXP_DEFAULT;
1.1 jfb 87:
1.22 jfb 88: /* modules */
89: static char **co_mods;
90: static int co_nmod;
91:
92: static int
93: cvs_checkout_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 94: {
1.14 joris 95: int ch;
1.33 xsa 96: RCSNUM *rcs;
1.14 joris 97:
1.33 xsa 98: date = tag = koptstr = tgtdir = rcsid = NULL;
1.13 jfb 99:
1.22 jfb 100: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 101: switch (ch) {
1.13 jfb 102: case 'A':
103: break;
1.10 jfb 104: case 'c':
1.13 jfb 105: statmod = CVS_LISTMOD;
106: break;
107: case 'D':
108: date = optarg;
1.32 xsa 109: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.13 jfb 110: break;
111: case 'd':
112: tgtdir = optarg;
1.37 joris 113: shorten = 1;
1.13 jfb 114: break;
115: case 'f':
1.21 xsa 116: usehead = 1;
1.13 jfb 117: break;
118: case 'j':
119: break;
120: case 'k':
121: koptstr = optarg;
122: kflag = rcs_kflag_get(koptstr);
123: if (RCS_KWEXP_INVAL(kflag)) {
124: cvs_log(LP_ERR,
125: "invalid RCS keyword expansion mode");
126: rcs_kflag_usage();
1.18 joris 127: return (CVS_EX_USAGE);
1.13 jfb 128: }
1.24 joris 129: break;
130: case 'P':
131: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.20 xsa 132: break;
1.22 jfb 133: case 'N':
134: shorten = 0;
135: break;
1.20 xsa 136: case 'p':
137: cvs_noexec = 1; /* no locks will be created */
1.13 jfb 138: break;
139: case 'r':
1.33 xsa 140: tag = optarg;
1.25 xsa 141: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.13 jfb 142: break;
143: case 's':
144: statmod = CVS_STATMOD;
145: break;
146: case 't':
147: rcsid = optarg;
1.10 jfb 148: break;
1.1 jfb 149: default:
1.18 joris 150: return (CVS_EX_USAGE);
1.1 jfb 151: }
152: }
153:
154: argc -= optind;
155: argv += optind;
156:
1.22 jfb 157: co_mods = argv;
158: co_nmod = argc;
159:
1.38 ! xsa 160: if ((statmod == 0) && (argc == 0)) {
1.31 xsa 161: cvs_log(LP_ABORT,
1.1 jfb 162: "must specify at least one module or directory");
1.26 xsa 163: return (-1);
1.1 jfb 164: }
165:
1.13 jfb 166: if (statmod && (argc > 0)) {
1.31 xsa 167: cvs_log(LP_ABORT, "-c and -s must not get any arguments");
1.26 xsa 168: return (-1);
1.13 jfb 169: }
170:
1.33 xsa 171: /* `export' command exceptions */
172: if (cvs_cmdop == CVS_OP_EXPORT) {
173: if (!tag && !date) {
174: cvs_log(LP_ABORT, "must specify a tag or date");
175: return (-1);
176: }
177:
178: /* we don't want numerical revisions here */
179: if (tag && (rcs = rcsnum_parse(tag)) != NULL) {
180: cvs_log(LP_ABORT, "tag `%s' must be a symbolic tag",
181: tag);
182: rcsnum_free(rcs);
183: return (-1);
184: }
185: }
186:
1.14 joris 187: *arg = optind;
188: return (0);
189: }
1.9 jfb 190:
1.22 jfb 191: static int
192: cvs_checkout_pre_exec(struct cvsroot *root)
1.14 joris 193: {
1.22 jfb 194: int i;
1.35 joris 195: char *sp;
1.1 jfb 196:
1.22 jfb 197: for (i = 0; i < co_nmod; i++) {
1.23 joris 198: if ((sp = strchr(co_mods[i], '/')) != NULL)
199: *sp = '\0';
200:
201: if ((mkdir(co_mods[i], 0755) == -1) && (errno != EEXIST)) {
202: cvs_log(LP_ERRNO, "can't create base directory '%s'",
203: co_mods[i]);
204: return (CVS_EX_DATA);
205: }
206:
207: if (cvs_mkadmin(co_mods[i], root->cr_str, co_mods[i]) < 0) {
1.27 xsa 208: cvs_log(LP_ERR, "can't create base directory '%s'",
1.23 joris 209: co_mods[i]);
1.22 jfb 210: return (CVS_EX_DATA);
1.23 joris 211: }
212:
213: if (sp != NULL)
214: *sp = '/';
1.22 jfb 215: }
216:
217: if (root->cr_method != CVS_METHOD_LOCAL) {
1.37 joris 218: /*
219: * These arguments are for the expand-modules
220: * command that we send to the server before requesting
221: * a checkout.
222: */
223: for (i = 0; i < co_nmod; i++)
224: if (cvs_sendarg(root, co_mods[i], 0) < 0)
225: return (CVS_EX_PROTO);
1.22 jfb 226: if (cvs_sendreq(root, CVS_REQ_DIRECTORY, ".") < 0)
227: return (CVS_EX_PROTO);
228: if (cvs_sendln(root, root->cr_dir) < 0)
229: return (CVS_EX_PROTO);
230:
231: if (cvs_sendreq(root, CVS_REQ_XPANDMOD, NULL) < 0)
232: cvs_log(LP_ERR, "failed to expand module");
233:
1.38 ! xsa 234: if ((usehead == 1) && (cvs_sendarg(root, "-f", 0) < 0))
1.22 jfb 235: return (CVS_EX_PROTO);
236:
237: if ((tgtdir != NULL) &&
238: ((cvs_sendarg(root, "-d", 0) < 0) ||
239: (cvs_sendarg(root, tgtdir, 0) < 0)))
240: return (CVS_EX_PROTO);
241:
1.38 ! xsa 242: if ((shorten == 0) && cvs_sendarg(root, "-N", 0) < 0)
1.29 xsa 243: return (CVS_EX_PROTO);
244:
1.30 xsa 245: if ((cvs_cmd_checkout.cmd_flags & CVS_CMD_PRUNEDIRS) &&
1.29 xsa 246: (cvs_sendarg(root, "-P", 0) < 0))
1.22 jfb 247: return (CVS_EX_PROTO);
248:
249: for (i = 0; i < co_nmod; i++)
250: if (cvs_sendarg(root, co_mods[i], 0) < 0)
251: return (CVS_EX_PROTO);
252:
253: if ((statmod == CVS_LISTMOD) &&
254: (cvs_sendarg(root, "-c", 0) < 0))
255: return (CVS_EX_PROTO);
256: else if ((statmod == CVS_STATMOD) &&
257: (cvs_sendarg(root, "-s", 0) < 0))
1.33 xsa 258: return (CVS_EX_PROTO);
259:
260: if ((tag != NULL) && ((cvs_sendarg(root, "-r", 0) < 0) ||
261: (cvs_sendarg(root, tag, 0) < 0)))
1.34 xsa 262: return (CVS_EX_PROTO);
263:
264: if ((date != NULL) && ((cvs_sendarg(root, "-D", 0) < 0) ||
265: (cvs_sendarg(root, date, 0) < 0)))
1.22 jfb 266: return (CVS_EX_PROTO);
267: }
1.1 jfb 268: return (0);
269: }