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