Annotation of src/usr.bin/cvs/update.c, Revision 1.46
1.46 ! joris 1: /* $OpenBSD: update.c,v 1.45 2005/12/03 01:02:09 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.11 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.11 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.11 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.11 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.11 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/stat.h>
28:
29: #include <errno.h>
1.34 xsa 30: #include <fcntl.h>
1.1 jfb 31: #include <stdio.h>
32: #include <stdlib.h>
1.34 xsa 33: #include <string.h>
1.1 jfb 34: #include <unistd.h>
35:
36: #include "cvs.h"
37: #include "log.h"
1.4 jfb 38: #include "proto.h"
1.45 joris 39: #include "diff.h"
1.1 jfb 40:
1.42 xsa 41: static int cvs_update_init(struct cvs_cmd *, int, char **, int *);
42: static int cvs_update_pre_exec(struct cvsroot *);
43: static int cvs_update_remote(CVSFILE *, void *);
44: static int cvs_update_local(CVSFILE *, void *);
1.18 joris 45:
1.29 jfb 46: struct cvs_cmd cvs_cmd_update = {
47: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
48: { "up", "upd" },
49: "Bring work tree in sync with repository",
50: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
51: "[-t id] ...",
1.35 xsa 52: "ACD:dfI:j:k:lPpQqRr:t:",
1.29 jfb 53: NULL,
1.37 joris 54: CF_SORT | CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.29 jfb 55: cvs_update_init,
56: cvs_update_pre_exec,
1.26 jfb 57: cvs_update_remote,
1.29 jfb 58: cvs_update_local,
59: NULL,
60: NULL,
1.18 joris 61: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
62: };
1.2 jfb 63:
1.35 xsa 64: static char *date, *rev, *koptstr;
1.31 joris 65: static int dflag, Aflag;
1.35 xsa 66: static int kflag = RCS_KWEXP_DEFAULT;
1.24 joris 67:
1.26 jfb 68: static int
1.29 jfb 69: cvs_update_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 70: {
1.18 joris 71: int ch;
1.2 jfb 72:
1.31 joris 73: dflag = Aflag = 0;
1.32 xsa 74: date = NULL;
75: rev = NULL;
1.24 joris 76:
1.29 jfb 77: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 78: switch (ch) {
79: case 'A':
1.24 joris 80: Aflag = 1;
81: break;
1.1 jfb 82: case 'C':
83: case 'D':
1.32 xsa 84: date = optarg;
85: break;
1.1 jfb 86: case 'd':
1.24 joris 87: dflag = 1;
88: break;
1.1 jfb 89: case 'f':
1.2 jfb 90: break;
1.35 xsa 91: case 'I':
92: break;
93: case 'j':
94: break;
95: case 'k':
96: koptstr = optarg;
97: kflag = rcs_kflag_get(koptstr);
98: if (RCS_KWEXP_INVAL(kflag)) {
99: cvs_log(LP_ERR,
100: "invalid RCS keyword expansion mode");
101: rcs_kflag_usage();
102: return (CVS_EX_USAGE);
103: }
104: break;
1.1 jfb 105: case 'l':
1.29 jfb 106: cmd->file_flags &= ~CF_RECURSE;
1.2 jfb 107: break;
1.1 jfb 108: case 'P':
1.31 joris 109: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24 joris 110: break;
1.1 jfb 111: case 'p':
1.27 xsa 112: cvs_noexec = 1; /* no locks will be created */
113: break;
1.1 jfb 114: case 'Q':
115: case 'q':
1.2 jfb 116: break;
1.1 jfb 117: case 'R':
1.29 jfb 118: cmd->file_flags |= CF_RECURSE;
1.2 jfb 119: break;
1.1 jfb 120: case 'r':
1.32 xsa 121: rev = optarg;
1.1 jfb 122: break;
123: default:
1.21 joris 124: return (CVS_EX_USAGE);
1.1 jfb 125: }
1.32 xsa 126: }
127:
1.18 joris 128: *arg = optind;
1.2 jfb 129: return (0);
130: }
131:
1.26 jfb 132: static int
1.29 jfb 133: cvs_update_pre_exec(struct cvsroot *root)
1.24 joris 134: {
1.36 joris 135: if (root->cr_method != CVS_METHOD_LOCAL) {
1.38 joris 136: if ((cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS) &&
1.36 joris 137: (cvs_sendarg(root, "-P", 0) < 0))
138: return (CVS_EX_PROTO);
139: if (Aflag && cvs_sendarg(root, "-A", 0) < 0)
140: return (CVS_EX_PROTO);
141: if (dflag && cvs_sendarg(root, "-d", 0) < 0)
142: return (CVS_EX_PROTO);
1.33 xsa 143:
1.36 joris 144: if ((rev != NULL) && ((cvs_sendarg(root, "-r", 0) < 0) ||
145: (cvs_sendarg(root, rev, 0) < 0)))
1.33 xsa 146: return (CVS_EX_PROTO);
1.35 xsa 147:
1.36 joris 148: if ((date != NULL) && ((cvs_sendarg(root, "-D", 0) < 0) ||
149: (cvs_sendarg(root, date, 0) < 0)))
150: return (CVS_EX_PROTO);
151: }
1.35 xsa 152:
1.24 joris 153: return (0);
154: }
1.2 jfb 155:
156: /*
1.26 jfb 157: * cvs_update_remote()
1.2 jfb 158: *
1.12 jfb 159: * Update a single file. In the case where we act as client, send any
160: * pertinent information about that file to the server.
1.2 jfb 161: */
1.26 jfb 162: static int
163: cvs_update_remote(CVSFILE *cf, void *arg)
1.2 jfb 164: {
1.26 jfb 165: int ret;
1.44 xsa 166: char fpath[MAXPATHLEN];
1.2 jfb 167: struct cvsroot *root;
168:
1.12 jfb 169: ret = 0;
170: root = CVS_DIR_ROOT(cf);
1.9 jfb 171:
1.13 jfb 172: if (cf->cf_type == DT_DIR) {
1.26 jfb 173: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
174: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
175: cf->cf_name);
176: else
177: ret = cvs_senddir(root, cf);
1.13 jfb 178:
1.28 joris 179: if (ret == -1)
180: ret = CVS_EX_PROTO;
181:
1.12 jfb 182: return (ret);
1.2 jfb 183: }
184:
1.12 jfb 185: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2 jfb 186:
1.26 jfb 187: if (cvs_sendentry(root, cf) < 0)
188: return (CVS_EX_PROTO);
1.41 joris 189:
190: if (!(cf->cf_flags & CVS_FILE_ONDISK))
191: return (0);
1.2 jfb 192:
1.26 jfb 193: switch (cf->cf_cvstat) {
194: case CVS_FST_UNKNOWN:
195: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
196: break;
197: case CVS_FST_UPTODATE:
198: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
199: break;
200: case CVS_FST_ADDED:
201: case CVS_FST_MODIFIED:
202: ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
203: if (ret == 0)
204: ret = cvs_sendfile(root, fpath);
205: break;
206: default:
207: break;
1.2 jfb 208: }
209:
1.28 joris 210: if (ret == -1)
211: ret = CVS_EX_PROTO;
212:
1.12 jfb 213: return (ret);
1.2 jfb 214: }
215:
216: /*
1.26 jfb 217: * cvs_update_local()
1.2 jfb 218: */
1.26 jfb 219: static int
220: cvs_update_local(CVSFILE *cf, void *arg)
1.2 jfb 221: {
1.45 joris 222: int ret, islocal, revdiff;
1.39 xsa 223: char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.45 joris 224: char *repo;
1.26 jfb 225: RCSFILE *rf;
1.45 joris 226: RCSNUM *frev;
227: BUF *fbuf;
228: struct cvsroot *root;
1.26 jfb 229:
1.45 joris 230: revdiff = ret = 0;
1.26 jfb 231: rf = NULL;
1.45 joris 232: islocal = (cvs_cmdop != CVS_OP_SERVER);
1.26 jfb 233:
1.45 joris 234: root = CVS_DIR_ROOT(cf);
235: repo = CVS_DIR_REPO(cf);
1.29 jfb 236: cvs_file_getpath(cf, fpath, sizeof(fpath));
237:
1.37 joris 238: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.46 ! joris 239: if (verbosity > 1)
! 240: cvs_printf("? %s\n", fpath);
1.37 joris 241: return (CVS_EX_OK);
242: }
243:
1.26 jfb 244: if (cf->cf_type == DT_DIR) {
1.45 joris 245: if (verbosity > 1)
246: cvs_log(LP_NOTICE, "Updating %s", fpath);
247:
1.26 jfb 248: return (CVS_EX_OK);
249: }
250:
1.39 xsa 251: if (cvs_rcs_getpath(cf, rcspath, sizeof(rcspath)) == NULL)
1.28 joris 252: return (CVS_EX_DATA);
1.1 jfb 253:
1.45 joris 254: /*
255: * Only open the RCS file for files that have not been added.
256: */
257: if (cf->cf_cvstat != CVS_FST_ADDED) {
258: rf = rcs_open(rcspath, RCS_READ);
259:
260: /*
261: * If there is no RCS file available in the repository
262: * directory that matches this file, it's gone.
263: * XXX: so what about the Attic?
264: */
265: if (rf == NULL) {
266: cvs_log(LP_WARN, "%s is no longer in the repository",
267: fpath);
268: if (cvs_checkout_rev(NULL, NULL, cf, fpath,
269: islocal, CHECKOUT_REV_REMOVED) < 0)
270: return (CVS_EX_FILE);
271: return (CVS_EX_OK);
272: }
273: }
274:
275: /* set keyword expansion */
276: /* XXX look at cf->cf_opts as well for this */
277: if (rcs_kwexp_set(rf, kflag) < 0) {
278: if (rf != NULL)
279: rcs_close(rf);
1.26 jfb 280: return (CVS_EX_DATA);
281: }
282:
1.45 joris 283: /* fill in the correct revision */
284: if (rev != NULL) {
285: if ((frev = rcsnum_parse(rev)) == NULL) {
286: if (rf != NULL)
287: rcs_close(rf);
288: return (CVS_EX_DATA);
289: }
290: } else {
291: frev = rf->rf_head;
292: }
1.26 jfb 293:
1.45 joris 294: /*
295: * Compare the headrevision with the revision we currently have.
296: */
297: if (rf != NULL && cf->cf_lrev != NULL)
298: revdiff = rcsnum_cmp(cf->cf_lrev, frev, 0);
299:
300: switch (cf->cf_cvstat) {
301: case CVS_FST_MODIFIED:
302: /*
303: * If the file has been modified but there is a newer version
304: * available, we try to merge it into the existing changes.
305: */
306: if (revdiff == 1) {
307: fbuf = cvs_diff3(rf, fpath, cf->cf_lrev, frev);
308: if (fbuf == NULL) {
309: cvs_log(LP_ERR, "merge failed");
310: break;
311: }
312:
313: /*
314: * Please note fbuf will be free'd in cvs_checkout_rev
315: */
316: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
317: CHECKOUT_REV_MERGED, fbuf) != -1) {
318: cvs_printf("%c %s\n",
319: (diff3_conflicts > 0) ? 'C' : 'M',
320: fpath);
321: if (diff3_conflicts > 0)
322: cf->cf_cvstat = CVS_FST_CONFLICT;
323: }
324: } else {
325: cvs_printf("M %s\n", fpath);
326: }
327: break;
328: case CVS_FST_ADDED:
329: cvs_printf("A %s\n", fpath);
330: break;
331: case CVS_FST_REMOVED:
332: cvs_printf("R %s\n", fpath);
333: break;
334: case CVS_FST_CONFLICT:
335: cvs_printf("C %s\n", fpath);
336: break;
337: case CVS_FST_LOST:
338: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
339: CHECKOUT_REV_CREATED) != -1) {
340: cf->cf_cvstat = CVS_FST_UPTODATE;
341: cvs_printf("U %s\n", fpath);
342: }
343: break;
344: case CVS_FST_UPTODATE:
345: if (revdiff == 1) {
346: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
347: CHECKOUT_REV_CREATED) != -1)
348: cvs_printf("P %s\n", fpath);
349: }
350: break;
351: default:
352: break;
353: }
354:
355: if ((frev != NULL) && (frev != rf->rf_head))
356: rcsnum_free(frev);
357: if (rf != NULL)
358: rcs_close(rf);
359:
360: return (CVS_EX_OK);
1.1 jfb 361: }