Annotation of src/usr.bin/cvs/remove.c, Revision 1.40
1.40 ! xsa 1: /* $OpenBSD: remove.c,v 1.39 2005/12/30 02:03:28 joris 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: {
95: int ret;
1.2 jfb 96: char fpath[MAXPATHLEN];
1.1 xsa 97: struct cvsroot *root;
98:
99: ret = 0;
100: root = CVS_DIR_ROOT(cf);
101:
102: if (cf->cf_type == DT_DIR) {
1.18 joris 103: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.39 joris 104: cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
1.18 joris 105: else
1.39 joris 106: cvs_senddir(root, cf);
107: return (0);
1.1 xsa 108: }
109:
1.2 jfb 110: cvs_file_getpath(cf, fpath, sizeof(fpath));
111:
1.20 xsa 112: if (cvs_remove_file(fpath) < 0)
113: return (CVS_EX_FILE);
1.18 joris 114:
1.39 joris 115: cvs_sendentry(root, cf);
1.3 xsa 116:
1.18 joris 117: if (cf->cf_cvstat != CVS_FST_LOST && force_remove != 1) {
1.39 joris 118: if (cf->cf_cvstat != CVS_FST_ADDED)
119: cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
1.17 joris 120:
1.39 joris 121: if (cf->cf_flags & CVS_FILE_ONDISK)
122: cvs_sendfile(root, fpath);
1.18 joris 123: }
1.11 xsa 124:
1.18 joris 125: return (0);
126: }
127:
128: static int
129: cvs_remove_local(CVSFILE *cf, void *arg)
130: {
1.21 xsa 131: int existing, l, removed;
132: char buf[MAXPATHLEN], fpath[MAXPATHLEN];
1.35 xsa 133: CVSENTRIES *entf;
1.36 xsa 134: struct cvs_ent *ent;
1.20 xsa 135:
136: existing = removed = 0;
1.35 xsa 137: entf = (CVSENTRIES *)cf->cf_entry;
1.20 xsa 138:
1.19 xsa 139: if (cf->cf_type == DT_DIR) {
140: if (verbosity > 1)
1.29 xsa 141: cvs_log(LP_NOTICE, "Removing %s", cf->cf_name);
1.19 xsa 142: return (0);
143: }
144:
1.20 xsa 145: if (cvs_cmdop != CVS_OP_SERVER) {
146: cvs_file_getpath(cf, fpath, sizeof(fpath));
147:
148: if (cvs_remove_file(fpath) < 0)
149: return (CVS_EX_FILE);
150: }
151:
1.25 xsa 152: if (nuked == 0) {
1.20 xsa 153: existing++;
154: if (verbosity > 1)
155: cvs_log(LP_WARN, "file `%s' still in working directory",
156: cf->cf_name);
157: } else if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
158: if (verbosity > 1)
159: cvs_log(LP_WARN, "nothing known about `%s'",
160: cf->cf_name);
161: return (0);
162: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
1.38 joris 163: if (cvs_ent_remove(entf, cf->cf_name, 0) == -1)
1.35 xsa 164: return (CVS_EX_FILE);
1.34 xsa 165:
1.21 xsa 166: l = snprintf(buf, sizeof(buf), "%s/%s%s",
167: CVS_PATH_CVSDIR, cf->cf_name, CVS_DESCR_FILE_EXT);
168: if (l == -1 || l >= (int)sizeof(buf)) {
169: errno = ENAMETOOLONG;
170: cvs_log(LP_ERRNO, "%s", buf);
171: return (CVS_EX_DATA);
172: }
1.35 xsa 173:
1.28 xsa 174: if (cvs_unlink(buf) == -1)
1.21 xsa 175: return (CVS_EX_FILE);
1.24 xsa 176:
1.20 xsa 177: if (verbosity > 1)
1.30 xsa 178: cvs_log(LP_NOTICE, "removed `%s'", cf->cf_name);
1.20 xsa 179: return (0);
180: } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
181: if (verbosity > 1 )
182: cvs_log(LP_WARN,
183: "file `%s' already scheduled for removal",
184: cf->cf_name);
185: return (0);
186: } else {
1.36 xsa 187: if ((ent = cvs_ent_get(entf, cf->cf_name)) == NULL)
188: return (CVS_EX_DATA);
1.34 xsa 189:
1.36 xsa 190: /* Prefix revision with `-' */
191: ent->ce_status = CVS_ENT_REMOVED;
192: entf->cef_flags &= ~CVS_ENTF_SYNC;
1.34 xsa 193:
1.20 xsa 194: if (verbosity > 1)
1.30 xsa 195: cvs_log(LP_NOTICE, "scheduling file `%s' for removal",
1.20 xsa 196: cf->cf_name);
197: removed++;
198: }
199:
1.25 xsa 200: if (removed != 0) {
1.20 xsa 201: if (verbosity > 0)
1.30 xsa 202: cvs_log(LP_NOTICE, "use '%s commit' to remove %s "
1.20 xsa 203: "permanently", __progname,
1.27 xsa 204: (removed == 1) ? "this file" : "these files");
1.20 xsa 205: return (0);
206: }
207:
1.25 xsa 208: if (existing != 0) {
1.20 xsa 209: cvs_log(LP_WARN, ((existing == 1) ?
210: "%d file exists; remove it first" :
211: "%d files exist; remove them first"), existing);
212: return (0);
213: }
214:
215: return (0);
216: }
217:
218: /*
219: * cvs_remove_file()
220: *
221: * Physically remove the file.
222: * Used by both remote and local handlers.
223: * Returns 0 on success, -1 on failure.
224: */
1.33 xsa 225: static int
226: cvs_remove_file(const char *fpath)
1.20 xsa 227: {
1.35 xsa 228: struct stat st;
229:
1.20 xsa 230: /* if -f option is used, physically remove the file */
1.24 xsa 231: if (force_remove == 1) {
1.37 reyk 232: if (cvs_unlink(fpath) == -1)
1.20 xsa 233: return (-1);
234: nuked++;
1.35 xsa 235: } else {
236: if ((stat(fpath, &st) == -1) && (errno == ENOENT))
237: nuked++;
1.20 xsa 238: }
1.1 xsa 239:
1.18 joris 240: return (0);
1.1 xsa 241: }