Annotation of src/usr.bin/cvs/checkout.c, Revision 1.33
1.33 ! xsa 1: /* $OpenBSD: checkout.c,v 1.32 2005/06/09 09:01:50 xsa 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.22 jfb 84: static int shorten = 1;
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;
113: break;
114: case 'f':
1.21 xsa 115: usehead = 1;
1.13 jfb 116: break;
117: case 'j':
118: break;
119: case 'k':
120: koptstr = optarg;
121: kflag = rcs_kflag_get(koptstr);
122: if (RCS_KWEXP_INVAL(kflag)) {
123: cvs_log(LP_ERR,
124: "invalid RCS keyword expansion mode");
125: rcs_kflag_usage();
1.18 joris 126: return (CVS_EX_USAGE);
1.13 jfb 127: }
1.24 joris 128: break;
129: case 'P':
130: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.20 xsa 131: break;
1.22 jfb 132: case 'N':
133: shorten = 0;
134: break;
1.20 xsa 135: case 'p':
136: cvs_noexec = 1; /* no locks will be created */
1.13 jfb 137: break;
138: case 'r':
1.33 ! xsa 139: tag = optarg;
1.25 xsa 140: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.13 jfb 141: break;
142: case 's':
143: statmod = CVS_STATMOD;
144: break;
145: case 't':
146: rcsid = optarg;
1.10 jfb 147: break;
1.1 jfb 148: default:
1.18 joris 149: return (CVS_EX_USAGE);
1.1 jfb 150: }
151: }
152:
153: argc -= optind;
154: argv += optind;
155:
1.22 jfb 156: co_mods = argv;
157: co_nmod = argc;
158:
1.13 jfb 159: if (!statmod && (argc == 0)) {
1.31 xsa 160: cvs_log(LP_ABORT,
1.1 jfb 161: "must specify at least one module or directory");
1.26 xsa 162: return (-1);
1.1 jfb 163: }
164:
1.13 jfb 165: if (statmod && (argc > 0)) {
1.31 xsa 166: cvs_log(LP_ABORT, "-c and -s must not get any arguments");
1.26 xsa 167: return (-1);
1.13 jfb 168: }
169:
1.33 ! xsa 170: /* `export' command exceptions */
! 171: if (cvs_cmdop == CVS_OP_EXPORT) {
! 172: if (!tag && !date) {
! 173: cvs_log(LP_ABORT, "must specify a tag or date");
! 174: return (-1);
! 175: }
! 176:
! 177: /* we don't want numerical revisions here */
! 178: if (tag && (rcs = rcsnum_parse(tag)) != NULL) {
! 179: cvs_log(LP_ABORT, "tag `%s' must be a symbolic tag",
! 180: tag);
! 181: rcsnum_free(rcs);
! 182: return (-1);
! 183: }
! 184: }
! 185:
1.14 joris 186: *arg = optind;
187: return (0);
188: }
1.9 jfb 189:
1.22 jfb 190: static int
191: cvs_checkout_pre_exec(struct cvsroot *root)
1.14 joris 192: {
1.22 jfb 193: int i;
1.23 joris 194: char *sp;
1.1 jfb 195:
1.22 jfb 196: for (i = 0; i < co_nmod; i++) {
1.23 joris 197: if ((sp = strchr(co_mods[i], '/')) != NULL)
198: *sp = '\0';
199:
200: if ((mkdir(co_mods[i], 0755) == -1) && (errno != EEXIST)) {
201: cvs_log(LP_ERRNO, "can't create base directory '%s'",
202: co_mods[i]);
203: return (CVS_EX_DATA);
204: }
205:
206: if (cvs_mkadmin(co_mods[i], root->cr_str, co_mods[i]) < 0) {
1.27 xsa 207: cvs_log(LP_ERR, "can't create base directory '%s'",
1.23 joris 208: co_mods[i]);
1.22 jfb 209: return (CVS_EX_DATA);
1.23 joris 210: }
211:
212: if (sp != NULL)
213: *sp = '/';
1.22 jfb 214: }
215:
216: if (root->cr_method != CVS_METHOD_LOCAL) {
217: for (i = 0; i < co_nmod; i++)
218: if (cvs_sendarg(root, co_mods[i], 0) < 0)
219: return (CVS_EX_PROTO);
220: if (cvs_sendreq(root, CVS_REQ_DIRECTORY, ".") < 0)
221: return (CVS_EX_PROTO);
222: if (cvs_sendln(root, root->cr_dir) < 0)
223: return (CVS_EX_PROTO);
224:
225: if (cvs_sendreq(root, CVS_REQ_XPANDMOD, NULL) < 0)
226: cvs_log(LP_ERR, "failed to expand module");
227:
228: if (usehead && (cvs_sendarg(root, "-f", 0) < 0))
229: return (CVS_EX_PROTO);
230:
231: if ((tgtdir != NULL) &&
232: ((cvs_sendarg(root, "-d", 0) < 0) ||
233: (cvs_sendarg(root, tgtdir, 0) < 0)))
234: return (CVS_EX_PROTO);
235:
236: if (!shorten && cvs_sendarg(root, "-N", 0) < 0)
1.29 xsa 237: return (CVS_EX_PROTO);
238:
1.30 xsa 239: if ((cvs_cmd_checkout.cmd_flags & CVS_CMD_PRUNEDIRS) &&
1.29 xsa 240: (cvs_sendarg(root, "-P", 0) < 0))
1.22 jfb 241: return (CVS_EX_PROTO);
242:
243: for (i = 0; i < co_nmod; i++)
244: if (cvs_sendarg(root, co_mods[i], 0) < 0)
245: return (CVS_EX_PROTO);
246:
247: if ((statmod == CVS_LISTMOD) &&
248: (cvs_sendarg(root, "-c", 0) < 0))
249: return (CVS_EX_PROTO);
250: else if ((statmod == CVS_STATMOD) &&
251: (cvs_sendarg(root, "-s", 0) < 0))
1.33 ! xsa 252: return (CVS_EX_PROTO);
! 253:
! 254: if ((tag != NULL) && ((cvs_sendarg(root, "-r", 0) < 0) ||
! 255: (cvs_sendarg(root, tag, 0) < 0)))
1.22 jfb 256: return (CVS_EX_PROTO);
257: }
1.1 jfb 258: return (0);
259: }