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