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