Annotation of src/usr.bin/cvs/add.c, Revision 1.32
1.32 ! xsa 1: /* $OpenBSD: add.c,v 1.31 2005/09/21 16:30:03 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:
28: #include <sys/types.h>
1.29 xsa 29: #include <sys/stat.h>
1.1 jfb 30:
31: #include <errno.h>
32: #include <stdio.h>
33: #include <stdlib.h>
34: #include <string.h>
1.10 xsa 35: #include <unistd.h>
1.1 jfb 36:
37: #include "cvs.h"
38: #include "log.h"
1.2 jfb 39: #include "proto.h"
1.1 jfb 40:
41:
1.3 jfb 42: extern char *__progname;
43:
44:
1.25 xsa 45: static int cvs_add_remote(CVSFILE *, void *);
46: static int cvs_add_local(CVSFILE *, void *);
47: static int cvs_add_init(struct cvs_cmd *, int, char **, int *);
48: static int cvs_add_pre_exec(struct cvsroot *);
1.29 xsa 49: static int cvs_add_directory(CVSFILE *);
1.28 xsa 50: static int cvs_add_build_entry(CVSFILE *);
1.21 jfb 51:
52: struct cvs_cmd cvs_cmd_add = {
53: CVS_OP_ADD, CVS_REQ_ADD, "add",
54: { "ad", "new" },
55: "Add a new file/directory to the repository",
56: "[-k mode] [-m msg] file ...",
57: "k:m:",
58: NULL,
59: 0,
60: cvs_add_init,
61: cvs_add_pre_exec,
1.22 joris 62: cvs_add_remote,
63: cvs_add_local,
1.21 jfb 64: NULL,
65: NULL,
1.15 joris 66: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
67: };
1.3 jfb 68:
1.15 joris 69: static int kflag = RCS_KWEXP_DEFAULT;
70: static char *koptstr;
1.28 xsa 71: static char kbuf[16];
1.3 jfb 72:
1.21 jfb 73: static int
74: cvs_add_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 75: {
1.15 joris 76: int ch;
1.1 jfb 77:
1.13 jfb 78: cvs_msg = NULL;
1.1 jfb 79:
1.21 jfb 80: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 81: switch (ch) {
82: case 'k':
1.13 jfb 83: koptstr = optarg;
84: kflag = rcs_kflag_get(koptstr);
85: if (RCS_KWEXP_INVAL(kflag)) {
86: cvs_log(LP_ERR,
87: "invalid RCS keyword expansion mode");
88: rcs_kflag_usage();
1.19 joris 89: return (CVS_EX_USAGE);
1.13 jfb 90: }
1.1 jfb 91: break;
92: case 'm':
1.13 jfb 93: if ((cvs_msg = strdup(optarg)) == NULL) {
94: cvs_log(LP_ERRNO, "failed to copy message");
1.19 joris 95: return (CVS_EX_DATA);
1.13 jfb 96: }
1.1 jfb 97: break;
98: default:
1.19 joris 99: return (CVS_EX_USAGE);
1.1 jfb 100: }
101: }
102:
1.15 joris 103: *arg = optind;
104: return (0);
105: }
1.10 xsa 106:
1.21 jfb 107: static int
108: cvs_add_pre_exec(struct cvsroot *root)
1.15 joris 109: {
1.28 xsa 110: kbuf[0] = '\0';
1.3 jfb 111:
1.28 xsa 112: if (kflag != RCS_KWEXP_DEFAULT) {
113: strlcpy(kbuf, "-k", sizeof(kbuf));
114: strlcat(kbuf, koptstr, sizeof(kbuf));
115:
116: if (root->cr_method != CVS_METHOD_LOCAL) {
117: if (cvs_sendarg(root, kbuf, 0) < 0)
118: return (CVS_EX_PROTO);
119: }
1.10 xsa 120: }
1.3 jfb 121:
122: return (0);
123: }
124:
1.21 jfb 125: static int
1.22 joris 126: cvs_add_remote(CVSFILE *cf, void *arg)
1.3 jfb 127: {
1.10 xsa 128: int ret;
1.3 jfb 129: struct cvsroot *root;
130:
1.10 xsa 131: ret = 0;
132: root = CVS_DIR_ROOT(cf);
133:
1.3 jfb 134: if (cf->cf_type == DT_DIR) {
1.22 joris 135: ret = cvs_senddir(root, cf);
1.20 joris 136: if (ret == -1)
137: ret = CVS_EX_PROTO;
1.10 xsa 138: return (ret);
1.1 jfb 139: }
140:
1.27 joris 141: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.22 joris 142: ret = cvs_sendreq(root, CVS_REQ_ISMODIFIED,
1.21 jfb 143: cf->cf_name);
1.23 joris 144:
145: if (ret == -1)
146: ret = CVS_EX_PROTO;
1.1 jfb 147:
1.10 xsa 148: return (ret);
1.22 joris 149: }
150:
1.28 xsa 151: static int
152: cvs_add_local(CVSFILE *cf, void *arg)
1.22 joris 153: {
1.32 ! xsa 154: int added, ret;
1.28 xsa 155: char numbuf[64];
156:
157: added = 0;
158:
1.31 xsa 159: if (cf->cf_type == DT_DIR)
160: return cvs_add_directory(cf);
1.29 xsa 161:
1.28 xsa 162: if ((!(cf->cf_flags & CVS_FILE_ONDISK)) &&
163: (cf->cf_cvstat != CVS_FST_LOST) &&
164: (cf->cf_cvstat != CVS_FST_REMOVED)) {
165: if (verbosity > 1)
166: cvs_log(LP_WARN, "nothing known about `%s'",
167: cf->cf_name);
168: return (0);
169: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
170: if (verbosity > 1)
171: cvs_log(LP_WARN, "`%s' has already been entered",
172: cf->cf_name);
173: return (0);
174: } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
175:
176: /* XXX remove '-' from CVS/Entries */
177:
178: /* XXX check the file out */
179:
180: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
181: cvs_log(LP_WARN, "%s, version %s, resurrected",
182: cf->cf_name, numbuf);
183:
184: return (0);
185: } else if ((cf->cf_cvstat == CVS_FST_CONFLICT) ||
186: (cf->cf_cvstat == CVS_FST_LOST) ||
187: (cf->cf_cvstat == CVS_FST_MODIFIED) ||
188: (cf->cf_cvstat == CVS_FST_UPTODATE)) {
189: if (verbosity > 1) {
190: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
191: cvs_log(LP_WARN,
192: "%s already exists, with version number %s",
193: cf->cf_name, numbuf);
194: }
195: return (0);
196: }
197:
1.32 ! xsa 198: if ((ret = cvs_add_build_entry(cf)) != 0)
! 199: return (ret);
! 200: else {
1.28 xsa 201: added++;
1.32 ! xsa 202: if (verbosity > 1)
! 203: cvs_log(LP_NOTICE, "scheduling file `%s' for addition",
! 204: cf->cf_name);
1.28 xsa 205: }
206:
207: if (added != 0) {
208: if (verbosity > 0)
209: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
210: "permanently", __progname,
211: (added == 1) ? "this file" : "these files");
212: return (0);
213: }
1.29 xsa 214:
215: return (0);
216: }
217:
218: /*
219: * cvs_add_directory()
220: *
221: * Add a directory to the repository.
222: *
223: * Returns 0 on success, -1 on failure.
224: */
225: static int
226: cvs_add_directory(CVSFILE *cf)
227: {
228: int l, nb;
229: char *date, *repo, *tag;
230: char entry[CVS_ENT_MAXLINELEN], fpath[MAXPATHLEN], rcsdir[MAXPATHLEN];
231: char msg[1024];
232: CVSENTRIES *entf;
233: struct cvsroot *root;
234: struct stat st;
235: struct cvs_ent *ent;
236:
237: entf = (CVSENTRIES *)cf->cf_entry;
238:
239: root = CVS_DIR_ROOT(cf);
240: repo = CVS_DIR_REPO(cf);
241:
242: if (strlcpy(fpath, cf->cf_name, sizeof(fpath)) >= sizeof(fpath))
1.31 xsa 243: return (CVS_EX_DATA);
1.29 xsa 244:
245: if (strchr(fpath, '/') != NULL) {
246: cvs_log(LP_ERR,
247: "directory %s not added; must be a direct sub-directory",
248: fpath);
1.31 xsa 249: return (CVS_EX_FILE);
1.29 xsa 250: }
251:
252: /* Let's see if we have any per-directory tags first */
253: cvs_parse_tagfile(&tag, &date, &nb);
254:
1.30 xsa 255: /* XXX check for <dir>/CVS */
256:
1.29 xsa 257: l = snprintf(rcsdir, sizeof(rcsdir), "%s/%s",
258: root->cr_dir, repo);
259: if (l == -1 || l >= (int)sizeof(rcsdir)) {
260: errno = ENAMETOOLONG;
261: cvs_log(LP_ERRNO, "%s", rcsdir);
1.31 xsa 262: return (CVS_EX_DATA);
1.29 xsa 263: }
264:
265: if ((stat(rcsdir, &st) == 0) && !(S_ISDIR(st.st_mode))) {
266: cvs_log(LP_ERRNO,
267: "%s is not a directory; %s not added", rcsdir, fpath);
1.31 xsa 268: return (CVS_EX_FILE);
1.29 xsa 269: }
270:
271: snprintf(msg, sizeof(msg),
272: "Directory %s added to the repository", rcsdir);
273:
274: if (tag != NULL) {
275: strlcat(msg, "\n--> Using per-directory sticky tag ",
276: sizeof(msg));
277: strlcat(msg, tag, sizeof(msg));
278: }
279: if (date != NULL) {
280: strlcat(msg, "\n--> Using per-directory sticky date ",
281: sizeof(msg));
282: strlcat(msg, date, sizeof(msg));
283: }
284: strlcat(msg, "\n", sizeof(msg));
285:
286: if (cvs_noexec == 0) {
287: if (mkdir(rcsdir, 0777) == -1) {
288: cvs_log(LP_ERRNO, "failed to create %s", rcsdir);
1.31 xsa 289: return (CVS_EX_FILE);
1.29 xsa 290: }
291: }
292:
293: /* create CVS/ admin files */
294: if (cvs_noexec == 0)
1.30 xsa 295: if (cvs_mkadmin(fpath, root->cr_str, repo, tag, date, nb) == -1)
1.31 xsa 296: return (CVS_EX_FILE);
1.29 xsa 297:
298: /* XXX Build the Entries line. */
299: l = snprintf(entry, sizeof(entry), "D/%s////", fpath);
300: if (l == -1 || l >= (int)sizeof(entry)) {
301: errno = ENAMETOOLONG;
302: cvs_log(LP_ERRNO, "%s", entry);
1.31 xsa 303: return (CVS_EX_DATA);
1.29 xsa 304: }
305:
306: if ((ent = cvs_ent_parse(entry)) == NULL) {
307: cvs_log(LP_ERR, "failed to parse entry");
1.31 xsa 308: return (CVS_EX_DATA);
1.29 xsa 309: }
310:
311: if (cvs_ent_add(entf, ent) < 0) {
312: cvs_log(LP_ERR, "failed to add entry");
1.31 xsa 313: return (CVS_EX_DATA);
1.29 xsa 314: }
315:
316: cvs_printf("%s", msg);
1.28 xsa 317:
318: return (0);
319: }
320:
321: static int
322: cvs_add_build_entry(CVSFILE *cf)
323: {
324: int l;
325: char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
326: FILE *fp;
327: CVSENTRIES *entf;
328: struct cvs_ent *ent;
329:
330: entf = (CVSENTRIES *)cf->cf_entry;
331:
332: if (cvs_noexec == 1)
333: return (0);
334:
335: /* Build the path to the <file>,t file. */
336: l = snprintf(path, sizeof(path), "%s/%s%s",
337: CVS_PATH_CVSDIR, cf->cf_name, CVS_DESCR_FILE_EXT);
338: if (l == -1 || l >= (int)sizeof(path)) {
339: errno = ENAMETOOLONG;
340: cvs_log(LP_ERRNO, "%s", path);
341: return (CVS_EX_DATA);
342: }
343:
344: fp = fopen(path, "w+");
345: if (fp == NULL) {
346: cvs_log(LP_ERRNO, "failed to open `%s'", path);
347: return (CVS_EX_FILE);
348: }
349:
350: if (cvs_msg != NULL) {
351: if (fputs(cvs_msg, fp) == EOF) {
352: cvs_log(LP_ERRNO, "cannot write to `%s'", path);
353: (void)fclose(fp);
354: return (CVS_EX_FILE);
355: }
356: }
357: (void)fclose(fp);
358:
359: /* XXX Build the Entries line. */
360: l = snprintf(entry, sizeof(entry), "/%s/0/Initial %s/%s/",
361: cf->cf_name, cf->cf_name, kbuf);
362: if (l == -1 || l >= (int)sizeof(entry)) {
363: errno = ENAMETOOLONG;
364: cvs_log(LP_ERRNO, "%s", entry);
365: (void)cvs_unlink(path);
366: return (CVS_EX_DATA);
367: }
368:
369: if ((ent = cvs_ent_parse(entry)) == NULL) {
370: cvs_log(LP_ERR, "failed to parse entry");
371: (void)cvs_unlink(path);
372: return (CVS_EX_DATA);
373: }
374:
375: if (cvs_ent_add(entf, ent) < 0) {
376: cvs_log(LP_ERR, "failed to add entry");
377: (void)cvs_unlink(path);
378: return (CVS_EX_DATA);
379: }
1.22 joris 380:
381: return (0);
1.1 jfb 382: }