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