Annotation of src/usr.bin/cvs/tag.c, Revision 1.8
1.8 ! xsa 1: /* $OpenBSD: tag.c,v 1.7 2005/03/30 17:43:04 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.5 joris 4: * Copyright (c) 2004 Joris Vink <joris@openbsd.org>
1.1 jfb 5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: *
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. The name of the author may not be used to endorse or promote products
14: * derived from this software without specific prior written permission.
15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26: */
27:
28: #include <sys/types.h>
29: #include <sys/stat.h>
30:
31: #include <errno.h>
32: #include <stdio.h>
33: #include <fcntl.h>
34: #include <stdlib.h>
35: #include <unistd.h>
36: #include <string.h>
37: #include <sysexits.h>
38:
39: #include "cvs.h"
40: #include "log.h"
41: #include "proto.h"
42:
43:
1.7 joris 44: int cvs_tag_file(CVSFILE *, void *);
45: int cvs_tag_options(char *, int, char **, int *);
46: int cvs_tag_sendflags(struct cvsroot *);
47:
48: static char *tag, *old_tag, *date;
49: static int branch, delete;
50:
51: struct cvs_cmd_info cvs_tag = {
52: cvs_tag_options,
53: cvs_tag_sendflags,
54: cvs_tag_file,
55: NULL, NULL,
56: CF_SORT | CF_IGNORE | CF_RECURSE,
57: CVS_REQ_TAG,
58: CVS_CMD_ALLOWSPEC
59: };
1.1 jfb 60:
61: int
1.7 joris 62: cvs_tag_options(char *opt, int argc, char **argv, int *arg)
1.1 jfb 63: {
1.7 joris 64: int ch;
1.1 jfb 65:
1.3 jfb 66: date = old_tag = NULL;
1.1 jfb 67: branch = delete = 0;
68:
1.7 joris 69: while ((ch = getopt(argc, argv, opt)) != -1) {
1.1 jfb 70: switch (ch) {
71: case 'b':
72: branch = 1;
73: break;
74: case 'd':
75: delete = 1;
76: break;
1.3 jfb 77: case 'D':
78: date = optarg;
79: break;
1.1 jfb 80: case 'l':
1.7 joris 81: cvs_tag.file_flags &= ~CF_RECURSE;
1.1 jfb 82: break;
83: case 'r':
84: old_tag = optarg;
85: break;
86: default:
87: return (EX_USAGE);
88: }
89: }
90:
1.7 joris 91: *arg = optind;
1.1 jfb 92: argc -= optind;
93: argv += optind;
94:
95: if (argc == 0) {
96: return (EX_USAGE);
97: } else {
98: tag = argv[0];
99: argc--;
100: argv++;
1.7 joris 101: *arg += 1;
1.1 jfb 102: }
103:
104: if (branch && delete) {
105: cvs_log(LP_WARN, "ignoring -b with -d options");
106: branch = 0;
107: }
108:
109: if (delete && old_tag)
110: old_tag = NULL;
111:
1.3 jfb 112: if (delete && date)
113: date = NULL;
114:
115: if (old_tag != NULL && date != NULL) {
116: cvs_log(LP_ERROR, "-r and -D options are mutually exclusive");
117: return (-1);
118: }
119:
1.7 joris 120: return (0);
121: }
122:
123: int
124: cvs_tag_sendflags(struct cvsroot *root)
125: {
126: if (branch && (cvs_sendarg(root, "-b", 0) < 0))
1.8 ! xsa 127: return (-1);
1.7 joris 128:
129: if (delete && (cvs_sendarg(root, "-d", 0) < 0))
1.8 ! xsa 130: return (-1);
1.1 jfb 131:
1.7 joris 132: if (old_tag) {
133: if ((cvs_sendarg(root, "-r", 0) < 0) ||
134: (cvs_sendarg(root, old_tag, 0) < 0))
1.8 ! xsa 135: return (-1);
1.1 jfb 136: }
137:
1.7 joris 138: if (date) {
139: if ((cvs_sendarg(root, "-D", 0) < 0) ||
140: (cvs_sendarg(root, date, 0) < 0))
1.8 ! xsa 141: return (-1);
1.1 jfb 142: }
1.7 joris 143:
144: if (cvs_sendarg(root, tag, 0) < 0)
1.8 ! xsa 145: return (-1);
1.1 jfb 146:
147: return (0);
148: }
149:
150:
151: /*
152: * cvs_tag_file()
153: *
154: * Get the status of a single file.
155: */
156: int
157: cvs_tag_file(CVSFILE *cfp, void *arg)
158: {
159: int ret;
160: char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
161: RCSFILE *rf;
162: struct cvs_ent *entp;
163: struct cvsroot *root;
164:
165: ret = 0;
166: rf = NULL;
167: root = CVS_DIR_ROOT(cfp);
168:
169: if ((root->cr_method != CVS_METHOD_LOCAL) && (cfp->cf_type == DT_DIR)) {
170: if (cvs_senddir(root, cfp) < 0)
171: return (-1);
172: return (0);
173: }
174:
175: cvs_file_getpath(cfp, fpath, sizeof(fpath));
176: entp = cvs_ent_getent(fpath);
177:
178: if (root->cr_method != CVS_METHOD_LOCAL) {
179: if ((entp != NULL) && (cvs_sendentry(root, entp) < 0)) {
180: cvs_ent_free(entp);
181: return (-1);
182: }
183:
184: switch (cfp->cf_cvstat) {
185: case CVS_FST_UNKNOWN:
186: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
187: CVS_FILE_NAME(cfp));
188: break;
189: case CVS_FST_UPTODATE:
190: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED,
191: CVS_FILE_NAME(cfp));
192: break;
193: case CVS_FST_MODIFIED:
194: ret = cvs_sendreq(root, CVS_REQ_ISMODIFIED,
195: CVS_FILE_NAME(cfp));
196: default:
197: break;
198: }
199: } else {
200: if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
201: cvs_log(LP_WARN, "I know nothing about %s", fpath);
202: return (0);
203: }
204:
205: if (cfp->cf_parent != NULL)
206: repo = cfp->cf_parent->cf_ddat->cd_repo;
207: else
208: repo = NULL;
209:
210: snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
211: root->cr_dir, repo, CVS_FILE_NAME(cfp), RCS_FILE_EXT);
212:
1.4 jfb 213: rf = rcs_open(rcspath, RCS_READ);
1.1 jfb 214: if (rf == NULL) {
1.6 tedu 215: if (entp != NULL)
216: cvs_ent_free(entp);
1.1 jfb 217: return (-1);
218: }
219:
220: rcs_close(rf);
221: }
222:
223: if (entp != NULL)
224: cvs_ent_free(entp);
225:
226: return (ret);
227: }