xref: /openbsd/usr.bin/cvs/remove.c (revision d415bd75)
1 /*	$OpenBSD: remove.c,v 1.84 2017/06/01 08:08:24 joris Exp $	*/
2 /*
3  * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include "cvs.h"
24 #include "remote.h"
25 
26 extern char *__progname;
27 
28 void		cvs_remove_force(struct cvs_file *);
29 
30 static int	removed = 0;
31 static int	existing = 0;
32 
33 struct cvs_cmd cvs_cmd_remove = {
34 	CVS_OP_REMOVE, CVS_USE_WDIR, "remove",
35 	{ "rm", "delete" },
36 	"Remove an entry from the repository",
37 	"[-flR] [file ...]",
38 	"flR",
39 	NULL,
40 	cvs_remove
41 };
42 
43 int
44 cvs_remove(int argc, char **argv)
45 {
46 	int ch;
47 	int flags;
48 	char *arg = ".";
49 	struct cvs_recursion cr;
50 	int force_remove = 0;
51 
52 	flags = CR_RECURSE_DIRS;
53 	while ((ch = getopt(argc, argv, cvs_cmd_remove.cmd_opts)) != -1) {
54 		switch (ch) {
55 		case 'f':
56 			force_remove = 1;
57 			break;
58 		case 'l':
59 			flags &= ~CR_RECURSE_DIRS;
60 			break;
61 		case 'R':
62 			flags |= CR_RECURSE_DIRS;
63 			break;
64 		default:
65 			fatal("%s", cvs_cmd_remove.cmd_synopsis);
66 		}
67 	}
68 
69 	argc -= optind;
70 	argv += optind;
71 
72 	cr.enterdir = NULL;
73 	cr.leavedir = NULL;
74 	cr.flags = flags;
75 
76 	if (force_remove == 1 && cvs_noexec == 0) {
77 		cr.fileproc = cvs_remove_force;
78 		if (argc > 0)
79 			cvs_file_run(argc, argv, &cr);
80 		else
81 			cvs_file_run(1, &arg, &cr);
82 	}
83 
84 	if (cvsroot_is_remote()) {
85 		cvs_client_connect_to_server();
86 		cr.fileproc = cvs_client_sendfile;
87 
88 		if (!(flags & CR_RECURSE_DIRS))
89 			cvs_client_send_request("Argument -l");
90 	} else {
91 		cr.fileproc = cvs_remove_local;
92 	}
93 
94 	if (argc > 0)
95 		cvs_file_run(argc, argv, &cr);
96 	else
97 		cvs_file_run(1, &arg, &cr);
98 
99 	if (cvsroot_is_remote()) {
100 		cvs_client_send_files(argv, argc);
101 		cvs_client_senddir(".");
102 		cvs_client_send_request("remove");
103 		cvs_client_get_responses();
104 	} else {
105 		if (existing != 0) {
106 			cvs_log(LP_ERR, (existing == 1) ?
107 			    "%d file exists; remove it first" :
108 			    "%d files exist; remove them first", existing);
109 		}
110 
111 		if (removed != 0) {
112 			if (verbosity > 0) {
113 				cvs_log(LP_NOTICE,
114 				    "use '%s commit' to remove %s "
115 				    "permanently", __progname, (removed > 1) ?
116 				    "these files" : "this file");
117 			}
118 		}
119 	}
120 
121 	return (0);
122 }
123 
124 void
125 cvs_remove_force(struct cvs_file *cf)
126 {
127 	if (cf->file_type != CVS_DIR) {
128 		if (cf->file_flags & FILE_ON_DISK) {
129 			if (unlink(cf->file_path) == -1)
130 				fatal("cvs_remove_force: %s", strerror(errno));
131 			(void)close(cf->fd);
132 			cf->fd = -1;
133 		}
134 	}
135 }
136 
137 void
138 cvs_remove_local(struct cvs_file *cf)
139 {
140 	CVSENTRIES *entlist;
141 	char *entry, buf[PATH_MAX], tbuf[CVS_TIME_BUFSZ], rbuf[CVS_REV_BUFSZ];
142 	char sticky[CVS_ENT_MAXLINELEN];
143 
144 	cvs_log(LP_TRACE, "cvs_remove_local(%s)", cf->file_path);
145 
146 	if (cf->file_type == CVS_DIR) {
147 		if (verbosity > 1)
148 			cvs_log(LP_NOTICE, "Removing %s", cf->file_path);
149 		return;
150 	}
151 
152 	if (cvs_cmdop != CVS_OP_CHECKOUT && cvs_cmdop != CVS_OP_UPDATE)
153 		cvs_file_classify(cf, cvs_directory_tag);
154 
155 	if (cf->file_status == FILE_UNKNOWN) {
156 		if (verbosity > 1)
157 			cvs_log(LP_NOTICE, "nothing known about '%s'",
158 			    cf->file_path);
159 		return;
160 	}
161 
162 	if (cf->file_flags & FILE_ON_DISK) {
163 		if (verbosity > 1)
164 			cvs_log(LP_ERR, "file `%s' still in working directory",
165 			    cf->file_name);
166 		existing++;
167 	} else {
168 		switch (cf->file_status) {
169 		case FILE_REMOVE_ENTRY:
170 			entlist = cvs_ent_open(cf->file_wd);
171 			cvs_ent_remove(entlist, cf->file_name);
172 
173 			(void)xsnprintf(buf, sizeof(buf), "%s/%s/%s%s",
174 			    cf->file_wd, CVS_PATH_CVSDIR, cf->file_name,
175 			    CVS_DESCR_FILE_EXT);
176 
177 			(void)unlink(buf);
178 
179 			if (verbosity > 1) {
180 				cvs_log(LP_NOTICE, "removed `%s'",
181 				    cf->file_name);
182 			}
183 			return;
184 		case FILE_REMOVED:
185 			if (verbosity > 0) {
186 				cvs_log(LP_ERR,
187 				    "file `%s' already scheduled for removal",
188 				    cf->file_name);
189 			}
190 			return;
191 		case FILE_LOST:
192 			rcsnum_tostr(cf->file_ent->ce_rev, rbuf, sizeof(rbuf));
193 
194 			ctime_r(&cf->file_ent->ce_mtime, tbuf);
195 			tbuf[strcspn(tbuf, "\n")] = '\0';
196 
197 			sticky[0] = '\0';
198 			if (cf->file_ent->ce_tag != NULL)
199 				(void)xsnprintf(sticky, sizeof(sticky), "T%s",
200 				    cf->file_ent->ce_tag);
201 
202 			entry = xmalloc(CVS_ENT_MAXLINELEN);
203 			cvs_ent_line_str(cf->file_name, rbuf, tbuf,
204 			    cf->file_ent->ce_opts ?
205 			    cf->file_ent->ce_opts : "", sticky, 0, 1,
206 			    entry, CVS_ENT_MAXLINELEN);
207 
208 			if (cvs_server_active == 1) {
209 				cvs_server_update_entry("Checked-in", cf);
210 				cvs_remote_output(entry);
211 			} else {
212 				entlist = cvs_ent_open(cf->file_wd);
213 				cvs_ent_add(entlist, entry);
214 			}
215 
216 			free(entry);
217 
218 			if (verbosity > 0) {
219 				cvs_log(LP_NOTICE,
220 				    "scheduling file `%s' for removal",
221 				    cf->file_name);
222 			}
223 
224 			cf->file_status = FILE_REMOVED;
225 			removed++;
226 			break;
227 		}
228 	}
229 }
230