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