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