Annotation of src/usr.bin/cvs/add.c, Revision 1.28
1.28 ! xsa 1: /* $OpenBSD: add.c,v 1.27 2005/07/30 00:01:50 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:
28: #include <sys/types.h>
29:
30: #include <errno.h>
31: #include <stdio.h>
32: #include <stdlib.h>
33: #include <string.h>
1.10 xsa 34: #include <unistd.h>
1.1 jfb 35:
36: #include "cvs.h"
37: #include "log.h"
1.2 jfb 38: #include "proto.h"
1.1 jfb 39:
40:
1.3 jfb 41: extern char *__progname;
42:
43:
1.25 xsa 44: static int cvs_add_remote(CVSFILE *, void *);
45: static int cvs_add_local(CVSFILE *, void *);
46: static int cvs_add_init(struct cvs_cmd *, int, char **, int *);
47: static int cvs_add_pre_exec(struct cvsroot *);
1.28 ! xsa 48: #if 0
! 49: static int cvs_add_build_entry(CVSFILE *);
! 50: #endif
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.28 ! xsa 154: int added;
! 155: char numbuf[64];
! 156:
! 157: added = 0;
! 158:
! 159: if ((!(cf->cf_flags & CVS_FILE_ONDISK)) &&
! 160: (cf->cf_cvstat != CVS_FST_LOST) &&
! 161: (cf->cf_cvstat != CVS_FST_REMOVED)) {
! 162: if (verbosity > 1)
! 163: cvs_log(LP_WARN, "nothing known about `%s'",
! 164: cf->cf_name);
! 165: return (0);
! 166: } else if (cf->cf_cvstat == CVS_FST_ADDED) {
! 167: if (verbosity > 1)
! 168: cvs_log(LP_WARN, "`%s' has already been entered",
! 169: cf->cf_name);
! 170: return (0);
! 171: } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
! 172:
! 173: /* XXX remove '-' from CVS/Entries */
! 174:
! 175: /* XXX check the file out */
! 176:
! 177: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
! 178: cvs_log(LP_WARN, "%s, version %s, resurrected",
! 179: cf->cf_name, numbuf);
! 180:
! 181: return (0);
! 182: } else if ((cf->cf_cvstat == CVS_FST_CONFLICT) ||
! 183: (cf->cf_cvstat == CVS_FST_LOST) ||
! 184: (cf->cf_cvstat == CVS_FST_MODIFIED) ||
! 185: (cf->cf_cvstat == CVS_FST_UPTODATE)) {
! 186: if (verbosity > 1) {
! 187: rcsnum_tostr(cf->cf_lrev, numbuf, sizeof(numbuf));
! 188: cvs_log(LP_WARN,
! 189: "%s already exists, with version number %s",
! 190: cf->cf_name, numbuf);
! 191: }
! 192: return (0);
! 193: }
! 194:
! 195: if (verbosity > 1) {
! 196: cvs_log(LP_NOTICE, "scheduling file `%s' for addition",
! 197: cf->cf_name);
! 198: added++;
! 199: }
! 200:
! 201: if (added != 0) {
! 202: if (verbosity > 0)
! 203: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
! 204: "permanently", __progname,
! 205: (added == 1) ? "this file" : "these files");
! 206: return (0);
! 207: }
! 208:
! 209: return (0);
! 210: }
! 211:
! 212: #if 0
! 213: static int
! 214: cvs_add_build_entry(CVSFILE *cf)
! 215: {
! 216: int l;
! 217: char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
! 218: FILE *fp;
! 219: CVSENTRIES *entf;
! 220: struct cvs_ent *ent;
! 221:
! 222: entf = (CVSENTRIES *)cf->cf_entry;
! 223:
! 224: if (cvs_noexec == 1)
! 225: return (0);
! 226:
! 227: /* Build the path to the <file>,t file. */
! 228: l = snprintf(path, sizeof(path), "%s/%s%s",
! 229: CVS_PATH_CVSDIR, cf->cf_name, CVS_DESCR_FILE_EXT);
! 230: if (l == -1 || l >= (int)sizeof(path)) {
! 231: errno = ENAMETOOLONG;
! 232: cvs_log(LP_ERRNO, "%s", path);
! 233: return (CVS_EX_DATA);
! 234: }
! 235:
! 236: fp = fopen(path, "w+");
! 237: if (fp == NULL) {
! 238: cvs_log(LP_ERRNO, "failed to open `%s'", path);
! 239: return (CVS_EX_FILE);
! 240: }
! 241:
! 242: if (cvs_msg != NULL) {
! 243: if (fputs(cvs_msg, fp) == EOF) {
! 244: cvs_log(LP_ERRNO, "cannot write to `%s'", path);
! 245: (void)fclose(fp);
! 246: return (CVS_EX_FILE);
! 247: }
! 248: }
! 249: (void)fclose(fp);
! 250:
! 251: /* XXX Build the Entries line. */
! 252: l = snprintf(entry, sizeof(entry), "/%s/0/Initial %s/%s/",
! 253: cf->cf_name, cf->cf_name, kbuf);
! 254: if (l == -1 || l >= (int)sizeof(entry)) {
! 255: errno = ENAMETOOLONG;
! 256: cvs_log(LP_ERRNO, "%s", entry);
! 257: (void)cvs_unlink(path);
! 258: return (CVS_EX_DATA);
! 259: }
! 260:
! 261: if ((ent = cvs_ent_parse(entry)) == NULL) {
! 262: cvs_log(LP_ERR, "failed to parse entry");
! 263: (void)cvs_unlink(path);
! 264: return (CVS_EX_DATA);
! 265: }
! 266:
! 267: if (cvs_ent_add(entf, ent) < 0) {
! 268: cvs_log(LP_ERR, "failed to add entry");
! 269: (void)cvs_unlink(path);
! 270: return (CVS_EX_DATA);
! 271: }
1.22 joris 272:
273: return (0);
1.1 jfb 274: }
1.28 ! xsa 275: #endif