Annotation of src/usr.bin/cvs/remove.c, Revision 1.44
1.44 ! deraadt 1: /* $OpenBSD: remove.c,v 1.43 2006/03/16 09:06:19 xsa Exp $ */
1.1 xsa 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.20 xsa 4: * Copyright (c) 2004, 2005 Xavier Santolaria <xsa@openbsd.org>
1.1 xsa 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:
1.40 xsa 28: #include "includes.h"
1.1 xsa 29:
30: #include "cvs.h"
31: #include "log.h"
32: #include "proto.h"
33:
34:
35: extern char *__progname;
36:
37:
1.26 xsa 38: static int cvs_remove_init(struct cvs_cmd *, int, char **, int *);
39: static int cvs_remove_remote(CVSFILE *, void *);
40: static int cvs_remove_local(CVSFILE *, void *);
41: static int cvs_remove_file(const char *);
1.1 xsa 42:
1.4 joris 43: static int force_remove = 0; /* -f option */
1.20 xsa 44: static int nuked = 0;
1.4 joris 45:
1.16 jfb 46: struct cvs_cmd cvs_cmd_remove = {
47: CVS_OP_REMOVE, CVS_REQ_REMOVE, "remove",
48: { "rm", "delete" },
49: "Remove an entry from the repository",
50: "[-flR] [file ...]",
51: "flR",
1.4 joris 52: NULL,
1.16 jfb 53: CF_IGNORE | CF_RECURSE,
54: cvs_remove_init,
55: NULL,
1.18 joris 56: cvs_remove_remote,
57: cvs_remove_local,
1.16 jfb 58: NULL,
59: NULL,
1.4 joris 60: CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
61: };
1.1 xsa 62:
1.13 xsa 63: static int
1.16 jfb 64: cvs_remove_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 xsa 65: {
1.4 joris 66: int ch;
1.1 xsa 67:
1.16 jfb 68: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 xsa 69: switch (ch) {
70: case 'f':
1.3 xsa 71: force_remove = 1;
1.1 xsa 72: break;
73: case 'l':
1.16 jfb 74: cmd->file_flags &= ~CF_RECURSE;
1.1 xsa 75: break;
76: case 'R':
1.16 jfb 77: cmd->file_flags |= CF_RECURSE;
1.1 xsa 78: break;
79: default:
1.7 joris 80: return (CVS_EX_USAGE);
1.1 xsa 81: }
82: }
83:
84: argc -= optind;
85: argv += optind;
86:
1.4 joris 87: *arg = optind;
1.1 xsa 88: return (0);
89: }
90:
91:
1.13 xsa 92: static int
1.18 joris 93: cvs_remove_remote(CVSFILE *cf, void *arg)
1.1 xsa 94: {
1.2 jfb 95: char fpath[MAXPATHLEN];
1.1 xsa 96: struct cvsroot *root;
97:
98: root = CVS_DIR_ROOT(cf);
99:
100: if (cf->cf_type == DT_DIR) {
1.18 joris 101: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.39 joris 102: cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
1.18 joris 103: else
1.39 joris 104: cvs_senddir(root, cf);
105: return (0);
1.1 xsa 106: }
107:
1.2 jfb 108: cvs_file_getpath(cf, fpath, sizeof(fpath));
109:
1.20 xsa 110: if (cvs_remove_file(fpath) < 0)
1.42 xsa 111: fatal("cvs_remove_remote: cvs_remove_file `%s' failed", fpath);
1.18 joris 112:
1.39 joris 113: cvs_sendentry(root, cf);
1.3 xsa 114:
1.18 joris 115: if (cf->cf_cvstat != CVS_FST_LOST && force_remove != 1) {
1.39 joris 116: if (cf->cf_cvstat != CVS_FST_ADDED)
117: cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
1.17 joris 118:
1.39 joris 119: if (cf->cf_flags & CVS_FILE_ONDISK)
120: cvs_sendfile(root, fpath);
1.18 joris 121: }
1.11 xsa 122:
1.18 joris 123: return (0);
124: }
125:
126: static int
127: cvs_remove_local(CVSFILE *cf, void *arg)
128: {
1.41 xsa 129: int existing, removed;
1.21 xsa 130: char buf[MAXPATHLEN], fpath[MAXPATHLEN];
1.35 xsa 131: CVSENTRIES *entf;
1.36 xsa 132: struct cvs_ent *ent;
1.20 xsa 133:
134: existing = removed = 0;
1.35 xsa 135: entf = (CVSENTRIES *)cf->cf_entry;
1.20 xsa 136:
1.19 xsa 137: if (cf->cf_type == DT_DIR) {
138: if (verbosity > 1)
1.29 xsa 139: cvs_log(LP_NOTICE, "Removing %s", cf->cf_name);
1.19 xsa 140: return (0);
141: }
142:
1.20 xsa 143: if (cvs_cmdop != CVS_OP_SERVER) {
144: cvs_file_getpath(cf, fpath, sizeof(fpath));
145:
146: if (cvs_remove_file(fpath) < 0)
1.42 xsa 147: fatal("cvs_remove_local: cvs_remove_file `%s' failed",
148: fpath);
1.20 xsa 149: }
150:
1.25 xsa 151: if (nuked == 0) {
1.20 xsa 152: existing++;
153: if (verbosity > 1)
154: cvs_log(LP_WARN, "file `%s' still in working directory",
155: cf->cf_name);
156: } else if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
157: if (verbosity > 1)
158: cvs_log(LP_WARN, "nothing known about `%s'",
159: cf->cf_name);
160: return (0);
161: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
1.38 joris 162: if (cvs_ent_remove(entf, cf->cf_name, 0) == -1)
1.42 xsa 163: fatal("cvs_remove_local: cvs_ent_remove failed");
1.34 xsa 164:
1.41 xsa 165: if (strlcpy(buf, CVS_PATH_CVSDIR, sizeof(buf)) >= sizeof(buf) ||
166: strlcat(buf, "/", sizeof(buf)) >= sizeof(buf) ||
167: strlcat(buf, cf->cf_name, sizeof(buf)) >= sizeof(buf) ||
168: strlcat(buf, CVS_DESCR_FILE_EXT,
169: sizeof(buf)) >= sizeof(buf))
170: fatal("cvs_remove_local: path truncation");
1.35 xsa 171:
1.28 xsa 172: if (cvs_unlink(buf) == -1)
1.42 xsa 173: fatal("cvs_remove_local: cvs_unlink `%s' failed", buf);
1.24 xsa 174:
1.20 xsa 175: if (verbosity > 1)
1.30 xsa 176: cvs_log(LP_NOTICE, "removed `%s'", cf->cf_name);
1.20 xsa 177: return (0);
178: } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
179: if (verbosity > 1 )
180: cvs_log(LP_WARN,
181: "file `%s' already scheduled for removal",
182: cf->cf_name);
183: return (0);
184: } else {
1.36 xsa 185: if ((ent = cvs_ent_get(entf, cf->cf_name)) == NULL)
1.42 xsa 186: fatal("cvs_remove_local: cvs_ent_get failed");
1.34 xsa 187:
1.36 xsa 188: /* Prefix revision with `-' */
189: ent->ce_status = CVS_ENT_REMOVED;
190: entf->cef_flags &= ~CVS_ENTF_SYNC;
1.34 xsa 191:
1.20 xsa 192: if (verbosity > 1)
1.30 xsa 193: cvs_log(LP_NOTICE, "scheduling file `%s' for removal",
1.20 xsa 194: cf->cf_name);
195: removed++;
196: }
197:
1.25 xsa 198: if (removed != 0) {
1.20 xsa 199: if (verbosity > 0)
1.30 xsa 200: cvs_log(LP_NOTICE, "use '%s commit' to remove %s "
1.20 xsa 201: "permanently", __progname,
1.27 xsa 202: (removed == 1) ? "this file" : "these files");
1.20 xsa 203: return (0);
204: }
205:
1.25 xsa 206: if (existing != 0) {
1.44 ! deraadt 207: cvs_log(LP_WARN, existing == 1 ?
1.20 xsa 208: "%d file exists; remove it first" :
1.44 ! deraadt 209: "%d files exist; remove them first", existing);
1.20 xsa 210: return (0);
211: }
212:
213: return (0);
214: }
215:
216: /*
217: * cvs_remove_file()
218: *
219: * Physically remove the file.
220: * Used by both remote and local handlers.
221: * Returns 0 on success, -1 on failure.
222: */
1.33 xsa 223: static int
224: cvs_remove_file(const char *fpath)
1.20 xsa 225: {
1.35 xsa 226: struct stat st;
227:
1.20 xsa 228: /* if -f option is used, physically remove the file */
1.24 xsa 229: if (force_remove == 1) {
1.37 reyk 230: if (cvs_unlink(fpath) == -1)
1.20 xsa 231: return (-1);
232: nuked++;
1.35 xsa 233: } else {
1.44 ! deraadt 234: if (stat(fpath, &st) == -1 && errno == ENOENT)
1.35 xsa 235: nuked++;
1.20 xsa 236: }
1.1 xsa 237:
1.18 joris 238: return (0);
1.1 xsa 239: }