Annotation of src/usr.bin/cvs/update.c, Revision 1.13
1.13 ! jfb 1: /* $OpenBSD: update.c,v 1.12 2004/12/14 22:30:48 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: #include <sysexits.h>
37:
38: #include "cvs.h"
39: #include "rcs.h"
40: #include "log.h"
1.4 jfb 41: #include "proto.h"
1.1 jfb 42:
43:
1.2 jfb 44: int cvs_update_file (CVSFILE *, void *);
45: int cvs_update_prune (CVSFILE *, void *);
46:
47:
1.1 jfb 48: /*
49: * cvs_update()
50: *
51: * Handle the `cvs update' command.
1.2 jfb 52: * Returns 0 on success, or the appropriate exit code on error.
1.1 jfb 53: */
54: int
55: cvs_update(int argc, char **argv)
56: {
1.12 jfb 57: int i, ch, flags;
58: struct cvsroot *root;
1.2 jfb 59:
1.8 jfb 60: flags = CF_SORT|CF_RECURSE|CF_IGNORE|CF_KNOWN|CF_NOSYMS;
1.1 jfb 61:
62: while ((ch = getopt(argc, argv, "ACD:dflPpQqRr:")) != -1) {
63: switch (ch) {
64: case 'A':
65: case 'C':
66: case 'D':
67: case 'd':
68: case 'f':
1.2 jfb 69: break;
1.1 jfb 70: case 'l':
1.2 jfb 71: flags &= ~CF_RECURSE;
72: break;
1.1 jfb 73: case 'P':
74: case 'p':
75: case 'Q':
76: case 'q':
1.2 jfb 77: break;
1.1 jfb 78: case 'R':
1.2 jfb 79: flags |= CF_RECURSE;
80: break;
1.1 jfb 81: case 'r':
82: break;
83: default:
84: return (EX_USAGE);
85: }
86: }
87:
88: argc -= optind;
89: argv += optind;
1.2 jfb 90:
1.12 jfb 91: if (argc == 0)
1.7 jfb 92: cvs_files = cvs_file_get(".", flags);
1.12 jfb 93: else {
1.2 jfb 94: /* don't perform ignore on explicitly listed files */
1.7 jfb 95: flags &= ~(CF_IGNORE | CF_RECURSE | CF_SORT);
96: cvs_files = cvs_file_getspec(argv, argc, flags);
1.2 jfb 97: }
1.7 jfb 98: if (cvs_files == NULL)
99: return (EX_DATAERR);
1.2 jfb 100:
1.12 jfb 101: root = CVS_DIR_ROOT(cvs_files);
102: if (root == NULL) {
103: cvs_log(LP_ERR,
104: "No CVSROOT specified! Please use the `-d' option");
105: cvs_log(LP_ERR,
106: "or set the CVSROOT environment variable.");
107: return (EX_USAGE);
108: }
109: if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
110: return (EX_PROTOCOL);
111:
1.7 jfb 112: cvs_file_examine(cvs_files, cvs_update_file, NULL);
1.2 jfb 113:
1.12 jfb 114: if (root->cr_method != CVS_METHOD_LOCAL) {
115: if (cvs_senddir(root, cvs_files) < 0)
116: return (EX_PROTOCOL);
117:
118: for (i = 0; i < argc; i++)
119: if (cvs_sendarg(root, argv[i], 0) < 0)
120: return (EX_PROTOCOL);
121: if (cvs_sendreq(root, CVS_REQ_UPDATE, NULL) < 0)
122: return (EX_PROTOCOL);
123: }
1.2 jfb 124:
125: return (0);
126: }
127:
128:
129: /*
130: * cvs_update_file()
131: *
1.12 jfb 132: * Update a single file. In the case where we act as client, send any
133: * pertinent information about that file to the server.
1.2 jfb 134: */
135: int
136: cvs_update_file(CVSFILE *cf, void *arg)
137: {
1.12 jfb 138: int ret;
1.9 jfb 139: char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.2 jfb 140: RCSFILE *rf;
141: struct cvsroot *root;
142: struct cvs_ent *entp;
143:
1.12 jfb 144: ret = 0;
145: rf = NULL;
146: root = CVS_DIR_ROOT(cf);
147: repo = CVS_DIR_REPO(cf);
1.9 jfb 148:
1.13 ! jfb 149: if (cf->cf_type == DT_DIR) {
! 150: if (root->cr_method != CVS_METHOD_LOCAL) {
! 151: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
! 152: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
! 153: CVS_FILE_NAME(cf));
! 154: else
! 155: ret = cvs_senddir(root, cf);
! 156: }
! 157:
1.12 jfb 158: return (ret);
1.2 jfb 159: }
160:
1.12 jfb 161: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.9 jfb 162: entp = cvs_ent_getent(fpath);
1.2 jfb 163:
1.12 jfb 164: if (root->cr_method != CVS_METHOD_LOCAL) {
165: if ((entp != NULL) && (cvs_sendentry(root, entp) < 0)) {
166: cvs_ent_free(entp);
167: return (-1);
168: }
1.2 jfb 169:
170: switch (cf->cf_cvstat) {
1.12 jfb 171: case CVS_FST_UNKNOWN:
172: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
173: CVS_FILE_NAME(cf));
174: break;
1.2 jfb 175: case CVS_FST_UPTODATE:
1.12 jfb 176: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED,
177: CVS_FILE_NAME(cf));
1.2 jfb 178: break;
179: case CVS_FST_ADDED:
180: case CVS_FST_MODIFIED:
1.12 jfb 181: ret = cvs_sendreq(root, CVS_REQ_MODIFIED,
182: CVS_FILE_NAME(cf));
183: if (ret == 0)
184: ret = cvs_sendfile(root, fpath);
1.2 jfb 185: break;
186: default:
1.12 jfb 187: break;
188: }
189: } else {
190: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
191: cvs_printf("? %s\n", fpath);
192: return (0);
193: }
194:
195: snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
196: root->cr_dir, repo, fpath, RCS_FILE_EXT);
197:
198: rf = rcs_open(rcspath, RCS_MODE_READ);
199: if (rf == NULL) {
200: cvs_ent_free(entp);
1.2 jfb 201: return (-1);
202: }
203:
1.12 jfb 204: rcs_close(rf);
1.2 jfb 205: }
206:
1.12 jfb 207: if (entp != NULL)
1.2 jfb 208: cvs_ent_free(entp);
1.12 jfb 209: return (ret);
1.2 jfb 210: }
211:
212:
213: /*
214: * cvs_update_prune()
215: *
216: * Prune all directories which contain no more files known to CVS.
217: */
218: int
219: cvs_update_prune(CVSFILE *cf, void *arg)
220: {
1.1 jfb 221:
222: return (0);
223: }