xref: /openbsd-src/usr.bin/cvs/remove.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: remove.c,v 1.61 2007/01/27 20:23:26 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 "includes.h"
19 
20 #include "cvs.h"
21 #include "log.h"
22 #include "remote.h"
23 
24 extern char *__progname;
25 
26 void		cvs_remove_local(struct cvs_file *);
27 void		cvs_remove_force(struct cvs_file *);
28 
29 static int	force_remove = 0;
30 static int	removed = 0;
31 static int	existing = 0;
32 
33 struct cvs_cmd cvs_cmd_remove = {
34 	CVS_OP_REMOVE, 0, "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 
51 	flags = CR_RECURSE_DIRS;
52 	while ((ch = getopt(argc, argv, cvs_cmd_remove.cmd_opts)) != -1) {
53 		switch (ch) {
54 		case 'f':
55 			force_remove = 1;
56 			break;
57 		case 'l':
58 			flags &= ~CR_RECURSE_DIRS;
59 			break;
60 		case 'R':
61 			break;
62 		default:
63 			fatal("%s", cvs_cmd_remove.cmd_synopsis);
64 		}
65 	}
66 
67 	argc -= optind;
68 	argv += optind;
69 
70 	cr.enterdir = NULL;
71 	cr.leavedir = NULL;
72 	cr.flags = flags;
73 
74 	cr.fileproc = cvs_remove_force;
75 	if (argc > 0)
76 		cvs_file_run(argc, argv, &cr);
77 	else
78 		cvs_file_run(1, &arg, &cr);
79 
80 	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
81 		cvs_client_connect_to_server();
82 		cr.fileproc = cvs_client_sendfile;
83 
84 		if (!(flags & CR_RECURSE_DIRS))
85 			cvs_client_send_request("Argument -l");
86 	} else {
87 		cr.fileproc = cvs_remove_local;
88 	}
89 
90 	if (argc > 0)
91 		cvs_file_run(argc, argv, &cr);
92 	else
93 		cvs_file_run(1, &arg, &cr);
94 
95 	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
96 		cvs_client_send_files(argv, argc);
97 		cvs_client_senddir(".");
98 		cvs_client_send_request("remove");
99 		cvs_client_get_responses();
100 	} else {
101 		if (existing != 0) {
102 			cvs_log(LP_ERR, (existing == 1) ?
103 			    "%d file exists; remove it first" :
104 			    "%d files exist; remove them first", existing);
105 		}
106 
107 		if (removed != 0) {
108 			if (verbosity > 0) {
109 				cvs_log(LP_NOTICE,
110 				    "use '%s commit' to remove %s "
111 				    "permanently", __progname, (removed > 1) ?
112 				    "these files" : "this file");
113 			}
114 		}
115 	}
116 
117 	return (0);
118 }
119 
120 void
121 cvs_remove_force(struct cvs_file *cf)
122 {
123 	if (cf->file_type != CVS_DIR) {
124 		if (cf->fd != -1 && force_remove == 1 && cvs_noexec == 0) {
125 			if (unlink(cf->file_path) == -1)
126 				fatal("cvs_remove_force: %s", strerror(errno));
127 			(void)close(cf->fd);
128 			cf->fd = -1;
129 		}
130 	}
131 }
132 
133 void
134 cvs_remove_local(struct cvs_file *cf)
135 {
136 	int l;
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, 0);
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 			l = snprintf(buf, sizeof(buf), "%s/%s/%s%s",
170 			    cf->file_path, CVS_PATH_CVSDIR, cf->file_name,
171 			    CVS_DESCR_FILE_EXT);
172 			if (l == -1 || l >= (int)sizeof(buf))
173 				fatal("cvs_remove_local: overflow");
174 
175 			(void)unlink(buf);
176 
177 			if (verbosity > 1) {
178 				cvs_log(LP_NOTICE, "removed `%s'",
179 				    cf->file_name);
180 			}
181 			return;
182 		case FILE_REMOVED:
183 			if (verbosity > 0) {
184 				cvs_log(LP_ERR,
185 				    "file `%s' already scheduled for removal",
186 				    cf->file_name);
187 			}
188 			return;
189 		default:
190 			rcsnum_tostr(cf->file_ent->ce_rev, rbuf,
191 			     sizeof(rbuf));
192 
193 			ctime_r(&cf->file_ent->ce_mtime, tbuf);
194 			if (tbuf[strlen(tbuf) - 1] == '\n')
195 				tbuf[strlen(tbuf) - 1] = '\0';
196 
197 			entry = xmalloc(CVS_ENT_MAXLINELEN);
198 			l = snprintf(entry, CVS_ENT_MAXLINELEN,
199 			     "/%s/-%s/%s//", cf->file_name, rbuf, tbuf);
200 			if (l == -1 || l >= CVS_ENT_MAXLINELEN)
201 				fatal("cvs_remove_local: overflow");
202 
203 			if (cvs_server_active == 1) {
204 				cvs_server_update_entry("Checked-in", cf);
205 				cvs_remote_output(entry);
206 			} else {
207 				entlist = cvs_ent_open(cf->file_wd);
208 				cvs_ent_add(entlist, entry);
209 				cvs_ent_close(entlist, ENT_SYNC);
210 			}
211 
212 			xfree(entry);
213 
214 			if (verbosity > 0) {
215 				cvs_log(LP_NOTICE,
216 				    "scheduling file `%s' for removal",
217 				    cf->file_name);
218 			}
219 
220 			cf->file_status = FILE_REMOVED;
221 			removed++;
222 			break;
223 		}
224 	}
225 }
226