Annotation of src/usr.bin/cvs/update.c, Revision 1.35
1.35 ! xsa 1: /* $OpenBSD: update.c,v 1.34 2005/05/31 08:58:48 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.31 joris 137: if ((cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS) &&
138: (cvs_sendarg(root, "-P", 0) < 0))
1.24 joris 139: return (CVS_EX_PROTO);
1.25 joris 140: if (Aflag && cvs_sendarg(root, "-A", 0) < 0)
1.24 joris 141: return (CVS_EX_PROTO);
142: if (dflag && cvs_sendarg(root, "-d", 0) < 0)
143: return (CVS_EX_PROTO);
1.33 xsa 144:
1.35 ! xsa 145: if ((rev != NULL) && ((cvs_sendarg(root, "-r", 0) < 0) ||
! 146: (cvs_sendarg(root, rev, 0) < 0)))
1.33 xsa 147: return (CVS_EX_PROTO);
1.35 ! xsa 148:
! 149: if ((date != NULL) && ((cvs_sendarg(root, "-D", 0) < 0) ||
! 150: (cvs_sendarg(root, date, 0) < 0)))
! 151: return (CVS_EX_PROTO);
! 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;
166: char *repo, fpath[MAXPATHLEN];
1.2 jfb 167: struct cvsroot *root;
168:
1.12 jfb 169: ret = 0;
170: root = CVS_DIR_ROOT(cf);
171: repo = CVS_DIR_REPO(cf);
1.9 jfb 172:
1.13 jfb 173: if (cf->cf_type == DT_DIR) {
1.26 jfb 174: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
175: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
176: cf->cf_name);
177: else
178: ret = cvs_senddir(root, cf);
1.13 jfb 179:
1.28 joris 180: if (ret == -1)
181: ret = CVS_EX_PROTO;
182:
1.12 jfb 183: return (ret);
1.2 jfb 184: }
185:
1.12 jfb 186: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2 jfb 187:
1.26 jfb 188: if (cvs_sendentry(root, cf) < 0)
189: return (CVS_EX_PROTO);
1.2 jfb 190:
1.26 jfb 191: switch (cf->cf_cvstat) {
192: case CVS_FST_UNKNOWN:
193: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
194: break;
195: case CVS_FST_UPTODATE:
196: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
197: break;
198: case CVS_FST_ADDED:
199: case CVS_FST_MODIFIED:
200: ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
201: if (ret == 0)
202: ret = cvs_sendfile(root, fpath);
203: break;
204: default:
205: break;
1.2 jfb 206: }
207:
1.28 joris 208: if (ret == -1)
209: ret = CVS_EX_PROTO;
210:
1.12 jfb 211: return (ret);
1.2 jfb 212: }
213:
214: /*
1.26 jfb 215: * cvs_update_local()
1.2 jfb 216: */
1.26 jfb 217: static int
218: cvs_update_local(CVSFILE *cf, void *arg)
1.2 jfb 219: {
1.26 jfb 220: int ret, l;
221: char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
222: RCSFILE *rf;
223: struct cvsroot *root;
224:
225: ret = 0;
226: rf = NULL;
227: root = CVS_DIR_ROOT(cf);
228: repo = CVS_DIR_REPO(cf);
229:
1.29 jfb 230: cvs_file_getpath(cf, fpath, sizeof(fpath));
231:
1.26 jfb 232: if (cf->cf_type == DT_DIR) {
1.29 jfb 233: cvs_log(LP_INFO, "Updating %s", fpath);
1.26 jfb 234: return (CVS_EX_OK);
235: }
236:
237: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
238: cvs_printf("? %s\n", fpath);
239: return (0);
240: }
241:
242: l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
243: root->cr_dir, repo, cf->cf_name, RCS_FILE_EXT);
244: if (l == -1 || l >= (int)sizeof(rcspath)) {
245: errno = ENAMETOOLONG;
246: cvs_log(LP_ERRNO, "%s", rcspath);
1.28 joris 247: return (CVS_EX_DATA);
1.26 jfb 248: }
1.1 jfb 249:
1.26 jfb 250: rf = rcs_open(rcspath, RCS_RDWR);
251: if (rf == NULL) {
1.29 jfb 252: printf("failed here?\n");
1.26 jfb 253: return (CVS_EX_DATA);
254: }
255:
256: rcs_close(rf);
257:
258: return (ret);
1.1 jfb 259: }