Annotation of src/usr.bin/cvs/add.c, Revision 1.78
1.78 ! xsa 1: /* $OpenBSD: add.c,v 1.77 2007/02/22 06:42:09 otto 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.1 jfb 32:
1.45 joris 33: static void add_directory(struct cvs_file *);
34: static void add_file(struct cvs_file *);
1.55 xsa 35: static void add_entry(struct cvs_file *);
1.45 joris 36:
1.72 xsa 37: static int kflag = RCS_KWEXP_DEFAULT;
38: static char kbuf[8], *koptstr;
39:
1.43 joris 40: char *logmsg;
1.21 jfb 41:
42: struct cvs_cmd cvs_cmd_add = {
1.57 joris 43: CVS_OP_ADD, 0, "add",
1.21 jfb 44: { "ad", "new" },
1.43 joris 45: "Add a new file or directory to the repository",
1.72 xsa 46: "[-k mode] [-m message] ...",
47: "k:m:",
1.21 jfb 48: NULL,
1.43 joris 49: cvs_add
1.15 joris 50: };
1.3 jfb 51:
1.43 joris 52: int
53: cvs_add(int argc, char **argv)
1.1 jfb 54: {
1.15 joris 55: int ch;
1.43 joris 56: int flags;
57: struct cvs_recursion cr;
1.1 jfb 58:
1.45 joris 59: flags = CR_REPO;
1.1 jfb 60:
1.43 joris 61: while ((ch = getopt(argc, argv, cvs_cmd_add.cmd_opts)) != -1) {
1.1 jfb 62: switch (ch) {
1.72 xsa 63: case 'k':
64: koptstr = optarg;
65: kflag = rcs_kflag_get(koptstr);
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.74 xsa 71: (void)xsnprintf(kbuf, sizeof(kbuf), "-k%s", koptstr);
1.72 xsa 72: break;
1.1 jfb 73: case 'm':
1.43 joris 74: logmsg = xstrdup(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.61 xsa 92: cr.fileproc = cvs_client_sendfile;
93:
1.72 xsa 94: if (kflag != RCS_KWEXP_DEFAULT)
95: cvs_client_send_request("Argument %s", kbuf);
96:
1.61 xsa 97: if (logmsg != NULL)
98: cvs_client_send_request("Argument -m%s", logmsg);
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) {
108: cvs_client_send_files(argv, argc);
109: cvs_client_senddir(".");
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,
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.75 joris 145: cvs_file_classify(cf, NULL);
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:
164: static void
165: add_directory(struct cvs_file *cf)
166: {
1.74 xsa 167: int added, nb;
1.45 joris 168: struct stat st;
1.44 joris 169: CVSENTRIES *entlist;
1.70 otto 170: char *date, entry[MAXPATHLEN], msg[1024], repo[MAXPATHLEN], *tag, *p;
1.45 joris 171:
172: cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44 joris 173:
1.74 xsa 174: (void)xsnprintf(entry, MAXPATHLEN, "%s%s",
175: cf->file_rpath, RCS_FILE_EXT);
1.45 joris 176:
1.53 joris 177: added = 1;
1.45 joris 178: if (stat(entry, &st) != -1) {
179: cvs_log(LP_NOTICE, "cannot add directory %s: "
180: "a file with that name already exists",
181: cf->file_path);
1.53 joris 182: added = 0;
1.45 joris 183: } else {
1.56 xsa 184: /* Let's see if we have any per-directory tags first. */
185: cvs_parse_tagfile(cf->file_wd, &tag, &date, &nb);
186:
1.76 xsa 187: (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
188: cf->file_path, CVS_PATH_CVSDIR);
1.45 joris 189:
190: if (stat(entry, &st) != -1) {
191: if (!S_ISDIR(st.st_mode)) {
192: cvs_log(LP_ERR, "%s exists but is not "
193: "directory", entry);
194: } else {
195: cvs_log(LP_NOTICE, "%s already exists",
196: entry);
197: }
1.53 joris 198: added = 0;
199: } else if (cvs_noexec != 1) {
1.45 joris 200: if (mkdir(cf->file_rpath, 0755) == -1 &&
201: errno != EEXIST)
202: fatal("add_directory: %s: %s", cf->file_path,
203: strerror(errno));
204:
205: cvs_get_repository_name(cf->file_wd, repo,
206: MAXPATHLEN);
207:
1.76 xsa 208: (void)xsnprintf(entry, MAXPATHLEN, "%s/%s",
209: repo, cf->file_name);
1.45 joris 210:
211: cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
1.56 xsa 212: entry, tag, date, nb);
1.45 joris 213:
1.70 otto 214: p = xmalloc(CVS_ENT_MAXLINELEN);
1.74 xsa 215: (void)xsnprintf(p, CVS_ENT_MAXLINELEN,
1.45 joris 216: "D/%s/////", cf->file_name);
217: entlist = cvs_ent_open(cf->file_wd);
1.70 otto 218: cvs_ent_add(entlist, p);
1.45 joris 219: cvs_ent_close(entlist, ENT_SYNC);
1.53 joris 220: }
221: }
1.45 joris 222:
1.53 joris 223: if (added == 1) {
1.74 xsa 224: (void)xsnprintf(msg, sizeof(msg),
1.56 xsa 225: "Directory %s added to the repository", cf->file_rpath);
226:
227: if (tag != NULL) {
228: (void)strlcat(msg,
229: "\n--> Using per-directory sticky tag ",
230: sizeof(msg));
231: (void)strlcat(msg, tag, sizeof(msg));
232: }
233: if (date != NULL) {
234: (void)strlcat(msg,
235: "\n--> Using per-directory sticky date ",
236: sizeof(msg));
237: (void)strlcat(msg, date, sizeof(msg));
238: }
239: cvs_printf("%s\n", msg);
240:
241: if (tag != NULL)
242: xfree(tag);
243: if (date != NULL)
244: xfree(date);
1.45 joris 245: }
246:
247: cf->file_status = FILE_SKIP;
248: }
1.3 jfb 249:
1.45 joris 250: static void
251: add_file(struct cvs_file *cf)
252: {
1.55 xsa 253: int added, stop;
1.78 ! xsa 254: char revbuf[CVS_REV_BUFSZ];
1.52 joris 255: RCSNUM *head;
1.10 xsa 256:
1.68 joris 257: if (cf->file_rcs != NULL) {
258: head = rcs_head_get(cf->file_rcs);
259: rcsnum_tostr(head, revbuf, sizeof(revbuf));
260: rcsnum_free(head);
261: }
1.44 joris 262:
1.48 xsa 263: added = stop = 0;
1.43 joris 264: switch (cf->file_status) {
265: case FILE_ADDED:
1.48 xsa 266: if (verbosity > 1)
267: cvs_log(LP_NOTICE, "%s has already been entered",
268: cf->file_path);
269: stop = 1;
270: break;
271: case FILE_REMOVED:
272: if (cf->file_rcs == NULL) {
273: cvs_log(LP_NOTICE, "cannot resurrect %s; "
274: "RCS file removed by second party", cf->file_name);
1.73 joris 275: } else if (cf->fd == -1) {
1.55 xsa 276: add_entry(cf);
1.48 xsa 277:
1.55 xsa 278: /* Restore the file. */
1.52 joris 279: head = rcs_head_get(cf->file_rcs);
1.69 joris 280: cvs_checkout_file(cf, head, 0);
1.68 joris 281: rcsnum_free(head);
282:
1.48 xsa 283: cvs_printf("U %s\n", cf->file_path);
284:
285: cvs_log(LP_NOTICE, "%s, version %s, resurrected",
286: cf->file_name, revbuf);
287:
288: cf->file_status = FILE_UPTODATE;
289: }
1.44 joris 290: stop = 1;
291: break;
1.50 xsa 292: case FILE_CONFLICT:
293: case FILE_LOST:
294: case FILE_MODIFIED:
1.44 joris 295: case FILE_UPTODATE:
296: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
297: cvs_log(LP_NOTICE, "%s already exists, with version "
298: "number %s", cf->file_path, revbuf);
299: stop = 1;
300: }
1.43 joris 301: break;
302: case FILE_UNKNOWN:
1.44 joris 303: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
304: cvs_log(LP_NOTICE, "re-adding file %s "
305: "(instead of dead revision %s)",
306: cf->file_path, revbuf);
1.73 joris 307: added++;
308: } else if (cf->fd != -1) {
1.44 joris 309: cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
310: cf->file_path);
1.73 joris 311: added++;
312: } else {
313: stop = 1;
1.44 joris 314: }
1.43 joris 315: break;
316: default:
317: break;
1.1 jfb 318: }
1.44 joris 319:
320: if (stop == 1)
321: return;
322:
1.55 xsa 323: add_entry(cf);
1.44 joris 324:
1.48 xsa 325: if (added != 0) {
1.67 joris 326: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
327: "permanently", __progname,
328: (added == 1) ? "this file" : "these files");
1.48 xsa 329: }
1.55 xsa 330: }
331:
332: static void
333: add_entry(struct cvs_file *cf)
334: {
335: FILE *fp;
1.78 ! xsa 336: char entry[CVS_ENT_MAXLINELEN], path[MAXPATHLEN];
! 337: char revbuf[CVS_REV_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.55 xsa 338: CVSENTRIES *entlist;
339:
340: if (cvs_noexec == 1)
341: return;
342:
343: if (cf->file_status == FILE_REMOVED) {
344: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
345:
346: ctime_r(&cf->file_ent->ce_mtime, tbuf);
347: if (tbuf[strlen(tbuf) - 1] == '\n')
348: tbuf[strlen(tbuf) - 1] = '\0';
349:
350: /* Remove the '-' prefixing the version number. */
1.74 xsa 351: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.72 xsa 352: "/%s/%s/%s/%s/", cf->file_name, revbuf, tbuf,
353: cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "");
1.55 xsa 354: } else {
355: if (logmsg != NULL) {
1.74 xsa 356: (void)xsnprintf(path, MAXPATHLEN, "%s/%s%s",
1.55 xsa 357: CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
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: }
370:
1.74 xsa 371: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.72 xsa 372: "/%s/0/Initial %s/%s/", cf->file_name, cf->file_name,
373: (kflag != RCS_KWEXP_DEFAULT) ? kbuf : "");
1.55 xsa 374: }
375:
376: entlist = cvs_ent_open(cf->file_wd);
377: cvs_ent_add(entlist, entry);
378: cvs_ent_close(entlist, ENT_SYNC);
1.1 jfb 379: }