Annotation of src/usr.bin/cvs/checkout.c, Revision 1.118
1.118 ! tobias 1: /* $OpenBSD: checkout.c,v 1.117 2008/02/03 15:08:04 tobias 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.92 otto 18: #include <sys/param.h>
19: #include <sys/dirent.h>
20: #include <sys/stat.h>
1.117 tobias 21: #include <sys/time.h>
1.92 otto 22:
23: #include <errno.h>
24: #include <fcntl.h>
1.100 tobias 25: #include <libgen.h>
1.92 otto 26: #include <string.h>
27: #include <unistd.h>
1.1 jfb 28:
29: #include "cvs.h"
1.53 joris 30: #include "diff.h"
1.66 joris 31: #include "remote.h"
1.72 joris 32:
1.61 xsa 33: static void checkout_check_repository(int, char **);
1.113 tobias 34: static int checkout_classify(const char *, const char *);
1.53 joris 35: static void checkout_repository(const char *, const char *);
1.14 joris 36:
1.101 joris 37: extern int print_stdout;
1.54 joris 38: extern int prune_dirs;
1.57 joris 39: extern int build_dirs;
1.54 joris 40:
1.78 xsa 41: static int flags = CR_REPO | CR_RECURSE_DIRS;
42:
1.22 jfb 43: struct cvs_cmd cvs_cmd_checkout = {
1.112 tobias 44: CVS_OP_CHECKOUT, CVS_USE_WDIR, "checkout",
1.22 jfb 45: { "co", "get" },
1.53 joris 46: "Checkout a working copy of a repository",
1.33 xsa 47: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 48: "[-t id] module ...",
1.77 xsa 49: "AcD:d:fj:k:lNnPpRr:st:",
1.59 joris 50: NULL,
51: cvs_checkout
52: };
53:
54: struct cvs_cmd cvs_cmd_export = {
1.112 tobias 55: CVS_OP_EXPORT, CVS_USE_WDIR, "export",
1.59 joris 56: { "exp", "ex" },
57: "Export sources from CVS, similar to checkout",
1.61 xsa 58: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
59: "D:d:k:flNnRr:",
1.22 jfb 60: NULL,
1.61 xsa 61: cvs_export
1.33 xsa 62: };
63:
1.53 joris 64: int
65: cvs_checkout(int argc, char **argv)
1.1 jfb 66: {
1.78 xsa 67: int ch;
1.13 jfb 68:
1.53 joris 69: while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1 jfb 70: switch (ch) {
1.76 xsa 71: case 'A':
72: reset_stickies = 1;
73: break;
1.77 xsa 74: case 'l':
75: flags &= ~CR_RECURSE_DIRS;
76: break;
1.104 joris 77: case 'N':
78: break;
1.54 joris 79: case 'P':
80: prune_dirs = 1;
1.77 xsa 81: break;
1.101 joris 82: case 'p':
1.112 tobias 83: cmdp->cmd_flags &= ~CVS_USE_WDIR;
1.101 joris 84: print_stdout = 1;
85: cvs_noexec = 1;
86: break;
1.77 xsa 87: case 'R':
1.110 tobias 88: flags |= CR_RECURSE_DIRS;
1.54 joris 89: break;
1.93 niallo 90: case 'r':
1.97 joris 91: cvs_specified_tag = optarg;
1.93 niallo 92: break;
1.1 jfb 93: default:
1.53 joris 94: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1 jfb 95: }
96: }
97:
98: argc -= optind;
99: argv += optind;
100:
1.53 joris 101: if (argc == 0)
102: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22 jfb 103:
1.61 xsa 104: checkout_check_repository(argc, argv);
105:
106: return (0);
107: }
108:
109: int
110: cvs_export(int argc, char **argv)
111: {
1.78 xsa 112: int ch;
1.61 xsa 113:
114: prune_dirs = 1;
115:
116: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
117: switch (ch) {
118: case 'l':
119: flags &= ~CR_RECURSE_DIRS;
120: break;
121: case 'R':
1.110 tobias 122: flags |= CR_RECURSE_DIRS;
1.98 xsa 123: break;
124: case 'r':
125: cvs_specified_tag = optarg;
1.61 xsa 126: break;
127: default:
128: fatal("%s", cvs_cmd_export.cmd_synopsis);
129: }
130: }
131:
132: argc -= optind;
133: argv += optind;
134:
1.108 tobias 135: if (cvs_specified_tag == NULL)
136: fatal("must specify a tag or date");
137:
1.61 xsa 138: if (argc == 0)
139: fatal("%s", cvs_cmd_export.cmd_synopsis);
140:
141: checkout_check_repository(argc, argv);
142:
143: return (0);
144: }
145:
146: static void
147: checkout_check_repository(int argc, char **argv)
148: {
1.67 xsa 149: int i;
1.116 joris 150: char *module;
1.61 xsa 151: char repo[MAXPATHLEN];
1.79 joris 152: struct cvs_recursion cr;
153:
1.102 tobias 154: build_dirs = print_stdout ? 0 : 1;
155:
1.79 joris 156: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
157: cvs_client_connect_to_server();
158:
1.97 joris 159: if (cvs_specified_tag != NULL)
160: cvs_client_send_request("Argument -r%s",
161: cvs_specified_tag);
1.79 joris 162: if (reset_stickies == 1)
163: cvs_client_send_request("Argument -A");
1.80 xsa 164:
165: if (!(flags & CR_RECURSE_DIRS))
166: cvs_client_send_request("Argument -l");
1.79 joris 167:
168: if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
169: cvs_client_send_request("Argument -P");
170:
1.101 joris 171: if (print_stdout == 1)
172: cvs_client_send_request("Argument -p");
173:
1.79 joris 174: cr.enterdir = NULL;
175: cr.leavedir = NULL;
1.103 tobias 176: if (print_stdout)
177: cr.fileproc = NULL;
178: else
179: cr.fileproc = cvs_client_sendfile;
180:
181: flags &= ~CR_REPO;
1.79 joris 182: cr.flags = flags;
183:
1.108 tobias 184: if (cvs_cmdop != CVS_OP_EXPORT)
185: cvs_file_run(argc, argv, &cr);
1.79 joris 186:
187: cvs_client_send_files(argv, argc);
188: cvs_client_senddir(".");
189:
190: cvs_client_send_request("%s",
191: (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
192:
193: cvs_client_get_responses();
194:
195: return;
196: }
1.61 xsa 197:
1.104 joris 198: cvs_directory_tag = cvs_specified_tag;
199:
1.53 joris 200: for (i = 0; i < argc; i++) {
1.116 joris 201: module = cvs_module_lookup(argv[i]);
202:
1.90 xsa 203: (void)xsnprintf(repo, sizeof(repo), "%s/%s",
1.116 joris 204: current_cvsroot->cr_dir, module);
1.13 jfb 205:
1.116 joris 206: switch (checkout_classify(repo, module)) {
1.113 tobias 207: case CVS_FILE:
1.100 tobias 208: cr.fileproc = cvs_update_local;
209: cr.flags = flags;
1.102 tobias 210:
211: if (build_dirs == 1)
1.116 joris 212: cvs_mkpath(dirname(module), cvs_specified_tag);
213: cvs_file_run(1, &(module), &cr);
1.113 tobias 214: break;
215: case CVS_DIR:
216: if (build_dirs == 1)
1.116 joris 217: cvs_mkpath(module, cvs_specified_tag);
218: checkout_repository(repo, module);
1.113 tobias 219: break;
220: default:
221: break;
222: }
223: }
224: }
1.100 tobias 225:
1.113 tobias 226: static int
227: checkout_classify(const char *repo, const char *arg)
228: {
229: char *d, *f, fpath[MAXPATHLEN];
230: struct stat sb;
231:
232: if (stat(repo, &sb) == 0) {
233: if (!S_ISDIR(sb.st_mode)) {
234: cvs_log(LP_ERR, "ignoring %s: not a directory", arg);
235: return 0;
1.53 joris 236: }
1.113 tobias 237: return CVS_DIR;
238: }
1.97 joris 239:
1.113 tobias 240: d = dirname(repo);
241: f = basename(repo);
242:
243: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s%s", d, f, RCS_FILE_EXT);
244: if (stat(fpath, &sb) == 0) {
245: if (!S_ISREG(sb.st_mode)) {
246: cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
247: return 0;
248: }
249: return CVS_FILE;
1.33 xsa 250: }
1.113 tobias 251:
252: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s/%s%s",
253: d, CVS_PATH_ATTIC, f, RCS_FILE_EXT);
254: if (stat(fpath, &sb) == 0) {
255: if (!S_ISREG(sb.st_mode)) {
256: cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
257: return 0;
258: }
259: return CVS_FILE;
260: }
261:
262: cvs_log(LP_ERR, "cannot find module `%s' - ignored", arg);
263: return 0;
1.14 joris 264: }
1.9 jfb 265:
1.53 joris 266: static void
267: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 268: {
1.53 joris 269: struct cvs_flisthead fl, dl;
270: struct cvs_recursion cr;
271:
272: TAILQ_INIT(&fl);
273: TAILQ_INIT(&dl);
1.94 joris 274:
1.99 xsa 275: cvs_history_add((cvs_cmdop == CVS_OP_CHECKOUT) ?
276: CVS_HISTORY_CHECKOUT : CVS_HISTORY_EXPORT, NULL, wdbase);
1.41 joris 277:
1.112 tobias 278: if (print_stdout) {
279: cr.enterdir = NULL;
280: cr.leavedir = NULL;
281: } else {
282: cr.enterdir = cvs_update_enterdir;
1.118 ! tobias 283: cr.leavedir = prune_dirs ? cvs_update_leavedir : NULL;
1.112 tobias 284: }
1.64 joris 285: cr.fileproc = cvs_update_local;
1.78 xsa 286: cr.flags = flags;
1.41 joris 287:
1.53 joris 288: cvs_repository_lock(repobase);
1.111 tobias 289: cvs_repository_getdir(repobase, wdbase, &fl, &dl,
290: flags & CR_RECURSE_DIRS ? 1 : 0);
1.1 jfb 291:
1.53 joris 292: cvs_file_walklist(&fl, &cr);
293: cvs_file_freelist(&fl);
1.23 joris 294:
1.53 joris 295: cvs_repository_unlock(repobase);
1.23 joris 296:
1.53 joris 297: cvs_file_walklist(&dl, &cr);
298: cvs_file_freelist(&dl);
299: }
1.23 joris 300:
1.58 joris 301: void
1.105 joris 302: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, char *tag, int co_flags)
1.53 joris 303: {
1.89 xsa 304: int kflag, oflags, exists;
1.53 joris 305: time_t rcstime;
306: CVSENTRIES *ent;
307: struct timeval tv[2];
1.87 joris 308: char *tosend;
1.97 joris 309: char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.115 xsa 310: char kbuf[8], sticky[CVS_REV_BUFSZ], rev[CVS_REV_BUFSZ];
1.96 xsa 311: char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22 jfb 312:
1.87 joris 313: exists = 0;
314: tosend = NULL;
1.104 joris 315:
316: if (!(co_flags & CO_REMOVE))
317: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 318:
1.66 joris 319: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 320: cf->file_path, rev, co_flags,
1.66 joris 321: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 322:
1.78 xsa 323: if (co_flags & CO_DUMP) {
1.106 tobias 324: rcs_rev_write_fd(cf->file_rcs, rnum, STDOUT_FILENO, 0);
1.65 reyk 325: return;
326: }
1.87 joris 327:
1.66 joris 328: if (cvs_server_active == 0) {
1.87 joris 329: if (!(co_flags & CO_MERGE)) {
330: oflags = O_WRONLY | O_TRUNC;
331: if (cf->fd != -1) {
332: exists = 1;
333: (void)close(cf->fd);
334: } else {
335: oflags |= O_CREAT;
336: }
337:
338: cf->fd = open(cf->file_path, oflags);
339: if (cf->fd == -1)
340: fatal("cvs_checkout_file: open: %s",
341: strerror(errno));
342:
1.106 tobias 343: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 0);
1.87 joris 344: } else {
345: cvs_merge_file(cf, 1);
1.66 joris 346: }
347:
348: if (fchmod(cf->fd, 0644) == -1)
349: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
350:
1.87 joris 351: if ((exists == 0) && (cf->file_ent == NULL) &&
352: !(co_flags & CO_MERGE))
1.66 joris 353: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 354: else
1.66 joris 355: time(&rcstime);
356:
357: tv[0].tv_sec = rcstime;
358: tv[0].tv_usec = 0;
359: tv[1] = tv[0];
360: if (futimes(cf->fd, tv) == -1)
361: fatal("cvs_checkout_file: futimes: %s",
362: strerror(errno));
1.53 joris 363: } else {
364: time(&rcstime);
1.41 joris 365: }
366:
1.84 joris 367: asctime_r(gmtime(&rcstime), tbuf);
1.107 tobias 368: tbuf[strcspn(tbuf, "\n")] = '\0';
1.58 joris 369:
1.78 xsa 370: if (co_flags & CO_MERGE) {
1.89 xsa 371: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 372: tbuf);
373: } else {
374: strlcpy(timebuf, tbuf, sizeof(timebuf));
375: }
1.41 joris 376:
1.89 xsa 377: if (co_flags & CO_SETSTICKY)
1.105 joris 378: if (tag != NULL)
1.114 xsa 379: (void)xsnprintf(sticky, sizeof(sticky), "T%s", tag);
1.93 niallo 380: else
1.114 xsa 381: (void)xsnprintf(sticky, sizeof(sticky), "T%s", rev);
1.109 tobias 382: else if (!reset_stickies && cf->file_ent != NULL &&
383: cf->file_ent->ce_tag != NULL)
1.114 xsa 384: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.109 tobias 385: cf->file_ent->ce_tag);
1.89 xsa 386: else
1.114 xsa 387: sticky[0] = '\0';
1.60 joris 388:
1.85 xsa 389: kbuf[0] = '\0';
390: if (cf->file_ent != NULL) {
391: if (cf->file_ent->ce_opts != NULL)
392: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
393: } else if (cf->file_rcs->rf_expand != NULL) {
394: kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
395: if (!(kflag & RCS_KWEXP_DEFAULT))
1.89 xsa 396: (void)xsnprintf(kbuf, sizeof(kbuf),
1.85 xsa 397: "-k%s", cf->file_rcs->rf_expand);
398: }
399:
1.89 xsa 400: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.114 xsa 401: cf->file_name, rev, timebuf, kbuf, sticky);
1.41 joris 402:
1.66 joris 403: if (cvs_server_active == 0) {
1.108 tobias 404: if (!(co_flags & CO_REMOVE) && cvs_cmdop != CVS_OP_EXPORT) {
1.95 joris 405: ent = cvs_ent_open(cf->file_wd);
406: cvs_ent_add(ent, entry);
407: cvs_ent_close(ent, ENT_SYNC);
408: }
1.66 joris 409: } else {
1.87 joris 410: if (co_flags & CO_MERGE) {
411: cvs_merge_file(cf, 1);
412: tosend = cf->file_path;
413: }
414:
1.78 xsa 415: if (co_flags & CO_COMMIT)
1.69 joris 416: cvs_server_update_entry("Checked-in", cf);
1.87 joris 417: else if (co_flags & CO_MERGE)
418: cvs_server_update_entry("Merged", cf);
1.95 joris 419: else if (co_flags & CO_REMOVE)
420: cvs_server_update_entry("Removed", cf);
1.69 joris 421: else
422: cvs_server_update_entry("Updated", cf);
1.68 joris 423:
1.95 joris 424: if (!(co_flags & CO_REMOVE))
425: cvs_remote_output(entry);
1.66 joris 426:
1.95 joris 427: if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87 joris 428: if (!(co_flags & CO_MERGE)) {
1.89 xsa 429: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 430: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 xsa 431:
1.87 joris 432: rcs_rev_write_stmp(cf->file_rcs, rnum,
433: template, 0);
434: tosend = template;
435: }
436:
437: cvs_remote_send_file(tosend);
438:
439: if (!(co_flags & CO_MERGE)) {
440: (void)unlink(template);
441: cvs_worklist_run(&temp_files,
442: cvs_worklist_unlink);
443: }
1.68 joris 444: }
1.66 joris 445: }
1.1 jfb 446: }