Annotation of src/usr.bin/cvs/remove.c, Revision 1.34
1.34 ! xsa 1: /* $OpenBSD: remove.c,v 1.33 2005/08/10 15:34:46 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.26 xsa 45: static int cvs_remove_init(struct cvs_cmd *, int, char **, int *);
46: static int cvs_remove_remote(CVSFILE *, void *);
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.32 joris 130: if (cf->cf_cvstat != CVS_FST_ADDED) {
131: if (cvs_sendreq(root, CVS_REQ_MODIFIED,
132: cf->cf_name) < 0) {
133: return (CVS_EX_PROTO);
134: }
1.18 joris 135: }
1.17 joris 136:
1.31 joris 137: if (cf->cf_flags & CVS_FILE_ONDISK) {
138: if (cvs_sendfile(root, fpath) < 0)
139: return (CVS_EX_PROTO);
140: }
1.18 joris 141: }
1.11 xsa 142:
1.18 joris 143: return (0);
144: }
145:
146: static int
147: cvs_remove_local(CVSFILE *cf, void *arg)
148: {
1.21 xsa 149: int existing, l, removed;
150: char buf[MAXPATHLEN], fpath[MAXPATHLEN];
1.20 xsa 151:
152: existing = removed = 0;
153:
1.19 xsa 154: if (cf->cf_type == DT_DIR) {
155: if (verbosity > 1)
1.29 xsa 156: cvs_log(LP_NOTICE, "Removing %s", cf->cf_name);
1.19 xsa 157: return (0);
158: }
159:
1.20 xsa 160: if (cvs_cmdop != CVS_OP_SERVER) {
161: cvs_file_getpath(cf, fpath, sizeof(fpath));
162:
163: if (cvs_remove_file(fpath) < 0)
164: return (CVS_EX_FILE);
165: }
166:
1.25 xsa 167: if (nuked == 0) {
1.20 xsa 168: existing++;
169: if (verbosity > 1)
170: cvs_log(LP_WARN, "file `%s' still in working directory",
171: cf->cf_name);
172: } else if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
173: if (verbosity > 1)
174: cvs_log(LP_WARN, "nothing known about `%s'",
175: cf->cf_name);
176: return (0);
177: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
1.34 ! xsa 178:
! 179: /* XXX scratch it from CVS/Entries */
! 180:
1.21 xsa 181: l = snprintf(buf, sizeof(buf), "%s/%s%s",
182: CVS_PATH_CVSDIR, cf->cf_name, CVS_DESCR_FILE_EXT);
183: if (l == -1 || l >= (int)sizeof(buf)) {
184: errno = ENAMETOOLONG;
185: cvs_log(LP_ERRNO, "%s", buf);
186: return (CVS_EX_DATA);
187: }
1.28 xsa 188: if (cvs_unlink(buf) == -1)
1.21 xsa 189: return (CVS_EX_FILE);
1.24 xsa 190:
1.20 xsa 191: if (verbosity > 1)
1.30 xsa 192: cvs_log(LP_NOTICE, "removed `%s'", cf->cf_name);
1.20 xsa 193: return (0);
194: } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
195: if (verbosity > 1 )
196: cvs_log(LP_WARN,
197: "file `%s' already scheduled for removal",
198: cf->cf_name);
199: return (0);
200: } else {
1.34 ! xsa 201:
! 202: /* XXX prefix file version with the `-' char in CVS/Entries */
! 203:
1.20 xsa 204: if (verbosity > 1)
1.30 xsa 205: cvs_log(LP_NOTICE, "scheduling file `%s' for removal",
1.20 xsa 206: cf->cf_name);
207: removed++;
208: }
209:
1.25 xsa 210: if (removed != 0) {
1.20 xsa 211: if (verbosity > 0)
1.30 xsa 212: cvs_log(LP_NOTICE, "use '%s commit' to remove %s "
1.20 xsa 213: "permanently", __progname,
1.27 xsa 214: (removed == 1) ? "this file" : "these files");
1.20 xsa 215: return (0);
216: }
217:
1.25 xsa 218: if (existing != 0) {
1.20 xsa 219: cvs_log(LP_WARN, ((existing == 1) ?
220: "%d file exists; remove it first" :
221: "%d files exist; remove them first"), existing);
222: return (0);
223: }
224:
225: return (0);
226: }
227:
228: /*
229: * cvs_remove_file()
230: *
231: * Physically remove the file.
232: * Used by both remote and local handlers.
233: * Returns 0 on success, -1 on failure.
234: */
1.33 xsa 235: static int
236: cvs_remove_file(const char *fpath)
1.20 xsa 237: {
238: /* if -f option is used, physically remove the file */
1.24 xsa 239: if (force_remove == 1) {
1.28 xsa 240: if(cvs_unlink(fpath) == -1)
1.20 xsa 241: return (-1);
242: nuked++;
243: }
1.1 xsa 244:
1.18 joris 245: return (0);
1.1 xsa 246: }