Annotation of src/usr.bin/cvs/remove.c, Revision 1.21
1.21 ! xsa 1: /* $OpenBSD: remove.c,v 1.20 2005/07/20 06:59:27 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:
28: #include <sys/types.h>
29:
30: #include <errno.h>
1.2 jfb 31: #include <fcntl.h>
1.1 xsa 32: #include <stdio.h>
33: #include <stdlib.h>
34: #include <string.h>
35: #include <unistd.h>
36:
37: #include "cvs.h"
38: #include "log.h"
39: #include "proto.h"
40:
41:
42: extern char *__progname;
43:
44:
1.20 xsa 45: static int cvs_remove_init (struct cvs_cmd *, int, char **, int *);
1.18 joris 46: static int cvs_remove_remote (CVSFILE *, void *);
1.20 xsa 47: static int cvs_remove_local (CVSFILE *, void *);
48: static int cvs_remove_file (const char *);
1.1 xsa 49:
1.4 joris 50: static int force_remove = 0; /* -f option */
1.20 xsa 51: static int nuked = 0;
1.4 joris 52:
1.16 jfb 53: struct cvs_cmd cvs_cmd_remove = {
54: CVS_OP_REMOVE, CVS_REQ_REMOVE, "remove",
55: { "rm", "delete" },
56: "Remove an entry from the repository",
57: "[-flR] [file ...]",
58: "flR",
1.4 joris 59: NULL,
1.16 jfb 60: CF_IGNORE | CF_RECURSE,
61: cvs_remove_init,
62: NULL,
1.18 joris 63: cvs_remove_remote,
64: cvs_remove_local,
1.16 jfb 65: NULL,
66: NULL,
1.4 joris 67: CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
68: };
1.1 xsa 69:
1.13 xsa 70: static int
1.16 jfb 71: cvs_remove_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 xsa 72: {
1.4 joris 73: int ch;
1.1 xsa 74:
1.16 jfb 75: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 xsa 76: switch (ch) {
77: case 'f':
1.3 xsa 78: force_remove = 1;
1.1 xsa 79: break;
80: case 'l':
1.16 jfb 81: cmd->file_flags &= ~CF_RECURSE;
1.1 xsa 82: break;
83: case 'R':
1.16 jfb 84: cmd->file_flags |= CF_RECURSE;
1.1 xsa 85: break;
86: default:
1.7 joris 87: return (CVS_EX_USAGE);
1.1 xsa 88: }
89: }
90:
91: argc -= optind;
92: argv += optind;
93:
1.4 joris 94: *arg = optind;
1.1 xsa 95: return (0);
96: }
97:
98:
1.13 xsa 99: static int
1.18 joris 100: cvs_remove_remote(CVSFILE *cf, void *arg)
1.1 xsa 101: {
102: int ret;
1.2 jfb 103: char fpath[MAXPATHLEN];
1.1 xsa 104: struct cvsroot *root;
105:
106: ret = 0;
107: root = CVS_DIR_ROOT(cf);
108:
109: if (cf->cf_type == DT_DIR) {
1.18 joris 110: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
111: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
1.20 xsa 112: cf->cf_name);
1.18 joris 113: else
114: ret = cvs_senddir(root, cf);
1.1 xsa 115:
1.18 joris 116: if (ret == -1)
117: ret = CVS_EX_PROTO;
1.1 xsa 118: return (ret);
119: }
120:
1.2 jfb 121: cvs_file_getpath(cf, fpath, sizeof(fpath));
122:
1.20 xsa 123: if (cvs_remove_file(fpath) < 0)
124: return (CVS_EX_FILE);
1.18 joris 125:
126: if (cvs_sendentry(root, cf) < 0)
127: return (CVS_EX_PROTO);
1.3 xsa 128:
1.18 joris 129: if (cf->cf_cvstat != CVS_FST_LOST && force_remove != 1) {
1.20 xsa 130: if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name < 0)) {
1.10 xsa 131: return (CVS_EX_PROTO);
1.18 joris 132: }
1.17 joris 133:
1.18 joris 134: if (cvs_sendfile(root, fpath) < 0)
135: return (CVS_EX_PROTO);
136: }
1.11 xsa 137:
1.18 joris 138: return (0);
139: }
140:
141: static int
142: cvs_remove_local(CVSFILE *cf, void *arg)
143: {
1.21 ! xsa 144: int existing, l, removed;
! 145: char buf[MAXPATHLEN], fpath[MAXPATHLEN];
1.20 xsa 146:
147: existing = removed = 0;
148:
1.19 xsa 149: if (cf->cf_type == DT_DIR) {
150: if (verbosity > 1)
151: cvs_log(LP_INFO, "Removing %s", cf->cf_name);
152: return (0);
153: }
154:
1.20 xsa 155: if (cvs_cmdop != CVS_OP_SERVER) {
156: cvs_file_getpath(cf, fpath, sizeof(fpath));
157:
158: if (cvs_remove_file(fpath) < 0)
159: return (CVS_EX_FILE);
160: }
161:
162: if (!nuked) {
163: existing++;
164: if (verbosity > 1)
165: cvs_log(LP_WARN, "file `%s' still in working directory",
166: cf->cf_name);
167: } else if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
168: if (verbosity > 1)
169: cvs_log(LP_WARN, "nothing known about `%s'",
170: cf->cf_name);
171: return (0);
172: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
1.21 ! xsa 173: l = snprintf(buf, sizeof(buf), "%s/%s%s",
! 174: CVS_PATH_CVSDIR, cf->cf_name, CVS_DESCR_FILE_EXT);
! 175: if (l == -1 || l >= (int)sizeof(buf)) {
! 176: errno = ENAMETOOLONG;
! 177: cvs_log(LP_ERRNO, "%s", buf);
! 178: return (CVS_EX_DATA);
! 179: }
! 180: if (!cvs_noexec && (unlink(buf) == -1) && (errno != ENOENT)) {
! 181: cvs_log(LP_ERRNO, "cannot remove %s", buf);
! 182: return (CVS_EX_FILE);
! 183: }
1.20 xsa 184: if (verbosity > 1)
185: cvs_log(LP_INFO, "removed `%s'", cf->cf_name);
186: return (0);
187: } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
188: if (verbosity > 1 )
189: cvs_log(LP_WARN,
190: "file `%s' already scheduled for removal",
191: cf->cf_name);
192: return (0);
193: } else {
194: if (verbosity > 1)
195: cvs_log(LP_INFO, "scheduling file `%s' for removal",
196: cf->cf_name);
197: removed++;
198: }
199:
200: if (removed) {
201: if (verbosity > 0)
202: cvs_log(LP_INFO, "use '%s commit' to remove %s "
203: "permanently", __progname,
204: (removed == 1) ? "file file" : "these files");
205: return (0);
206: }
207:
208: if (existing) {
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: */
225: static
226: int cvs_remove_file(const char *fpath)
227: {
228: /* if -f option is used, physically remove the file */
229: if ((force_remove == 1) && !cvs_noexec) {
230: if((unlink(fpath) == -1) && (errno != ENOENT)) {
231: cvs_log(LP_ERRNO, "unable to remove %s", fpath);
232: return (-1);
233: }
234: nuked++;
235: }
1.1 xsa 236:
1.18 joris 237: return (0);
1.1 xsa 238: }