Annotation of src/usr.bin/cvs/update.c, Revision 1.31
1.31 ! joris 1: /* $OpenBSD: update.c,v 1.30 2005/05/24 04:21:54 jfb 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>
31: #include <stdio.h>
32: #include <fcntl.h>
33: #include <stdlib.h>
34: #include <unistd.h>
35: #include <string.h>
36:
37: #include "cvs.h"
38: #include "rcs.h"
39: #include "log.h"
1.4 jfb 40: #include "proto.h"
1.1 jfb 41:
42:
1.29 jfb 43: static int cvs_update_init (struct cvs_cmd *, int, char **, int *);
44: static int cvs_update_pre_exec (struct cvsroot *);
1.26 jfb 45: static int cvs_update_remote (CVSFILE *, void *);
46: static int cvs_update_local (CVSFILE *, void *);
1.18 joris 47:
1.29 jfb 48:
49: struct cvs_cmd cvs_cmd_update = {
50: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
51: { "up", "upd" },
52: "Bring work tree in sync with repository",
53: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
54: "[-t id] ...",
55: "ACD:dflPpQqRr:",
56: NULL,
57: CF_SORT | CF_RECURSE | CF_IGNORE | CF_KNOWN | CF_NOSYMS,
58: cvs_update_init,
59: cvs_update_pre_exec,
1.26 jfb 60: cvs_update_remote,
1.29 jfb 61: cvs_update_local,
62: NULL,
63: NULL,
1.18 joris 64: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
65: };
1.2 jfb 66:
1.31 ! joris 67: static int dflag, Aflag;
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.24 joris 75:
1.29 jfb 76: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 77: switch (ch) {
78: case 'A':
1.24 joris 79: Aflag = 1;
80: break;
1.1 jfb 81: case 'C':
82: case 'D':
83: case 'd':
1.24 joris 84: dflag = 1;
85: break;
1.1 jfb 86: case 'f':
1.2 jfb 87: break;
1.1 jfb 88: case 'l':
1.29 jfb 89: cmd->file_flags &= ~CF_RECURSE;
1.2 jfb 90: break;
1.1 jfb 91: case 'P':
1.31 ! joris 92: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24 joris 93: break;
1.1 jfb 94: case 'p':
1.27 xsa 95: cvs_noexec = 1; /* no locks will be created */
96: break;
1.1 jfb 97: case 'Q':
98: case 'q':
1.2 jfb 99: break;
1.1 jfb 100: case 'R':
1.29 jfb 101: cmd->file_flags |= CF_RECURSE;
1.2 jfb 102: break;
1.1 jfb 103: case 'r':
104: break;
105: default:
1.21 joris 106: return (CVS_EX_USAGE);
1.1 jfb 107: }
108: }
109:
1.18 joris 110: *arg = optind;
1.2 jfb 111: return (0);
112: }
113:
1.26 jfb 114: static int
1.29 jfb 115: cvs_update_pre_exec(struct cvsroot *root)
1.24 joris 116: {
1.31 ! joris 117: if ((cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS) &&
! 118: (cvs_sendarg(root, "-P", 0) < 0))
1.24 joris 119: return (CVS_EX_PROTO);
1.25 joris 120: if (Aflag && cvs_sendarg(root, "-A", 0) < 0)
1.24 joris 121: return (CVS_EX_PROTO);
122: if (dflag && cvs_sendarg(root, "-d", 0) < 0)
123: return (CVS_EX_PROTO);
124: return (0);
125: }
1.2 jfb 126:
127: /*
1.26 jfb 128: * cvs_update_remote()
1.2 jfb 129: *
1.12 jfb 130: * Update a single file. In the case where we act as client, send any
131: * pertinent information about that file to the server.
1.2 jfb 132: */
1.26 jfb 133: static int
134: cvs_update_remote(CVSFILE *cf, void *arg)
1.2 jfb 135: {
1.26 jfb 136: int ret;
137: char *repo, fpath[MAXPATHLEN];
1.2 jfb 138: struct cvsroot *root;
139:
1.12 jfb 140: ret = 0;
141: root = CVS_DIR_ROOT(cf);
142: repo = CVS_DIR_REPO(cf);
1.9 jfb 143:
1.13 jfb 144: if (cf->cf_type == DT_DIR) {
1.26 jfb 145: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
146: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
147: cf->cf_name);
148: else
149: ret = cvs_senddir(root, cf);
1.13 jfb 150:
1.28 joris 151: if (ret == -1)
152: ret = CVS_EX_PROTO;
153:
1.12 jfb 154: return (ret);
1.2 jfb 155: }
156:
1.12 jfb 157: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2 jfb 158:
1.26 jfb 159: if (cvs_sendentry(root, cf) < 0)
160: return (CVS_EX_PROTO);
1.2 jfb 161:
1.26 jfb 162: switch (cf->cf_cvstat) {
163: case CVS_FST_UNKNOWN:
164: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
165: break;
166: case CVS_FST_UPTODATE:
167: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
168: break;
169: case CVS_FST_ADDED:
170: case CVS_FST_MODIFIED:
171: ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
172: if (ret == 0)
173: ret = cvs_sendfile(root, fpath);
174: break;
175: default:
176: break;
1.2 jfb 177: }
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:
185: /*
1.26 jfb 186: * cvs_update_local()
1.2 jfb 187: */
1.26 jfb 188: static int
189: cvs_update_local(CVSFILE *cf, void *arg)
1.2 jfb 190: {
1.26 jfb 191: int ret, l;
192: char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
193: RCSFILE *rf;
194: struct cvsroot *root;
195:
196: ret = 0;
197: rf = NULL;
198: root = CVS_DIR_ROOT(cf);
199: repo = CVS_DIR_REPO(cf);
200:
1.29 jfb 201: cvs_file_getpath(cf, fpath, sizeof(fpath));
202:
1.26 jfb 203: if (cf->cf_type == DT_DIR) {
1.29 jfb 204: cvs_log(LP_INFO, "Updating %s", fpath);
1.26 jfb 205: return (CVS_EX_OK);
206: }
207:
208: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
209: cvs_printf("? %s\n", fpath);
210: return (0);
211: }
212:
213: l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
214: root->cr_dir, repo, cf->cf_name, RCS_FILE_EXT);
215: if (l == -1 || l >= (int)sizeof(rcspath)) {
216: errno = ENAMETOOLONG;
217: cvs_log(LP_ERRNO, "%s", rcspath);
1.28 joris 218: return (CVS_EX_DATA);
1.26 jfb 219: }
1.1 jfb 220:
1.26 jfb 221: rf = rcs_open(rcspath, RCS_RDWR);
222: if (rf == NULL) {
1.29 jfb 223: printf("failed here?\n");
1.26 jfb 224: return (CVS_EX_DATA);
225: }
226:
227: rcs_close(rf);
228:
229: return (ret);
1.1 jfb 230: }