1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/* $OpenBSD: co.c,v 1.64 2006/03/16 23:53:27 niallo Exp $ */
/*
* Copyright (c) 2005 Joris Vink <joris@openbsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "includes.h"
#include "rcsprog.h"
#define CO_OPTSTRING "d:f::k:l::M::p::q::r::s:Tu::Vw::x:z:"
static void checkout_err_nobranch(RCSFILE *, const char *, const char *,
const char *, int);
int
checkout_main(int argc, char **argv)
{
int i, ch, flags, kflag, status;
RCSNUM *frev, *rev;
RCSFILE *file;
char fpath[MAXPATHLEN];
char *author, *username, *date;
const char *state;
time_t rcs_mtime = -1;
flags = status = 0;
kflag = RCS_KWEXP_ERR;
rev = RCS_HEAD_REV;
frev = NULL;
state = NULL;
author = NULL;
date = NULL;
while ((ch = rcs_getopt(argc, argv, CO_OPTSTRING)) != -1) {
switch (ch) {
case 'd':
date = xstrdup(rcs_optarg);
break;
case 'f':
rcs_set_rev(rcs_optarg, &rev);
flags |= FORCE;
break;
case 'k':
kflag = rcs_kflag_get(rcs_optarg);
if (RCS_KWEXP_INVAL(kflag)) {
cvs_log(LP_ERR,
"invalid RCS keyword expansion mode");
(usage)();
exit(1);
}
break;
case 'l':
rcs_set_rev(rcs_optarg, &rev);
if (flags & CO_UNLOCK) {
cvs_log(LP_ERR, "warning: -u overridden by -l");
flags &= ~CO_UNLOCK;
}
flags |= CO_LOCK;
break;
case 'M':
rcs_set_rev(rcs_optarg, &rev);
flags |= CO_REVDATE;
break;
case 'p':
rcs_set_rev(rcs_optarg, &rev);
pipeout = 1;
break;
case 'q':
rcs_set_rev(rcs_optarg, &rev);
verbose = 0;
break;
case 'r':
rcs_set_rev(rcs_optarg, &rev);
break;
case 's':
state = xstrdup(rcs_optarg);
flags |= CO_STATE;
break;
case 'T':
flags |= PRESERVETIME;
break;
case 'u':
rcs_set_rev(rcs_optarg, &rev);
if (flags & CO_LOCK) {
cvs_log(LP_ERR, "warning: -l overridden by -u");
flags &= ~CO_LOCK;
}
flags |= CO_UNLOCK;
break;
case 'V':
printf("%s\n", rcs_version);
exit(0);
/* NOTREACHED */
case 'w':
/* if no argument, assume current user */
if (rcs_optarg == NULL) {
if ((author = getlogin()) == NULL)
fatal("getlogin failed");
} else
author = xstrdup(rcs_optarg);
flags |= CO_AUTHOR;
break;
case 'x':
rcs_suffixes = rcs_optarg;
break;
case 'z':
timezone_flag = rcs_optarg;
break;
default:
(usage)();
exit(1);
}
}
argc -= rcs_optind;
argv += rcs_optind;
if (argc == 0) {
cvs_log(LP_ERR, "no input file");
(usage)();
exit (1);
}
if ((username = getlogin()) == NULL) {
cvs_log(LP_ERRNO, "failed to get username");
exit (1);
}
for (i = 0; i < argc; i++) {
if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
continue;
if (verbose == 1)
printf("%s --> %s\n", fpath,
(pipeout == 1) ? "standard output" : argv[i]);
if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
continue;
}
if ((file = rcs_open(fpath, RCS_RDWR|RCS_PARSE_FULLY)) == NULL)
continue;
if (flags & PRESERVETIME)
rcs_mtime = rcs_get_mtime(file->rf_path);
if (kflag != RCS_KWEXP_ERR)
rcs_kwexp_set(file, kflag);
if (rev == RCS_HEAD_REV)
frev = file->rf_head;
else
frev = rev;
if ((status = checkout_rev(file, frev, argv[i], flags,
username, author, state, date)) < 0) {
rcs_close(file);
continue;
}
if (verbose == 1)
printf("done\n");
rcs_close(file);
if (flags & PRESERVETIME)
rcs_set_mtime(fpath, rcs_mtime);
}
if (rev != RCS_HEAD_REV)
rcsnum_free(frev);
return (status);
}
void
checkout_usage(void)
{
fprintf(stderr,
"usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
" [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
" [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
}
/*
* Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
* Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
*
* Looks up revision based upon <lockname>, <author>, <state> and <date>
*
* Returns 0 on success, -1 on failure.
*/
int
checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
const char *lockname, const char *author, const char *state,
const char *date)
{
BUF *bp;
int lcount;
char buf[16];
mode_t mode = 0444;
struct stat st;
struct rcs_delta *rdp;
struct rcs_lock *lkp;
char *content, msg[128], *fdate;
time_t rcsdate, givendate;
rcsdate = givendate = -1;
if (date != NULL)
givendate = cvs_date_parse(date);
/* Check out the latest revision if <frev> is greater than HEAD */
if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
frev = file->rf_head;
lcount = 0;
TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
if (!strcmp(lkp->rl_name, lockname))
lcount++;
}
/*
* If the user didn't specify any revision, we cycle through
* revisions to lookup the first one that matches what he specified.
*
* If we cannot find one, we return an error.
*/
rdp = NULL;
if (frev == file->rf_head) {
if (lcount > 1) {
cvs_log(LP_WARN,
"multiple revisions locked by %s; "
"please specify one", lockname);
return (-1);
}
TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
if (date != NULL) {
fdate = asctime(&rdp->rd_date);
rcsdate = cvs_date_parse(fdate);
if (givendate <= rcsdate)
continue;
}
if ((author != NULL) &&
(strcmp(rdp->rd_author, author)))
continue;
if ((state != NULL) &&
(strcmp(rdp->rd_state, state)))
continue;
frev = rdp->rd_num;
break;
}
} else {
rdp = rcs_findrev(file, frev);
}
if (rdp == NULL) {
checkout_err_nobranch(file, author, date, state, flags);
return (-1);
}
rcsnum_tostr(frev, buf, sizeof(buf));
if (rdp->rd_locker != NULL) {
if (strcmp(lockname, rdp->rd_locker)) {
strlcpy(msg, "Revision %s is already locked by %s; ",
sizeof(msg));
if (flags & CO_UNLOCK)
strlcat(msg, "use co -r or rcs -u", sizeof(msg));
cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
return (-1);
}
}
if ((verbose == 1) && !(flags & NEWFILE))
printf("revision %s", buf);
if ((bp = rcs_getrev(file, frev)) == NULL) {
cvs_log(LP_ERR, "cannot find revision `%s'", buf);
return (-1);
}
/*
* Do keyword expansion if required.
*/
bp = rcs_kwexp_buf(bp, file, frev);
if (flags & CO_LOCK) {
if ((lockname != NULL)
&& (rcs_lock_add(file, lockname, frev) < 0)) {
if (rcs_errno != RCS_ERR_DUPENT)
return (-1);
}
mode = 0644;
if ((verbose == 1) && !(flags & NEWFILE))
printf(" (locked)");
} else if (flags & CO_UNLOCK) {
if (rcs_lock_remove(file, lockname, frev) < 0) {
if (rcs_errno != RCS_ERR_NOENT)
return (-1);
}
mode = 0444;
if ((verbose == 1) && !(flags & NEWFILE))
printf(" (unlocked)");
}
if ((verbose == 1) && !(flags & NEWFILE))
printf("\n");
if (flags & CO_LOCK) {
lcount++;
if ((verbose == 1) && (lcount > 1))
cvs_log(LP_WARN, "%s: warning: You now have %d locks.",
file->rf_path, lcount);
}
if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
if (verbose == 0) {
cvs_log(LP_ERR,
"writable %s exists; checkout aborted",
dst);
return (-1);
}
if (st.st_mode & S_IWUSR)
printf("writable ");
printf("%s exists%s; ", dst,
(getuid() == st.st_uid) ? "" :
", and you do not own it");
printf("remove it? [ny](n): ");
/* default is n */
if (cvs_yesno() == -1) {
cvs_log(LP_ERR, "checkout aborted");
return (-1);
}
}
if (pipeout == 1) {
cvs_buf_putc(bp, '\0');
content = cvs_buf_release(bp);
printf("%s", content);
xfree(content);
} else {
if (cvs_buf_write(bp, dst, mode) < 0) {
cvs_log(LP_ERR, "failed to write revision to file");
cvs_buf_free(bp);
return (-1);
}
cvs_buf_free(bp);
if (flags & CO_REVDATE) {
struct timeval tv[2];
memset(&tv, 0, sizeof(tv));
tv[0].tv_sec = (long)rcs_rev_getdate(file, frev);
tv[1].tv_sec = tv[0].tv_sec;
if (utimes(dst, (const struct timeval *)&tv) < 0)
cvs_log(LP_ERRNO, "error setting utimes");
}
}
return (0);
}
/*
* checkout_err_nobranch()
*
* XXX - should handle the dates too.
*/
static void
checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
const char *state, int flags)
{
if (!(flags & CO_AUTHOR))
author = NULL;
if (!(flags & CO_STATE))
state = NULL;
cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
file->rf_path,
date ? " a date before " : "",
date ? date : "",
author ? " and author " + (date ? 0:4 ) : "",
author ? author : "",
state ? " and state " + (date || author ? 0:4) : "",
state ? state : "");
}
|