Annotation of src/usr.bin/cvs/remove.c, Revision 1.53
1.53 ! joris 1: /* $OpenBSD: remove.c,v 1.52 2006/05/31 07:12:32 joris Exp $ */
1.1 xsa 2: /*
1.46 joris 3: * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.1 xsa 4: *
1.46 joris 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.
1.1 xsa 8: *
1.46 joris 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.
1.1 xsa 16: */
17:
1.40 xsa 18: #include "includes.h"
1.1 xsa 19:
20: #include "cvs.h"
21: #include "log.h"
22:
23: extern char *__progname;
24:
1.46 joris 25: int cvs_remove(int, char **);
26: void cvs_remove_local(struct cvs_file *);
1.1 xsa 27:
1.46 joris 28: static int force_remove = 0;
29: static int removed = 0;
30: static int existing = 0;
1.4 joris 31:
1.16 jfb 32: struct cvs_cmd cvs_cmd_remove = {
1.53 ! joris 33: CVS_OP_REMOVE, 0, "remove",
1.16 jfb 34: { "rm", "delete" },
35: "Remove an entry from the repository",
36: "[-flR] [file ...]",
37: "flR",
1.4 joris 38: NULL,
1.46 joris 39: cvs_remove
1.4 joris 40: };
1.1 xsa 41:
1.46 joris 42: int
43: cvs_remove(int argc, char **argv)
1.1 xsa 44: {
1.4 joris 45: int ch;
1.47 joris 46: int flags;
1.46 joris 47: char *arg = ".";
48: struct cvs_recursion cr;
1.1 xsa 49:
1.47 joris 50: flags = CR_RECURSE_DIRS;
1.48 xsa 51: while ((ch = getopt(argc, argv, cvs_cmd_remove.cmd_opts)) != -1) {
1.1 xsa 52: switch (ch) {
53: case 'f':
1.3 xsa 54: force_remove = 1;
1.1 xsa 55: break;
56: case 'l':
1.47 joris 57: flags &= ~CR_RECURSE_DIRS;
1.1 xsa 58: break;
59: case 'R':
60: break;
61: default:
1.48 xsa 62: fatal("%s", cvs_cmd_remove.cmd_synopsis);
1.1 xsa 63: }
64: }
65:
66: argc -= optind;
67: argv += optind;
68:
1.46 joris 69: cr.enterdir = NULL;
70: cr.leavedir = NULL;
71: cr.local = cvs_remove_local;
72: cr.remote = NULL;
1.47 joris 73: cr.flags = flags;
1.46 joris 74:
75: if (argc > 0)
76: cvs_file_run(argc, argv, &cr);
77: else
78: cvs_file_run(1, &arg, &cr);
1.11 xsa 79:
1.49 joris 80: if (existing != 0) {
81: cvs_log(LP_ERR, "%d file(s) exist, remove them first",
82: existing);
83: }
84:
85: if (removed != 0) {
86: if (verbosity > 1)
87: cvs_log(LP_NOTICE, "use '%s commit' to remove %s "
88: "permanently", __progname, (removed > 1) ?
89: "these files" : "this file");
90: }
91:
1.18 joris 92: return (0);
93: }
94:
1.46 joris 95: void
96: cvs_remove_local(struct cvs_file *cf)
1.18 joris 97: {
1.46 joris 98: int l;
99: CVSENTRIES *entlist;
100: char *entry, buf[MAXPATHLEN], tbuf[32], rbuf[16];
1.20 xsa 101:
1.46 joris 102: cvs_log(LP_TRACE, "cvs_remove_local(%s)", cf->file_path);
1.20 xsa 103:
1.46 joris 104: if (cf->file_type == CVS_DIR) {
1.19 xsa 105: if (verbosity > 1)
1.46 joris 106: cvs_log(LP_NOTICE, "Removing %s", cf->file_path);
107: return;
1.19 xsa 108: }
109:
1.50 joris 110: cvs_file_classify(cf, NULL, 0);
1.51 joris 111:
112: if (cf->file_status == FILE_UNKNOWN) {
113: if (verbosity > 1)
114: cvs_log(LP_NOTICE, "nothing known about '%s'",
115: cf->file_path);
116: return;
117: }
1.20 xsa 118:
1.46 joris 119: if (force_remove == 1) {
120: if (unlink(cf->file_path) == -1)
121: fatal("cvs_remove_local: %s", strerror(errno));
122: (void)close(cf->fd);
123: cf->fd = -1;
1.20 xsa 124: }
125:
1.46 joris 126: if (cf->fd != -1) {
127: if (verbosity > 1)
128: cvs_log(LP_ERR, "file `%s' still in working directory",
129: cf->file_name);
1.20 xsa 130: existing++;
131: } else {
1.46 joris 132: switch(cf->file_status) {
133: case FILE_ADDED:
134: entlist = cvs_ent_open(cf->file_wd);
135: cvs_ent_remove(entlist, cf->file_name);
136: cvs_ent_close(entlist, ENT_SYNC);
137:
138: l = snprintf(buf, sizeof(buf), "%s/%s/%s%s",
139: cf->file_path, CVS_PATH_CVSDIR, cf->file_name,
140: CVS_DESCR_FILE_EXT);
141: if (l == -1 || l >= (int)sizeof(buf))
142: fatal("cvs_remove_local: overflow");
143:
144: (void)unlink(buf);
145:
146: if (verbosity > 1) {
147: cvs_log(LP_NOTICE, "removed `%s'",
148: cf->file_name);
149: }
150: return;
151: case FILE_REMOVED:
152: if (verbosity > 1 ) {
153: cvs_log(LP_ERR,
154: "file `%s' already scheduled for removal",
155: cf->file_name);
156: }
157: return;
158: default:
159: rcsnum_tostr(cf->file_ent->ce_rev, rbuf,
160: sizeof(rbuf));
1.34 xsa 161:
1.46 joris 162: ctime_r(&cf->file_ent->ce_mtime, tbuf);
163: if (tbuf[strlen(tbuf) - 1] == '\n')
164: tbuf[strlen(tbuf) - 1] = '\0';
165:
166: entry = xmalloc(CVS_ENT_MAXLINELEN);
167: l = snprintf(entry, CVS_ENT_MAXLINELEN,
168: "/%s/-%s/%s//", cf->file_name, rbuf, tbuf);
169: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
170: fatal("cvs_remove_local: overflow");
171:
172: entlist = cvs_ent_open(cf->file_wd);
173: cvs_ent_add(entlist, entry);
174: cvs_ent_close(entlist, ENT_SYNC);
175:
176: xfree(entry);
177:
178: if (verbosity > 1) {
179: cvs_log(LP_NOTICE,
180: "scheduling file `%s' for removal",
181: cf->file_name);
182: }
1.34 xsa 183:
1.46 joris 184: cf->file_status = FILE_REMOVED;
185: removed++;
186: break;
187: }
1.20 xsa 188: }
1.1 xsa 189: }