Annotation of src/usr.bin/cvs/update.c, Revision 1.36
1.36 ! joris 1: /* $OpenBSD: update.c,v 1.35 2005/06/17 08:40:42 xsa 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/param.h>
28: #include <sys/stat.h>
29:
30: #include <errno.h>
1.34 xsa 31: #include <fcntl.h>
1.1 jfb 32: #include <stdio.h>
33: #include <stdlib.h>
1.34 xsa 34: #include <string.h>
1.1 jfb 35: #include <unistd.h>
36:
37: #include "cvs.h"
38: #include "log.h"
1.4 jfb 39: #include "proto.h"
1.1 jfb 40:
41:
1.29 jfb 42: static int cvs_update_init (struct cvs_cmd *, int, char **, int *);
43: static int cvs_update_pre_exec (struct cvsroot *);
1.26 jfb 44: static int cvs_update_remote (CVSFILE *, void *);
45: static int cvs_update_local (CVSFILE *, void *);
1.18 joris 46:
1.29 jfb 47:
48: struct cvs_cmd cvs_cmd_update = {
49: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
50: { "up", "upd" },
51: "Bring work tree in sync with repository",
52: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
53: "[-t id] ...",
1.35 xsa 54: "ACD:dfI:j:k:lPpQqRr:t:",
1.29 jfb 55: NULL,
56: CF_SORT | CF_RECURSE | CF_IGNORE | CF_KNOWN | CF_NOSYMS,
57: cvs_update_init,
58: cvs_update_pre_exec,
1.26 jfb 59: cvs_update_remote,
1.29 jfb 60: cvs_update_local,
61: NULL,
62: NULL,
1.18 joris 63: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
64: };
1.2 jfb 65:
1.35 xsa 66: static char *date, *rev, *koptstr;
1.31 joris 67: static int dflag, Aflag;
1.35 xsa 68: static int kflag = RCS_KWEXP_DEFAULT;
1.24 joris 69:
1.26 jfb 70: static int
1.29 jfb 71: cvs_update_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 72: {
1.18 joris 73: int ch;
1.2 jfb 74:
1.31 joris 75: dflag = Aflag = 0;
1.32 xsa 76: date = NULL;
77: rev = NULL;
1.24 joris 78:
1.29 jfb 79: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 80: switch (ch) {
81: case 'A':
1.24 joris 82: Aflag = 1;
83: break;
1.1 jfb 84: case 'C':
85: case 'D':
1.32 xsa 86: date = optarg;
87: break;
1.1 jfb 88: case 'd':
1.24 joris 89: dflag = 1;
90: break;
1.1 jfb 91: case 'f':
1.2 jfb 92: break;
1.35 xsa 93: case 'I':
94: break;
95: case 'j':
96: break;
97: case 'k':
98: koptstr = optarg;
99: kflag = rcs_kflag_get(koptstr);
100: if (RCS_KWEXP_INVAL(kflag)) {
101: cvs_log(LP_ERR,
102: "invalid RCS keyword expansion mode");
103: rcs_kflag_usage();
104: return (CVS_EX_USAGE);
105: }
106: break;
1.1 jfb 107: case 'l':
1.29 jfb 108: cmd->file_flags &= ~CF_RECURSE;
1.2 jfb 109: break;
1.1 jfb 110: case 'P':
1.31 joris 111: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24 joris 112: break;
1.1 jfb 113: case 'p':
1.27 xsa 114: cvs_noexec = 1; /* no locks will be created */
115: break;
1.1 jfb 116: case 'Q':
117: case 'q':
1.2 jfb 118: break;
1.1 jfb 119: case 'R':
1.29 jfb 120: cmd->file_flags |= CF_RECURSE;
1.2 jfb 121: break;
1.1 jfb 122: case 'r':
1.32 xsa 123: rev = optarg;
1.1 jfb 124: break;
125: default:
1.21 joris 126: return (CVS_EX_USAGE);
1.1 jfb 127: }
1.32 xsa 128: }
129:
1.18 joris 130: *arg = optind;
1.2 jfb 131: return (0);
132: }
133:
1.26 jfb 134: static int
1.29 jfb 135: cvs_update_pre_exec(struct cvsroot *root)
1.24 joris 136: {
1.36 ! joris 137: if (root->cr_method != CVS_METHOD_LOCAL) {
! 138: if ((cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS) &&
! 139: (cvs_sendarg(root, "-P", 0) < 0))
! 140: return (CVS_EX_PROTO);
! 141: if (Aflag && cvs_sendarg(root, "-A", 0) < 0)
! 142: return (CVS_EX_PROTO);
! 143: if (dflag && cvs_sendarg(root, "-d", 0) < 0)
! 144: return (CVS_EX_PROTO);
1.33 xsa 145:
1.36 ! joris 146: if ((rev != NULL) && ((cvs_sendarg(root, "-r", 0) < 0) ||
! 147: (cvs_sendarg(root, rev, 0) < 0)))
1.33 xsa 148: return (CVS_EX_PROTO);
1.35 xsa 149:
1.36 ! joris 150: if ((date != NULL) && ((cvs_sendarg(root, "-D", 0) < 0) ||
! 151: (cvs_sendarg(root, date, 0) < 0)))
! 152: return (CVS_EX_PROTO);
! 153: }
1.35 xsa 154:
1.24 joris 155: return (0);
156: }
1.2 jfb 157:
158: /*
1.26 jfb 159: * cvs_update_remote()
1.2 jfb 160: *
1.12 jfb 161: * Update a single file. In the case where we act as client, send any
162: * pertinent information about that file to the server.
1.2 jfb 163: */
1.26 jfb 164: static int
165: cvs_update_remote(CVSFILE *cf, void *arg)
1.2 jfb 166: {
1.26 jfb 167: int ret;
168: char *repo, fpath[MAXPATHLEN];
1.2 jfb 169: struct cvsroot *root;
170:
1.12 jfb 171: ret = 0;
172: root = CVS_DIR_ROOT(cf);
173: repo = CVS_DIR_REPO(cf);
1.9 jfb 174:
1.13 jfb 175: if (cf->cf_type == DT_DIR) {
1.26 jfb 176: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
177: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
178: cf->cf_name);
179: else
180: ret = cvs_senddir(root, cf);
1.13 jfb 181:
1.28 joris 182: if (ret == -1)
183: ret = CVS_EX_PROTO;
184:
1.12 jfb 185: return (ret);
1.2 jfb 186: }
187:
1.12 jfb 188: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2 jfb 189:
1.26 jfb 190: if (cvs_sendentry(root, cf) < 0)
191: return (CVS_EX_PROTO);
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.26 jfb 222: int ret, l;
223: char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
224: RCSFILE *rf;
225: struct cvsroot *root;
226:
227: ret = 0;
228: rf = NULL;
229: root = CVS_DIR_ROOT(cf);
230: repo = CVS_DIR_REPO(cf);
231:
1.29 jfb 232: cvs_file_getpath(cf, fpath, sizeof(fpath));
233:
1.26 jfb 234: if (cf->cf_type == DT_DIR) {
1.29 jfb 235: cvs_log(LP_INFO, "Updating %s", fpath);
1.26 jfb 236: return (CVS_EX_OK);
237: }
238:
239: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
240: cvs_printf("? %s\n", fpath);
241: return (0);
242: }
243:
244: l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
245: root->cr_dir, repo, cf->cf_name, RCS_FILE_EXT);
246: if (l == -1 || l >= (int)sizeof(rcspath)) {
247: errno = ENAMETOOLONG;
248: cvs_log(LP_ERRNO, "%s", rcspath);
1.28 joris 249: return (CVS_EX_DATA);
1.26 jfb 250: }
1.1 jfb 251:
1.26 jfb 252: rf = rcs_open(rcspath, RCS_RDWR);
253: if (rf == NULL) {
1.29 jfb 254: printf("failed here?\n");
1.26 jfb 255: return (CVS_EX_DATA);
256: }
257:
258: rcs_close(rf);
259:
260: return (ret);
1.1 jfb 261: }