Annotation of src/usr.bin/cvs/update.c, Revision 1.42
1.42 ! xsa 1: /* $OpenBSD: update.c,v 1.41 2005/07/23 11:19:46 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.1 jfb 39:
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:
47: struct cvs_cmd cvs_cmd_update = {
48: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
49: { "up", "upd" },
50: "Bring work tree in sync with repository",
51: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
52: "[-t id] ...",
1.35 xsa 53: "ACD:dfI:j:k:lPpQqRr:t:",
1.29 jfb 54: NULL,
1.37 joris 55: CF_SORT | CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.29 jfb 56: cvs_update_init,
57: cvs_update_pre_exec,
1.26 jfb 58: cvs_update_remote,
1.29 jfb 59: cvs_update_local,
60: NULL,
61: NULL,
1.18 joris 62: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
63: };
1.2 jfb 64:
1.35 xsa 65: static char *date, *rev, *koptstr;
1.31 joris 66: static int dflag, Aflag;
1.35 xsa 67: static int kflag = RCS_KWEXP_DEFAULT;
1.24 joris 68:
1.26 jfb 69: static int
1.29 jfb 70: cvs_update_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 71: {
1.18 joris 72: int ch;
1.2 jfb 73:
1.31 joris 74: dflag = Aflag = 0;
1.32 xsa 75: date = NULL;
76: rev = NULL;
1.24 joris 77:
1.29 jfb 78: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 79: switch (ch) {
80: case 'A':
1.24 joris 81: Aflag = 1;
82: break;
1.1 jfb 83: case 'C':
84: case 'D':
1.32 xsa 85: date = optarg;
86: break;
1.1 jfb 87: case 'd':
1.24 joris 88: dflag = 1;
89: break;
1.1 jfb 90: case 'f':
1.2 jfb 91: break;
1.35 xsa 92: case 'I':
93: break;
94: case 'j':
95: break;
96: case 'k':
97: koptstr = optarg;
98: kflag = rcs_kflag_get(koptstr);
99: if (RCS_KWEXP_INVAL(kflag)) {
100: cvs_log(LP_ERR,
101: "invalid RCS keyword expansion mode");
102: rcs_kflag_usage();
103: return (CVS_EX_USAGE);
104: }
105: break;
1.1 jfb 106: case 'l':
1.29 jfb 107: cmd->file_flags &= ~CF_RECURSE;
1.2 jfb 108: break;
1.1 jfb 109: case 'P':
1.31 joris 110: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24 joris 111: break;
1.1 jfb 112: case 'p':
1.27 xsa 113: cvs_noexec = 1; /* no locks will be created */
114: break;
1.1 jfb 115: case 'Q':
116: case 'q':
1.2 jfb 117: break;
1.1 jfb 118: case 'R':
1.29 jfb 119: cmd->file_flags |= CF_RECURSE;
1.2 jfb 120: break;
1.1 jfb 121: case 'r':
1.32 xsa 122: rev = optarg;
1.1 jfb 123: break;
124: default:
1.21 joris 125: return (CVS_EX_USAGE);
1.1 jfb 126: }
1.32 xsa 127: }
128:
1.18 joris 129: *arg = optind;
1.2 jfb 130: return (0);
131: }
132:
1.26 jfb 133: static int
1.29 jfb 134: cvs_update_pre_exec(struct cvsroot *root)
1.24 joris 135: {
1.36 joris 136: if (root->cr_method != CVS_METHOD_LOCAL) {
1.38 joris 137: if ((cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS) &&
1.36 joris 138: (cvs_sendarg(root, "-P", 0) < 0))
139: return (CVS_EX_PROTO);
140: if (Aflag && cvs_sendarg(root, "-A", 0) < 0)
141: return (CVS_EX_PROTO);
142: if (dflag && cvs_sendarg(root, "-d", 0) < 0)
143: return (CVS_EX_PROTO);
1.33 xsa 144:
1.36 joris 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:
1.36 joris 149: if ((date != NULL) && ((cvs_sendarg(root, "-D", 0) < 0) ||
150: (cvs_sendarg(root, date, 0) < 0)))
151: return (CVS_EX_PROTO);
152: }
1.35 xsa 153:
1.24 joris 154: return (0);
155: }
1.2 jfb 156:
157: /*
1.26 jfb 158: * cvs_update_remote()
1.2 jfb 159: *
1.12 jfb 160: * Update a single file. In the case where we act as client, send any
161: * pertinent information about that file to the server.
1.2 jfb 162: */
1.26 jfb 163: static int
164: cvs_update_remote(CVSFILE *cf, void *arg)
1.2 jfb 165: {
1.26 jfb 166: int ret;
167: char *repo, fpath[MAXPATHLEN];
1.2 jfb 168: struct cvsroot *root;
169:
1.12 jfb 170: ret = 0;
171: root = CVS_DIR_ROOT(cf);
172: repo = CVS_DIR_REPO(cf);
1.9 jfb 173:
1.13 jfb 174: if (cf->cf_type == DT_DIR) {
1.26 jfb 175: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
176: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
177: cf->cf_name);
178: else
179: ret = cvs_senddir(root, cf);
1.13 jfb 180:
1.28 joris 181: if (ret == -1)
182: ret = CVS_EX_PROTO;
183:
1.12 jfb 184: return (ret);
1.2 jfb 185: }
186:
1.12 jfb 187: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2 jfb 188:
1.26 jfb 189: if (cvs_sendentry(root, cf) < 0)
190: return (CVS_EX_PROTO);
1.41 joris 191:
192: if (!(cf->cf_flags & CVS_FILE_ONDISK))
193: return (0);
1.2 jfb 194:
1.26 jfb 195: switch (cf->cf_cvstat) {
196: case CVS_FST_UNKNOWN:
197: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
198: break;
199: case CVS_FST_UPTODATE:
200: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
201: break;
202: case CVS_FST_ADDED:
203: case CVS_FST_MODIFIED:
204: ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
205: if (ret == 0)
206: ret = cvs_sendfile(root, fpath);
207: break;
208: default:
209: break;
1.2 jfb 210: }
211:
1.28 joris 212: if (ret == -1)
213: ret = CVS_EX_PROTO;
214:
1.12 jfb 215: return (ret);
1.2 jfb 216: }
217:
218: /*
1.26 jfb 219: * cvs_update_local()
1.2 jfb 220: */
1.26 jfb 221: static int
222: cvs_update_local(CVSFILE *cf, void *arg)
1.2 jfb 223: {
1.39 xsa 224: int ret;
225: char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.26 jfb 226: RCSFILE *rf;
227:
228: ret = 0;
229: rf = NULL;
230:
1.29 jfb 231: cvs_file_getpath(cf, fpath, sizeof(fpath));
232:
1.37 joris 233: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
234: cvs_printf("? %s\n", fpath);
235: return (CVS_EX_OK);
236: }
237:
1.26 jfb 238: if (cf->cf_type == DT_DIR) {
1.29 jfb 239: cvs_log(LP_INFO, "Updating %s", fpath);
1.26 jfb 240: return (CVS_EX_OK);
241: }
242:
1.39 xsa 243: if (cvs_rcs_getpath(cf, rcspath, sizeof(rcspath)) == NULL)
1.28 joris 244: return (CVS_EX_DATA);
1.1 jfb 245:
1.26 jfb 246: rf = rcs_open(rcspath, RCS_RDWR);
247: if (rf == NULL) {
1.29 jfb 248: printf("failed here?\n");
1.26 jfb 249: return (CVS_EX_DATA);
250: }
251:
252: rcs_close(rf);
253:
254: return (ret);
1.1 jfb 255: }