xref: /openbsd-src/usr.bin/cvs/remove.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1 /*	$OpenBSD: remove.c,v 1.1 2004/12/21 18:15:55 xsa Exp $	*/
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>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sysexits.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 
45 int  cvs_remove_file (CVSFILE *, void *);
46 
47 
48 /*
49  * cvs_remove()
50  *
51  * Handler for the `cvs remove' command.
52  * Returns 0 on success, or one of the known system exit codes on failure.
53  */
54 int
55 cvs_remove(int argc, char **argv)
56 {
57 	int i, ch;
58 	struct cvsroot *root;
59 
60 	while ((ch = getopt(argc, argv, "flR")) != -1) {
61 		switch (ch) {
62 		case 'f':
63 			break;
64 		case 'l':
65 			break;
66 		case 'R':
67 			break;
68 		default:
69 			return (EX_USAGE);
70 		}
71 	}
72 
73 	argc -= optind;
74 	argv += optind;
75 
76 	if (argc == 0)
77 		return (EX_USAGE);
78 
79 	cvs_files = cvs_file_getspec(argv, argc, 0);
80 	if (cvs_files == NULL)
81 		return (EX_DATAERR);
82 
83 	root = CVS_DIR_ROOT(cvs_files);
84 	if (root == NULL) {
85 		cvs_log(LP_ERR,
86 		    "No CVSROOT specified!  Please use the `-d' option");
87 		cvs_log(LP_ERR,
88 		    "or set the CVSROOT environment variable.");
89 		return (EX_USAGE);
90 	}
91 
92 	if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
93 		return (EX_PROTOCOL);
94 
95 	cvs_file_examine(cvs_files, cvs_remove_file, NULL);
96 
97 	if (root->cr_method != CVS_METHOD_LOCAL) {
98 		if (cvs_senddir(root, cvs_files) < 0)
99 			return (EX_PROTOCOL);
100 
101 		for (i = 0; i < argc; i++)
102 			if (cvs_sendarg(root, argv[i], 0) < 0)
103 				return (EX_PROTOCOL);
104 
105 		if (cvs_sendreq(root, CVS_REQ_REMOVE, NULL) < 0)
106 			return (EX_PROTOCOL);
107 	}
108 
109 	return (0);
110 }
111 
112 
113 int
114 cvs_remove_file(CVSFILE *cf, void *arg)
115 {
116 	int ret;
117 	struct cvsroot *root;
118 
119 	ret = 0;
120 	root = CVS_DIR_ROOT(cf);
121 
122 	if (cf->cf_type == DT_DIR) {
123 		if (root->cr_method != CVS_METHOD_LOCAL) {
124 			if (cf->cf_cvstat == CVS_FST_UNKNOWN)
125 				ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
126 				    CVS_FILE_NAME(cf));
127 			else
128 				ret = cvs_senddir(root, cf);
129 		}
130 
131 		return (ret);
132 	}
133 
134 	if (root->cr_method != CVS_METHOD_LOCAL) {
135 		ret = cvs_sendreq(root, CVS_REQ_REMOVE, CVS_FILE_NAME(cf));
136 	} else {
137 		cvs_log(LP_INFO, "scheduling file `%s' for removal",
138 		    CVS_FILE_NAME(cf));
139 		cvs_log(LP_INFO, "use `%s commit' to remove this file permanently",
140 		    __progname);
141 	}
142 
143 	return (ret);
144 }
145