Annotation of src/usr.bin/cvs/update.c, Revision 1.26
1.26 ! jfb 1: /* $OpenBSD: update.c,v 1.25 2005/04/19 00:55:07 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/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.26 ! jfb 43: static int cvs_update_remote (CVSFILE *, void *);
! 44: static int cvs_update_local (CVSFILE *, void *);
! 45: static int cvs_update_options (char *, int, char **, int *);
! 46: static int cvs_update_sendflags (struct cvsroot *);
1.18 joris 47:
48: struct cvs_cmd_info cvs_update = {
49: cvs_update_options,
1.24 joris 50: cvs_update_sendflags,
1.26 ! jfb 51: cvs_update_remote,
1.18 joris 52: NULL, NULL,
53: CF_SORT | CF_RECURSE | CF_IGNORE | CF_KNOWN | CF_NOSYMS,
54: CVS_REQ_UPDATE,
55: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
56: };
1.2 jfb 57:
1.24 joris 58: static int Pflag, dflag, Aflag;
59:
1.26 ! jfb 60: static int
1.18 joris 61: cvs_update_options(char *opt, int argc, char **argv, int *arg)
1.1 jfb 62: {
1.18 joris 63: int ch;
1.2 jfb 64:
1.24 joris 65: Pflag = dflag = Aflag = 0;
66:
1.18 joris 67: while ((ch = getopt(argc, argv, opt)) != -1) {
1.1 jfb 68: switch (ch) {
69: case 'A':
1.24 joris 70: Aflag = 1;
71: break;
1.1 jfb 72: case 'C':
73: case 'D':
74: case 'd':
1.24 joris 75: dflag = 1;
76: break;
1.1 jfb 77: case 'f':
1.2 jfb 78: break;
1.1 jfb 79: case 'l':
1.18 joris 80: cvs_update.file_flags &= ~CF_RECURSE;
1.2 jfb 81: break;
1.1 jfb 82: case 'P':
1.24 joris 83: Pflag = 1;
84: break;
1.1 jfb 85: case 'p':
86: case 'Q':
87: case 'q':
1.2 jfb 88: break;
1.1 jfb 89: case 'R':
1.18 joris 90: cvs_update.file_flags |= CF_RECURSE;
1.2 jfb 91: break;
1.1 jfb 92: case 'r':
93: break;
94: default:
1.21 joris 95: return (CVS_EX_USAGE);
1.1 jfb 96: }
97: }
98:
1.18 joris 99: *arg = optind;
1.2 jfb 100: return (0);
101: }
102:
1.26 ! jfb 103: static int
1.24 joris 104: cvs_update_sendflags(struct cvsroot *root)
105: {
106: if (Pflag && cvs_sendarg(root, "-P", 0) < 0)
107: return (CVS_EX_PROTO);
1.25 joris 108: if (Aflag && cvs_sendarg(root, "-A", 0) < 0)
1.24 joris 109: return (CVS_EX_PROTO);
110: if (dflag && cvs_sendarg(root, "-d", 0) < 0)
111: return (CVS_EX_PROTO);
112: return (0);
113: }
1.2 jfb 114:
115: /*
1.26 ! jfb 116: * cvs_update_remote()
1.2 jfb 117: *
1.12 jfb 118: * Update a single file. In the case where we act as client, send any
119: * pertinent information about that file to the server.
1.2 jfb 120: */
1.26 ! jfb 121: static int
! 122: cvs_update_remote(CVSFILE *cf, void *arg)
1.2 jfb 123: {
1.26 ! jfb 124: int ret;
! 125: char *repo, fpath[MAXPATHLEN];
1.2 jfb 126: struct cvsroot *root;
127:
1.12 jfb 128: ret = 0;
129: root = CVS_DIR_ROOT(cf);
130: repo = CVS_DIR_REPO(cf);
1.9 jfb 131:
1.13 jfb 132: if (cf->cf_type == DT_DIR) {
1.26 ! jfb 133: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
! 134: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
! 135: cf->cf_name);
! 136: else
! 137: ret = cvs_senddir(root, cf);
1.13 jfb 138:
1.12 jfb 139: return (ret);
1.2 jfb 140: }
141:
1.12 jfb 142: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2 jfb 143:
1.26 ! jfb 144: if (cvs_sendentry(root, cf) < 0)
! 145: return (CVS_EX_PROTO);
1.2 jfb 146:
1.26 ! jfb 147: switch (cf->cf_cvstat) {
! 148: case CVS_FST_UNKNOWN:
! 149: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
! 150: break;
! 151: case CVS_FST_UPTODATE:
! 152: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
! 153: break;
! 154: case CVS_FST_ADDED:
! 155: case CVS_FST_MODIFIED:
! 156: ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
! 157: if (ret == 0)
! 158: ret = cvs_sendfile(root, fpath);
! 159: break;
! 160: default:
! 161: break;
1.2 jfb 162: }
163:
1.12 jfb 164: return (ret);
1.2 jfb 165: }
166:
167: /*
1.26 ! jfb 168: * cvs_update_local()
1.2 jfb 169: */
1.26 ! jfb 170: static int
! 171: cvs_update_local(CVSFILE *cf, void *arg)
1.2 jfb 172: {
1.26 ! jfb 173: int ret, l;
! 174: char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
! 175: RCSFILE *rf;
! 176: struct cvsroot *root;
! 177:
! 178: ret = 0;
! 179: rf = NULL;
! 180: root = CVS_DIR_ROOT(cf);
! 181: repo = CVS_DIR_REPO(cf);
! 182:
! 183: if (cf->cf_type == DT_DIR) {
! 184: return (CVS_EX_OK);
! 185: }
! 186:
! 187: cvs_file_getpath(cf, fpath, sizeof(fpath));
! 188:
! 189: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
! 190: cvs_printf("? %s\n", fpath);
! 191: return (0);
! 192: }
! 193:
! 194: l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
! 195: root->cr_dir, repo, cf->cf_name, RCS_FILE_EXT);
! 196: if (l == -1 || l >= (int)sizeof(rcspath)) {
! 197: errno = ENAMETOOLONG;
! 198: cvs_log(LP_ERRNO, "%s", rcspath);
! 199: return (-1);
! 200: }
1.1 jfb 201:
1.26 ! jfb 202: rf = rcs_open(rcspath, RCS_RDWR);
! 203: if (rf == NULL) {
! 204: return (CVS_EX_DATA);
! 205: }
! 206:
! 207: rcs_close(rf);
! 208:
! 209: return (ret);
1.1 jfb 210: }