Annotation of src/usr.bin/cvs/add.c, Revision 1.54
1.54 ! joris 1: /* $OpenBSD: add.c,v 1.53 2006/06/04 09:52:56 joris Exp $ */
1.1 jfb 2: /*
1.43 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.43 joris 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 jfb 8: *
1.43 joris 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.37 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
1.43 joris 21: #include "diff.h"
1.1 jfb 22: #include "log.h"
1.2 jfb 23: #include "proto.h"
1.1 jfb 24:
1.48 xsa 25: extern char *__progname;
26:
1.43 joris 27: int cvs_add(int, char **);
28: void cvs_add_local(struct cvs_file *);
1.1 jfb 29:
1.45 joris 30: static void add_directory(struct cvs_file *);
31: static void add_file(struct cvs_file *);
32:
1.43 joris 33: char *logmsg;
1.21 jfb 34:
35: struct cvs_cmd cvs_cmd_add = {
36: CVS_OP_ADD, CVS_REQ_ADD, "add",
37: { "ad", "new" },
1.43 joris 38: "Add a new file or directory to the repository",
39: "[-m message] ...",
1.47 joris 40: "m:",
1.21 jfb 41: NULL,
1.43 joris 42: cvs_add
1.15 joris 43: };
1.3 jfb 44:
1.43 joris 45: int
46: cvs_add(int argc, char **argv)
1.1 jfb 47: {
1.15 joris 48: int ch;
1.43 joris 49: int flags;
50: struct cvs_recursion cr;
1.1 jfb 51:
1.45 joris 52: flags = CR_REPO;
1.1 jfb 53:
1.43 joris 54: while ((ch = getopt(argc, argv, cvs_cmd_add.cmd_opts)) != -1) {
1.1 jfb 55: switch (ch) {
56: case 'm':
1.43 joris 57: logmsg = xstrdup(optarg);
1.1 jfb 58: break;
59: default:
1.43 joris 60: fatal("%s", cvs_cmd_add.cmd_synopsis);
1.1 jfb 61: }
62: }
63:
1.43 joris 64: argc -= optind;
65: argv += optind;
1.28 xsa 66:
1.46 joris 67: if (argc == 0)
68: fatal("%s", cvs_cmd_add.cmd_synopsis);
69:
1.43 joris 70: cr.enterdir = NULL;
71: cr.leavedir = NULL;
72: cr.local = cvs_add_local;
73: cr.remote = NULL;
74: cr.flags = flags;
75:
1.46 joris 76: cvs_file_run(argc, argv, &cr);
1.3 jfb 77: return (0);
78: }
79:
1.43 joris 80: void
81: cvs_add_local(struct cvs_file *cf)
1.3 jfb 82: {
1.45 joris 83: cvs_log(LP_TRACE, "cvs_add_local(%s)", cf->file_path);
84:
1.51 joris 85: cvs_file_classify(cf, NULL, 0);
1.49 xsa 86:
87: /* dont use `cvs add *' */
88: if (strcmp(cf->file_name, ".") == 0 ||
89: strcmp(cf->file_name, "..") == 0 ||
90: strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
91: if (verbosity > 1)
92: cvs_log(LP_ERR,
93: "cannot add special file `%s'; skipping",
94: cf->file_name);
95: return;
96: }
1.45 joris 97:
98: if (cf->file_type == CVS_DIR)
99: add_directory(cf);
100: else
101: add_file(cf);
102: }
103:
104: static void
105: add_directory(struct cvs_file *cf)
106: {
1.53 joris 107: int l, added;
1.45 joris 108: struct stat st;
1.44 joris 109: CVSENTRIES *entlist;
1.45 joris 110: char *entry, *repo;
111:
112: cvs_log(LP_TRACE, "add_directory(%s)", cf->file_path);
1.44 joris 113:
1.45 joris 114: entry = xmalloc(MAXPATHLEN);
115: l = snprintf(entry, MAXPATHLEN, "%s%s", cf->file_rpath, RCS_FILE_EXT);
116: if (l == -1 || l >= MAXPATHLEN)
117: fatal("cvs_add_local: overflow");
118:
1.53 joris 119: added = 1;
1.45 joris 120: if (stat(entry, &st) != -1) {
121: cvs_log(LP_NOTICE, "cannot add directory %s: "
122: "a file with that name already exists",
123: cf->file_path);
1.53 joris 124: added = 0;
1.45 joris 125: } else {
126: l = snprintf(entry, MAXPATHLEN, "%s/%s", cf->file_path,
127: CVS_PATH_CVSDIR);
128: if (l == -1 || l >= MAXPATHLEN)
129: fatal("add_directory: overflow");
130:
131: if (stat(entry, &st) != -1) {
132: if (!S_ISDIR(st.st_mode)) {
133: cvs_log(LP_ERR, "%s exists but is not "
134: "directory", entry);
135: } else {
136: cvs_log(LP_NOTICE, "%s already exists",
137: entry);
138: }
1.53 joris 139: added = 0;
140: } else if (cvs_noexec != 1) {
1.45 joris 141: if (mkdir(cf->file_rpath, 0755) == -1 &&
142: errno != EEXIST)
143: fatal("add_directory: %s: %s", cf->file_path,
144: strerror(errno));
145:
146: repo = xmalloc(MAXPATHLEN);
147: cvs_get_repository_name(cf->file_wd, repo,
148: MAXPATHLEN);
149:
150: l = snprintf(entry, MAXPATHLEN, "%s/%s", repo,
151: cf->file_path);
152:
153: cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
154: entry);
155:
156: xfree(repo);
157: xfree(entry);
158:
159: entry = xmalloc(CVS_ENT_MAXLINELEN);
160: l = snprintf(entry, CVS_ENT_MAXLINELEN,
161: "D/%s/////", cf->file_name);
162: entlist = cvs_ent_open(cf->file_wd);
163: cvs_ent_add(entlist, entry);
164: cvs_ent_close(entlist, ENT_SYNC);
1.53 joris 165: }
166: }
1.45 joris 167:
1.53 joris 168: if (added == 1) {
169: cvs_printf("Directory %s added to the repository\n",
170: cf->file_rpath);
1.45 joris 171: }
172:
173: cf->file_status = FILE_SKIP;
174: xfree(entry);
175: }
1.3 jfb 176:
1.45 joris 177: static void
178: add_file(struct cvs_file *cf)
179: {
1.48 xsa 180: BUF *b;
181: int added, l, stop;
182: char *entry, revbuf[16], tbuf[32];
1.52 joris 183: RCSNUM *head;
1.45 joris 184: CVSENTRIES *entlist;
1.10 xsa 185:
1.44 joris 186: if (cf->file_rcs != NULL)
1.52 joris 187: rcsnum_tostr(rcs_head_get(cf->file_rcs),
188: revbuf, sizeof(revbuf));
1.44 joris 189:
1.48 xsa 190: added = stop = 0;
1.43 joris 191: switch (cf->file_status) {
192: case FILE_ADDED:
1.48 xsa 193: if (verbosity > 1)
194: cvs_log(LP_NOTICE, "%s has already been entered",
195: cf->file_path);
196: stop = 1;
197: break;
198: case FILE_REMOVED:
199: if (cf->file_rcs == NULL) {
200: cvs_log(LP_NOTICE, "cannot resurrect %s; "
201: "RCS file removed by second party", cf->file_name);
1.53 joris 202: } else if (cvs_noexec != 1) {
1.48 xsa 203: /*
204: * Remove the '-' prefixing the version number and
205: * restore the file.
206: */
207: rcsnum_tostr(cf->file_ent->ce_rev, revbuf,
208: sizeof(revbuf));
209:
210: ctime_r(&cf->file_ent->ce_mtime, tbuf);
211: if (tbuf[strlen(tbuf) - 1] == '\n')
212: tbuf[strlen(tbuf) - 1] = '\0';
213:
214: entry = xmalloc(CVS_ENT_MAXLINELEN);
215: l = snprintf(entry, CVS_ENT_MAXLINELEN,
216: "/%s/%s/%s//", cf->file_name, revbuf, tbuf);
217: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
218: fatal("cvs_add_local: overflow");
219:
220: entlist = cvs_ent_open(cf->file_wd);
221: cvs_ent_add(entlist, entry);
222: cvs_ent_close(entlist, ENT_SYNC);
223:
224: xfree(entry);
225:
1.52 joris 226: head = rcs_head_get(cf->file_rcs);
227: b = rcs_getrev(cf->file_rcs, head);
1.48 xsa 228: if (b == NULL)
229: fatal("cvs_add_local: failed to get HEAD");
230:
1.53 joris 231: cvs_checkout_file(cf, head, b, 0);
1.48 xsa 232: cvs_printf("U %s\n", cf->file_path);
233:
234: cvs_log(LP_NOTICE, "%s, version %s, resurrected",
235: cf->file_name, revbuf);
236:
237: cf->file_status = FILE_UPTODATE;
1.53 joris 238: } else {
239: cvs_log(LP_NOTICE, "%s, version %s, ressurected",
240: cf->file_name, revbuf);
1.48 xsa 241: }
1.53 joris 242:
1.44 joris 243: stop = 1;
244: break;
1.50 xsa 245: case FILE_CONFLICT:
246: case FILE_LOST:
247: case FILE_MODIFIED:
1.44 joris 248: case FILE_UPTODATE:
249: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
250: cvs_log(LP_NOTICE, "%s already exists, with version "
251: "number %s", cf->file_path, revbuf);
252: stop = 1;
253: }
1.43 joris 254: break;
255: case FILE_UNKNOWN:
1.44 joris 256: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
257: cvs_log(LP_NOTICE, "re-adding file %s "
258: "(instead of dead revision %s)",
259: cf->file_path, revbuf);
260: } else {
261: cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
262: cf->file_path);
263: }
1.54 ! joris 264: added++;
1.43 joris 265: break;
266: default:
267: break;
1.1 jfb 268: }
1.44 joris 269:
270: if (stop == 1)
271: return;
272:
1.53 joris 273: if (added != 0 && cvs_noexec != 1) {
274: entry = xmalloc(CVS_ENT_MAXLINELEN);
275: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/0/Initial %s//",
276: cf->file_name, cf->file_name);
277:
278: entlist = cvs_ent_open(cf->file_wd);
279: cvs_ent_add(entlist, entry);
280: cvs_ent_close(entlist, ENT_SYNC);
1.44 joris 281:
1.53 joris 282: xfree(entry);
283: }
1.44 joris 284:
1.48 xsa 285: if (added != 0) {
286: if (verbosity > 0)
287: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
288: "permanently", __progname,
289: (added == 1) ? "this file" : "these files");
290: }
1.1 jfb 291: }