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