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