Annotation of src/usr.bin/cvs/remove.c, Revision 1.3
1.3 ! xsa 1: /* $OpenBSD: remove.c,v 1.2 2005/01/31 16:49:28 jfb 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>
1.2 jfb 35: #include <libgen.h>
1.1 xsa 36: #include <sysexits.h>
37: #include <unistd.h>
38:
39: #include "cvs.h"
40: #include "log.h"
41: #include "proto.h"
42:
43:
44: extern char *__progname;
45:
46:
47: int cvs_remove_file (CVSFILE *, void *);
48:
1.3 ! xsa 49: int force_remove = 0; /* -f option */
1.1 xsa 50:
51: /*
52: * cvs_remove()
53: *
54: * Handler for the `cvs remove' command.
55: * Returns 0 on success, or one of the known system exit codes on failure.
56: */
57: int
58: cvs_remove(int argc, char **argv)
59: {
60: int i, ch;
61: struct cvsroot *root;
62:
63: while ((ch = getopt(argc, argv, "flR")) != -1) {
64: switch (ch) {
65: case 'f':
1.3 ! xsa 66: force_remove = 1;
1.1 xsa 67: break;
68: case 'l':
69: break;
70: case 'R':
71: break;
72: default:
73: return (EX_USAGE);
74: }
75: }
76:
77: argc -= optind;
78: argv += optind;
79:
80: if (argc == 0)
81: return (EX_USAGE);
82:
83: cvs_files = cvs_file_getspec(argv, argc, 0);
84: if (cvs_files == NULL)
85: return (EX_DATAERR);
86:
87: root = CVS_DIR_ROOT(cvs_files);
88: if (root == NULL) {
89: cvs_log(LP_ERR,
90: "No CVSROOT specified! Please use the `-d' option");
91: cvs_log(LP_ERR,
92: "or set the CVSROOT environment variable.");
93: return (EX_USAGE);
94: }
95:
96: if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
97: return (EX_PROTOCOL);
98:
99: cvs_file_examine(cvs_files, cvs_remove_file, NULL);
100:
101: if (root->cr_method != CVS_METHOD_LOCAL) {
102: if (cvs_senddir(root, cvs_files) < 0)
103: return (EX_PROTOCOL);
104:
105: for (i = 0; i < argc; i++)
106: if (cvs_sendarg(root, argv[i], 0) < 0)
107: return (EX_PROTOCOL);
108:
109: if (cvs_sendreq(root, CVS_REQ_REMOVE, NULL) < 0)
110: return (EX_PROTOCOL);
111: }
112:
113: return (0);
114: }
115:
116:
117: int
118: cvs_remove_file(CVSFILE *cf, void *arg)
119: {
120: int ret;
1.2 jfb 121: char fpath[MAXPATHLEN];
1.1 xsa 122: struct cvsroot *root;
1.2 jfb 123: CVSENTRIES *entfile;
124: struct cvs_ent *ent;
1.1 xsa 125:
126: ret = 0;
1.2 jfb 127: ent = NULL;
1.1 xsa 128: root = CVS_DIR_ROOT(cf);
129:
130: if (cf->cf_type == DT_DIR) {
131: if (root->cr_method != CVS_METHOD_LOCAL) {
132: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
133: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
134: CVS_FILE_NAME(cf));
135: else
136: ret = cvs_senddir(root, cf);
137: }
138:
139: return (ret);
140: }
141:
1.2 jfb 142: cvs_file_getpath(cf, fpath, sizeof(fpath));
143:
144: entfile = cvs_ent_open(dirname(fpath), O_RDWR);
145: if (entfile == NULL) {
146: cvs_log(LP_ERR, "failed to remove `%s'", fpath);
147: return (-1);
148: }
149:
150: ent = cvs_ent_get(entfile, CVS_FILE_NAME(cf));
151:
1.1 xsa 152: if (root->cr_method != CVS_METHOD_LOCAL) {
1.2 jfb 153: if (ent != NULL)
154: ret = cvs_sendentry(root, ent);
1.1 xsa 155: } else {
1.3 ! xsa 156: /* if -f option is used, physically remove the file */
! 157: if (force_remove == 1) {
! 158: if((unlink(fpath) == -1) && (errno != ENOENT)) {
! 159: cvs_log(LP_ERRNO, "failed to unlink `%s'",
! 160: fpath);
! 161: return (-1);
! 162: }
! 163: }
! 164:
1.1 xsa 165: cvs_log(LP_INFO, "scheduling file `%s' for removal",
166: CVS_FILE_NAME(cf));
1.2 jfb 167: cvs_log(LP_INFO,
168: "use `%s commit' to remove this file permanently",
1.1 xsa 169: __progname);
170: }
1.2 jfb 171:
172: cvs_ent_close(entfile);
1.1 xsa 173:
174: return (ret);
175: }