Annotation of src/usr.bin/cvs/add.c, Revision 1.93
1.93 ! tobias 1: /* $OpenBSD: add.c,v 1.92 2008/02/04 19:54:21 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.77 otto 19: #include <sys/stat.h>
20:
21: #include <errno.h>
22: #include <string.h>
23: #include <unistd.h>
1.1 jfb 24:
25: #include "cvs.h"
1.61 xsa 26: #include "remote.h"
1.1 jfb 27:
1.48 xsa 28: extern char *__progname;
29:
1.43 joris 30: void cvs_add_local(struct cvs_file *);
1.63 joris 31: void cvs_add_entry(struct cvs_file *);
1.85 tobias 32: void cvs_add_remote(struct cvs_file *);
1.1 jfb 33:
1.45 joris 34: static void add_directory(struct cvs_file *);
35: static void add_file(struct cvs_file *);
1.55 xsa 36: static void add_entry(struct cvs_file *);
1.45 joris 37:
1.90 tobias 38: int kflag = 0;
39: static char kbuf[8];
1.72 xsa 40:
1.43 joris 41: char *logmsg;
1.21 jfb 42:
43: struct cvs_cmd cvs_cmd_add = {
1.88 tobias 44: CVS_OP_ADD, CVS_USE_WDIR, "add",
1.21 jfb 45: { "ad", "new" },
1.43 joris 46: "Add a new file or directory to the repository",
1.72 xsa 47: "[-k mode] [-m message] ...",
48: "k:m:",
1.21 jfb 49: NULL,
1.43 joris 50: cvs_add
1.15 joris 51: };
1.3 jfb 52:
1.43 joris 53: int
54: cvs_add(int argc, char **argv)
1.1 jfb 55: {
1.15 joris 56: int ch;
1.43 joris 57: int flags;
58: struct cvs_recursion cr;
1.1 jfb 59:
1.45 joris 60: flags = CR_REPO;
1.1 jfb 61:
1.43 joris 62: while ((ch = getopt(argc, argv, cvs_cmd_add.cmd_opts)) != -1) {
1.1 jfb 63: switch (ch) {
1.72 xsa 64: case 'k':
1.90 tobias 65: kflag = rcs_kflag_get(optarg);
1.72 xsa 66: if (RCS_KWEXP_INVAL(kflag)) {
67: cvs_log(LP_ERR,
68: "invalid RCS keyword expension mode");
69: fatal("%s", cvs_cmd_add.cmd_synopsis);
70: }
1.90 tobias 71: (void)xsnprintf(kbuf, sizeof(kbuf), "-k%s", optarg);
1.72 xsa 72: break;
1.1 jfb 73: case 'm':
1.87 tobias 74: logmsg = optarg;
1.1 jfb 75: break;
76: default:
1.43 joris 77: fatal("%s", cvs_cmd_add.cmd_synopsis);
1.1 jfb 78: }
79: }
80:
1.43 joris 81: argc -= optind;
82: argv += optind;
1.28 xsa 83:
1.46 joris 84: if (argc == 0)
85: fatal("%s", cvs_cmd_add.cmd_synopsis);
86:
1.43 joris 87: cr.enterdir = NULL;
88: cr.leavedir = NULL;
1.61 xsa 89:
90: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.66 joris 91: cvs_client_connect_to_server();
1.85 tobias 92: cr.fileproc = cvs_add_remote;
1.61 xsa 93:
1.92 joris 94: if (kflag)
1.72 xsa 95: cvs_client_send_request("Argument %s", kbuf);
96:
1.61 xsa 97: if (logmsg != NULL)
1.80 joris 98: cvs_client_send_logmsg(logmsg);
1.61 xsa 99: } else {
100: cr.fileproc = cvs_add_local;
101: }
102:
1.43 joris 103: cr.flags = flags;
104:
1.46 joris 105: cvs_file_run(argc, argv, &cr);
1.61 xsa 106:
107: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.85 tobias 108: cvs_client_senddir(".");
1.61 xsa 109: cvs_client_send_files(argv, argc);
110: cvs_client_send_request("add");
111: cvs_client_get_responses();
1.63 joris 112:
113: if (server_response == SERVER_OK) {
114: cr.fileproc = cvs_add_entry;
115: cvs_file_run(argc, argv, &cr);
116: }
1.61 xsa 117: }
118:
1.3 jfb 119: return (0);
1.63 joris 120: }
121:
122: void
123: cvs_add_entry(struct cvs_file *cf)
124: {
1.70 otto 125: char entry[CVS_ENT_MAXLINELEN];
1.63 joris 126: CVSENTRIES *entlist;
127:
128: if (cf->file_type == CVS_DIR) {
1.74 xsa 129: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.89 tobias 130: "D/%s////", cf->file_name);
1.64 joris 131:
1.63 joris 132: entlist = cvs_ent_open(cf->file_wd);
133: cvs_ent_add(entlist, entry);
134: cvs_ent_close(entlist, ENT_SYNC);
135: } else {
136: add_entry(cf);
137: }
1.3 jfb 138: }
139:
1.43 joris 140: void
141: cvs_add_local(struct cvs_file *cf)
1.3 jfb 142: {
1.45 joris 143: cvs_log(LP_TRACE, "cvs_add_local(%s)", cf->file_path);
144:
1.81 joris 145: cvs_file_classify(cf, cvs_directory_tag);
1.49 xsa 146:
147: /* dont use `cvs add *' */
148: if (strcmp(cf->file_name, ".") == 0 ||
149: strcmp(cf->file_name, "..") == 0 ||
150: strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
151: if (verbosity > 1)
152: cvs_log(LP_ERR,
153: "cannot add special file `%s'; skipping",
154: cf->file_name);
155: return;
156: }
1.45 joris 157:
158: if (cf->file_type == CVS_DIR)
159: add_directory(cf);
160: else
161: add_file(cf);
162: }
163:
1.85 tobias 164: void
165: cvs_add_remote(struct cvs_file *cf)
166: {
167: char path[MAXPATHLEN];
168:
169: cvs_log(LP_TRACE, "cvs_add_remote(%s)", cf->file_path);
170:
171: cvs_file_classify(cf, cvs_directory_tag);
172:
173: if (cf->file_type == CVS_DIR) {
174: cvs_get_repository_path(cf->file_wd, path, MAXPATHLEN);
175: if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
176: fatal("cvs_add_remote: truncation");
177: if (strlcat(path, cf->file_path, sizeof(path)) >= sizeof(path))
178: fatal("cvs_add_remote: truncation");
179: cvs_client_send_request("Directory %s\n%s", cf->file_path,
180: path);
181:
182: add_directory(cf);
183: } else {
184: cvs_client_sendfile(cf);
185: }
186: }
187:
1.45 joris 188: static void
189: add_directory(struct cvs_file *cf)
190: {
1.74 xsa 191: int added, nb;
1.45 joris 192: struct stat st;
1.44 joris 193: CVSENTRIES *entlist;
1.70 otto 194: char *date, entry[MAXPATHLEN], msg[1024], repo[MAXPATHLEN], *tag, *p;
1.45 joris 195:
196: cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44 joris 197:
1.74 xsa 198: (void)xsnprintf(entry, MAXPATHLEN, "%s%s",
199: cf->file_rpath, RCS_FILE_EXT);
1.45 joris 200:
1.53 joris 201: added = 1;
1.45 joris 202: if (stat(entry, &st) != -1) {
203: cvs_log(LP_NOTICE, "cannot add directory %s: "
204: "a file with that name already exists",
205: cf->file_path);
1.53 joris 206: added = 0;
1.45 joris 207: } else {
1.56 xsa 208: /* Let's see if we have any per-directory tags first. */
209: cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
210:
1.76 xsa 211: (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
212: cf->file_path, CVS_PATH_CVSDIR);
1.45 joris 213:
1.85 tobias 214: if (cvs_server_active) {
215: if (mkdir(cf->file_rpath, 0755) == -1 &&
216: errno != EEXIST)
217: fatal("add_directory: %s: %s", cf->file_rpath,
218: strerror(errno));
219: } else if (stat(entry, &st) != -1) {
1.45 joris 220: if (!S_ISDIR(st.st_mode)) {
221: cvs_log(LP_ERR, "%s exists but is not "
222: "directory", entry);
223: } else {
224: cvs_log(LP_NOTICE, "%s already exists",
225: entry);
226: }
1.53 joris 227: added = 0;
228: } else if (cvs_noexec != 1) {
1.45 joris 229: if (mkdir(cf->file_rpath, 0755) == -1 &&
230: errno != EEXIST)
1.85 tobias 231: fatal("add_directory: %s: %s", cf->file_rpath,
1.45 joris 232: strerror(errno));
233:
234: cvs_get_repository_name(cf->file_wd, repo,
235: MAXPATHLEN);
236:
1.76 xsa 237: (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
238: repo, cf->file_name);
1.45 joris 239:
240: cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
1.91 tobias 241: entry, tag, date);
1.45 joris 242:
1.70 otto 243: p = xmalloc(CVS_ENT_MAXLINELEN);
1.74 xsa 244: (void)xsnprintf(p, CVS_ENT_MAXLINELEN,
1.89 tobias 245: "D/%s////", cf->file_name);
1.45 joris 246: entlist = cvs_ent_open(cf->file_wd);
1.70 otto 247: cvs_ent_add(entlist, p);
1.45 joris 248: cvs_ent_close(entlist, ENT_SYNC);
1.79 joris 249: xfree(p);
1.53 joris 250: }
251: }
1.45 joris 252:
1.85 tobias 253: if (added == 1 && current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.74 xsa 254: (void)xsnprintf(msg, sizeof(msg),
1.56 xsa 255: "Directory %s added to the repository", cf->file_rpath);
256:
257: if (tag != NULL) {
258: (void)strlcat(msg,
259: "\n--> Using per-directory sticky tag ",
260: sizeof(msg));
261: (void)strlcat(msg, tag, sizeof(msg));
262: }
263: if (date != NULL) {
264: (void)strlcat(msg,
265: "\n--> Using per-directory sticky date ",
266: sizeof(msg));
267: (void)strlcat(msg, date, sizeof(msg));
268: }
269: cvs_printf("%s\n", msg);
270:
271: if (tag != NULL)
272: xfree(tag);
273: if (date != NULL)
274: xfree(date);
1.45 joris 275: }
276:
277: cf->file_status = FILE_SKIP;
278: }
1.3 jfb 279:
1.45 joris 280: static void
281: add_file(struct cvs_file *cf)
282: {
1.93 ! tobias 283: int added, nb, stop;
1.78 xsa 284: char revbuf[CVS_REV_BUFSZ];
1.52 joris 285: RCSNUM *head;
1.93 ! tobias 286: char *tag;
! 287:
! 288: cvs_parse_tagfile(cf->file_wd, &tag, NULL, &nb);
! 289: if (nb) {
! 290: cvs_log(LP_ERR, "cannot add file on non-branch tag %s", tag);
! 291: return;
! 292: }
1.10 xsa 293:
1.68 joris 294: if (cf->file_rcs != NULL) {
295: head = rcs_head_get(cf->file_rcs);
1.83 tobias 296: if (head == NULL)
297: fatal("RCS head empty or missing in %s\n",
298: cf->file_rcs->rf_path);
1.68 joris 299: rcsnum_tostr(head, revbuf, sizeof(revbuf));
300: rcsnum_free(head);
301: }
1.44 joris 302:
1.48 xsa 303: added = stop = 0;
1.43 joris 304: switch (cf->file_status) {
305: case FILE_ADDED:
1.48 xsa 306: if (verbosity > 1)
307: cvs_log(LP_NOTICE, "%s has already been entered",
308: cf->file_path);
309: stop = 1;
310: break;
311: case FILE_REMOVED:
312: if (cf->file_rcs == NULL) {
313: cvs_log(LP_NOTICE, "cannot resurrect %s; "
314: "RCS file removed by second party", cf->file_name);
1.73 joris 315: } else if (cf->fd == -1) {
1.55 xsa 316: add_entry(cf);
1.48 xsa 317:
1.55 xsa 318: /* Restore the file. */
1.52 joris 319: head = rcs_head_get(cf->file_rcs);
1.83 tobias 320: if (head == NULL)
321: fatal("RCS head empty or missing in %s\n",
322: cf->file_rcs->rf_path);
1.82 joris 323: cvs_checkout_file(cf, head, NULL, 0);
1.68 joris 324: rcsnum_free(head);
325:
1.48 xsa 326: cvs_printf("U %s\n", cf->file_path);
327:
328: cvs_log(LP_NOTICE, "%s, version %s, resurrected",
329: cf->file_name, revbuf);
330:
331: cf->file_status = FILE_UPTODATE;
332: }
1.44 joris 333: stop = 1;
334: break;
1.50 xsa 335: case FILE_CONFLICT:
336: case FILE_LOST:
337: case FILE_MODIFIED:
1.44 joris 338: case FILE_UPTODATE:
339: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
340: cvs_log(LP_NOTICE, "%s already exists, with version "
341: "number %s", cf->file_path, revbuf);
342: stop = 1;
343: }
1.43 joris 344: break;
345: case FILE_UNKNOWN:
1.44 joris 346: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
347: cvs_log(LP_NOTICE, "re-adding file %s "
348: "(instead of dead revision %s)",
349: cf->file_path, revbuf);
1.73 joris 350: added++;
351: } else if (cf->fd != -1) {
1.44 joris 352: cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
353: cf->file_path);
1.73 joris 354: added++;
355: } else {
356: stop = 1;
1.44 joris 357: }
1.43 joris 358: break;
359: default:
360: break;
1.1 jfb 361: }
1.44 joris 362:
363: if (stop == 1)
364: return;
365:
1.55 xsa 366: add_entry(cf);
1.44 joris 367:
1.48 xsa 368: if (added != 0) {
1.67 joris 369: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
370: "permanently", __progname,
371: (added == 1) ? "this file" : "these files");
1.48 xsa 372: }
1.55 xsa 373: }
374:
375: static void
376: add_entry(struct cvs_file *cf)
377: {
378: FILE *fp;
1.78 xsa 379: char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
380: char revbuf[CVS_REV_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.93 ! tobias 381: char sticky[CVS_ENT_MAXLINELEN];
1.55 xsa 382: CVSENTRIES *entlist;
383:
384: if (cvs_noexec == 1)
385: return;
386:
1.93 ! tobias 387: sticky[0] = '\0';
! 388:
1.55 xsa 389: if (cf->file_status == FILE_REMOVED) {
390: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
391:
392: ctime_r(&cf->file_ent->ce_mtime, tbuf);
1.86 tobias 393: tbuf[strcspn(tbuf, "\n")] = '\0';
1.55 xsa 394:
1.93 ! tobias 395: if (cf->file_ent->ce_tag != NULL)
! 396: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
! 397: cf->file_ent->ce_tag);
! 398:
1.55 xsa 399: /* Remove the '-' prefixing the version number. */
1.74 xsa 400: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.93 ! tobias 401: "/%s/%s/%s/%s/%s", cf->file_name, revbuf, tbuf,
! 402: cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "", sticky);
1.55 xsa 403: } else {
404: if (logmsg != NULL) {
1.74 xsa 405: (void)xsnprintf(path, MAXPATHLEN, "%s/%s%s",
1.55 xsa 406: CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
407:
408: if ((fp = fopen(path, "w+")) == NULL)
409: fatal("add_entry: fopen `%s': %s",
410: path, strerror(errno));
411:
412: if (fputs(logmsg, fp) == EOF) {
413: (void)unlink(path);
414: fatal("add_entry: fputs `%s': %s",
415: path, strerror(errno));
416: }
417: (void)fclose(fp);
418: }
419:
1.93 ! tobias 420: if (cvs_directory_tag != NULL)
! 421: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
! 422: cvs_directory_tag);
! 423:
! 424: tbuf[0] = '\0';
! 425: if (!cvs_server_active)
! 426: (void)xsnprintf(tbuf, sizeof(tbuf), "Initial %s",
! 427: cf->file_name);
! 428:
1.74 xsa 429: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.93 ! tobias 430: "/%s/0/%s/%s/%s", cf->file_name, tbuf, kflag ? kbuf : "",
! 431: sticky);
1.55 xsa 432: }
433:
1.84 tobias 434: if (cvs_server_active) {
435: cvs_server_send_response("Checked-in %s/", cf->file_wd);
436: cvs_server_send_response(cf->file_path);
437: cvs_server_send_response(entry);
438: } else {
439: entlist = cvs_ent_open(cf->file_wd);
440: cvs_ent_add(entlist, entry);
441: cvs_ent_close(entlist, ENT_SYNC);
442: }
1.1 jfb 443: }