Annotation of src/usr.bin/cvs/remove.c, Revision 1.11
1.11 ! xsa 1: /* $OpenBSD: remove.c,v 1.10 2005/04/21 18:54:50 xsa Exp $ */
1.1 xsa 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * Copyright (c) 2004 Xavier Santolaria <xsa@openbsd.org>
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.4 joris 45: int cvs_remove_file(CVSFILE *, void *);
46: int cvs_remove_options(char *, int, char **, int *);
1.1 xsa 47:
1.4 joris 48: static int force_remove = 0; /* -f option */
49:
50: struct cvs_cmd_info cvs_remove = {
51: cvs_remove_options,
52: NULL,
53: cvs_remove_file,
54: NULL, NULL,
1.10 xsa 55: CF_IGNORE | CF_RECURSE,
1.4 joris 56: CVS_REQ_REMOVE,
57: CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
58: };
1.1 xsa 59:
60: int
1.4 joris 61: cvs_remove_options(char *opt, int argc, char **argv, int *arg)
1.1 xsa 62: {
1.4 joris 63: int ch;
1.1 xsa 64:
1.4 joris 65: while ((ch = getopt(argc, argv, opt)) != -1) {
1.1 xsa 66: switch (ch) {
67: case 'f':
1.3 xsa 68: force_remove = 1;
1.1 xsa 69: break;
70: case 'l':
1.9 xsa 71: cvs_remove.file_flags &= ~CF_RECURSE;
1.1 xsa 72: break;
73: case 'R':
1.9 xsa 74: cvs_remove.file_flags |= CF_RECURSE;
1.1 xsa 75: break;
76: default:
1.7 joris 77: return (CVS_EX_USAGE);
1.1 xsa 78: }
79: }
80:
81: argc -= optind;
82: argv += optind;
83:
84: if (argc == 0)
1.7 joris 85: return (CVS_EX_USAGE);
1.1 xsa 86:
1.4 joris 87: *arg = optind;
1.1 xsa 88: return (0);
89: }
90:
91:
92: int
93: cvs_remove_file(CVSFILE *cf, void *arg)
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) {
103: if (root->cr_method != CVS_METHOD_LOCAL) {
104: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
105: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
106: CVS_FILE_NAME(cf));
107: else
108: ret = cvs_senddir(root, cf);
109: }
110:
111: return (ret);
112: }
113:
1.2 jfb 114: cvs_file_getpath(cf, fpath, sizeof(fpath));
115:
1.1 xsa 116: if (root->cr_method != CVS_METHOD_LOCAL) {
1.3 xsa 117: /* if -f option is used, physically remove the file */
118: if (force_remove == 1) {
119: if((unlink(fpath) == -1) && (errno != ENOENT)) {
1.10 xsa 120: cvs_log(LP_ERRNO,
121: "failed to unlink `%s'", fpath);
1.7 joris 122: return (CVS_EX_FILE);
1.3 xsa 123: }
124: }
125:
1.10 xsa 126: if (cvs_sendentry(root, cf) < 0)
127: return (CVS_EX_PROTO);
128:
129: if (cf->cf_cvstat != CVS_FST_LOST && force_remove != 1) {
130: if (cvs_sendreq(root, CVS_REQ_MODIFIED,
131: CVS_FILE_NAME(cf)) < 0) {
132: return (CVS_EX_PROTO);
133: }
1.11 ! xsa 134:
! 135: if (cvs_sendfile(root, fpath) < 0)
! 136: return (CVS_EX_PROTO);
1.10 xsa 137: }
138: } else {
1.1 xsa 139: cvs_log(LP_INFO, "scheduling file `%s' for removal",
140: CVS_FILE_NAME(cf));
1.2 jfb 141: cvs_log(LP_INFO,
142: "use `%s commit' to remove this file permanently",
1.1 xsa 143: __progname);
144: }
145:
146: return (ret);
147: }