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