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