Annotation of src/usr.bin/cvs/checkout.c, Revision 1.97
1.97 ! joris 1: /* $OpenBSD: checkout.c,v 1.96 2007/06/28 21:38:09 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>
24: #include <string.h>
25: #include <unistd.h>
1.1 jfb 26:
27: #include "cvs.h"
1.53 joris 28: #include "diff.h"
1.66 joris 29: #include "remote.h"
1.72 joris 30:
1.61 xsa 31: static void checkout_check_repository(int, char **);
1.53 joris 32: static void checkout_repository(const char *, const char *);
1.14 joris 33:
1.54 joris 34: extern int prune_dirs;
1.57 joris 35: extern int build_dirs;
1.54 joris 36:
1.78 xsa 37: static int flags = CR_REPO | CR_RECURSE_DIRS;
38:
1.22 jfb 39: struct cvs_cmd cvs_cmd_checkout = {
1.63 joris 40: CVS_OP_CHECKOUT, 0, "checkout",
1.22 jfb 41: { "co", "get" },
1.53 joris 42: "Checkout a working copy of a repository",
1.33 xsa 43: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 44: "[-t id] module ...",
1.77 xsa 45: "AcD:d:fj:k:lNnPpRr:st:",
1.59 joris 46: NULL,
47: cvs_checkout
48: };
49:
50: struct cvs_cmd cvs_cmd_export = {
1.63 joris 51: CVS_OP_EXPORT, 0, "export",
1.59 joris 52: { "exp", "ex" },
53: "Export sources from CVS, similar to checkout",
1.61 xsa 54: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
55: "D:d:k:flNnRr:",
1.22 jfb 56: NULL,
1.61 xsa 57: cvs_export
1.33 xsa 58: };
59:
1.53 joris 60: int
61: cvs_checkout(int argc, char **argv)
1.1 jfb 62: {
1.78 xsa 63: int ch;
1.13 jfb 64:
1.53 joris 65: while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1 jfb 66: switch (ch) {
1.76 xsa 67: case 'A':
68: reset_stickies = 1;
69: break;
1.77 xsa 70: case 'l':
71: flags &= ~CR_RECURSE_DIRS;
72: break;
1.54 joris 73: case 'P':
74: prune_dirs = 1;
1.77 xsa 75: break;
76: case 'R':
1.54 joris 77: break;
1.93 niallo 78: case 'r':
1.97 ! joris 79: cvs_specified_tag = optarg;
1.93 niallo 80: break;
1.1 jfb 81: default:
1.53 joris 82: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1 jfb 83: }
84: }
85:
86: argc -= optind;
87: argv += optind;
88:
1.53 joris 89: if (argc == 0)
90: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22 jfb 91:
1.61 xsa 92: checkout_check_repository(argc, argv);
93:
94: return (0);
95: }
96:
97: int
98: cvs_export(int argc, char **argv)
99: {
1.78 xsa 100: int ch;
1.61 xsa 101:
102: prune_dirs = 1;
103:
104: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
105: switch (ch) {
106: case 'l':
107: flags &= ~CR_RECURSE_DIRS;
108: break;
109: case 'R':
110: break;
111: default:
112: fatal("%s", cvs_cmd_export.cmd_synopsis);
113: }
114: }
115:
116: argc -= optind;
117: argv += optind;
118:
119: if (argc == 0)
120: fatal("%s", cvs_cmd_export.cmd_synopsis);
121:
122: checkout_check_repository(argc, argv);
123:
124: return (0);
125: }
126:
127: static void
128: checkout_check_repository(int argc, char **argv)
129: {
1.67 xsa 130: int i;
1.61 xsa 131: char repo[MAXPATHLEN];
132: struct stat st;
1.79 joris 133: struct cvs_recursion cr;
134:
135: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
136: cvs_client_connect_to_server();
137:
1.97 ! joris 138: if (cvs_specified_tag != NULL)
! 139: cvs_client_send_request("Argument -r%s",
! 140: cvs_specified_tag);
1.79 joris 141: if (reset_stickies == 1)
142: cvs_client_send_request("Argument -A");
1.80 xsa 143:
144: if (!(flags & CR_RECURSE_DIRS))
145: cvs_client_send_request("Argument -l");
1.79 joris 146:
147: if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
148: cvs_client_send_request("Argument -P");
149:
150: cr.enterdir = NULL;
151: cr.leavedir = NULL;
152: cr.fileproc = cvs_client_sendfile;
153: cr.flags = flags;
154:
155: cvs_file_run(argc, argv, &cr);
156:
157: cvs_client_send_files(argv, argc);
158: cvs_client_senddir(".");
159:
160: cvs_client_send_request("%s",
161: (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
162:
163: cvs_client_get_responses();
164:
165: return;
166: }
1.61 xsa 167:
1.53 joris 168: for (i = 0; i < argc; i++) {
1.90 xsa 169: (void)xsnprintf(repo, sizeof(repo), "%s/%s",
170: current_cvsroot->cr_dir, argv[i]);
1.13 jfb 171:
1.53 joris 172: if (stat(repo, &st) == -1) {
1.81 thib 173: cvs_log(LP_ERR, "cannot find module `%s' - ignored",
1.53 joris 174: argv[i]);
175: continue;
176: }
1.97 ! joris 177:
! 178: cvs_mkpath(argv[i], cvs_specified_tag);
1.53 joris 179: checkout_repository(repo, argv[i]);
1.33 xsa 180: }
1.14 joris 181: }
1.9 jfb 182:
1.53 joris 183: static void
184: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 185: {
1.53 joris 186: struct cvs_flisthead fl, dl;
187: struct cvs_recursion cr;
188:
189: TAILQ_INIT(&fl);
190: TAILQ_INIT(&dl);
1.94 joris 191:
192: cvs_history_add(CVS_HISTORY_CHECKOUT, NULL, wdbase);
1.41 joris 193:
1.57 joris 194: build_dirs = 1;
1.53 joris 195: cr.enterdir = cvs_update_enterdir;
1.54 joris 196: cr.leavedir = cvs_update_leavedir;
1.64 joris 197: cr.fileproc = cvs_update_local;
1.78 xsa 198: cr.flags = flags;
1.41 joris 199:
1.53 joris 200: cvs_repository_lock(repobase);
1.56 joris 201: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 202:
1.53 joris 203: cvs_file_walklist(&fl, &cr);
204: cvs_file_freelist(&fl);
1.23 joris 205:
1.53 joris 206: cvs_repository_unlock(repobase);
1.23 joris 207:
1.53 joris 208: cvs_file_walklist(&dl, &cr);
209: cvs_file_freelist(&dl);
210: }
1.23 joris 211:
1.58 joris 212: void
1.78 xsa 213: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, int co_flags)
1.53 joris 214: {
1.89 xsa 215: int kflag, oflags, exists;
1.53 joris 216: time_t rcstime;
217: CVSENTRIES *ent;
218: struct timeval tv[2];
1.87 joris 219: char *tosend;
1.97 ! joris 220: char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.96 xsa 221: char kbuf[8], stickytag[32], rev[CVS_REV_BUFSZ];
222: char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22 jfb 223:
1.87 joris 224: exists = 0;
225: tosend = NULL;
1.53 joris 226: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 227:
1.66 joris 228: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 229: cf->file_path, rev, co_flags,
1.66 joris 230: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 231:
1.78 xsa 232: if (co_flags & CO_DUMP) {
1.66 joris 233: if (cvs_server_active) {
234: cvs_printf("dump file %s to client\n", cf->file_path);
235: } else {
1.71 joris 236: rcs_rev_write_fd(cf->file_rcs, rnum,
237: STDOUT_FILENO, 1);
1.66 joris 238: }
239:
1.65 reyk 240: return;
241: }
1.87 joris 242:
1.66 joris 243: if (cvs_server_active == 0) {
1.87 joris 244: if (!(co_flags & CO_MERGE)) {
245: oflags = O_WRONLY | O_TRUNC;
246: if (cf->fd != -1) {
247: exists = 1;
248: (void)close(cf->fd);
249: } else {
250: oflags |= O_CREAT;
251: }
252:
253: cf->fd = open(cf->file_path, oflags);
254: if (cf->fd == -1)
255: fatal("cvs_checkout_file: open: %s",
256: strerror(errno));
257:
258: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
259: } else {
260: cvs_merge_file(cf, 1);
1.66 joris 261: }
262:
263: if (fchmod(cf->fd, 0644) == -1)
264: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
265:
1.87 joris 266: if ((exists == 0) && (cf->file_ent == NULL) &&
267: !(co_flags & CO_MERGE))
1.66 joris 268: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 269: else
1.66 joris 270: time(&rcstime);
271:
272: tv[0].tv_sec = rcstime;
273: tv[0].tv_usec = 0;
274: tv[1] = tv[0];
275: if (futimes(cf->fd, tv) == -1)
276: fatal("cvs_checkout_file: futimes: %s",
277: strerror(errno));
1.53 joris 278: } else {
279: time(&rcstime);
1.41 joris 280: }
281:
1.84 joris 282: asctime_r(gmtime(&rcstime), tbuf);
1.58 joris 283: if (tbuf[strlen(tbuf) - 1] == '\n')
284: tbuf[strlen(tbuf) - 1] = '\0';
285:
1.78 xsa 286: if (co_flags & CO_MERGE) {
1.89 xsa 287: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 288: tbuf);
289: } else {
290: strlcpy(timebuf, tbuf, sizeof(timebuf));
291: }
1.41 joris 292:
1.89 xsa 293: if (co_flags & CO_SETSTICKY)
1.97 ! joris 294: if (cvs_specified_tag != NULL)
1.93 niallo 295: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
1.97 ! joris 296: cvs_specified_tag);
1.93 niallo 297: else
298: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
299: rev);
1.89 xsa 300: else
1.60 joris 301: stickytag[0] = '\0';
302:
1.85 xsa 303: kbuf[0] = '\0';
304: if (cf->file_ent != NULL) {
305: if (cf->file_ent->ce_opts != NULL)
306: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
307: } else if (cf->file_rcs->rf_expand != NULL) {
308: kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
309: if (!(kflag & RCS_KWEXP_DEFAULT))
1.89 xsa 310: (void)xsnprintf(kbuf, sizeof(kbuf),
1.85 xsa 311: "-k%s", cf->file_rcs->rf_expand);
312: }
313:
1.89 xsa 314: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.85 xsa 315: cf->file_name, rev, timebuf, kbuf, stickytag);
1.41 joris 316:
1.66 joris 317: if (cvs_server_active == 0) {
1.95 joris 318: if (!(co_flags & CO_REMOVE)) {
319: ent = cvs_ent_open(cf->file_wd);
320: cvs_ent_add(ent, entry);
321: cvs_ent_close(ent, ENT_SYNC);
322: }
1.66 joris 323: } else {
1.87 joris 324: if (co_flags & CO_MERGE) {
325: cvs_merge_file(cf, 1);
326: tosend = cf->file_path;
327: }
328:
1.78 xsa 329: if (co_flags & CO_COMMIT)
1.69 joris 330: cvs_server_update_entry("Checked-in", cf);
1.87 joris 331: else if (co_flags & CO_MERGE)
332: cvs_server_update_entry("Merged", cf);
1.95 joris 333: else if (co_flags & CO_REMOVE)
334: cvs_server_update_entry("Removed", cf);
1.69 joris 335: else
336: cvs_server_update_entry("Updated", cf);
1.68 joris 337:
1.95 joris 338: if (!(co_flags & CO_REMOVE))
339: cvs_remote_output(entry);
1.66 joris 340:
1.95 joris 341: if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87 joris 342: if (!(co_flags & CO_MERGE)) {
1.89 xsa 343: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 344: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 xsa 345:
1.87 joris 346: rcs_rev_write_stmp(cf->file_rcs, rnum,
347: template, 0);
348: tosend = template;
349: }
350:
351: cvs_remote_send_file(tosend);
352:
353: if (!(co_flags & CO_MERGE)) {
354: (void)unlink(template);
355: cvs_worklist_run(&temp_files,
356: cvs_worklist_unlink);
357: }
1.68 joris 358: }
1.66 joris 359: }
1.1 jfb 360: }