Annotation of src/usr.bin/cvs/update.c, Revision 1.64
1.64 ! joris 1: /* $OpenBSD: update.c,v 1.63 2006/05/27 16:10:01 joris Exp $ */
1.1 jfb 2: /*
1.59 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.59 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.59 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.51 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
21: #include "log.h"
1.59 joris 22: #include "diff.h"
1.4 jfb 23: #include "proto.h"
1.1 jfb 24:
1.59 joris 25: int cvs_update(int, char **);
1.60 joris 26: int prune_dirs = 0;
1.63 joris 27: int build_dirs = 0;
28:
29: #define UPDATE_SKIP 100
1.18 joris 30:
1.29 jfb 31: struct cvs_cmd cvs_cmd_update = {
32: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
33: { "up", "upd" },
34: "Bring work tree in sync with repository",
35: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
36: "[-t id] ...",
1.35 xsa 37: "ACD:dfI:j:k:lPpQqRr:t:",
1.29 jfb 38: NULL,
1.59 joris 39: cvs_update
1.18 joris 40: };
1.2 jfb 41:
1.59 joris 42: int
43: cvs_update(int argc, char **argv)
1.1 jfb 44: {
1.18 joris 45: int ch;
1.59 joris 46: char *arg = ".";
1.61 joris 47: int flags;
1.59 joris 48: struct cvs_recursion cr;
1.2 jfb 49:
1.62 joris 50: flags = CR_REPO | CR_RECURSE_DIRS;
1.61 joris 51:
1.59 joris 52: while ((ch = getopt(argc, argv, cvs_cmd_update.cmd_opts)) != -1) {
1.1 jfb 53: switch (ch) {
54: case 'A':
1.24 joris 55: break;
1.1 jfb 56: case 'C':
57: case 'D':
1.32 xsa 58: break;
1.1 jfb 59: case 'd':
1.63 joris 60: build_dirs = 1;
1.24 joris 61: break;
1.1 jfb 62: case 'f':
1.2 jfb 63: break;
1.35 xsa 64: case 'I':
65: break;
66: case 'j':
67: break;
68: case 'k':
69: break;
1.1 jfb 70: case 'l':
1.61 joris 71: flags &= ~CR_RECURSE_DIRS;
1.2 jfb 72: break;
1.1 jfb 73: case 'P':
1.60 joris 74: prune_dirs = 1;
1.24 joris 75: break;
1.1 jfb 76: case 'p':
1.27 xsa 77: break;
1.1 jfb 78: case 'Q':
79: case 'q':
1.2 jfb 80: break;
1.1 jfb 81: case 'R':
1.2 jfb 82: break;
1.1 jfb 83: case 'r':
84: break;
85: default:
1.59 joris 86: fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1 jfb 87: }
1.32 xsa 88: }
89:
1.59 joris 90: argc -= optind;
91: argv += optind;
1.2 jfb 92:
1.59 joris 93: cr.enterdir = cvs_update_enterdir;
1.60 joris 94: cr.leavedir = cvs_update_leavedir;
1.59 joris 95: cr.local = cvs_update_local;
96: cr.remote = NULL;
1.61 joris 97: cr.flags = flags;
1.49 joris 98:
1.59 joris 99: if (argc > 0)
100: cvs_file_run(argc, argv, &cr);
101: else
102: cvs_file_run(1, &arg, &cr);
1.35 xsa 103:
1.24 joris 104: return (0);
105: }
1.2 jfb 106:
1.59 joris 107: void
108: cvs_update_enterdir(struct cvs_file *cf)
1.2 jfb 109: {
1.59 joris 110: int l;
111: char *entry;
112: CVSENTRIES *entlist;
113:
114: cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
115:
116: cvs_file_classify(cf);
117:
1.63 joris 118: if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.59 joris 119: cvs_mkpath(cf->file_path);
120: if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
121: fatal("cvs_update_enterdir: %s", strerror(errno));
122:
123: entry = xmalloc(CVS_ENT_MAXLINELEN);
124: l = snprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////",
125: cf->file_name);
126: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
127: fatal("cvs_update_enterdir: overflow");
128:
129: entlist = cvs_ent_open(cf->file_wd);
130: cvs_ent_add(entlist, entry);
131: cvs_ent_close(entlist, ENT_SYNC);
132: xfree(entry);
1.63 joris 133: } else if (cf->file_status == DIR_CREATE && build_dirs == 0) {
134: cf->file_status = UPDATE_SKIP;
1.60 joris 135: }
136: }
137:
138: void
139: cvs_update_leavedir(struct cvs_file *cf)
140: {
141: long base;
142: int nbytes;
143: int isempty;
144: size_t bufsize;
145: struct stat st;
146: struct dirent *dp;
147: char *buf, *ebuf, *cp;
148: struct cvs_ent *ent;
149: struct cvs_ent_line *line;
150: CVSENTRIES *entlist;
151:
152: if (fstat(cf->fd, &st) == -1)
153: fatal("cvs_update_leavedir: %s", strerror(errno));
154:
155: bufsize = st.st_size;
156: if (bufsize < st.st_blksize)
157: bufsize = st.st_blksize;
158:
159: isempty = 1;
160: buf = xmalloc(bufsize);
161:
162: if (lseek(cf->fd, SEEK_SET, 0) == -1)
163: fatal("cvs_update_leavedir: %s", strerror(errno));
164:
165: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
166: ebuf = buf + nbytes;
167: cp = buf;
168:
169: while (cp < ebuf) {
170: dp = (struct dirent *)cp;
171: if (!strcmp(dp->d_name, ".") ||
172: !strcmp(dp->d_name, "..") ||
173: dp->d_reclen == 0) {
174: cp += dp->d_reclen;
175: continue;
176: }
177:
178: if (!strcmp(dp->d_name, CVS_PATH_CVSDIR)) {
179: entlist = cvs_ent_open(cf->file_path);
180: TAILQ_FOREACH(line, &(entlist->cef_ent),
181: entries_list) {
182: ent = cvs_ent_parse(line->buf);
183:
184: if (ent->ce_status == CVS_ENT_REMOVED) {
185: isempty = 0;
186: cvs_ent_free(ent);
187: break;
188: }
189:
190: cvs_ent_free(ent);
191: }
192: cvs_ent_close(entlist, ENT_NOSYNC);
193: } else {
194: isempty = 0;
195: }
196:
197: if (isempty == 0)
198: break;
199:
200: cp += dp->d_reclen;
201: }
202: }
203:
204: if (nbytes == -1)
205: fatal("cvs_update_leavedir: %s", strerror(errno));
206:
207: xfree(buf);
208:
209: if (isempty == 1 && prune_dirs == 1) {
210: cvs_rmdir(cf->file_path);
211:
212: entlist = cvs_ent_open(cf->file_wd);
213: cvs_ent_remove(entlist, cf->file_name);
214: cvs_ent_close(entlist, ENT_SYNC);
1.2 jfb 215: }
216: }
217:
1.59 joris 218: void
219: cvs_update_local(struct cvs_file *cf)
1.2 jfb 220: {
1.59 joris 221: CVSENTRIES *entlist;
222:
223: cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37 joris 224:
1.59 joris 225: if (cf->file_type == CVS_DIR) {
1.63 joris 226: if (cf->file_status == UPDATE_SKIP)
227: return;
228:
1.59 joris 229: if (cf->file_status != FILE_UNKNOWN &&
230: verbosity > 1)
231: cvs_log(LP_NOTICE, "Updating %s", cf->file_path);
232: return;
1.26 jfb 233: }
234:
1.59 joris 235: cvs_file_classify(cf);
1.45 joris 236:
1.59 joris 237: switch (cf->file_status) {
238: case FILE_UNKNOWN:
239: cvs_printf("? %s\n", cf->file_path);
1.45 joris 240: break;
1.59 joris 241: case FILE_MODIFIED:
242: if (cf->file_ent->ce_conflict != NULL)
243: cvs_printf("C %s\n", cf->file_path);
244: else
245: cvs_printf("M %s\n", cf->file_path);
1.45 joris 246: break;
1.59 joris 247: case FILE_ADDED:
248: cvs_printf("A %s\n", cf->file_path);
1.45 joris 249: break;
1.59 joris 250: case FILE_REMOVED:
251: cvs_printf("R %s\n", cf->file_path);
1.45 joris 252: break;
1.59 joris 253: case FILE_CONFLICT:
254: cvs_printf("C %s\n", cf->file_path);
255: break;
256: case FILE_LOST:
257: case FILE_CHECKOUT:
258: case FILE_PATCH:
259: if (cvs_checkout_file(cf, cf->file_rcs->rf_head, 0))
260: cvs_printf("U %s\n", cf->file_path);
261: break;
262: case FILE_MERGE:
263: cvs_printf("needs merge: %s\n", cf->file_path);
264: break;
265: case FILE_UNLINK:
266: (void)unlink(cf->file_path);
267: case FILE_REMOVE_ENTRY:
268: entlist = cvs_ent_open(cf->file_wd);
269: cvs_ent_remove(entlist, cf->file_name);
270: cvs_ent_close(entlist, ENT_SYNC);
1.45 joris 271: break;
272: default:
273: break;
274: }
1.1 jfb 275: }