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