Annotation of src/usr.bin/cvs/add.c, Revision 1.56
1.56 ! xsa 1: /* $OpenBSD: add.c,v 1.55 2006/06/07 07:01:12 xsa Exp $ */
1.1 jfb 2: /*
1.43 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.55 xsa 4: * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.1 jfb 5: *
1.43 joris 6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 jfb 9: *
1.43 joris 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 17: */
18:
1.37 xsa 19: #include "includes.h"
1.1 jfb 20:
21: #include "cvs.h"
1.43 joris 22: #include "diff.h"
1.1 jfb 23: #include "log.h"
1.2 jfb 24: #include "proto.h"
1.1 jfb 25:
1.48 xsa 26: extern char *__progname;
27:
1.43 joris 28: int cvs_add(int, char **);
29: void cvs_add_local(struct cvs_file *);
1.1 jfb 30:
1.45 joris 31: static void add_directory(struct cvs_file *);
32: static void add_file(struct cvs_file *);
1.55 xsa 33: static void add_entry(struct cvs_file *);
1.45 joris 34:
1.43 joris 35: char *logmsg;
1.21 jfb 36:
37: struct cvs_cmd cvs_cmd_add = {
38: CVS_OP_ADD, CVS_REQ_ADD, "add",
39: { "ad", "new" },
1.43 joris 40: "Add a new file or directory to the repository",
41: "[-m message] ...",
1.47 joris 42: "m:",
1.21 jfb 43: NULL,
1.43 joris 44: cvs_add
1.15 joris 45: };
1.3 jfb 46:
1.43 joris 47: int
48: cvs_add(int argc, char **argv)
1.1 jfb 49: {
1.15 joris 50: int ch;
1.43 joris 51: int flags;
52: struct cvs_recursion cr;
1.1 jfb 53:
1.45 joris 54: flags = CR_REPO;
1.1 jfb 55:
1.43 joris 56: while ((ch = getopt(argc, argv, cvs_cmd_add.cmd_opts)) != -1) {
1.1 jfb 57: switch (ch) {
58: case 'm':
1.43 joris 59: logmsg = xstrdup(optarg);
1.1 jfb 60: break;
61: default:
1.43 joris 62: fatal("%s", cvs_cmd_add.cmd_synopsis);
1.1 jfb 63: }
64: }
65:
1.43 joris 66: argc -= optind;
67: argv += optind;
1.28 xsa 68:
1.46 joris 69: if (argc == 0)
70: fatal("%s", cvs_cmd_add.cmd_synopsis);
71:
1.43 joris 72: cr.enterdir = NULL;
73: cr.leavedir = NULL;
74: cr.local = cvs_add_local;
75: cr.remote = NULL;
76: cr.flags = flags;
77:
1.46 joris 78: cvs_file_run(argc, argv, &cr);
1.3 jfb 79: return (0);
80: }
81:
1.43 joris 82: void
83: cvs_add_local(struct cvs_file *cf)
1.3 jfb 84: {
1.45 joris 85: cvs_log(LP_TRACE, "cvs_add_local(%s)", cf->file_path);
86:
1.51 joris 87: cvs_file_classify(cf, NULL, 0);
1.49 xsa 88:
89: /* dont use `cvs add *' */
90: if (strcmp(cf->file_name, ".") == 0 ||
91: strcmp(cf->file_name, "..") == 0 ||
92: strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
93: if (verbosity > 1)
94: cvs_log(LP_ERR,
95: "cannot add special file `%s'; skipping",
96: cf->file_name);
97: return;
98: }
1.45 joris 99:
100: if (cf->file_type == CVS_DIR)
101: add_directory(cf);
102: else
103: add_file(cf);
104: }
105:
106: static void
107: add_directory(struct cvs_file *cf)
108: {
1.56 ! xsa 109: int l, added, nb;
1.45 joris 110: struct stat st;
1.44 joris 111: CVSENTRIES *entlist;
1.56 ! xsa 112: char *date, *entry, msg[1024], *repo, *tag;
1.45 joris 113:
114: cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44 joris 115:
1.45 joris 116: entry = xmalloc(MAXPATHLEN);
117: l = snprintf(entry, MAXPATHLEN, "%s%s", cf->file_rpath, RCS_FILE_EXT);
118: if (l == -1 || l >= MAXPATHLEN)
119: fatal("cvs_add_local: overflow");
120:
1.53 joris 121: added = 1;
1.45 joris 122: if (stat(entry, &st) != -1) {
123: cvs_log(LP_NOTICE, "cannot add directory %s: "
124: "a file with that name already exists",
125: cf->file_path);
1.53 joris 126: added = 0;
1.45 joris 127: } else {
1.56 ! xsa 128: /* Let's see if we have any per-directory tags first. */
! 129: cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
! 130:
1.45 joris 131: l = snprintf(entry, MAXPATHLEN, "%s/%s", cf->file_path,
132: CVS_PATH_CVSDIR);
133: if (l == -1 || l >= MAXPATHLEN)
134: fatal("add_directory: overflow");
135:
136: if (stat(entry, &st) != -1) {
137: if (!S_ISDIR(st.st_mode)) {
138: cvs_log(LP_ERR, "%s exists but is not "
139: "directory", entry);
140: } else {
141: cvs_log(LP_NOTICE, "%s already exists",
142: entry);
143: }
1.53 joris 144: added = 0;
145: } else if (cvs_noexec != 1) {
1.45 joris 146: if (mkdir(cf->file_rpath, 0755) == -1 &&
147: errno != EEXIST)
148: fatal("add_directory: %s: %s", cf->file_path,
149: strerror(errno));
150:
151: repo = xmalloc(MAXPATHLEN);
152: cvs_get_repository_name(cf->file_wd, repo,
153: MAXPATHLEN);
154:
155: l = snprintf(entry, MAXPATHLEN, "%s/%s", repo,
156: cf->file_path);
157:
158: cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
1.56 ! xsa 159: entry, tag, date, nb);
1.45 joris 160:
161: xfree(repo);
162: xfree(entry);
163:
164: entry = xmalloc(CVS_ENT_MAXLINELEN);
165: l = snprintf(entry, CVS_ENT_MAXLINELEN,
166: "D/%s/////", cf->file_name);
167: entlist = cvs_ent_open(cf->file_wd);
168: cvs_ent_add(entlist, entry);
169: cvs_ent_close(entlist, ENT_SYNC);
1.53 joris 170: }
171: }
1.45 joris 172:
1.53 joris 173: if (added == 1) {
1.56 ! xsa 174: snprintf(msg, sizeof(msg),
! 175: "Directory %s added to the repository", cf->file_rpath);
! 176:
! 177: if (tag != NULL) {
! 178: (void)strlcat(msg,
! 179: "\n--> Using per-directory sticky tag ",
! 180: sizeof(msg));
! 181: (void)strlcat(msg, tag, sizeof(msg));
! 182: }
! 183: if (date != NULL) {
! 184: (void)strlcat(msg,
! 185: "\n--> Using per-directory sticky date ",
! 186: sizeof(msg));
! 187: (void)strlcat(msg, date, sizeof(msg));
! 188: }
! 189: cvs_printf("%s\n", msg);
! 190:
! 191: if (tag != NULL)
! 192: xfree(tag);
! 193: if (date != NULL)
! 194: xfree(date);
1.45 joris 195: }
196:
197: cf->file_status = FILE_SKIP;
198: xfree(entry);
199: }
1.3 jfb 200:
1.45 joris 201: static void
202: add_file(struct cvs_file *cf)
203: {
1.48 xsa 204: BUF *b;
1.55 xsa 205: int added, stop;
206: char revbuf[16];
1.52 joris 207: RCSNUM *head;
1.10 xsa 208:
1.44 joris 209: if (cf->file_rcs != NULL)
1.52 joris 210: rcsnum_tostr(rcs_head_get(cf->file_rcs),
211: revbuf, sizeof(revbuf));
1.44 joris 212:
1.48 xsa 213: added = stop = 0;
1.43 joris 214: switch (cf->file_status) {
215: case FILE_ADDED:
1.48 xsa 216: if (verbosity > 1)
217: cvs_log(LP_NOTICE, "%s has already been entered",
218: cf->file_path);
219: stop = 1;
220: break;
221: case FILE_REMOVED:
222: if (cf->file_rcs == NULL) {
223: cvs_log(LP_NOTICE, "cannot resurrect %s; "
224: "RCS file removed by second party", cf->file_name);
1.55 xsa 225: } else {
226: add_entry(cf);
1.48 xsa 227:
1.55 xsa 228: /* Restore the file. */
1.52 joris 229: head = rcs_head_get(cf->file_rcs);
230: b = rcs_getrev(cf->file_rcs, head);
1.48 xsa 231: if (b == NULL)
232: fatal("cvs_add_local: failed to get HEAD");
233:
1.53 joris 234: cvs_checkout_file(cf, head, b, 0);
1.48 xsa 235: cvs_printf("U %s\n", cf->file_path);
236:
237: cvs_log(LP_NOTICE, "%s, version %s, resurrected",
238: cf->file_name, revbuf);
239:
240: cf->file_status = FILE_UPTODATE;
241: }
1.44 joris 242: stop = 1;
243: break;
1.50 xsa 244: case FILE_CONFLICT:
245: case FILE_LOST:
246: case FILE_MODIFIED:
1.44 joris 247: case FILE_UPTODATE:
248: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
249: cvs_log(LP_NOTICE, "%s already exists, with version "
250: "number %s", cf->file_path, revbuf);
251: stop = 1;
252: }
1.43 joris 253: break;
254: case FILE_UNKNOWN:
1.44 joris 255: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
256: cvs_log(LP_NOTICE, "re-adding file %s "
257: "(instead of dead revision %s)",
258: cf->file_path, revbuf);
259: } else {
260: cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
261: cf->file_path);
262: }
1.54 joris 263: added++;
1.43 joris 264: break;
265: default:
266: break;
1.1 jfb 267: }
1.44 joris 268:
269: if (stop == 1)
270: return;
271:
1.55 xsa 272: add_entry(cf);
1.44 joris 273:
1.48 xsa 274: if (added != 0) {
275: if (verbosity > 0)
276: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
277: "permanently", __progname,
278: (added == 1) ? "this file" : "these files");
279: }
1.55 xsa 280: }
281:
282: static void
283: add_entry(struct cvs_file *cf)
284: {
285: FILE *fp;
286: int l;
287: char *entry, *path, revbuf[16], tbuf[32];
288: CVSENTRIES *entlist;
289:
290: if (cvs_noexec == 1)
291: return;
292:
293: entry = xmalloc(CVS_ENT_MAXLINELEN);
294:
295: if (cf->file_status == FILE_REMOVED) {
296: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
297:
298: ctime_r(&cf->file_ent->ce_mtime, tbuf);
299: if (tbuf[strlen(tbuf) - 1] == '\n')
300: tbuf[strlen(tbuf) - 1] = '\0';
301:
302: /* Remove the '-' prefixing the version number. */
303: l = snprintf(entry, CVS_ENT_MAXLINELEN,
304: "/%s/%s/%s//", cf->file_name, revbuf, tbuf);
305: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
306: fatal("add_entry: truncation");
307: } else {
308: if (logmsg != NULL) {
309: path = xmalloc(MAXPATHLEN);
310:
311: l = snprintf(path, MAXPATHLEN, "%s/%s%s",
312: CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
313: if (l == -1 || l >= MAXPATHLEN)
314: fatal("add_entry: truncation");
315:
316: if ((fp = fopen(path, "w+")) == NULL)
317: fatal("add_entry: fopen `%s': %s",
318: path, strerror(errno));
319:
320: if (fputs(logmsg, fp) == EOF) {
321: (void)unlink(path);
322: fatal("add_entry: fputs `%s': %s",
323: path, strerror(errno));
324: }
325: (void)fclose(fp);
326: xfree(path);
327: }
328:
329: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/0/Initial %s//",
330: cf->file_name, cf->file_name);
331: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
332: fatal("add_entry: truncation");
333: }
334:
335: entlist = cvs_ent_open(cf->file_wd);
336: cvs_ent_add(entlist, entry);
337: cvs_ent_close(entlist, ENT_SYNC);
338:
339: xfree(entry);
1.1 jfb 340: }