Annotation of src/usr.bin/cvs/add.c, Revision 1.52
1.52 ! joris 1: /* $OpenBSD: add.c,v 1.51 2006/05/30 21:32:52 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: {
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.52 ! joris 178: RCSNUM *head;
1.45 joris 179: CVSENTRIES *entlist;
1.10 xsa 180:
1.44 joris 181: if (cf->file_rcs != NULL)
1.52 ! joris 182: rcsnum_tostr(rcs_head_get(cf->file_rcs),
! 183: revbuf, sizeof(revbuf));
1.44 joris 184:
1.48 xsa 185: added = stop = 0;
1.43 joris 186: switch (cf->file_status) {
187: case FILE_ADDED:
1.48 xsa 188: if (verbosity > 1)
189: cvs_log(LP_NOTICE, "%s has already been entered",
190: cf->file_path);
191: stop = 1;
192: break;
193: case FILE_REMOVED:
194: if (cf->file_rcs == NULL) {
195: cvs_log(LP_NOTICE, "cannot resurrect %s; "
196: "RCS file removed by second party", cf->file_name);
197: } else {
198: /*
199: * Remove the '-' prefixing the version number and
200: * restore the file.
201: */
202: rcsnum_tostr(cf->file_ent->ce_rev, revbuf,
203: sizeof(revbuf));
204:
205: ctime_r(&cf->file_ent->ce_mtime, tbuf);
206: if (tbuf[strlen(tbuf) - 1] == '\n')
207: tbuf[strlen(tbuf) - 1] = '\0';
208:
209: entry = xmalloc(CVS_ENT_MAXLINELEN);
210: l = snprintf(entry, CVS_ENT_MAXLINELEN,
211: "/%s/%s/%s//", cf->file_name, revbuf, tbuf);
212: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
213: fatal("cvs_add_local: overflow");
214:
215: entlist = cvs_ent_open(cf->file_wd);
216: cvs_ent_add(entlist, entry);
217: cvs_ent_close(entlist, ENT_SYNC);
218:
219: xfree(entry);
220:
1.52 ! joris 221: head = rcs_head_get(cf->file_rcs);
! 222: b = rcs_getrev(cf->file_rcs, head);
1.48 xsa 223: if (b == NULL)
224: fatal("cvs_add_local: failed to get HEAD");
225:
226: cvs_checkout_file(cf, cf->file_rcs->rf_head, b, 0);
227: cvs_printf("U %s\n", cf->file_path);
228:
229: cvs_log(LP_NOTICE, "%s, version %s, resurrected",
230: cf->file_name, revbuf);
231:
232: cf->file_status = FILE_UPTODATE;
233: }
1.44 joris 234: stop = 1;
235: break;
1.50 xsa 236: case FILE_CONFLICT:
237: case FILE_LOST:
238: case FILE_MODIFIED:
1.44 joris 239: case FILE_UPTODATE:
240: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 0) {
241: cvs_log(LP_NOTICE, "%s already exists, with version "
242: "number %s", cf->file_path, revbuf);
243: stop = 1;
244: }
1.43 joris 245: break;
246: case FILE_UNKNOWN:
1.44 joris 247: if (cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1) {
248: cvs_log(LP_NOTICE, "re-adding file %s "
249: "(instead of dead revision %s)",
250: cf->file_path, revbuf);
251: } else {
1.48 xsa 252: added++;
1.44 joris 253: cvs_log(LP_NOTICE, "scheduling file '%s' for addition",
254: cf->file_path);
255: }
1.43 joris 256: break;
257: default:
258: break;
1.1 jfb 259: }
1.44 joris 260:
261: if (stop == 1)
262: return;
263:
264: entry = xmalloc(CVS_ENT_MAXLINELEN);
265: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/0/Initial %s//",
266: cf->file_name, cf->file_name);
267:
268: entlist = cvs_ent_open(cf->file_wd);
269: cvs_ent_add(entlist, entry);
270: cvs_ent_close(entlist, ENT_SYNC);
271:
272: xfree(entry);
273:
1.48 xsa 274: if (added != 0) {
275: if (verbosity > 0)
276: cvs_log(LP_NOTICE, "use '%s commit' to add %s "
277: "permanently", __progname,
278: (added == 1) ? "this file" : "these files");
279: }
1.1 jfb 280: }