Annotation of src/usr.bin/cvs/add.c, Revision 1.64
1.64 ! joris 1: /* $OpenBSD: add.c,v 1.63 2006/12/19 15:12:59 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"
1.61 xsa 24: #include "remote.h"
1.1 jfb 25:
1.48 xsa 26: extern char *__progname;
27:
1.43 joris 28: void cvs_add_local(struct cvs_file *);
1.63 joris 29: void cvs_add_entry(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 = {
1.57 joris 38: CVS_OP_ADD, 0, "add",
1.21 jfb 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;
1.61 xsa 74:
75: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
76: cr.fileproc = cvs_client_sendfile;
77:
78: if (logmsg != NULL)
79: cvs_client_send_request("Argument -m%s", logmsg);
80: } else {
81: cr.fileproc = cvs_add_local;
82: }
83:
1.43 joris 84: cr.flags = flags;
85:
1.46 joris 86: cvs_file_run(argc, argv, &cr);
1.61 xsa 87:
88: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
89: cvs_client_send_files(argv, argc);
90: cvs_client_senddir(".");
91: cvs_client_send_request("add");
92: cvs_client_get_responses();
1.63 joris 93:
94: if (server_response == SERVER_OK) {
95: cr.fileproc = cvs_add_entry;
96: cvs_file_run(argc, argv, &cr);
97: }
1.61 xsa 98: }
99:
1.3 jfb 100: return (0);
1.63 joris 101: }
102:
103: void
104: cvs_add_entry(struct cvs_file *cf)
105: {
106: int l;
107: char *entry;
108: CVSENTRIES *entlist;
109:
110: if (cf->file_type == CVS_DIR) {
111: entry = xmalloc(CVS_ENT_MAXLINELEN);
112: l = snprintf(entry, CVS_ENT_MAXLINELEN,
113: "D/%s/////", cf->file_name);
1.64 ! joris 114: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
! 115: fatal("cvs_add_entry: overflow");
! 116:
1.63 joris 117: entlist = cvs_ent_open(cf->file_wd);
118: cvs_ent_add(entlist, entry);
119: cvs_ent_close(entlist, ENT_SYNC);
120: } else {
121: add_entry(cf);
122: }
1.3 jfb 123: }
124:
1.43 joris 125: void
126: cvs_add_local(struct cvs_file *cf)
1.3 jfb 127: {
1.45 joris 128: cvs_log(LP_TRACE, "cvs_add_local(%s)", cf->file_path);
129:
1.51 joris 130: cvs_file_classify(cf, NULL, 0);
1.49 xsa 131:
132: /* dont use `cvs add *' */
133: if (strcmp(cf->file_name, ".") == 0 ||
134: strcmp(cf->file_name, "..") == 0 ||
135: strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
136: if (verbosity > 1)
137: cvs_log(LP_ERR,
138: "cannot add special file `%s'; skipping",
139: cf->file_name);
140: return;
141: }
1.45 joris 142:
143: if (cf->file_type == CVS_DIR)
144: add_directory(cf);
145: else
146: add_file(cf);
147: }
148:
149: static void
150: add_directory(struct cvs_file *cf)
151: {
1.56 xsa 152: int l, added, nb;
1.45 joris 153: struct stat st;
1.44 joris 154: CVSENTRIES *entlist;
1.56 xsa 155: char *date, *entry, msg[1024], *repo, *tag;
1.45 joris 156:
157: cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44 joris 158:
1.45 joris 159: entry = xmalloc(MAXPATHLEN);
160: l = snprintf(entry, MAXPATHLEN, "%s%s", cf->file_rpath, RCS_FILE_EXT);
161: if (l == -1 || l >= MAXPATHLEN)
162: fatal("cvs_add_local: overflow");
163:
1.53 joris 164: added = 1;
1.45 joris 165: if (stat(entry, &st) != -1) {
166: cvs_log(LP_NOTICE, "cannot add directory %s: "
167: "a file with that name already exists",
168: cf->file_path);
1.53 joris 169: added = 0;
1.45 joris 170: } else {
1.56 xsa 171: /* Let's see if we have any per-directory tags first. */
172: cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
173:
1.62 xsa 174: if (cvs_path_cat(cf->file_path, CVS_PATH_CVSDIR,
175: entry, MAXPATHLEN) >= MAXPATHLEN)
176: fatal("add_directory: truncation");
1.45 joris 177:
178: if (stat(entry, &st) != -1) {
179: if (!S_ISDIR(st.st_mode)) {
180: cvs_log(LP_ERR, "%s exists but is not "
181: "directory", entry);
182: } else {
183: cvs_log(LP_NOTICE, "%s already exists",
184: entry);
185: }
1.53 joris 186: added = 0;
187: } else if (cvs_noexec != 1) {
1.45 joris 188: if (mkdir(cf->file_rpath, 0755) == -1 &&
189: errno != EEXIST)
190: fatal("add_directory: %s: %s", cf->file_path,
191: strerror(errno));
192:
193: repo = xmalloc(MAXPATHLEN);
194: cvs_get_repository_name(cf->file_wd, repo,
195: MAXPATHLEN);
196:
1.62 xsa 197: if (cvs_path_cat(repo, cf->file_name, entry,
198: MAXPATHLEN) >= MAXPATHLEN)
199: fatal("add_directory: truncation");
1.45 joris 200:
201: cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
1.56 xsa 202: entry, tag, date, nb);
1.45 joris 203:
204: xfree(repo);
205: xfree(entry);
206:
207: entry = xmalloc(CVS_ENT_MAXLINELEN);
208: l = snprintf(entry, CVS_ENT_MAXLINELEN,
209: "D/%s/////", cf->file_name);
210: entlist = cvs_ent_open(cf->file_wd);
211: cvs_ent_add(entlist, entry);
212: cvs_ent_close(entlist, ENT_SYNC);
1.53 joris 213: }
214: }
1.45 joris 215:
1.53 joris 216: if (added == 1) {
1.56 xsa 217: snprintf(msg, sizeof(msg),
218: "Directory %s added to the repository", cf->file_rpath);
219:
220: if (tag != NULL) {
221: (void)strlcat(msg,
222: "\n--> Using per-directory sticky tag ",
223: sizeof(msg));
224: (void)strlcat(msg, tag, sizeof(msg));
225: }
226: if (date != NULL) {
227: (void)strlcat(msg,
228: "\n--> Using per-directory sticky date ",
229: sizeof(msg));
230: (void)strlcat(msg, date, sizeof(msg));
231: }
232: cvs_printf("%s\n", msg);
233:
234: if (tag != NULL)
235: xfree(tag);
236: if (date != NULL)
237: xfree(date);
1.45 joris 238: }
239:
240: cf->file_status = FILE_SKIP;
241: xfree(entry);
242: }
1.3 jfb 243:
1.45 joris 244: static void
245: add_file(struct cvs_file *cf)
246: {
1.48 xsa 247: BUF *b;
1.55 xsa 248: int added, stop;
249: char revbuf[16];
1.52 joris 250: RCSNUM *head;
1.10 xsa 251:
1.44 joris 252: if (cf->file_rcs != NULL)
1.52 joris 253: rcsnum_tostr(rcs_head_get(cf->file_rcs),
254: revbuf, sizeof(revbuf));
1.44 joris 255:
1.48 xsa 256: added = stop = 0;
1.43 joris 257: switch (cf->file_status) {
258: case FILE_ADDED:
1.48 xsa 259: if (verbosity > 1)
260: cvs_log(LP_NOTICE, "%s has already been entered",
261: cf->file_path);
262: stop = 1;
263: break;
264: case FILE_REMOVED:
265: if (cf->file_rcs == NULL) {
266: cvs_log(LP_NOTICE, "cannot resurrect %s; "
267: "RCS file removed by second party", cf->file_name);
1.55 xsa 268: } else {
269: add_entry(cf);
1.48 xsa 270:
1.55 xsa 271: /* Restore the file. */
1.52 joris 272: head = rcs_head_get(cf->file_rcs);
273: b = rcs_getrev(cf->file_rcs, head);
1.48 xsa 274: if (b == NULL)
275: fatal("cvs_add_local: failed to get HEAD");
276:
1.53 joris 277: cvs_checkout_file(cf, head, b, 0);
1.48 xsa 278: cvs_printf("U %s\n", cf->file_path);
279:
280: cvs_log(LP_NOTICE, "%s, version %s, resurrected",
281: cf->file_name, revbuf);
282:
283: cf->file_status = FILE_UPTODATE;
284: }
1.44 joris 285: stop = 1;
286: break;
1.50 xsa 287: case FILE_CONFLICT:
288: case FILE_LOST:
289: case FILE_MODIFIED:
1.44 joris 290: case FILE_UPTODATE:
291: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
292: cvs_log(LP_NOTICE, "%s already exists, with version "
293: "number %s", cf->file_path, revbuf);
294: stop = 1;
295: }
1.43 joris 296: break;
297: case FILE_UNKNOWN:
1.44 joris 298: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
299: cvs_log(LP_NOTICE, "re-adding file %s "
300: "(instead of dead revision %s)",
301: cf->file_path, revbuf);
302: } else {
303: cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
304: cf->file_path);
305: }
1.54 joris 306: added++;
1.43 joris 307: break;
308: default:
309: break;
1.1 jfb 310: }
1.44 joris 311:
312: if (stop == 1)
313: return;
314:
1.55 xsa 315: add_entry(cf);
1.44 joris 316:
1.48 xsa 317: if (added != 0) {
1.59 reyk 318: if (verbosity > 1)
1.48 xsa 319: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
320: "permanently", __progname,
321: (added == 1) ? "this file" : "these files");
322: }
1.55 xsa 323: }
324:
325: static void
326: add_entry(struct cvs_file *cf)
327: {
328: FILE *fp;
329: int l;
330: char *entry, *path, revbuf[16], tbuf[32];
331: CVSENTRIES *entlist;
332:
333: if (cvs_noexec == 1)
334: return;
335:
336: entry = xmalloc(CVS_ENT_MAXLINELEN);
337:
338: if (cf->file_status == FILE_REMOVED) {
339: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
340:
341: ctime_r(&cf->file_ent->ce_mtime, tbuf);
342: if (tbuf[strlen(tbuf) - 1] == '\n')
343: tbuf[strlen(tbuf) - 1] = '\0';
344:
345: /* Remove the '-' prefixing the version number. */
346: l = snprintf(entry, CVS_ENT_MAXLINELEN,
347: "/%s/%s/%s//", cf->file_name, revbuf, tbuf);
348: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
349: fatal("add_entry: truncation");
350: } else {
351: if (logmsg != NULL) {
352: path = xmalloc(MAXPATHLEN);
353:
354: l = snprintf(path, MAXPATHLEN, "%s/%s%s",
355: CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
356: if (l == -1 || l >= MAXPATHLEN)
357: fatal("add_entry: truncation");
358:
359: if ((fp = fopen(path, "w+")) == NULL)
360: fatal("add_entry: fopen `%s': %s",
361: path, strerror(errno));
362:
363: if (fputs(logmsg, fp) == EOF) {
364: (void)unlink(path);
365: fatal("add_entry: fputs `%s': %s",
366: path, strerror(errno));
367: }
368: (void)fclose(fp);
369: xfree(path);
370: }
371:
372: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/0/Initial %s//",
373: cf->file_name, cf->file_name);
374: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
375: fatal("add_entry: truncation");
376: }
377:
378: entlist = cvs_ent_open(cf->file_wd);
379: cvs_ent_add(entlist, entry);
380: cvs_ent_close(entlist, ENT_SYNC);
381:
382: xfree(entry);
1.1 jfb 383: }