Annotation of src/usr.bin/cvs/checkout.c, Revision 1.52
1.52 ! deraadt 1: /* $OpenBSD: checkout.c,v 1.51 2006/03/24 13:34:27 ray 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:
1.44 xsa 27: #include "includes.h"
1.1 jfb 28:
29: #include "cvs.h"
30: #include "log.h"
1.5 jfb 31: #include "proto.h"
1.1 jfb 32:
33:
1.39 xsa 34: #define CVS_LISTMOD 1
35: #define CVS_STATMOD 2
1.13 jfb 36:
1.39 xsa 37: static int cvs_checkout_init(struct cvs_cmd *, int, char **, int *);
38: static int cvs_checkout_pre_exec(struct cvsroot *);
1.41 joris 39: static int cvs_checkout_local(CVSFILE *cf, void *);
1.14 joris 40:
1.22 jfb 41: struct cvs_cmd cvs_cmd_checkout = {
42: CVS_OP_CHECKOUT, CVS_REQ_CO, "checkout",
43: { "co", "get" },
44: "Checkout sources for editing",
1.33 xsa 45: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 46: "[-t id] module ...",
47: "AcD:d:fj:k:lNnPRr:st:",
48: NULL,
1.14 joris 49: 0,
1.22 jfb 50: cvs_checkout_init,
51: cvs_checkout_pre_exec,
52: NULL,
53: NULL,
54: NULL,
55: NULL,
1.23 joris 56: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR
1.14 joris 57: };
58:
1.33 xsa 59: struct cvs_cmd cvs_cmd_export = {
60: CVS_OP_EXPORT, CVS_REQ_EXPORT, "export",
61: { "ex", "exp" },
62: "Extract copy of a module without management directories",
63: "[-flNnR] [-d dir] [-k mode] -D date | -r tag module ...",
64: "D:d:fk:lNnRr:",
65: NULL,
66: 0,
67: cvs_checkout_init,
68: cvs_checkout_pre_exec,
69: NULL,
70: NULL,
71: NULL,
72: NULL,
73: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR
74: };
75:
1.41 joris 76: static char *currepo = NULL;
77: static DIR *dirp = NULL;
78: static int cwdfd = -1;
1.33 xsa 79: static char *date, *tag, *koptstr, *tgtdir, *rcsid;
1.14 joris 80: static int statmod = 0;
1.37 joris 81: static int shorten = 0;
1.22 jfb 82: static int usehead = 0;
1.14 joris 83: static int kflag = RCS_KWEXP_DEFAULT;
1.1 jfb 84:
1.22 jfb 85: /* modules */
86: static char **co_mods;
87: static int co_nmod;
88:
1.41 joris 89: /* XXX checkout has issues in remote mode, -N gets seen as module */
90:
1.22 jfb 91: static int
92: cvs_checkout_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 93: {
1.14 joris 94: int ch;
95:
1.33 xsa 96: date = tag = koptstr = tgtdir = rcsid = NULL;
1.13 jfb 97:
1.22 jfb 98: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 99: switch (ch) {
1.13 jfb 100: case 'A':
101: break;
1.10 jfb 102: case 'c':
1.13 jfb 103: statmod = CVS_LISTMOD;
104: break;
105: case 'D':
106: date = optarg;
1.32 xsa 107: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.13 jfb 108: break;
109: case 'd':
110: tgtdir = optarg;
1.37 joris 111: shorten = 1;
1.13 jfb 112: break;
113: case 'f':
1.21 xsa 114: usehead = 1;
1.13 jfb 115: break;
116: case 'j':
117: break;
118: case 'k':
119: koptstr = optarg;
120: kflag = rcs_kflag_get(koptstr);
121: if (RCS_KWEXP_INVAL(kflag)) {
122: cvs_log(LP_ERR,
123: "invalid RCS keyword expansion mode");
124: rcs_kflag_usage();
1.18 joris 125: return (CVS_EX_USAGE);
1.13 jfb 126: }
1.24 joris 127: break;
128: case 'P':
129: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.20 xsa 130: break;
1.22 jfb 131: case 'N':
132: shorten = 0;
133: break;
1.20 xsa 134: case 'p':
135: cvs_noexec = 1; /* no locks will be created */
1.13 jfb 136: break;
137: case 'r':
1.33 xsa 138: tag = optarg;
1.25 xsa 139: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.13 jfb 140: break;
141: case 's':
142: statmod = CVS_STATMOD;
143: break;
144: case 't':
145: rcsid = optarg;
1.10 jfb 146: break;
1.1 jfb 147: default:
1.18 joris 148: return (CVS_EX_USAGE);
1.1 jfb 149: }
150: }
151:
152: argc -= optind;
153: argv += optind;
154:
1.22 jfb 155: co_mods = argv;
156: co_nmod = argc;
157:
1.52 ! deraadt 158: if (statmod == 0 && argc == 0)
1.50 xsa 159: fatal("must specify at least one module or directory");
1.1 jfb 160:
1.50 xsa 161: if (statmod && (argc > 0))
162: fatal("-c and -s must not get any arguments");
1.13 jfb 163:
1.33 xsa 164: /* `export' command exceptions */
165: if (cvs_cmdop == CVS_OP_EXPORT) {
1.52 ! deraadt 166: if (tag == NULL && date == NULL)
1.50 xsa 167: fatal("must specify a tag or date");
1.33 xsa 168:
169: /* we don't want numerical revisions here */
1.52 ! deraadt 170: if (tag != NULL && rcsnum_parse(tag) != NULL)
1.50 xsa 171: fatal("tag `%s' must be a symbolic tag", tag);
1.33 xsa 172: }
173:
1.14 joris 174: *arg = optind;
175: return (0);
176: }
1.9 jfb 177:
1.22 jfb 178: static int
179: cvs_checkout_pre_exec(struct cvsroot *root)
1.14 joris 180: {
1.41 joris 181: int i, ret;
182: char *sp, repo[MAXPATHLEN];
183:
1.46 xsa 184: if ((dirp = opendir(".")) == NULL)
185: fatal("cvs_checkout_pre_exec: opendir failed");
1.41 joris 186:
187: cwdfd = dirfd(dirp);
1.1 jfb 188:
1.22 jfb 189: for (i = 0; i < co_nmod; i++) {
1.23 joris 190: if ((sp = strchr(co_mods[i], '/')) != NULL)
191: *sp = '\0';
192:
1.52 ! deraadt 193: if (mkdir(co_mods[i], 0755) == -1 && errno != EEXIST)
1.46 xsa 194: fatal("cvs_checkout_pre_exec: mkdir `%s': %s",
195: co_mods[i], strerror(errno));
1.23 joris 196:
1.48 xsa 197: cvs_mkadmin(co_mods[i], root->cr_str, co_mods[i], NULL,
198: NULL, 0);
1.23 joris 199:
200: if (sp != NULL)
201: *sp = '/';
1.22 jfb 202: }
203:
1.41 joris 204: if (root->cr_method == CVS_METHOD_LOCAL) {
205: if ((dirp = opendir(".")) == NULL)
1.46 xsa 206: fatal("cvs_checkout_pre_exec: opendir failed");
207:
1.41 joris 208: cwdfd = dirfd(dirp);
209:
210: for (i = 0; i < co_nmod; i++) {
1.45 xsa 211: if (strlcpy(repo, root->cr_dir, sizeof(repo)) >=
212: sizeof(repo) ||
213: strlcat(repo, "/", sizeof(repo)) >= sizeof(repo) ||
214: strlcat(repo, co_mods[i], sizeof(repo)) >=
215: sizeof(repo))
216: fatal("cvs_checkout_pre_exec: path truncation");
217:
1.41 joris 218: currepo = co_mods[i];
1.43 reyk 219: ret = cvs_file_get(repo, CF_RECURSE | CF_REPO |
220: CF_IGNORE, cvs_checkout_local, NULL, NULL);
1.41 joris 221: if (ret != CVS_EX_OK) {
222: closedir(dirp);
223: return (ret);
224: }
225: }
226:
227: closedir(dirp);
228: } else {
1.37 joris 229: /*
230: * These arguments are for the expand-modules
231: * command that we send to the server before requesting
232: * a checkout.
233: */
234: for (i = 0; i < co_nmod; i++)
1.42 joris 235: cvs_sendarg(root, co_mods[i], 0);
236:
237: cvs_sendreq(root, CVS_REQ_DIRECTORY, ".");
238: cvs_sendln(root, root->cr_dir);
239: cvs_sendreq(root, CVS_REQ_XPANDMOD, NULL);
240:
241: if (usehead == 1)
242: cvs_sendarg(root, "-f", 0);
243:
244: if (tgtdir != NULL) {
245: cvs_sendarg(root, "-d", 0);
246: cvs_sendarg(root, tgtdir, 0);
247: }
248:
249: if (shorten == 0)
250: cvs_sendarg(root, "-N", 0);
251:
252: if (cvs_cmd_checkout.cmd_flags & CVS_CMD_PRUNEDIRS);
253: cvs_sendarg(root, "-P", 0);
1.22 jfb 254:
255: for (i = 0; i < co_nmod; i++)
1.42 joris 256: cvs_sendarg(root, co_mods[i], 0);
1.22 jfb 257:
1.42 joris 258: if (statmod == CVS_LISTMOD)
259: cvs_sendarg(root, "-c", 0);
260: else if (statmod == CVS_STATMOD)
261: cvs_sendarg(root, "-s", 0);
262:
263: if (tag != NULL) {
264: cvs_sendarg(root, "-r", 0);
265: cvs_sendarg(root, tag, 0);
266: }
267:
268: if (date != NULL) {
269: cvs_sendarg(root, "-D", 0);
270: cvs_sendarg(root, date, 0);
271: }
1.22 jfb 272: }
1.42 joris 273:
1.1 jfb 274: return (0);
1.41 joris 275: }
276:
277: static int
278: cvs_checkout_local(CVSFILE *cf, void *arg)
279: {
280: char rcspath[MAXPATHLEN], fpath[MAXPATHLEN];
281: RCSFILE *rf;
282: struct cvsroot *root;
283: static int inattic = 0;
284:
285: /* we don't want these */
1.52 ! deraadt 286: if (cf->cf_type == DT_DIR && !strcmp(cf->cf_name, "Attic")) {
1.41 joris 287: inattic = 1;
288: return (CVS_EX_OK);
289: }
290:
291: root = CVS_DIR_ROOT(cf);
1.45 xsa 292:
1.41 joris 293: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.45 xsa 294: cvs_rcs_getpath(cf, rcspath, sizeof(rcspath));
1.41 joris 295:
296: if (cf->cf_type == DT_DIR) {
297: inattic = 0;
298: if (verbosity > 1)
299: cvs_log(LP_INFO, "Updating %s", fpath);
300:
301: if (cvs_cmdop != CVS_OP_SERVER) {
302: /*
303: * We pass an empty repository name to
304: * cvs_create_dir(), because it will correctly
305: * create the repository directory for us.
306: */
307: if (cvs_create_dir(fpath, 1, root->cr_dir, NULL) < 0)
1.47 xsa 308: fatal("cvs_checkout_local: cvs_create_dir failed");
309: if (fchdir(cwdfd) < 0)
310: fatal("cvs_checkout_local: fchdir failed");
1.41 joris 311: } else {
312: /*
313: * TODO: send responses to client so it'll
314: * create it's directories.
315: */
316: }
317:
318: return (CVS_EX_OK);
319: }
320:
321: if (inattic == 1)
322: return (CVS_EX_OK);
323:
1.46 xsa 324: if ((rf = rcs_open(rcspath, RCS_READ)) == NULL)
325: fatal("cvs_checkout_local: rcs_open `%s': %s", rcspath,
1.49 xsa 326: rcs_errstr(rcs_errno));
1.41 joris 327:
328: if (cvs_checkout_rev(rf, rf->rf_head, cf, fpath,
329: (cvs_cmdop != CVS_OP_SERVER) ? 1 : 0,
1.46 xsa 330: CHECKOUT_REV_CREATED) < 0)
331: fatal("cvs_checkout_local: cvs_checkout_rev failed");
1.41 joris 332:
333: rcs_close(rf);
334:
335: cvs_printf("U %s\n", fpath);
1.46 xsa 336: return (0);
1.1 jfb 337: }