Annotation of src/usr.bin/cvs/checkout.c, Revision 1.79
1.79 ! joris 1: /* $OpenBSD: checkout.c,v 1.78 2007/01/16 08:33: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.78 xsa 35: static int flags = CR_REPO | CR_RECURSE_DIRS;
36:
1.22 jfb 37: struct cvs_cmd cvs_cmd_checkout = {
1.63 joris 38: CVS_OP_CHECKOUT, 0, "checkout",
1.22 jfb 39: { "co", "get" },
1.53 joris 40: "Checkout a working copy of a repository",
1.33 xsa 41: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 42: "[-t id] module ...",
1.77 xsa 43: "AcD:d:fj:k:lNnPpRr:st:",
1.59 joris 44: NULL,
45: cvs_checkout
46: };
47:
48: struct cvs_cmd cvs_cmd_export = {
1.63 joris 49: CVS_OP_EXPORT, 0, "export",
1.59 joris 50: { "exp", "ex" },
51: "Export sources from CVS, similar to checkout",
1.61 xsa 52: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
53: "D:d:k:flNnRr:",
1.22 jfb 54: NULL,
1.61 xsa 55: cvs_export
1.33 xsa 56: };
57:
1.53 joris 58: int
59: cvs_checkout(int argc, char **argv)
1.1 jfb 60: {
1.78 xsa 61: int ch;
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: {
1.78 xsa 95: int ch;
1.61 xsa 96:
97: prune_dirs = 1;
98:
99: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
100: switch (ch) {
101: case 'l':
102: flags &= ~CR_RECURSE_DIRS;
103: break;
104: case 'R':
105: break;
106: default:
107: fatal("%s", cvs_cmd_export.cmd_synopsis);
108: }
109: }
110:
111: argc -= optind;
112: argv += optind;
113:
114: if (argc == 0)
115: fatal("%s", cvs_cmd_export.cmd_synopsis);
116:
117: checkout_check_repository(argc, argv);
118:
119: return (0);
120: }
121:
122: static void
123: checkout_check_repository(int argc, char **argv)
124: {
1.67 xsa 125: int i;
1.61 xsa 126: char repo[MAXPATHLEN];
127: struct stat st;
1.79 ! joris 128: struct cvs_recursion cr;
! 129:
! 130: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
! 131: cvs_client_connect_to_server();
! 132:
! 133: if (reset_stickies == 1)
! 134: cvs_client_send_request("Argument -A");
! 135:
! 136: if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
! 137: cvs_client_send_request("Argument -P");
! 138:
! 139: cr.enterdir = NULL;
! 140: cr.leavedir = NULL;
! 141: cr.fileproc = cvs_client_sendfile;
! 142: cr.flags = flags;
! 143:
! 144: cvs_file_run(argc, argv, &cr);
! 145:
! 146: cvs_client_send_files(argv, argc);
! 147: cvs_client_senddir(".");
! 148:
! 149: cvs_client_send_request("%s",
! 150: (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
! 151:
! 152: cvs_client_get_responses();
! 153:
! 154: return;
! 155: }
1.61 xsa 156:
1.53 joris 157: for (i = 0; i < argc; i++) {
158: cvs_mkpath(argv[i]);
1.1 jfb 159:
1.67 xsa 160: if (cvs_path_cat(current_cvsroot->cr_dir, argv[i], repo,
161: sizeof(repo)) >= sizeof(repo))
162: fatal("checkout_check_repository: truncation");
1.13 jfb 163:
1.53 joris 164: if (stat(repo, &st) == -1) {
165: cvs_log(LP_ERR, "cannot find repository %s - ignored",
166: argv[i]);
167: continue;
168: }
169:
170: checkout_repository(repo, argv[i]);
1.33 xsa 171: }
1.14 joris 172: }
1.9 jfb 173:
1.53 joris 174: static void
175: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 176: {
1.53 joris 177: struct cvs_flisthead fl, dl;
178: struct cvs_recursion cr;
179:
180: TAILQ_INIT(&fl);
181: TAILQ_INIT(&dl);
1.41 joris 182:
1.57 joris 183: build_dirs = 1;
1.53 joris 184: cr.enterdir = cvs_update_enterdir;
1.54 joris 185: cr.leavedir = cvs_update_leavedir;
1.64 joris 186: cr.fileproc = cvs_update_local;
1.78 xsa 187: cr.flags = flags;
1.41 joris 188:
1.53 joris 189: cvs_repository_lock(repobase);
1.56 joris 190: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 191:
1.53 joris 192: cvs_file_walklist(&fl, &cr);
193: cvs_file_freelist(&fl);
1.23 joris 194:
1.53 joris 195: cvs_repository_unlock(repobase);
1.23 joris 196:
1.53 joris 197: cvs_file_walklist(&dl, &cr);
198: cvs_file_freelist(&dl);
199: }
1.23 joris 200:
1.58 joris 201: void
1.78 xsa 202: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, int co_flags)
1.53 joris 203: {
204: int l, oflags, exists;
205: time_t rcstime;
206: CVSENTRIES *ent;
207: struct timeval tv[2];
1.72 joris 208: char *template, *p, *entry, rev[16], timebuf[64];
209: char tbuf[32], stickytag[32];
1.22 jfb 210:
1.53 joris 211: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 212:
1.66 joris 213: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 214: cf->file_path, rev, co_flags,
1.66 joris 215: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 216:
1.78 xsa 217: if (co_flags & CO_DUMP) {
1.66 joris 218: if (cvs_server_active) {
219: cvs_printf("dump file %s to client\n", cf->file_path);
220: } else {
1.71 joris 221: rcs_rev_write_fd(cf->file_rcs, rnum,
222: STDOUT_FILENO, 1);
1.66 joris 223: }
224:
1.65 reyk 225: return;
226: }
1.42 joris 227:
1.66 joris 228: if (cvs_server_active == 0) {
229: oflags = O_WRONLY | O_TRUNC;
230: if (cf->fd != -1) {
231: exists = 1;
232: (void)close(cf->fd);
233: } else {
234: exists = 0;
235: oflags |= O_CREAT;
236: }
237:
238: cf->fd = open(cf->file_path, oflags);
239: if (cf->fd == -1)
240: fatal("cvs_checkout_file: open: %s", strerror(errno));
241:
1.71 joris 242: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
1.66 joris 243:
244: if (fchmod(cf->fd, 0644) == -1)
245: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
246:
247: if (exists == 0) {
248: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
249: rcstime = cvs_hack_time(rcstime, 0);
250: } else {
251: time(&rcstime);
252: }
253:
254: tv[0].tv_sec = rcstime;
255: tv[0].tv_usec = 0;
256: tv[1] = tv[0];
257: if (futimes(cf->fd, tv) == -1)
258: fatal("cvs_checkout_file: futimes: %s",
259: strerror(errno));
1.53 joris 260: } else {
261: time(&rcstime);
1.41 joris 262: }
263:
1.62 joris 264: rcstime = cvs_hack_time(rcstime, 1);
1.41 joris 265:
1.58 joris 266: ctime_r(&rcstime, tbuf);
267: if (tbuf[strlen(tbuf) - 1] == '\n')
268: tbuf[strlen(tbuf) - 1] = '\0';
269:
1.78 xsa 270: if (co_flags & CO_MERGE) {
1.58 joris 271: l = snprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
272: tbuf);
273: if (l == -1 || l >= (int)sizeof(timebuf))
274: fatal("cvs_checkout_file: overflow");
275: } else {
276: strlcpy(timebuf, tbuf, sizeof(timebuf));
277: }
1.41 joris 278:
1.78 xsa 279: if (co_flags & CO_SETSTICKY) {
1.60 joris 280: l = snprintf(stickytag, sizeof(stickytag), "T%s", rev);
281: if (l == -1 || l >= (int)sizeof(stickytag))
282: fatal("cvs_checkout_file: overflow");
283: } else {
284: stickytag[0] = '\0';
285: }
286:
1.53 joris 287: entry = xmalloc(CVS_ENT_MAXLINELEN);
1.60 joris 288: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//%s", cf->file_name,
289: rev, timebuf, stickytag);
1.41 joris 290:
1.66 joris 291: if (cvs_server_active == 0) {
292: ent = cvs_ent_open(cf->file_wd);
293: cvs_ent_add(ent, entry);
294: cvs_ent_close(ent, ENT_SYNC);
295: } else {
296: if ((p = strrchr(cf->file_rpath, ',')) != NULL)
297: *p = '\0';
298:
1.78 xsa 299: if (co_flags & CO_COMMIT)
1.69 joris 300: cvs_server_update_entry("Checked-in", cf);
301: else
302: cvs_server_update_entry("Updated", cf);
1.68 joris 303:
1.66 joris 304: cvs_remote_output(entry);
305:
1.78 xsa 306: if (!(co_flags & CO_COMMIT)) {
1.72 joris 307: (void)xasprintf(&template,
308: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.66 joris 309:
1.72 joris 310: /* XXX - fd race below */
1.75 joris 311: rcs_rev_write_stmp(cf->file_rcs, rnum, template, 0);
1.72 joris 312: cvs_remote_send_file(template);
313: (void)unlink(template);
1.74 joris 314: xfree(template);
1.68 joris 315: }
1.66 joris 316:
317: if (p != NULL)
318: *p = ',';
319: }
1.41 joris 320:
1.53 joris 321: xfree(entry);
1.1 jfb 322: }