Annotation of src/usr.bin/cvs/checkout.c, Revision 1.98
1.98 ! xsa 1: /* $OpenBSD: checkout.c,v 1.97 2007/07/03 13:22:42 joris 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':
1.98 ! xsa 110: break;
! 111: case 'r':
! 112: cvs_specified_tag = optarg;
1.61 xsa 113: break;
114: default:
115: fatal("%s", cvs_cmd_export.cmd_synopsis);
116: }
117: }
118:
119: argc -= optind;
120: argv += optind;
121:
122: if (argc == 0)
123: fatal("%s", cvs_cmd_export.cmd_synopsis);
124:
125: checkout_check_repository(argc, argv);
126:
127: return (0);
128: }
129:
130: static void
131: checkout_check_repository(int argc, char **argv)
132: {
1.67 xsa 133: int i;
1.61 xsa 134: char repo[MAXPATHLEN];
135: struct stat st;
1.79 joris 136: struct cvs_recursion cr;
137:
138: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
139: cvs_client_connect_to_server();
140:
1.97 joris 141: if (cvs_specified_tag != NULL)
142: cvs_client_send_request("Argument -r%s",
143: cvs_specified_tag);
1.79 joris 144: if (reset_stickies == 1)
145: cvs_client_send_request("Argument -A");
1.80 xsa 146:
147: if (!(flags & CR_RECURSE_DIRS))
148: cvs_client_send_request("Argument -l");
1.79 joris 149:
150: if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
151: cvs_client_send_request("Argument -P");
152:
153: cr.enterdir = NULL;
154: cr.leavedir = NULL;
155: cr.fileproc = cvs_client_sendfile;
156: cr.flags = flags;
157:
158: cvs_file_run(argc, argv, &cr);
159:
160: cvs_client_send_files(argv, argc);
161: cvs_client_senddir(".");
162:
163: cvs_client_send_request("%s",
164: (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
165:
166: cvs_client_get_responses();
167:
168: return;
169: }
1.61 xsa 170:
1.53 joris 171: for (i = 0; i < argc; i++) {
1.90 xsa 172: (void)xsnprintf(repo, sizeof(repo), "%s/%s",
173: current_cvsroot->cr_dir, argv[i]);
1.13 jfb 174:
1.53 joris 175: if (stat(repo, &st) == -1) {
1.81 thib 176: cvs_log(LP_ERR, "cannot find module `%s' - ignored",
1.53 joris 177: argv[i]);
178: continue;
179: }
1.97 joris 180:
181: cvs_mkpath(argv[i], cvs_specified_tag);
1.53 joris 182: checkout_repository(repo, argv[i]);
1.33 xsa 183: }
1.14 joris 184: }
1.9 jfb 185:
1.53 joris 186: static void
187: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 188: {
1.53 joris 189: struct cvs_flisthead fl, dl;
190: struct cvs_recursion cr;
191:
192: TAILQ_INIT(&fl);
193: TAILQ_INIT(&dl);
1.94 joris 194:
195: cvs_history_add(CVS_HISTORY_CHECKOUT, NULL, wdbase);
1.41 joris 196:
1.57 joris 197: build_dirs = 1;
1.53 joris 198: cr.enterdir = cvs_update_enterdir;
1.54 joris 199: cr.leavedir = cvs_update_leavedir;
1.64 joris 200: cr.fileproc = cvs_update_local;
1.78 xsa 201: cr.flags = flags;
1.41 joris 202:
1.53 joris 203: cvs_repository_lock(repobase);
1.56 joris 204: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 205:
1.53 joris 206: cvs_file_walklist(&fl, &cr);
207: cvs_file_freelist(&fl);
1.23 joris 208:
1.53 joris 209: cvs_repository_unlock(repobase);
1.23 joris 210:
1.53 joris 211: cvs_file_walklist(&dl, &cr);
212: cvs_file_freelist(&dl);
213: }
1.23 joris 214:
1.58 joris 215: void
1.78 xsa 216: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, int co_flags)
1.53 joris 217: {
1.89 xsa 218: int kflag, oflags, exists;
1.53 joris 219: time_t rcstime;
220: CVSENTRIES *ent;
221: struct timeval tv[2];
1.87 joris 222: char *tosend;
1.97 joris 223: char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.96 xsa 224: char kbuf[8], stickytag[32], rev[CVS_REV_BUFSZ];
225: char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22 jfb 226:
1.87 joris 227: exists = 0;
228: tosend = NULL;
1.53 joris 229: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 230:
1.66 joris 231: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 232: cf->file_path, rev, co_flags,
1.66 joris 233: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 234:
1.78 xsa 235: if (co_flags & CO_DUMP) {
1.66 joris 236: if (cvs_server_active) {
237: cvs_printf("dump file %s to client\n", cf->file_path);
238: } else {
1.71 joris 239: rcs_rev_write_fd(cf->file_rcs, rnum,
240: STDOUT_FILENO, 1);
1.66 joris 241: }
242:
1.65 reyk 243: return;
244: }
1.87 joris 245:
1.66 joris 246: if (cvs_server_active == 0) {
1.87 joris 247: if (!(co_flags & CO_MERGE)) {
248: oflags = O_WRONLY | O_TRUNC;
249: if (cf->fd != -1) {
250: exists = 1;
251: (void)close(cf->fd);
252: } else {
253: oflags |= O_CREAT;
254: }
255:
256: cf->fd = open(cf->file_path, oflags);
257: if (cf->fd == -1)
258: fatal("cvs_checkout_file: open: %s",
259: strerror(errno));
260:
261: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
262: } else {
263: cvs_merge_file(cf, 1);
1.66 joris 264: }
265:
266: if (fchmod(cf->fd, 0644) == -1)
267: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
268:
1.87 joris 269: if ((exists == 0) && (cf->file_ent == NULL) &&
270: !(co_flags & CO_MERGE))
1.66 joris 271: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 272: else
1.66 joris 273: time(&rcstime);
274:
275: tv[0].tv_sec = rcstime;
276: tv[0].tv_usec = 0;
277: tv[1] = tv[0];
278: if (futimes(cf->fd, tv) == -1)
279: fatal("cvs_checkout_file: futimes: %s",
280: strerror(errno));
1.53 joris 281: } else {
282: time(&rcstime);
1.41 joris 283: }
284:
1.84 joris 285: asctime_r(gmtime(&rcstime), tbuf);
1.58 joris 286: if (tbuf[strlen(tbuf) - 1] == '\n')
287: tbuf[strlen(tbuf) - 1] = '\0';
288:
1.78 xsa 289: if (co_flags & CO_MERGE) {
1.89 xsa 290: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 291: tbuf);
292: } else {
293: strlcpy(timebuf, tbuf, sizeof(timebuf));
294: }
1.41 joris 295:
1.89 xsa 296: if (co_flags & CO_SETSTICKY)
1.97 joris 297: if (cvs_specified_tag != NULL)
1.93 niallo 298: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
1.97 joris 299: cvs_specified_tag);
1.93 niallo 300: else
301: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
302: rev);
1.89 xsa 303: else
1.60 joris 304: stickytag[0] = '\0';
305:
1.85 xsa 306: kbuf[0] = '\0';
307: if (cf->file_ent != NULL) {
308: if (cf->file_ent->ce_opts != NULL)
309: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
310: } else if (cf->file_rcs->rf_expand != NULL) {
311: kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
312: if (!(kflag & RCS_KWEXP_DEFAULT))
1.89 xsa 313: (void)xsnprintf(kbuf, sizeof(kbuf),
1.85 xsa 314: "-k%s", cf->file_rcs->rf_expand);
315: }
316:
1.89 xsa 317: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.85 xsa 318: cf->file_name, rev, timebuf, kbuf, stickytag);
1.41 joris 319:
1.66 joris 320: if (cvs_server_active == 0) {
1.95 joris 321: if (!(co_flags & CO_REMOVE)) {
322: ent = cvs_ent_open(cf->file_wd);
323: cvs_ent_add(ent, entry);
324: cvs_ent_close(ent, ENT_SYNC);
325: }
1.66 joris 326: } else {
1.87 joris 327: if (co_flags & CO_MERGE) {
328: cvs_merge_file(cf, 1);
329: tosend = cf->file_path;
330: }
331:
1.78 xsa 332: if (co_flags & CO_COMMIT)
1.69 joris 333: cvs_server_update_entry("Checked-in", cf);
1.87 joris 334: else if (co_flags & CO_MERGE)
335: cvs_server_update_entry("Merged", cf);
1.95 joris 336: else if (co_flags & CO_REMOVE)
337: cvs_server_update_entry("Removed", cf);
1.69 joris 338: else
339: cvs_server_update_entry("Updated", cf);
1.68 joris 340:
1.95 joris 341: if (!(co_flags & CO_REMOVE))
342: cvs_remote_output(entry);
1.66 joris 343:
1.95 joris 344: if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87 joris 345: if (!(co_flags & CO_MERGE)) {
1.89 xsa 346: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 347: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 xsa 348:
1.87 joris 349: rcs_rev_write_stmp(cf->file_rcs, rnum,
350: template, 0);
351: tosend = template;
352: }
353:
354: cvs_remote_send_file(tosend);
355:
356: if (!(co_flags & CO_MERGE)) {
357: (void)unlink(template);
358: cvs_worklist_run(&temp_files,
359: cvs_worklist_unlink);
360: }
1.68 joris 361: }
1.66 joris 362: }
1.1 jfb 363: }