Annotation of src/usr.bin/cvs/checkout.c, Revision 1.31
1.31 ! xsa 1: /* $OpenBSD: checkout.c,v 1.30 2005/06/07 15:34:24 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",
51: "[-AcflNnPpRs] [-D date | -r rev] [-d dir] [-j rev] [-k mode] "
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:
65: static char *date, *rev, *koptstr, *tgtdir, *rcsid;
66: static int statmod = 0;
1.22 jfb 67: static int shorten = 1;
68: static int usehead = 0;
1.14 joris 69: static int kflag = RCS_KWEXP_DEFAULT;
1.1 jfb 70:
1.22 jfb 71: /* modules */
72: static char **co_mods;
73: static int co_nmod;
74:
75: static int
76: cvs_checkout_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 77: {
1.14 joris 78: int ch;
79:
80: date = rev = koptstr = tgtdir = rcsid = NULL;
1.13 jfb 81:
1.22 jfb 82: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 83: switch (ch) {
1.13 jfb 84: case 'A':
85: break;
1.10 jfb 86: case 'c':
1.13 jfb 87: statmod = CVS_LISTMOD;
88: break;
89: case 'D':
90: date = optarg;
91: break;
92: case 'd':
93: tgtdir = optarg;
94: break;
95: case 'f':
1.21 xsa 96: usehead = 1;
1.13 jfb 97: break;
98: case 'j':
99: break;
100: case 'k':
101: koptstr = optarg;
102: kflag = rcs_kflag_get(koptstr);
103: if (RCS_KWEXP_INVAL(kflag)) {
104: cvs_log(LP_ERR,
105: "invalid RCS keyword expansion mode");
106: rcs_kflag_usage();
1.18 joris 107: return (CVS_EX_USAGE);
1.13 jfb 108: }
1.24 joris 109: break;
110: case 'P':
111: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.20 xsa 112: break;
1.22 jfb 113: case 'N':
114: shorten = 0;
115: break;
1.20 xsa 116: case 'p':
117: cvs_noexec = 1; /* no locks will be created */
1.13 jfb 118: break;
119: case 'r':
120: rev = optarg;
1.25 xsa 121: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.13 jfb 122: break;
123: case 's':
124: statmod = CVS_STATMOD;
125: break;
126: case 't':
127: rcsid = optarg;
1.10 jfb 128: break;
1.1 jfb 129: default:
1.18 joris 130: return (CVS_EX_USAGE);
1.1 jfb 131: }
132: }
133:
134: argc -= optind;
135: argv += optind;
136:
1.22 jfb 137: co_mods = argv;
138: co_nmod = argc;
139:
1.13 jfb 140: if (!statmod && (argc == 0)) {
1.31 ! xsa 141: cvs_log(LP_ABORT,
1.1 jfb 142: "must specify at least one module or directory");
1.26 xsa 143: return (-1);
1.1 jfb 144: }
145:
1.13 jfb 146: if (statmod && (argc > 0)) {
1.31 ! xsa 147: cvs_log(LP_ABORT, "-c and -s must not get any arguments");
1.26 xsa 148: return (-1);
1.13 jfb 149: }
150:
1.14 joris 151: *arg = optind;
152: return (0);
153: }
1.9 jfb 154:
1.22 jfb 155: static int
156: cvs_checkout_pre_exec(struct cvsroot *root)
1.14 joris 157: {
1.22 jfb 158: int i;
1.23 joris 159: char *sp;
1.1 jfb 160:
1.22 jfb 161: for (i = 0; i < co_nmod; i++) {
1.23 joris 162: if ((sp = strchr(co_mods[i], '/')) != NULL)
163: *sp = '\0';
164:
165: if ((mkdir(co_mods[i], 0755) == -1) && (errno != EEXIST)) {
166: cvs_log(LP_ERRNO, "can't create base directory '%s'",
167: co_mods[i]);
168: return (CVS_EX_DATA);
169: }
170:
171: if (cvs_mkadmin(co_mods[i], root->cr_str, co_mods[i]) < 0) {
1.27 xsa 172: cvs_log(LP_ERR, "can't create base directory '%s'",
1.23 joris 173: co_mods[i]);
1.22 jfb 174: return (CVS_EX_DATA);
1.23 joris 175: }
176:
177: if (sp != NULL)
178: *sp = '/';
1.22 jfb 179: }
180:
181: if (root->cr_method != CVS_METHOD_LOCAL) {
182: for (i = 0; i < co_nmod; i++)
183: if (cvs_sendarg(root, co_mods[i], 0) < 0)
184: return (CVS_EX_PROTO);
185: if (cvs_sendreq(root, CVS_REQ_DIRECTORY, ".") < 0)
186: return (CVS_EX_PROTO);
187: if (cvs_sendln(root, root->cr_dir) < 0)
188: return (CVS_EX_PROTO);
189:
190: if (cvs_sendreq(root, CVS_REQ_XPANDMOD, NULL) < 0)
191: cvs_log(LP_ERR, "failed to expand module");
192:
193: if (usehead && (cvs_sendarg(root, "-f", 0) < 0))
194: return (CVS_EX_PROTO);
195:
196: if ((tgtdir != NULL) &&
197: ((cvs_sendarg(root, "-d", 0) < 0) ||
198: (cvs_sendarg(root, tgtdir, 0) < 0)))
199: return (CVS_EX_PROTO);
200:
201: if (!shorten && cvs_sendarg(root, "-N", 0) < 0)
1.29 xsa 202: return (CVS_EX_PROTO);
203:
1.30 xsa 204: if ((cvs_cmd_checkout.cmd_flags & CVS_CMD_PRUNEDIRS) &&
1.29 xsa 205: (cvs_sendarg(root, "-P", 0) < 0))
1.22 jfb 206: return (CVS_EX_PROTO);
207:
208: for (i = 0; i < co_nmod; i++)
209: if (cvs_sendarg(root, co_mods[i], 0) < 0)
210: return (CVS_EX_PROTO);
211:
212: if ((statmod == CVS_LISTMOD) &&
213: (cvs_sendarg(root, "-c", 0) < 0))
214: return (CVS_EX_PROTO);
215: else if ((statmod == CVS_STATMOD) &&
216: (cvs_sendarg(root, "-s", 0) < 0))
217: return (CVS_EX_PROTO);
218: }
1.1 jfb 219: return (0);
220: }