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