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