Annotation of src/usr.bin/cvs/add.c, Revision 1.40
1.40 ! xsa 1: /* $OpenBSD: add.c,v 1.39 2006/01/27 12:45:21 xsa Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.28 xsa 4: * Copyright (c) 2005 Xavier Santolaria <xsa@openbsd.org>
1.8 tedu 5: * All rights reserved.
1.1 jfb 6: *
1.8 tedu 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
1.1 jfb 10: *
1.8 tedu 11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
1.1 jfb 13: * 2. The name of the author may not be used to endorse or promote products
1.8 tedu 14: * derived from this software without specific prior written permission.
1.1 jfb 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
1.8 tedu 25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 26: */
27:
1.37 xsa 28: #include "includes.h"
1.1 jfb 29:
30: #include "cvs.h"
31: #include "log.h"
1.2 jfb 32: #include "proto.h"
1.1 jfb 33:
34:
1.3 jfb 35: extern char *__progname;
36:
37:
1.25 xsa 38: static int cvs_add_remote(CVSFILE *, void *);
39: static int cvs_add_local(CVSFILE *, void *);
40: static int cvs_add_init(struct cvs_cmd *, int, char **, int *);
41: static int cvs_add_pre_exec(struct cvsroot *);
1.29 xsa 42: static int cvs_add_directory(CVSFILE *);
1.28 xsa 43: static int cvs_add_build_entry(CVSFILE *);
1.21 jfb 44:
45: struct cvs_cmd cvs_cmd_add = {
46: CVS_OP_ADD, CVS_REQ_ADD, "add",
47: { "ad", "new" },
48: "Add a new file/directory to the repository",
49: "[-k mode] [-m msg] file ...",
50: "k:m:",
51: NULL,
52: 0,
53: cvs_add_init,
54: cvs_add_pre_exec,
1.22 joris 55: cvs_add_remote,
56: cvs_add_local,
1.21 jfb 57: NULL,
58: NULL,
1.15 joris 59: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
60: };
1.3 jfb 61:
1.15 joris 62: static int kflag = RCS_KWEXP_DEFAULT;
63: static char *koptstr;
1.28 xsa 64: static char kbuf[16];
1.3 jfb 65:
1.21 jfb 66: static int
67: cvs_add_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 68: {
1.15 joris 69: int ch;
1.1 jfb 70:
1.13 jfb 71: cvs_msg = NULL;
1.1 jfb 72:
1.21 jfb 73: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 74: switch (ch) {
75: case 'k':
1.13 jfb 76: koptstr = optarg;
77: kflag = rcs_kflag_get(koptstr);
78: if (RCS_KWEXP_INVAL(kflag)) {
79: cvs_log(LP_ERR,
80: "invalid RCS keyword expansion mode");
81: rcs_kflag_usage();
1.19 joris 82: return (CVS_EX_USAGE);
1.13 jfb 83: }
1.1 jfb 84: break;
85: case 'm':
1.35 joris 86: cvs_msg = xstrdup(optarg);
1.1 jfb 87: break;
88: default:
1.19 joris 89: return (CVS_EX_USAGE);
1.1 jfb 90: }
91: }
92:
1.15 joris 93: *arg = optind;
94: return (0);
95: }
1.10 xsa 96:
1.21 jfb 97: static int
98: cvs_add_pre_exec(struct cvsroot *root)
1.15 joris 99: {
1.28 xsa 100: kbuf[0] = '\0';
1.3 jfb 101:
1.28 xsa 102: if (kflag != RCS_KWEXP_DEFAULT) {
103: strlcpy(kbuf, "-k", sizeof(kbuf));
104: strlcat(kbuf, koptstr, sizeof(kbuf));
105:
1.36 joris 106: if (root->cr_method != CVS_METHOD_LOCAL)
107: cvs_sendarg(root, kbuf, 0);
1.10 xsa 108: }
1.3 jfb 109:
110: return (0);
111: }
112:
1.21 jfb 113: static int
1.22 joris 114: cvs_add_remote(CVSFILE *cf, void *arg)
1.3 jfb 115: {
116: struct cvsroot *root;
117:
1.10 xsa 118: root = CVS_DIR_ROOT(cf);
119:
1.3 jfb 120: if (cf->cf_type == DT_DIR) {
1.36 joris 121: cvs_senddir(root, cf);
122: return (0);
1.1 jfb 123: }
124:
1.27 joris 125: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.36 joris 126: cvs_sendreq(root, CVS_REQ_ISMODIFIED, cf->cf_name);
1.23 joris 127:
1.36 joris 128: return (0);
1.22 joris 129: }
130:
1.28 xsa 131: static int
132: cvs_add_local(CVSFILE *cf, void *arg)
1.22 joris 133: {
1.32 xsa 134: int added, ret;
1.28 xsa 135: char numbuf[64];
136:
137: added = 0;
138:
1.33 xsa 139: /* dont use `cvs add *' */
140: if ((strcmp(cf->cf_name, ".") == 0) ||
141: (strcmp(cf->cf_name, "..") == 0) ||
142: (strcmp(cf->cf_name, CVS_PATH_CVSDIR) == 0)) {
143: if (verbosity > 1)
1.39 xsa 144: fatal("cannot add special file `%s'.", cf->cf_name);
1.33 xsa 145: }
146:
1.31 xsa 147: if (cf->cf_type == DT_DIR)
148: return cvs_add_directory(cf);
1.29 xsa 149:
1.28 xsa 150: if ((!(cf->cf_flags & CVS_FILE_ONDISK)) &&
151: (cf->cf_cvstat != CVS_FST_LOST) &&
152: (cf->cf_cvstat != CVS_FST_REMOVED)) {
153: if (verbosity > 1)
154: cvs_log(LP_WARN, "nothing known about `%s'",
155: cf->cf_name);
156: return (0);
157: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
158: if (verbosity > 1)
159: cvs_log(LP_WARN, "`%s' has already been entered",
160: cf->cf_name);
161: return (0);
162: } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
163:
164: /* XXX remove '-' from CVS/Entries */
165:
166: /* XXX check the file out */
167:
168: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
169: cvs_log(LP_WARN, "%s, version %s, resurrected",
170: cf->cf_name, numbuf);
171:
172: return (0);
173: } else if ((cf->cf_cvstat == CVS_FST_CONFLICT) ||
1.34 reyk 174: (cf->cf_cvstat == CVS_FST_LOST) ||
1.28 xsa 175: (cf->cf_cvstat == CVS_FST_MODIFIED) ||
176: (cf->cf_cvstat == CVS_FST_UPTODATE)) {
177: if (verbosity > 1) {
178: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
179: cvs_log(LP_WARN,
180: "%s already exists, with version number %s",
181: cf->cf_name, numbuf);
182: }
183: return (0);
184: }
185:
1.32 xsa 186: if ((ret = cvs_add_build_entry(cf)) != 0)
187: return (ret);
188: else {
1.28 xsa 189: added++;
1.32 xsa 190: if (verbosity > 1)
191: cvs_log(LP_NOTICE, "scheduling file `%s' for addition",
192: cf->cf_name);
1.28 xsa 193: }
194:
195: if (added != 0) {
196: if (verbosity > 0)
197: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
198: "permanently", __progname,
199: (added == 1) ? "this file" : "these files");
200: return (0);
201: }
1.29 xsa 202:
203: return (0);
204: }
205:
206: /*
207: * cvs_add_directory()
208: *
209: * Add a directory to the repository.
210: *
211: * Returns 0 on success, -1 on failure.
212: */
213: static int
214: cvs_add_directory(CVSFILE *cf)
215: {
1.38 xsa 216: int nb;
1.29 xsa 217: char *date, *repo, *tag;
218: char entry[CVS_ENT_MAXLINELEN], fpath[MAXPATHLEN], rcsdir[MAXPATHLEN];
219: char msg[1024];
220: CVSENTRIES *entf;
221: struct cvsroot *root;
222: struct stat st;
223: struct cvs_ent *ent;
224:
225: entf = (CVSENTRIES *)cf->cf_entry;
226:
227: root = CVS_DIR_ROOT(cf);
228: repo = CVS_DIR_REPO(cf);
229:
230: if (strlcpy(fpath, cf->cf_name, sizeof(fpath)) >= sizeof(fpath))
1.39 xsa 231: fatal("cvs_add_directory: path truncation");
1.29 xsa 232:
1.39 xsa 233: if (strchr(fpath, '/') != NULL)
234: fatal("directory %s not added; must be a direct sub-directory",
1.29 xsa 235: fpath);
236:
237: /* Let's see if we have any per-directory tags first */
238: cvs_parse_tagfile(&tag, &date, &nb);
239:
1.30 xsa 240: /* XXX check for <dir>/CVS */
241:
1.38 xsa 242: if (strlcpy(rcsdir, root->cr_dir, sizeof(rcsdir)) >= sizeof(rcsdir) ||
243: strlcat(rcsdir, "/", sizeof(rcsdir)) >= sizeof(rcsdir) ||
244: strlcat(rcsdir, repo, sizeof(rcsdir)) >= sizeof(rcsdir))
245: fatal("cvs_add_directory: path truncation");
1.29 xsa 246:
1.39 xsa 247: if ((stat(rcsdir, &st) == 0) && !(S_ISDIR(st.st_mode)))
248: fatal("%s is not a directory; %s not added: %s", rcsdir, fpath,
249: strerror(errno));
1.29 xsa 250:
251: snprintf(msg, sizeof(msg),
252: "Directory %s added to the repository", rcsdir);
253:
254: if (tag != NULL) {
255: strlcat(msg, "\n--> Using per-directory sticky tag ",
256: sizeof(msg));
257: strlcat(msg, tag, sizeof(msg));
258: }
259: if (date != NULL) {
260: strlcat(msg, "\n--> Using per-directory sticky date ",
261: sizeof(msg));
262: strlcat(msg, date, sizeof(msg));
263: }
264: strlcat(msg, "\n", sizeof(msg));
265:
266: if (cvs_noexec == 0) {
1.39 xsa 267: if (mkdir(rcsdir, 0777) == -1)
268: fatal("cvs_add_directory: mkdir `%s': %s",
269: rcsdir, strerror(errno));
1.29 xsa 270: }
271:
272: /* create CVS/ admin files */
273: if (cvs_noexec == 0)
1.40 ! xsa 274: cvs_mkadmin(fpath, root->cr_str, repo, tag, date, nb);
1.29 xsa 275:
276: /* XXX Build the Entries line. */
1.38 xsa 277: if (strlcpy(entry, "D/", sizeof(entry)) >= sizeof(entry) ||
278: strlcat(entry, fpath, sizeof(entry)) >= sizeof(entry) ||
279: strlcat(entry, "////", sizeof(entry)) >= sizeof(entry))
280: fatal("cvs_add_directory: path truncation");
1.29 xsa 281:
1.39 xsa 282: if ((ent = cvs_ent_parse(entry)) == NULL)
283: fatal("cvs_add_directory: cvs_ent_parse failed");
1.29 xsa 284:
1.39 xsa 285: if (cvs_ent_add(entf, ent) < 0)
286: fatal("cvs_add_directory: cvs_ent_parse failed");
1.29 xsa 287:
288: cvs_printf("%s", msg);
1.28 xsa 289:
290: return (0);
291: }
292:
293: static int
294: cvs_add_build_entry(CVSFILE *cf)
295: {
296: char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
297: FILE *fp;
298: CVSENTRIES *entf;
299: struct cvs_ent *ent;
300:
301: entf = (CVSENTRIES *)cf->cf_entry;
302:
303: if (cvs_noexec == 1)
304: return (0);
305:
306: /* Build the path to the <file>,t file. */
1.38 xsa 307: if (strlcpy(path, CVS_PATH_CVSDIR, sizeof(path)) >= sizeof(path) ||
308: strlcat(path, "/", sizeof(path)) >= sizeof(path) ||
309: strlcat(path, cf->cf_name, sizeof(path)) >= sizeof(path) ||
310: strlcat(path, CVS_DESCR_FILE_EXT, sizeof(path)) >= sizeof(path))
311: fatal("cvs_add_build_entry: path truncation");
1.28 xsa 312:
1.39 xsa 313: if ((fp = fopen(path, "w+")) == NULL)
314: fatal("cvs_add_build_entry: fopen `%s': %s", path,
315: strerror(errno));
1.28 xsa 316:
317: if (cvs_msg != NULL) {
1.39 xsa 318: if (fputs(cvs_msg, fp) == EOF)
319: fatal("cvs_add_build_entry: fputs `%s': %s", path,
320: strerror(errno));
1.28 xsa 321: }
322: (void)fclose(fp);
323:
324: /* XXX Build the Entries line. */
1.38 xsa 325: if (strlcpy(entry, "/", sizeof(entry)) >= sizeof(entry) ||
326: strlcat(entry, cf->cf_name, sizeof(entry)) >= sizeof(entry) ||
327: strlcat(entry, "/0/Initial ", sizeof(entry)) >= sizeof(entry) ||
328: strlcat(entry, cf->cf_name, sizeof(entry)) >= sizeof(entry) ||
329: strlcat(entry, "/", sizeof(entry)) >= sizeof(entry) ||
330: strlcat(entry, kbuf, sizeof(entry)) >= sizeof(entry) ||
331: strlcat(entry, "/", sizeof(entry)) >= sizeof(entry)) {
1.28 xsa 332: (void)cvs_unlink(path);
1.38 xsa 333: fatal("cvs_add_build_entry: path truncation");
1.28 xsa 334: }
335:
336: if ((ent = cvs_ent_parse(entry)) == NULL) {
337: (void)cvs_unlink(path);
1.39 xsa 338: fatal("cvs_add_build_entry: cvs_ent_parse failed");
1.34 reyk 339: }
1.28 xsa 340:
341: if (cvs_ent_add(entf, ent) < 0) {
342: (void)cvs_unlink(path);
1.39 xsa 343: fatal("cvs_add_build_entry: cvs_ent_add failed");
1.28 xsa 344: }
1.22 joris 345:
346: return (0);
1.1 jfb 347: }