Annotation of src/usr.bin/cvs/remove.c, Revision 1.4
1.4 ! joris 1: /* $OpenBSD: remove.c,v 1.3 2005/03/09 15:24:36 xsa 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:
1.4 ! joris 47: int cvs_remove_file(CVSFILE *, void *);
! 48: int cvs_remove_options(char *, int, char **, int *);
1.1 xsa 49:
1.4 ! joris 50: static int force_remove = 0; /* -f option */
! 51:
! 52: struct cvs_cmd_info cvs_remove = {
! 53: cvs_remove_options,
! 54: NULL,
! 55: cvs_remove_file,
! 56: NULL, NULL,
! 57: 0,
! 58: CVS_REQ_REMOVE,
! 59: CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
! 60: };
1.1 xsa 61:
62: int
1.4 ! joris 63: cvs_remove_options(char *opt, int argc, char **argv, int *arg)
1.1 xsa 64: {
1.4 ! joris 65: int ch;
1.1 xsa 66:
1.4 ! joris 67: while ((ch = getopt(argc, argv, opt)) != -1) {
1.1 xsa 68: switch (ch) {
69: case 'f':
1.3 xsa 70: force_remove = 1;
1.1 xsa 71: break;
72: case 'l':
73: break;
74: case 'R':
75: break;
76: default:
77: return (EX_USAGE);
78: }
79: }
80:
81: argc -= optind;
82: argv += optind;
83:
84: if (argc == 0)
85: return (EX_USAGE);
86:
1.4 ! joris 87: *arg = optind;
1.1 xsa 88: return (0);
89: }
90:
91:
92: int
93: cvs_remove_file(CVSFILE *cf, void *arg)
94: {
95: int ret;
1.2 jfb 96: char fpath[MAXPATHLEN];
1.1 xsa 97: struct cvsroot *root;
1.2 jfb 98: CVSENTRIES *entfile;
99: struct cvs_ent *ent;
1.1 xsa 100:
101: ret = 0;
1.2 jfb 102: ent = NULL;
1.1 xsa 103: root = CVS_DIR_ROOT(cf);
104:
105: if (cf->cf_type == DT_DIR) {
106: if (root->cr_method != CVS_METHOD_LOCAL) {
107: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
108: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
109: CVS_FILE_NAME(cf));
110: else
111: ret = cvs_senddir(root, cf);
112: }
113:
114: return (ret);
115: }
116:
1.2 jfb 117: cvs_file_getpath(cf, fpath, sizeof(fpath));
118:
119: entfile = cvs_ent_open(dirname(fpath), O_RDWR);
120: if (entfile == NULL) {
121: cvs_log(LP_ERR, "failed to remove `%s'", fpath);
122: return (-1);
123: }
124:
125: ent = cvs_ent_get(entfile, CVS_FILE_NAME(cf));
126:
1.1 xsa 127: if (root->cr_method != CVS_METHOD_LOCAL) {
1.2 jfb 128: if (ent != NULL)
129: ret = cvs_sendentry(root, ent);
1.1 xsa 130: } else {
1.3 xsa 131: /* if -f option is used, physically remove the file */
132: if (force_remove == 1) {
133: if((unlink(fpath) == -1) && (errno != ENOENT)) {
134: cvs_log(LP_ERRNO, "failed to unlink `%s'",
135: fpath);
136: return (-1);
137: }
138: }
139:
1.1 xsa 140: cvs_log(LP_INFO, "scheduling file `%s' for removal",
141: CVS_FILE_NAME(cf));
1.2 jfb 142: cvs_log(LP_INFO,
143: "use `%s commit' to remove this file permanently",
1.1 xsa 144: __progname);
145: }
1.2 jfb 146:
147: cvs_ent_close(entfile);
1.1 xsa 148:
149: return (ret);
150: }