Annotation of src/usr.bin/cvs/file.c, Revision 1.149
1.149 ! joris 1: /* $OpenBSD: file.c,v 1.148 2006/05/28 23:38:42 pedro Exp $ */
1.1 jfb 2: /*
1.138 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26 jfb 5: * All rights reserved.
1.1 jfb 6: *
1.26 jfb 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
1.1 jfb 10: *
1.26 jfb 11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
1.1 jfb 13: * 2. The name of the author may not be used to endorse or promote products
1.26 jfb 14: * derived from this software without specific prior written permission.
1.1 jfb 15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.26 jfb 25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 26: */
27:
1.134 xsa 28: #include "includes.h"
1.1 jfb 29:
30: #include "cvs.h"
1.83 xsa 31: #include "file.h"
1.1 jfb 32: #include "log.h"
33:
1.104 xsa 34: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
1.1 jfb 35:
1.104 xsa 36: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
1.1 jfb 37:
38: /*
39: * Standard patterns to ignore.
40: */
41: static const char *cvs_ign_std[] = {
42: ".",
43: "..",
1.142 joris 44: ".*",
1.1 jfb 45: "*.o",
1.45 xsa 46: "*.a",
1.1 jfb 47: "*.bak",
48: "*.orig",
49: "*.rej",
1.45 xsa 50: "*.old",
1.1 jfb 51: "*.exe",
52: "*.depend",
1.45 xsa 53: "*.obj",
54: "*.elc",
55: "*.ln",
56: "*.olb",
1.1 jfb 57: "CVS",
58: "core",
1.102 joris 59: "cvslog*",
1.49 jfb 60: "*.core",
1.8 jfb 61: ".#*",
1.45 xsa 62: "*~",
63: "_$*",
64: "*$",
1.1 jfb 65: };
66:
1.138 joris 67: struct ignore_head cvs_ign_pats;
68: struct ignore_head dir_ign_pats;
1.1 jfb 69:
1.138 joris 70: static int cvs_file_cmpname(const char *, const char *);
1.1 jfb 71:
1.138 joris 72: void
1.1 jfb 73: cvs_file_init(void)
74: {
1.61 xsa 75: int i, l;
1.138 joris 76: FILE *ifp;
1.1 jfb 77: size_t len;
1.138 joris 78: char *path, *buf;
79:
80: path = xmalloc(MAXPATHLEN);
81: buf = xmalloc(MAXNAMLEN);
1.1 jfb 82:
83: TAILQ_INIT(&cvs_ign_pats);
1.138 joris 84: TAILQ_INIT(&dir_ign_pats);
1.4 jfb 85:
1.1 jfb 86: /* standard patterns to ignore */
1.10 jfb 87: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.138 joris 88: cvs_file_ignore(cvs_ign_std[i], &cvs_ign_pats);
1.1 jfb 89:
90: /* read the cvsignore file in the user's home directory, if any */
1.138 joris 91: l = snprintf(path, MAXPATHLEN, "%s/.cvsignore", cvs_homedir);
92: if (l == -1 || l >= MAXPATHLEN)
93: fatal("overflow in cvs_file_init");
1.61 xsa 94:
1.111 xsa 95: ifp = fopen(path, "r");
96: if (ifp == NULL) {
97: if (errno != ENOENT)
98: cvs_log(LP_ERRNO,
99: "failed to open user's cvsignore file `%s'", path);
100: } else {
1.138 joris 101: while (fgets(buf, MAXNAMLEN, ifp) != NULL) {
1.111 xsa 102: len = strlen(buf);
103: if (len == 0)
104: continue;
1.138 joris 105:
106: if (buf[len - 1] != '\n')
1.111 xsa 107: cvs_log(LP_ERR, "line too long in `%s'", path);
1.138 joris 108:
1.111 xsa 109: buf[--len] = '\0';
1.138 joris 110: cvs_file_ignore(buf, &cvs_ign_pats);
1.1 jfb 111: }
1.138 joris 112:
1.111 xsa 113: (void)fclose(ifp);
1.1 jfb 114: }
115:
1.138 joris 116: xfree(path);
117: xfree(buf);
1.1 jfb 118: }
119:
1.138 joris 120: void
121: cvs_file_ignore(const char *pat, struct ignore_head *list)
1.1 jfb 122: {
123: char *cp;
1.138 joris 124: size_t len;
1.1 jfb 125: struct cvs_ignpat *ip;
126:
1.136 ray 127: ip = xmalloc(sizeof(*ip));
1.138 joris 128: len = strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
129: if (len >= sizeof(ip->ip_pat))
130: fatal("cvs_file_ignore: truncation of pattern '%s'", pat);
1.1 jfb 131:
132: /* check if we will need globbing for that pattern */
133: ip->ip_flags = CVS_IGN_STATIC;
134: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
135: if (CVS_CHAR_ISMETA(*cp)) {
136: ip->ip_flags &= ~CVS_IGN_STATIC;
137: break;
138: }
139: }
140:
1.138 joris 141: TAILQ_INSERT_TAIL(list, ip, ip_list);
1.1 jfb 142: }
143:
144: int
1.5 jfb 145: cvs_file_chkign(const char *file)
1.1 jfb 146: {
1.23 jfb 147: int flags;
1.1 jfb 148: struct cvs_ignpat *ip;
149:
1.23 jfb 150: flags = FNM_PERIOD;
151: if (cvs_nocase)
152: flags |= FNM_CASEFOLD;
153:
1.1 jfb 154: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
155: if (ip->ip_flags & CVS_IGN_STATIC) {
1.23 jfb 156: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1 jfb 157: return (1);
1.38 deraadt 158: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1 jfb 159: return (1);
160: }
161:
1.138 joris 162: TAILQ_FOREACH(ip, &dir_ign_pats, ip_list) {
163: if (ip->ip_flags & CVS_IGN_STATIC) {
164: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
165: return (1);
166: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
167: return (1);
168: }
169:
1.1 jfb 170: return (0);
171: }
172:
1.138 joris 173: void
174: cvs_file_run(int argc, char **argv, struct cvs_recursion *cr)
1.1 jfb 175: {
1.138 joris 176: int i;
177: struct cvs_flisthead fl;
1.84 joris 178:
1.138 joris 179: TAILQ_INIT(&fl);
1.85 joris 180:
1.138 joris 181: for (i = 0; i < argc; i++)
182: cvs_file_get(argv[i], &fl);
1.1 jfb 183:
1.138 joris 184: cvs_file_walklist(&fl, cr);
185: cvs_file_freelist(&fl);
1.3 jfb 186: }
187:
1.138 joris 188: struct cvs_filelist *
189: cvs_file_get(const char *name, struct cvs_flisthead *fl)
190: {
191: const char *p;
192: struct cvs_filelist *l;
1.3 jfb 193:
1.138 joris 194: for (p = name; p[0] == '.' && p[1] == '/';)
195: p += 2;
1.35 jfb 196:
1.138 joris 197: TAILQ_FOREACH(l, fl, flist)
198: if (!strcmp(l->file_path, p))
199: return (l);
1.35 jfb 200:
1.138 joris 201: l = (struct cvs_filelist *)xmalloc(sizeof(*l));
202: l->file_path = xstrdup(p);
1.35 jfb 203:
1.138 joris 204: TAILQ_INSERT_TAIL(fl, l, flist);
205: return (l);
1.35 jfb 206: }
207:
1.138 joris 208: struct cvs_file *
209: cvs_file_get_cf(const char *d, const char *f, int fd, int type)
210: {
211: int l;
212: struct cvs_file *cf;
213: char *p, *rpath;
1.35 jfb 214:
1.138 joris 215: rpath = xmalloc(MAXPATHLEN);
1.3 jfb 216:
1.138 joris 217: l = snprintf(rpath, MAXPATHLEN, "%s/%s", d, f);
218: if (l == -1 || l >= MAXPATHLEN)
219: fatal("cvs_file_get_cf: overflow");
220:
221: for (p = rpath; p[0] == '.' && p[1] == '/';)
222: p += 2;
223:
224: cf = (struct cvs_file *)xmalloc(sizeof(*cf));
225: memset(cf, 0, sizeof(*cf));
226:
227: cf->file_name = xstrdup(f);
228: cf->file_wd = xstrdup(d);
229: cf->file_path = xstrdup(p);
230: cf->fd = fd;
231: cf->repo_fd = -1;
232: cf->file_type = type;
233: cf->file_status = cf->file_flags = 0;
234: cf->file_ent = NULL;
1.68 joris 235:
1.138 joris 236: xfree(rpath);
237: return (cf);
1.9 jfb 238: }
239:
1.138 joris 240: void
241: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9 jfb 242: {
1.138 joris 243: int len, fd, type;
244: struct stat st;
245: struct cvs_file *cf;
246: struct cvs_filelist *l, *nxt;
247: char *d, *f, *repo, *fpath;
248:
249: fpath = xmalloc(MAXPATHLEN);
250: repo = xmalloc(MAXPATHLEN);
251:
252: for (l = TAILQ_FIRST(fl); l != NULL; l = nxt) {
253: if (cvs_quit)
254: fatal("received signal %d", sig_received);
255:
256: cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
257: l->file_path);
258:
259: if ((f = basename(l->file_path)) == NULL)
260: fatal("cvs_file_walklist: basename failed");
261: if ((d = dirname(l->file_path)) == NULL)
262: fatal("cvs_file_walklist: dirname failed");
263:
1.146 joris 264: type = CVS_FILE;
1.138 joris 265: if ((fd = open(l->file_path, O_RDONLY)) != -1) {
266: if (fstat(fd, &st) == -1) {
267: cvs_log(LP_ERRNO, "%s", l->file_path);
268: (void)close(fd);
269: goto next;
270: }
1.26 jfb 271:
1.138 joris 272: if (S_ISDIR(st.st_mode))
273: type = CVS_DIR;
274: else if (S_ISREG(st.st_mode))
275: type = CVS_FILE;
276: else {
277: cvs_log(LP_ERR,
278: "ignoring bad file type for %s",
279: l->file_path);
280: (void)close(fd);
281: goto next;
282: }
283: } else {
284: if (stat(d, &st) == -1) {
285: cvs_log(LP_ERRNO, "%s", d);
286: goto next;
287: }
1.87 joris 288:
1.146 joris 289: cvs_get_repository_path(d, repo, MAXPATHLEN);
1.138 joris 290: len = snprintf(fpath, MAXPATHLEN, "%s/%s",
291: repo, f);
292: if (len == -1 || len >= MAXPATHLEN)
293: fatal("cvs_file_walklist: overflow");
294:
295: if ((fd = open(fpath, O_RDONLY)) == -1) {
296: strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
297: fd = open(fpath, O_RDONLY);
298: }
1.26 jfb 299:
1.138 joris 300: if (fd != -1) {
301: if (fstat(fd, &st) == -1)
302: fatal("cvs_file_walklist: %s: %s",
303: fpath, strerror(errno));
304:
305: if (S_ISDIR(st.st_mode))
306: type = CVS_DIR;
307: else if (S_ISREG(st.st_mode))
308: type = CVS_FILE;
309: else {
310: cvs_log(LP_ERR,
311: "ignoring bad file type for %s",
312: l->file_path);
313: (void)close(fd);
314: goto next;
315: }
316:
317: /* this file is not in our working copy yet */
318: (void)close(fd);
319: fd = -1;
320: }
321: }
1.73 joris 322:
1.138 joris 323: cf = cvs_file_get_cf(d, f, fd, type);
324: if (cf->file_type == CVS_DIR) {
325: cvs_file_walkdir(cf, cr);
326: } else {
327: if (cr->local != NULL)
328: cr->local(cf);
1.128 joris 329: }
1.100 joris 330:
1.138 joris 331: cvs_file_free(cf);
1.123 joris 332:
1.138 joris 333: next:
334: nxt = TAILQ_NEXT(l, flist);
335: TAILQ_REMOVE(fl, l, flist);
1.68 joris 336:
1.138 joris 337: xfree(l->file_path);
338: xfree(l);
1.100 joris 339: }
340:
1.138 joris 341: xfree(fpath);
342: xfree(repo);
1.100 joris 343: }
344:
1.138 joris 345: void
346: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100 joris 347: {
1.138 joris 348: int l;
349: FILE *fp;
350: int nbytes;
351: size_t len;
352: long base;
353: size_t bufsize;
354: struct stat st;
355: struct dirent *dp;
1.100 joris 356: struct cvs_ent *ent;
1.138 joris 357: struct cvs_ignpat *ip;
358: struct cvs_ent_line *line;
359: struct cvs_flisthead fl, dl;
360: CVSENTRIES *entlist;
361: char *buf, *ebuf, *cp, *repo, *fpath;
1.100 joris 362:
1.138 joris 363: cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100 joris 364:
1.138 joris 365: if (cr->enterdir != NULL)
366: cr->enterdir(cf);
1.100 joris 367:
1.138 joris 368: if (cr->local != NULL)
369: cr->local(cf);
1.100 joris 370:
1.146 joris 371: if (cf->file_status == FILE_SKIP)
372: return;
373:
1.138 joris 374: fpath = xmalloc(MAXPATHLEN);
1.100 joris 375:
376: /*
1.138 joris 377: * If we do not have a admin directory inside here, dont bother.
1.100 joris 378: */
1.138 joris 379: l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
380: CVS_PATH_CVSDIR);
381: if (l == -1 || l >= MAXPATHLEN)
382: fatal("cvs_file_walkdir: overflow");
1.100 joris 383:
1.146 joris 384: l = stat(fpath, &st);
385: if (l == -1 || (l == 0 && !S_ISDIR(st.st_mode))) {
1.138 joris 386: xfree(fpath);
387: return;
1.100 joris 388: }
389:
390: /*
1.138 joris 391: * check for a local .cvsignore file
1.100 joris 392: */
1.138 joris 393: l = snprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
394: if (l == -1 || l >= MAXPATHLEN)
395: fatal("cvs_file_walkdir: overflow");
1.100 joris 396:
1.138 joris 397: if ((fp = fopen(fpath, "r")) != NULL) {
398: while (fgets(fpath, MAXPATHLEN, fp)) {
399: len = strlen(fpath);
400: if (fpath[len - 1] == '\n')
401: fpath[len - 1] = '\0';
1.26 jfb 402:
1.138 joris 403: cvs_file_ignore(fpath, &dir_ign_pats);
1.68 joris 404: }
1.26 jfb 405:
1.138 joris 406: (void)fclose(fp);
1.100 joris 407: }
408:
1.138 joris 409: if (fstat(cf->fd, &st) == -1)
410: fatal("cvs_file_walkdir: %s %s", cf->file_path,
411: strerror(errno));
1.100 joris 412:
1.138 joris 413: bufsize = st.st_size;
414: if (bufsize < st.st_blksize)
415: bufsize = st.st_blksize;
1.116 joris 416:
1.138 joris 417: buf = xmalloc(bufsize);
418: TAILQ_INIT(&fl);
419: TAILQ_INIT(&dl);
1.87 joris 420:
1.138 joris 421: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
422: ebuf = buf + nbytes;
423: cp = buf;
1.26 jfb 424:
1.138 joris 425: while (cp < ebuf) {
426: dp = (struct dirent *)cp;
427: if (!strcmp(dp->d_name, ".") ||
428: !strcmp(dp->d_name, "..") ||
429: !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
430: dp->d_reclen == 0) {
431: cp += dp->d_reclen;
432: continue;
433: }
1.13 jfb 434:
1.138 joris 435: if (cvs_file_chkign(dp->d_name)) {
436: cp += dp->d_reclen;
1.13 jfb 437: continue;
1.138 joris 438: }
1.13 jfb 439:
1.140 joris 440: if (!(cr->flags & CR_RECURSE_DIRS) &&
441: dp->d_type == DT_DIR) {
442: cp += dp->d_reclen;
443: continue;
444: }
445:
1.138 joris 446: l = snprintf(fpath, MAXPATHLEN, "%s/%s",
447: cf->file_path, dp->d_name);
448: if (l == -1 || l >= MAXPATHLEN)
449: fatal("cvs_file_walkdir: overflow");
1.13 jfb 450:
1.138 joris 451: /*
452: * Anticipate the file type to sort them,
453: * note that we do not determine the final
454: * type until we actually have the fd floating
455: * around.
456: */
457: if (dp->d_type == DT_DIR)
458: cvs_file_get(fpath, &dl);
459: else if (dp->d_type == DT_REG)
460: cvs_file_get(fpath, &fl);
1.13 jfb 461:
1.138 joris 462: cp += dp->d_reclen;
463: }
1.34 jfb 464: }
465:
1.138 joris 466: if (nbytes == -1)
467: fatal("cvs_file_walkdir: %s %s", cf->file_path,
468: strerror(errno));
1.22 jfb 469:
1.138 joris 470: xfree(buf);
1.22 jfb 471:
1.138 joris 472: while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
473: TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
474: xfree(ip);
1.91 joris 475: }
1.114 xsa 476:
1.138 joris 477: entlist = cvs_ent_open(cf->file_path);
478: TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
479: ent = cvs_ent_parse(line->buf);
1.61 xsa 480:
1.138 joris 481: l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
482: ent->ce_name);
483: if (l == -1 || l >= MAXPATHLEN)
484: fatal("cvs_file_walkdir: overflow");
1.100 joris 485:
1.140 joris 486: if (!(cr->flags & CR_RECURSE_DIRS) &&
487: ent->ce_type == CVS_ENT_DIR)
488: continue;
489:
1.138 joris 490: if (ent->ce_type == CVS_ENT_DIR)
491: cvs_file_get(fpath, &dl);
492: else if (ent->ce_type == CVS_ENT_FILE)
493: cvs_file_get(fpath, &fl);
1.68 joris 494:
1.138 joris 495: cvs_ent_free(ent);
1.128 joris 496: }
497:
1.138 joris 498: cvs_ent_close(entlist, ENT_NOSYNC);
1.91 joris 499:
1.140 joris 500: if (cr->flags & CR_REPO) {
501: repo = xmalloc(MAXPATHLEN);
1.146 joris 502: cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.140 joris 503: cvs_repository_lock(repo);
1.68 joris 504:
1.140 joris 505: cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
506: (cr->flags & CR_RECURSE_DIRS));
507: }
1.68 joris 508:
1.138 joris 509: cvs_file_walklist(&fl, cr);
510: cvs_file_freelist(&fl);
1.68 joris 511:
1.140 joris 512: if (cr->flags & CR_REPO) {
513: cvs_repository_unlock(repo);
514: xfree(repo);
515: }
1.68 joris 516:
1.138 joris 517: cvs_file_walklist(&dl, cr);
518: cvs_file_freelist(&dl);
1.34 jfb 519:
1.138 joris 520: xfree(fpath);
1.26 jfb 521:
1.138 joris 522: if (cr->leavedir != NULL)
523: cr->leavedir(cf);
1.3 jfb 524: }
525:
526: void
1.138 joris 527: cvs_file_freelist(struct cvs_flisthead *fl)
1.3 jfb 528: {
1.138 joris 529: struct cvs_filelist *f;
1.62 jfb 530:
1.138 joris 531: while ((f = TAILQ_FIRST(fl)) != NULL) {
532: TAILQ_REMOVE(fl, f, flist);
533: xfree(f->file_path);
534: xfree(f);
1.62 jfb 535: }
1.5 jfb 536: }
537:
1.138 joris 538: void
1.143 joris 539: cvs_file_classify(struct cvs_file *cf, int loud)
1.3 jfb 540: {
1.138 joris 541: size_t len;
542: time_t mtime;
543: struct stat st;
1.149 ! joris 544: BUF *b1, *b2;
1.143 joris 545: int rflags, l, ismodified, rcsdead, verbose;
1.138 joris 546: CVSENTRIES *entlist = NULL;
547: const char *state;
548: char *repo, *rcsfile, r1[16], r2[16];
549:
550: cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
551:
552: if (!strcmp(cf->file_path, ".")) {
553: cf->file_status = FILE_UPTODATE;
554: return;
555: }
556:
1.143 joris 557: verbose = (verbosity > 1 && loud == 1);
1.138 joris 558: entlist = cvs_ent_open(cf->file_wd);
559:
560: repo = xmalloc(MAXPATHLEN);
561: rcsfile = xmalloc(MAXPATHLEN);
562:
1.146 joris 563: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.138 joris 564: l = snprintf(rcsfile, MAXPATHLEN, "%s/%s",
565: repo, cf->file_name);
566: if (l == -1 || l >= MAXPATHLEN)
567: fatal("cvs_file_classify: overflow");
568:
569: if (cf->file_type == CVS_FILE) {
570: len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
571: if (len >= MAXPATHLEN)
572: fatal("cvs_file_classify: truncation");
573: }
574:
575: cf->file_rpath = xstrdup(rcsfile);
576: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
577:
1.144 joris 578: if (cf->file_ent != NULL) {
579: if (cf->file_ent->ce_type == CVS_ENT_DIR &&
580: cf->file_type != CVS_DIR)
1.148 pedro 581: fatal("%s is supposed to be a directory, but it is not",
1.144 joris 582: cf->file_path);
583: if (cf->file_ent->ce_type == CVS_ENT_FILE &&
584: cf->file_type != CVS_FILE)
1.148 pedro 585: fatal("%s is supposed to be a file, but it is not",
1.144 joris 586: cf->file_path);
587: }
588:
1.138 joris 589: if (cf->file_type == CVS_DIR) {
590: if (cf->fd == -1 && stat(rcsfile, &st) != -1)
591: cf->file_status = DIR_CREATE;
592: else if (cf->file_ent != NULL)
593: cf->file_status = FILE_UPTODATE;
1.144 joris 594: else
595: cf->file_status = FILE_UNKNOWN;
596:
1.138 joris 597: xfree(repo);
598: xfree(rcsfile);
599: cvs_ent_close(entlist, ENT_NOSYNC);
600: return;
601: }
602:
1.147 joris 603: rflags = RCS_READ;
604: switch (cvs_cmdop) {
605: case CVS_OP_COMMIT:
606: rflags = RCS_WRITE;
607: break;
608: case CVS_OP_LOG:
609: rflags |= RCS_PARSE_FULLY;
610: break;
611: }
612:
1.138 joris 613: cf->repo_fd = open(cf->file_rpath, O_RDONLY);
614: if (cf->repo_fd != -1) {
615: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
616: if (cf->file_rcs == NULL)
617: fatal("cvs_file_classify: rcs_open failed while it "
618: "shouldn't");
619: } else {
620: cf->file_rcs = NULL;
1.3 jfb 621: }
622:
1.138 joris 623: if (cf->file_ent != NULL)
624: rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
625: if (cf->file_rcs != NULL)
626: rcsnum_tostr(cf->file_rcs->rf_head, r2, sizeof(r2));
1.6 jfb 627:
1.138 joris 628: ismodified = rcsdead = 0;
629: if (cf->fd != -1 && cf->file_ent != NULL) {
630: if (fstat(cf->fd, &st) == -1)
631: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 632:
1.138 joris 633: mtime = cvs_hack_time(st.st_mtime, 1);
634: if (mtime == 0)
635: fatal("to gmt failed");
1.62 jfb 636:
1.138 joris 637: if (mtime != cf->file_ent->ce_mtime)
638: ismodified = 1;
1.149 ! joris 639: }
! 640:
! 641: if (ismodified == 1 && cf->fd != -1 && cf->file_rcs != NULL) {
! 642: b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
! 643: if (b1 == NULL)
! 644: fatal("failed to get HEAD revision for comparison");
! 645:
! 646: b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head);
! 647:
! 648: /* XXX */
! 649: b2 = cvs_buf_load(cf->file_path, BUF_AUTOEXT);
! 650: if (b2 == NULL)
! 651: fatal("failed to get file content for comparison");
! 652:
! 653: /* b1 and b2 get released in cvs_buf_differ */
! 654: if (cvs_buf_differ(b1, b2))
! 655: ismodified = 1;
! 656: else
! 657: ismodified = 0;
1.62 jfb 658: }
659:
1.138 joris 660: if (cf->file_rcs != NULL) {
661: state = rcs_state_get(cf->file_rcs, cf->file_rcs->rf_head);
662: if (state == NULL)
663: fatal("failed to get state for HEAD for %s",
664: cf->file_path);
1.139 joris 665: if (!strcmp(state, RCS_STATE_DEAD))
1.138 joris 666: rcsdead = 1;
1.145 joris 667:
668: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 669: }
670:
1.138 joris 671: /*
672: * 10 Sin
673: * 20 Goto hell
674: * (I welcome you if-else hell)
675: */
676: if (cf->file_ent == NULL) {
677: if (cf->file_rcs == NULL) {
678: if (cf->fd == -1) {
1.143 joris 679: if (verbose)
1.138 joris 680: cvs_log(LP_NOTICE,
681: "nothing known about '%s'",
682: cf->file_path);
683: } else {
1.143 joris 684: if (verbose)
1.138 joris 685: cvs_log(LP_NOTICE,
686: "use add to create an entry for %s",
687: cf->file_path);
688: }
1.130 joris 689:
1.138 joris 690: cf->file_status = FILE_UNKNOWN;
691: } else if (rcsdead == 1) {
692: if (cf->fd == -1) {
693: cf->file_status = FILE_UPTODATE;
694: } else {
1.143 joris 695: if (verbose)
696: cvs_log(LP_NOTICE,
697: "use add to create an entry for %s",
698: cf->file_path);
1.138 joris 699: cf->file_status = FILE_UNKNOWN;
1.44 jfb 700: }
1.138 joris 701: } else {
702: cf->file_status = FILE_CHECKOUT;
1.14 jfb 703: }
1.138 joris 704: } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
705: if (cf->fd == -1) {
1.143 joris 706: if (verbose)
1.138 joris 707: cvs_log(LP_NOTICE,
708: "warning: new-born %s has dissapeared",
709: cf->file_path);
710: cf->file_status = FILE_REMOVE_ENTRY;
711: } else if (cf->file_rcs == NULL || rcsdead == 1) {
712: cf->file_status = FILE_ADDED;
713: } else {
1.143 joris 714: if (verbose)
1.138 joris 715: cvs_log(LP_NOTICE,
716: "conflict: %s already created by others",
717: cf->file_path);
718: cf->file_status = FILE_CONFLICT;
719: }
720: } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
721: if (cf->fd != -1) {
1.143 joris 722: if (verbose)
1.138 joris 723: cvs_log(LP_NOTICE,
724: "%s should be removed but is still there",
725: cf->file_path);
726: cf->file_status = FILE_REMOVED;
727: } else if (cf->file_rcs == NULL || rcsdead == 1) {
728: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 729: } else {
1.138 joris 730: if (strcmp(r1, r2)) {
1.143 joris 731: if (verbose)
1.138 joris 732: cvs_log(LP_NOTICE,
733: "conflict: removed %s was modified"
734: " by a second party",
735: cf->file_path);
736: cf->file_status = FILE_CONFLICT;
737: } else {
738: cf->file_status = FILE_REMOVED;
739: }
1.66 joris 740: }
1.138 joris 741: } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
742: if (cf->file_rcs == NULL || rcsdead == 1) {
743: if (cf->fd == -1) {
1.143 joris 744: if (verbose)
1.138 joris 745: cvs_log(LP_NOTICE,
746: "warning: %s's entry exists but"
747: " there is no longer a file"
748: " in the repository,"
749: " removing entry",
750: cf->file_path);
751: cf->file_status = FILE_REMOVE_ENTRY;
752: } else {
753: if (ismodified) {
1.143 joris 754: if (verbose)
1.138 joris 755: cvs_log(LP_NOTICE,
756: "conflict: %s is no longer "
757: "in the repository but is "
758: "locally modified",
759: cf->file_path);
760: cf->file_status = FILE_CONFLICT;
761: } else {
1.143 joris 762: if (verbose)
1.138 joris 763: cvs_log(LP_NOTICE,
764: "%s is no longer in the "
765: "repository",
766: cf->file_path);
1.128 joris 767:
1.138 joris 768: cf->file_status = FILE_UNLINK;
769: }
770: }
1.128 joris 771: } else {
1.138 joris 772: if (cf->fd == -1) {
1.143 joris 773: if (verbose)
1.138 joris 774: cvs_log(LP_NOTICE,
775: "warning: %s was lost",
776: cf->file_path);
777: cf->file_status = FILE_LOST;
778: } else {
779: if (ismodified == 1)
780: cf->file_status = FILE_MODIFIED;
781: else
782: cf->file_status = FILE_UPTODATE;
783:
784: if (strcmp(r1, r2)) {
785: if (cf->file_status == FILE_MODIFIED)
786: cf->file_status = FILE_MERGE;
787: else
788: cf->file_status = FILE_PATCH;
789: }
790: }
1.128 joris 791: }
792: }
793:
1.138 joris 794: xfree(repo);
795: xfree(rcsfile);
796: cvs_ent_close(entlist, ENT_NOSYNC);
797: }
1.14 jfb 798:
1.138 joris 799: void
800: cvs_file_free(struct cvs_file *cf)
801: {
802: xfree(cf->file_name);
803: xfree(cf->file_wd);
804: xfree(cf->file_path);
805:
806: if (cf->file_rpath != NULL)
807: xfree(cf->file_rpath);
808: if (cf->file_ent != NULL)
809: cvs_ent_free(cf->file_ent);
810: if (cf->file_rcs != NULL)
811: rcs_close(cf->file_rcs);
812: if (cf->fd != -1)
813: (void)close(cf->fd);
814: if (cf->repo_fd != -1)
815: (void)close(cf->repo_fd);
816: xfree(cf);
1.23 jfb 817: }
818:
819: static int
820: cvs_file_cmpname(const char *name1, const char *name2)
821: {
822: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
823: (strcasecmp(name1, name2));
1.14 jfb 824: }