xref: /openbsd-src/usr.bin/cvs/remove.c (revision 94fd4554194a14f126fba33b837cc68a1df42468)
1 /*	$OpenBSD: remove.c,v 1.64 2007/02/22 06:42:09 otto 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 <string.h>
20 #include <unistd.h>
21 
22 #include "cvs.h"
23 #include "remote.h"
24 
25 extern char *__progname;
26 
27 void		cvs_remove_local(struct cvs_file *);
28 void		cvs_remove_force(struct cvs_file *);
29 
30 static int	force_remove = 0;
31 static int	removed = 0;
32 static int	existing = 0;
33 
34 struct cvs_cmd cvs_cmd_remove = {
35 	CVS_OP_REMOVE, 0, "remove",
36 	{ "rm", "delete" },
37 	"Remove an entry from the repository",
38 	"[-flR] [file ...]",
39 	"flR",
40 	NULL,
41 	cvs_remove
42 };
43 
44 int
45 cvs_remove(int argc, char **argv)
46 {
47 	int ch;
48 	int flags;
49 	char *arg = ".";
50 	struct cvs_recursion cr;
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 			break;
63 		default:
64 			fatal("%s", cvs_cmd_remove.cmd_synopsis);
65 		}
66 	}
67 
68 	argc -= optind;
69 	argv += optind;
70 
71 	cr.enterdir = NULL;
72 	cr.leavedir = NULL;
73 	cr.flags = flags;
74 
75 	cr.fileproc = cvs_remove_force;
76 	if (argc > 0)
77 		cvs_file_run(argc, argv, &cr);
78 	else
79 		cvs_file_run(1, &arg, &cr);
80 
81 	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
82 		cvs_client_connect_to_server();
83 		cr.fileproc = cvs_client_sendfile;
84 
85 		if (!(flags & CR_RECURSE_DIRS))
86 			cvs_client_send_request("Argument -l");
87 	} else {
88 		cr.fileproc = cvs_remove_local;
89 	}
90 
91 	if (argc > 0)
92 		cvs_file_run(argc, argv, &cr);
93 	else
94 		cvs_file_run(1, &arg, &cr);
95 
96 	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
97 		cvs_client_send_files(argv, argc);
98 		cvs_client_senddir(".");
99 		cvs_client_send_request("remove");
100 		cvs_client_get_responses();
101 	} else {
102 		if (existing != 0) {
103 			cvs_log(LP_ERR, (existing == 1) ?
104 			    "%d file exists; remove it first" :
105 			    "%d files exist; remove them first", existing);
106 		}
107 
108 		if (removed != 0) {
109 			if (verbosity > 0) {
110 				cvs_log(LP_NOTICE,
111 				    "use '%s commit' to remove %s "
112 				    "permanently", __progname, (removed > 1) ?
113 				    "these files" : "this file");
114 			}
115 		}
116 	}
117 
118 	return (0);
119 }
120 
121 void
122 cvs_remove_force(struct cvs_file *cf)
123 {
124 	if (cf->file_type != CVS_DIR) {
125 		if (cf->fd != -1 && force_remove == 1 && cvs_noexec == 0) {
126 			if (unlink(cf->file_path) == -1)
127 				fatal("cvs_remove_force: %s", strerror(errno));
128 			(void)close(cf->fd);
129 			cf->fd = -1;
130 		}
131 	}
132 }
133 
134 void
135 cvs_remove_local(struct cvs_file *cf)
136 {
137 	CVSENTRIES *entlist;
138 	char *entry, buf[MAXPATHLEN], tbuf[32], rbuf[16];
139 
140 	cvs_log(LP_TRACE, "cvs_remove_local(%s)", cf->file_path);
141 
142 	if (cf->file_type == CVS_DIR) {
143 		if (verbosity > 1)
144 			cvs_log(LP_NOTICE, "Removing %s", cf->file_path);
145 		return;
146 	}
147 
148 	cvs_file_classify(cf, NULL);
149 
150 	if (cf->file_status == FILE_UNKNOWN) {
151 		if (verbosity > 1)
152 			cvs_log(LP_NOTICE, "nothing known about '%s'",
153 			    cf->file_path);
154 		return;
155 	}
156 
157 	if (cf->fd != -1) {
158 		if (verbosity > 1)
159 			cvs_log(LP_ERR, "file `%s' still in working directory",
160 			    cf->file_name);
161 		existing++;
162 	} else {
163 		switch (cf->file_status) {
164 		case FILE_REMOVE_ENTRY:
165 			entlist = cvs_ent_open(cf->file_wd);
166 			cvs_ent_remove(entlist, cf->file_name);
167 			cvs_ent_close(entlist, ENT_SYNC);
168 
169 			(void)xsnprintf(buf, sizeof(buf), "%s/%s/%s%s",
170 			    cf->file_path, CVS_PATH_CVSDIR, cf->file_name,
171 			    CVS_DESCR_FILE_EXT);
172 
173 			(void)unlink(buf);
174 
175 			if (verbosity > 1) {
176 				cvs_log(LP_NOTICE, "removed `%s'",
177 				    cf->file_name);
178 			}
179 			return;
180 		case FILE_REMOVED:
181 			if (verbosity > 0) {
182 				cvs_log(LP_ERR,
183 				    "file `%s' already scheduled for removal",
184 				    cf->file_name);
185 			}
186 			return;
187 		default:
188 			rcsnum_tostr(cf->file_ent->ce_rev, rbuf,
189 			     sizeof(rbuf));
190 
191 			ctime_r(&cf->file_ent->ce_mtime, tbuf);
192 			if (tbuf[strlen(tbuf) - 1] == '\n')
193 				tbuf[strlen(tbuf) - 1] = '\0';
194 
195 			entry = xmalloc(CVS_ENT_MAXLINELEN);
196 			(void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
197 			     "/%s/-%s/%s//", cf->file_name, rbuf, tbuf);
198 
199 			if (cvs_server_active == 1) {
200 				cvs_server_update_entry("Checked-in", cf);
201 				cvs_remote_output(entry);
202 			} else {
203 				entlist = cvs_ent_open(cf->file_wd);
204 				cvs_ent_add(entlist, entry);
205 				cvs_ent_close(entlist, ENT_SYNC);
206 			}
207 
208 			xfree(entry);
209 
210 			if (verbosity > 0) {
211 				cvs_log(LP_NOTICE,
212 				    "scheduling file `%s' for removal",
213 				    cf->file_name);
214 			}
215 
216 			cf->file_status = FILE_REMOVED;
217 			removed++;
218 			break;
219 		}
220 	}
221 }
222