Annotation of src/usr.bin/cvs/checkout.c, Revision 1.77
1.77 ! xsa 1: /* $OpenBSD: checkout.c,v 1.76 2007/01/14 22:20:46 xsa Exp $ */
1.1 jfb 2: /*
1.53 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.53 joris 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 jfb 8: *
1.53 joris 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.44 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
21: #include "log.h"
1.53 joris 22: #include "diff.h"
1.66 joris 23: #include "remote.h"
1.1 jfb 24:
1.53 joris 25: int cvs_checkout(int, char **);
1.61 xsa 26: int cvs_export(int, char **);
1.72 joris 27:
1.61 xsa 28: static void checkout_check_repository(int, char **);
1.53 joris 29: static void checkout_repository(const char *, const char *);
1.14 joris 30:
1.54 joris 31: extern int prune_dirs;
1.57 joris 32: extern int build_dirs;
1.76 xsa 33: extern int reset_stickies;
1.54 joris 34:
1.22 jfb 35: struct cvs_cmd cvs_cmd_checkout = {
1.63 joris 36: CVS_OP_CHECKOUT, 0, "checkout",
1.22 jfb 37: { "co", "get" },
1.53 joris 38: "Checkout a working copy of a repository",
1.33 xsa 39: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 40: "[-t id] module ...",
1.77 ! xsa 41: "AcD:d:fj:k:lNnPpRr:st:",
1.59 joris 42: NULL,
43: cvs_checkout
44: };
45:
46: struct cvs_cmd cvs_cmd_export = {
1.63 joris 47: CVS_OP_EXPORT, 0, "export",
1.59 joris 48: { "exp", "ex" },
49: "Export sources from CVS, similar to checkout",
1.61 xsa 50: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
51: "D:d:k:flNnRr:",
1.22 jfb 52: NULL,
1.61 xsa 53: cvs_export
1.33 xsa 54: };
55:
1.53 joris 56: int
57: cvs_checkout(int argc, char **argv)
1.1 jfb 58: {
1.77 ! xsa 59: int ch, flags;
! 60:
! 61: flags = CR_RECURSE_DIRS;
1.13 jfb 62:
1.53 joris 63: while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1 jfb 64: switch (ch) {
1.76 xsa 65: case 'A':
66: reset_stickies = 1;
67: break;
1.77 ! xsa 68: case 'l':
! 69: flags &= ~CR_RECURSE_DIRS;
! 70: break;
1.54 joris 71: case 'P':
72: prune_dirs = 1;
1.77 ! xsa 73: break;
! 74: case 'R':
1.54 joris 75: break;
1.1 jfb 76: default:
1.53 joris 77: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1 jfb 78: }
79: }
80:
81: argc -= optind;
82: argv += optind;
83:
1.53 joris 84: if (argc == 0)
85: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22 jfb 86:
1.61 xsa 87: checkout_check_repository(argc, argv);
88:
89: return (0);
90: }
91:
92: int
93: cvs_export(int argc, char **argv)
94: {
95: int ch, flags;
96:
97: prune_dirs = 1;
98: flags = CR_RECURSE_DIRS;
99:
100: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
101: switch (ch) {
102: case 'l':
103: flags &= ~CR_RECURSE_DIRS;
104: break;
105: case 'R':
106: break;
107: default:
108: fatal("%s", cvs_cmd_export.cmd_synopsis);
109: }
110: }
111:
112: argc -= optind;
113: argv += optind;
114:
115: if (argc == 0)
116: fatal("%s", cvs_cmd_export.cmd_synopsis);
117:
118: checkout_check_repository(argc, argv);
119:
120: return (0);
121: }
122:
123: static void
124: checkout_check_repository(int argc, char **argv)
125: {
1.67 xsa 126: int i;
1.61 xsa 127: char repo[MAXPATHLEN];
128: struct stat st;
129:
1.53 joris 130: for (i = 0; i < argc; i++) {
131: cvs_mkpath(argv[i]);
1.1 jfb 132:
1.67 xsa 133: if (cvs_path_cat(current_cvsroot->cr_dir, argv[i], repo,
134: sizeof(repo)) >= sizeof(repo))
135: fatal("checkout_check_repository: truncation");
1.13 jfb 136:
1.53 joris 137: if (stat(repo, &st) == -1) {
138: cvs_log(LP_ERR, "cannot find repository %s - ignored",
139: argv[i]);
140: continue;
141: }
142:
143: checkout_repository(repo, argv[i]);
1.33 xsa 144: }
1.14 joris 145: }
1.9 jfb 146:
1.53 joris 147: static void
148: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 149: {
1.53 joris 150: struct cvs_flisthead fl, dl;
151: struct cvs_recursion cr;
152:
153: TAILQ_INIT(&fl);
154: TAILQ_INIT(&dl);
1.41 joris 155:
1.57 joris 156: build_dirs = 1;
1.53 joris 157: cr.enterdir = cvs_update_enterdir;
1.54 joris 158: cr.leavedir = cvs_update_leavedir;
1.64 joris 159: cr.fileproc = cvs_update_local;
1.56 joris 160: cr.flags = CR_REPO | CR_RECURSE_DIRS;
1.41 joris 161:
1.53 joris 162: cvs_repository_lock(repobase);
1.56 joris 163: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 164:
1.53 joris 165: cvs_file_walklist(&fl, &cr);
166: cvs_file_freelist(&fl);
1.23 joris 167:
1.53 joris 168: cvs_repository_unlock(repobase);
1.23 joris 169:
1.53 joris 170: cvs_file_walklist(&dl, &cr);
171: cvs_file_freelist(&dl);
172: }
1.23 joris 173:
1.58 joris 174: void
1.73 joris 175: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, int flags)
1.53 joris 176: {
177: int l, oflags, exists;
178: time_t rcstime;
179: CVSENTRIES *ent;
180: struct timeval tv[2];
1.72 joris 181: char *template, *p, *entry, rev[16], timebuf[64];
182: char tbuf[32], stickytag[32];
1.22 jfb 183:
1.53 joris 184: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 185:
1.66 joris 186: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
187: cf->file_path, rev, flags,
188: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 189:
1.65 reyk 190: if (flags & CO_DUMP) {
1.66 joris 191: if (cvs_server_active) {
192: cvs_printf("dump file %s to client\n", cf->file_path);
193: } else {
1.71 joris 194: rcs_rev_write_fd(cf->file_rcs, rnum,
195: STDOUT_FILENO, 1);
1.66 joris 196: }
197:
1.65 reyk 198: return;
199: }
1.42 joris 200:
1.66 joris 201: if (cvs_server_active == 0) {
202: oflags = O_WRONLY | O_TRUNC;
203: if (cf->fd != -1) {
204: exists = 1;
205: (void)close(cf->fd);
206: } else {
207: exists = 0;
208: oflags |= O_CREAT;
209: }
210:
211: cf->fd = open(cf->file_path, oflags);
212: if (cf->fd == -1)
213: fatal("cvs_checkout_file: open: %s", strerror(errno));
214:
1.71 joris 215: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
1.66 joris 216:
217: if (fchmod(cf->fd, 0644) == -1)
218: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
219:
220: if (exists == 0) {
221: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
222: rcstime = cvs_hack_time(rcstime, 0);
223: } else {
224: time(&rcstime);
225: }
226:
227: tv[0].tv_sec = rcstime;
228: tv[0].tv_usec = 0;
229: tv[1] = tv[0];
230: if (futimes(cf->fd, tv) == -1)
231: fatal("cvs_checkout_file: futimes: %s",
232: strerror(errno));
1.53 joris 233: } else {
234: time(&rcstime);
1.41 joris 235: }
236:
1.62 joris 237: rcstime = cvs_hack_time(rcstime, 1);
1.41 joris 238:
1.58 joris 239: ctime_r(&rcstime, tbuf);
240: if (tbuf[strlen(tbuf) - 1] == '\n')
241: tbuf[strlen(tbuf) - 1] = '\0';
242:
243: if (flags & CO_MERGE) {
244: l = snprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
245: tbuf);
246: if (l == -1 || l >= (int)sizeof(timebuf))
247: fatal("cvs_checkout_file: overflow");
248: } else {
249: strlcpy(timebuf, tbuf, sizeof(timebuf));
250: }
1.41 joris 251:
1.60 joris 252: if (flags & CO_SETSTICKY) {
253: l = snprintf(stickytag, sizeof(stickytag), "T%s", rev);
254: if (l == -1 || l >= (int)sizeof(stickytag))
255: fatal("cvs_checkout_file: overflow");
256: } else {
257: stickytag[0] = '\0';
258: }
259:
1.53 joris 260: entry = xmalloc(CVS_ENT_MAXLINELEN);
1.60 joris 261: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//%s", cf->file_name,
262: rev, timebuf, stickytag);
1.41 joris 263:
1.66 joris 264: if (cvs_server_active == 0) {
265: ent = cvs_ent_open(cf->file_wd);
266: cvs_ent_add(ent, entry);
267: cvs_ent_close(ent, ENT_SYNC);
268: } else {
269: if ((p = strrchr(cf->file_rpath, ',')) != NULL)
270: *p = '\0';
271:
1.69 joris 272: if (flags & CO_COMMIT)
273: cvs_server_update_entry("Checked-in", cf);
274: else
275: cvs_server_update_entry("Updated", cf);
1.68 joris 276:
1.66 joris 277: cvs_remote_output(entry);
278:
1.68 joris 279: if (!(flags & CO_COMMIT)) {
1.72 joris 280: (void)xasprintf(&template,
281: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.66 joris 282:
1.72 joris 283: /* XXX - fd race below */
1.75 joris 284: rcs_rev_write_stmp(cf->file_rcs, rnum, template, 0);
1.72 joris 285: cvs_remote_send_file(template);
286: (void)unlink(template);
1.74 joris 287: xfree(template);
1.68 joris 288: }
1.66 joris 289:
290: if (p != NULL)
291: *p = ',';
292: }
1.41 joris 293:
1.53 joris 294: xfree(entry);
1.1 jfb 295: }