Annotation of src/usr.bin/cvs/add.c, Revision 1.37
1.37 ! xsa 1: /* $OpenBSD: add.c,v 1.36 2005/12/30 02:03:28 joris 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)
144: cvs_log(LP_ERR,
145: "cannot add special file `%s'.", cf->cf_name);
146: return (CVS_EX_FILE);
147: }
148:
1.31 xsa 149: if (cf->cf_type == DT_DIR)
150: return cvs_add_directory(cf);
1.29 xsa 151:
1.28 xsa 152: if ((!(cf->cf_flags & CVS_FILE_ONDISK)) &&
153: (cf->cf_cvstat != CVS_FST_LOST) &&
154: (cf->cf_cvstat != CVS_FST_REMOVED)) {
155: if (verbosity > 1)
156: cvs_log(LP_WARN, "nothing known about `%s'",
157: cf->cf_name);
158: return (0);
159: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
160: if (verbosity > 1)
161: cvs_log(LP_WARN, "`%s' has already been entered",
162: cf->cf_name);
163: return (0);
164: } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
165:
166: /* XXX remove '-' from CVS/Entries */
167:
168: /* XXX check the file out */
169:
170: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
171: cvs_log(LP_WARN, "%s, version %s, resurrected",
172: cf->cf_name, numbuf);
173:
174: return (0);
175: } else if ((cf->cf_cvstat == CVS_FST_CONFLICT) ||
1.34 reyk 176: (cf->cf_cvstat == CVS_FST_LOST) ||
1.28 xsa 177: (cf->cf_cvstat == CVS_FST_MODIFIED) ||
178: (cf->cf_cvstat == CVS_FST_UPTODATE)) {
179: if (verbosity > 1) {
180: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
181: cvs_log(LP_WARN,
182: "%s already exists, with version number %s",
183: cf->cf_name, numbuf);
184: }
185: return (0);
186: }
187:
1.32 xsa 188: if ((ret = cvs_add_build_entry(cf)) != 0)
189: return (ret);
190: else {
1.28 xsa 191: added++;
1.32 xsa 192: if (verbosity > 1)
193: cvs_log(LP_NOTICE, "scheduling file `%s' for addition",
194: cf->cf_name);
1.28 xsa 195: }
196:
197: if (added != 0) {
198: if (verbosity > 0)
199: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
200: "permanently", __progname,
201: (added == 1) ? "this file" : "these files");
202: return (0);
203: }
1.29 xsa 204:
205: return (0);
206: }
207:
208: /*
209: * cvs_add_directory()
210: *
211: * Add a directory to the repository.
212: *
213: * Returns 0 on success, -1 on failure.
214: */
215: static int
216: cvs_add_directory(CVSFILE *cf)
217: {
218: int l, nb;
219: char *date, *repo, *tag;
220: char entry[CVS_ENT_MAXLINELEN], fpath[MAXPATHLEN], rcsdir[MAXPATHLEN];
221: char msg[1024];
222: CVSENTRIES *entf;
223: struct cvsroot *root;
224: struct stat st;
225: struct cvs_ent *ent;
226:
227: entf = (CVSENTRIES *)cf->cf_entry;
228:
229: root = CVS_DIR_ROOT(cf);
230: repo = CVS_DIR_REPO(cf);
231:
232: if (strlcpy(fpath, cf->cf_name, sizeof(fpath)) >= sizeof(fpath))
1.31 xsa 233: return (CVS_EX_DATA);
1.29 xsa 234:
235: if (strchr(fpath, '/') != NULL) {
236: cvs_log(LP_ERR,
237: "directory %s not added; must be a direct sub-directory",
238: fpath);
1.31 xsa 239: return (CVS_EX_FILE);
1.29 xsa 240: }
241:
242: /* Let's see if we have any per-directory tags first */
243: cvs_parse_tagfile(&tag, &date, &nb);
244:
1.30 xsa 245: /* XXX check for <dir>/CVS */
246:
1.29 xsa 247: l = snprintf(rcsdir, sizeof(rcsdir), "%s/%s",
248: root->cr_dir, repo);
249: if (l == -1 || l >= (int)sizeof(rcsdir)) {
250: errno = ENAMETOOLONG;
251: cvs_log(LP_ERRNO, "%s", rcsdir);
1.31 xsa 252: return (CVS_EX_DATA);
1.29 xsa 253: }
254:
255: if ((stat(rcsdir, &st) == 0) && !(S_ISDIR(st.st_mode))) {
256: cvs_log(LP_ERRNO,
257: "%s is not a directory; %s not added", rcsdir, fpath);
1.31 xsa 258: return (CVS_EX_FILE);
1.29 xsa 259: }
260:
261: snprintf(msg, sizeof(msg),
262: "Directory %s added to the repository", rcsdir);
263:
264: if (tag != NULL) {
265: strlcat(msg, "\n--> Using per-directory sticky tag ",
266: sizeof(msg));
267: strlcat(msg, tag, sizeof(msg));
268: }
269: if (date != NULL) {
270: strlcat(msg, "\n--> Using per-directory sticky date ",
271: sizeof(msg));
272: strlcat(msg, date, sizeof(msg));
273: }
274: strlcat(msg, "\n", sizeof(msg));
275:
276: if (cvs_noexec == 0) {
277: if (mkdir(rcsdir, 0777) == -1) {
278: cvs_log(LP_ERRNO, "failed to create %s", rcsdir);
1.31 xsa 279: return (CVS_EX_FILE);
1.29 xsa 280: }
281: }
282:
283: /* create CVS/ admin files */
284: if (cvs_noexec == 0)
1.30 xsa 285: if (cvs_mkadmin(fpath, root->cr_str, repo, tag, date, nb) == -1)
1.31 xsa 286: return (CVS_EX_FILE);
1.29 xsa 287:
288: /* XXX Build the Entries line. */
289: l = snprintf(entry, sizeof(entry), "D/%s////", fpath);
290: if (l == -1 || l >= (int)sizeof(entry)) {
291: errno = ENAMETOOLONG;
292: cvs_log(LP_ERRNO, "%s", entry);
1.31 xsa 293: return (CVS_EX_DATA);
1.33 xsa 294: }
1.29 xsa 295:
296: if ((ent = cvs_ent_parse(entry)) == NULL) {
297: cvs_log(LP_ERR, "failed to parse entry");
1.31 xsa 298: return (CVS_EX_DATA);
1.34 reyk 299: }
1.29 xsa 300:
301: if (cvs_ent_add(entf, ent) < 0) {
302: cvs_log(LP_ERR, "failed to add entry");
1.31 xsa 303: return (CVS_EX_DATA);
1.29 xsa 304: }
305:
306: cvs_printf("%s", msg);
1.28 xsa 307:
308: return (0);
309: }
310:
311: static int
312: cvs_add_build_entry(CVSFILE *cf)
313: {
314: int l;
315: char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
316: FILE *fp;
317: CVSENTRIES *entf;
318: struct cvs_ent *ent;
319:
320: entf = (CVSENTRIES *)cf->cf_entry;
321:
322: if (cvs_noexec == 1)
323: return (0);
324:
325: /* Build the path to the <file>,t file. */
326: l = snprintf(path, sizeof(path), "%s/%s%s",
327: CVS_PATH_CVSDIR, cf->cf_name, CVS_DESCR_FILE_EXT);
328: if (l == -1 || l >= (int)sizeof(path)) {
329: errno = ENAMETOOLONG;
330: cvs_log(LP_ERRNO, "%s", path);
331: return (CVS_EX_DATA);
332: }
333:
334: fp = fopen(path, "w+");
335: if (fp == NULL) {
336: cvs_log(LP_ERRNO, "failed to open `%s'", path);
337: return (CVS_EX_FILE);
338: }
339:
340: if (cvs_msg != NULL) {
341: if (fputs(cvs_msg, fp) == EOF) {
342: cvs_log(LP_ERRNO, "cannot write to `%s'", path);
343: (void)fclose(fp);
344: return (CVS_EX_FILE);
345: }
346: }
347: (void)fclose(fp);
348:
349: /* XXX Build the Entries line. */
350: l = snprintf(entry, sizeof(entry), "/%s/0/Initial %s/%s/",
351: cf->cf_name, cf->cf_name, kbuf);
352: if (l == -1 || l >= (int)sizeof(entry)) {
353: errno = ENAMETOOLONG;
354: cvs_log(LP_ERRNO, "%s", entry);
355: (void)cvs_unlink(path);
356: return (CVS_EX_DATA);
357: }
358:
359: if ((ent = cvs_ent_parse(entry)) == NULL) {
360: cvs_log(LP_ERR, "failed to parse entry");
361: (void)cvs_unlink(path);
362: return (CVS_EX_DATA);
1.34 reyk 363: }
1.28 xsa 364:
365: if (cvs_ent_add(entf, ent) < 0) {
366: cvs_log(LP_ERR, "failed to add entry");
367: (void)cvs_unlink(path);
368: return (CVS_EX_DATA);
369: }
1.22 joris 370:
371: return (0);
1.1 jfb 372: }