Annotation of src/usr.bin/cvs/add.c, Revision 1.50
1.50 ! xsa 1: /* $OpenBSD: add.c,v 1.49 2006/05/30 08:23:31 xsa 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:
85: cvs_file_classify(cf, 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: {
107: int l;
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:
119: if (stat(entry, &st) != -1) {
120: cvs_log(LP_NOTICE, "cannot add directory %s: "
121: "a file with that name already exists",
122: cf->file_path);
123: } else {
124: l = snprintf(entry, MAXPATHLEN, "%s/%s", cf->file_path,
125: CVS_PATH_CVSDIR);
126: if (l == -1 || l >= MAXPATHLEN)
127: fatal("add_directory: overflow");
128:
129: if (stat(entry, &st) != -1) {
130: if (!S_ISDIR(st.st_mode)) {
131: cvs_log(LP_ERR, "%s exists but is not "
132: "directory", entry);
133: } else {
134: cvs_log(LP_NOTICE, "%s already exists",
135: entry);
136: }
137: } else {
138: if (mkdir(cf->file_rpath, 0755) == -1 &&
139: errno != EEXIST)
140: fatal("add_directory: %s: %s", cf->file_path,
141: strerror(errno));
142:
143: repo = xmalloc(MAXPATHLEN);
144: cvs_get_repository_name(cf->file_wd, repo,
145: MAXPATHLEN);
146:
147: l = snprintf(entry, MAXPATHLEN, "%s/%s", repo,
148: cf->file_path);
149:
150: cvs_mkadmin(cf->file_path, current_cvsroot->cr_dir,
151: entry);
152:
153: xfree(repo);
154: xfree(entry);
155:
156: entry = xmalloc(CVS_ENT_MAXLINELEN);
157: l = snprintf(entry, CVS_ENT_MAXLINELEN,
158: "D/%s/////", cf->file_name);
159: entlist = cvs_ent_open(cf->file_wd);
160: cvs_ent_add(entlist, entry);
161: cvs_ent_close(entlist, ENT_SYNC);
162:
163: cvs_printf("Directory %s added to the repository\n",
164: cf->file_rpath);
165: }
166: }
167:
168: cf->file_status = FILE_SKIP;
169: xfree(entry);
170: }
1.3 jfb 171:
1.45 joris 172: static void
173: add_file(struct cvs_file *cf)
174: {
1.48 xsa 175: BUF *b;
176: int added, l, stop;
177: char *entry, revbuf[16], tbuf[32];
1.45 joris 178: CVSENTRIES *entlist;
1.10 xsa 179:
1.44 joris 180: if (cf->file_rcs != NULL)
181: rcsnum_tostr(cf->file_rcs->rf_head, revbuf, sizeof(revbuf));
182:
1.48 xsa 183: added = stop = 0;
1.43 joris 184: switch (cf->file_status) {
185: case FILE_ADDED:
1.48 xsa 186: if (verbosity > 1)
187: cvs_log(LP_NOTICE, "%s has already been entered",
188: cf->file_path);
189: stop = 1;
190: break;
191: case FILE_REMOVED:
192: if (cf->file_rcs == NULL) {
193: cvs_log(LP_NOTICE, "cannot resurrect %s; "
194: "RCS file removed by second party", cf->file_name);
195: } else {
196: /*
197: * Remove the '-' prefixing the version number and
198: * restore the file.
199: */
200: rcsnum_tostr(cf->file_ent->ce_rev, revbuf,
201: sizeof(revbuf));
202:
203: ctime_r(&cf->file_ent->ce_mtime, tbuf);
204: if (tbuf[strlen(tbuf) - 1] == '\n')
205: tbuf[strlen(tbuf) - 1] = '\0';
206:
207: entry = xmalloc(CVS_ENT_MAXLINELEN);
208: l = snprintf(entry, CVS_ENT_MAXLINELEN,
209: "/%s/%s/%s//", cf->file_name, revbuf, tbuf);
210: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
211: fatal("cvs_add_local: overflow");
212:
213: entlist = cvs_ent_open(cf->file_wd);
214: cvs_ent_add(entlist, entry);
215: cvs_ent_close(entlist, ENT_SYNC);
216:
217: xfree(entry);
218:
219: b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
220: if (b == NULL)
221: fatal("cvs_add_local: failed to get HEAD");
222:
223: cvs_checkout_file(cf, cf->file_rcs->rf_head, b, 0);
224: cvs_printf("U %s\n", cf->file_path);
225:
226: cvs_log(LP_NOTICE, "%s, version %s, resurrected",
227: cf->file_name, revbuf);
228:
229: cf->file_status = FILE_UPTODATE;
230: }
1.44 joris 231: stop = 1;
232: break;
1.50 ! xsa 233: case FILE_CONFLICT:
! 234: case FILE_LOST:
! 235: case FILE_MODIFIED:
1.44 joris 236: case FILE_UPTODATE:
237: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
238: cvs_log(LP_NOTICE, "%s already exists, with version "
239: "number %s", cf->file_path, revbuf);
240: stop = 1;
241: }
1.43 joris 242: break;
243: case FILE_UNKNOWN:
1.44 joris 244: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
245: cvs_log(LP_NOTICE, "re-adding file %s "
246: "(instead of dead revision %s)",
247: cf->file_path, revbuf);
248: } else {
1.48 xsa 249: added++;
1.44 joris 250: cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
251: cf->file_path);
252: }
1.43 joris 253: break;
254: default:
255: break;
1.1 jfb 256: }
1.44 joris 257:
258: if (stop == 1)
259: return;
260:
261: entry = xmalloc(CVS_ENT_MAXLINELEN);
262: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/0/Initial %s//",
263: cf->file_name, cf->file_name);
264:
265: entlist = cvs_ent_open(cf->file_wd);
266: cvs_ent_add(entlist, entry);
267: cvs_ent_close(entlist, ENT_SYNC);
268:
269: xfree(entry);
270:
1.48 xsa 271: if (added != 0) {
272: if (verbosity > 0)
273: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
274: "permanently", __progname,
275: (added == 1) ? "this file" : "these files");
276: }
1.1 jfb 277: }