Annotation of src/usr.bin/cvs/file.c, Revision 1.164
1.164 ! xsa 1: /* $OpenBSD: file.c,v 1.163 2006/10/25 20:52:34 moritz 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:
1.164 ! xsa 30: #include <sys/mman.h>
! 31:
1.1 jfb 32: #include "cvs.h"
1.83 xsa 33: #include "file.h"
1.1 jfb 34: #include "log.h"
35:
1.104 xsa 36: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
1.1 jfb 37:
1.104 xsa 38: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
1.1 jfb 39:
40: /*
41: * Standard patterns to ignore.
42: */
43: static const char *cvs_ign_std[] = {
44: ".",
45: "..",
46: "*.o",
1.45 xsa 47: "*.a",
1.1 jfb 48: "*.bak",
49: "*.orig",
50: "*.rej",
1.45 xsa 51: "*.old",
1.1 jfb 52: "*.exe",
53: "*.depend",
1.45 xsa 54: "*.obj",
55: "*.elc",
56: "*.ln",
57: "*.olb",
1.1 jfb 58: "CVS",
59: "core",
1.102 joris 60: "cvslog*",
1.49 jfb 61: "*.core",
1.8 jfb 62: ".#*",
1.45 xsa 63: "*~",
64: "_$*",
65: "*$",
1.1 jfb 66: };
67:
1.138 joris 68: struct ignore_head cvs_ign_pats;
69: struct ignore_head dir_ign_pats;
1.1 jfb 70:
1.138 joris 71: static int cvs_file_cmpname(const char *, const char *);
1.1 jfb 72:
1.138 joris 73: void
1.1 jfb 74: cvs_file_init(void)
75: {
1.61 xsa 76: int i, l;
1.138 joris 77: FILE *ifp;
1.1 jfb 78: size_t len;
1.138 joris 79: char *path, *buf;
80:
81: path = xmalloc(MAXPATHLEN);
82: buf = xmalloc(MAXNAMLEN);
1.1 jfb 83:
84: TAILQ_INIT(&cvs_ign_pats);
1.138 joris 85: TAILQ_INIT(&dir_ign_pats);
1.4 jfb 86:
1.1 jfb 87: /* standard patterns to ignore */
1.10 jfb 88: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.138 joris 89: cvs_file_ignore(cvs_ign_std[i], &cvs_ign_pats);
1.1 jfb 90:
91: /* read the cvsignore file in the user's home directory, if any */
1.138 joris 92: l = snprintf(path, MAXPATHLEN, "%s/.cvsignore", cvs_homedir);
93: if (l == -1 || l >= MAXPATHLEN)
94: fatal("overflow in cvs_file_init");
1.61 xsa 95:
1.111 xsa 96: ifp = fopen(path, "r");
97: if (ifp == NULL) {
98: if (errno != ENOENT)
99: cvs_log(LP_ERRNO,
100: "failed to open user's cvsignore file `%s'", path);
101: } else {
1.138 joris 102: while (fgets(buf, MAXNAMLEN, ifp) != NULL) {
1.111 xsa 103: len = strlen(buf);
104: if (len == 0)
105: continue;
1.163 moritz 106: if (buf[len - 1] == '\n')
107: buf[len - 1] = '\0';
1.138 joris 108:
109: cvs_file_ignore(buf, &cvs_ign_pats);
1.1 jfb 110: }
1.138 joris 111:
1.111 xsa 112: (void)fclose(ifp);
1.1 jfb 113: }
114:
1.138 joris 115: xfree(path);
116: xfree(buf);
1.1 jfb 117: }
118:
1.138 joris 119: void
120: cvs_file_ignore(const char *pat, struct ignore_head *list)
1.1 jfb 121: {
122: char *cp;
1.138 joris 123: size_t len;
1.1 jfb 124: struct cvs_ignpat *ip;
125:
1.136 ray 126: ip = xmalloc(sizeof(*ip));
1.138 joris 127: len = strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
128: if (len >= sizeof(ip->ip_pat))
129: fatal("cvs_file_ignore: truncation of pattern '%s'", pat);
1.1 jfb 130:
131: /* check if we will need globbing for that pattern */
132: ip->ip_flags = CVS_IGN_STATIC;
133: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
134: if (CVS_CHAR_ISMETA(*cp)) {
135: ip->ip_flags &= ~CVS_IGN_STATIC;
136: break;
137: }
138: }
139:
1.138 joris 140: TAILQ_INSERT_TAIL(list, ip, ip_list);
1.1 jfb 141: }
142:
143: int
1.5 jfb 144: cvs_file_chkign(const char *file)
1.1 jfb 145: {
1.23 jfb 146: int flags;
1.1 jfb 147: struct cvs_ignpat *ip;
148:
1.23 jfb 149: flags = FNM_PERIOD;
150: if (cvs_nocase)
151: flags |= FNM_CASEFOLD;
152:
1.1 jfb 153: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
154: if (ip->ip_flags & CVS_IGN_STATIC) {
1.23 jfb 155: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1 jfb 156: return (1);
1.38 deraadt 157: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1 jfb 158: return (1);
159: }
160:
1.138 joris 161: TAILQ_FOREACH(ip, &dir_ign_pats, ip_list) {
162: if (ip->ip_flags & CVS_IGN_STATIC) {
163: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
164: return (1);
165: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
166: return (1);
167: }
168:
1.1 jfb 169: return (0);
170: }
171:
1.138 joris 172: void
173: cvs_file_run(int argc, char **argv, struct cvs_recursion *cr)
1.1 jfb 174: {
1.138 joris 175: int i;
176: struct cvs_flisthead fl;
1.84 joris 177:
1.138 joris 178: TAILQ_INIT(&fl);
1.85 joris 179:
1.138 joris 180: for (i = 0; i < argc; i++)
181: cvs_file_get(argv[i], &fl);
1.1 jfb 182:
1.138 joris 183: cvs_file_walklist(&fl, cr);
184: cvs_file_freelist(&fl);
1.3 jfb 185: }
186:
1.138 joris 187: struct cvs_filelist *
188: cvs_file_get(const char *name, struct cvs_flisthead *fl)
189: {
190: const char *p;
191: struct cvs_filelist *l;
1.3 jfb 192:
1.138 joris 193: for (p = name; p[0] == '.' && p[1] == '/';)
194: p += 2;
1.35 jfb 195:
1.138 joris 196: TAILQ_FOREACH(l, fl, flist)
197: if (!strcmp(l->file_path, p))
198: return (l);
1.35 jfb 199:
1.138 joris 200: l = (struct cvs_filelist *)xmalloc(sizeof(*l));
201: l->file_path = xstrdup(p);
1.35 jfb 202:
1.138 joris 203: TAILQ_INSERT_TAIL(fl, l, flist);
204: return (l);
1.35 jfb 205: }
206:
1.138 joris 207: struct cvs_file *
208: cvs_file_get_cf(const char *d, const char *f, int fd, int type)
209: {
210: int l;
211: struct cvs_file *cf;
212: char *p, *rpath;
1.35 jfb 213:
1.138 joris 214: rpath = xmalloc(MAXPATHLEN);
1.3 jfb 215:
1.138 joris 216: l = snprintf(rpath, MAXPATHLEN, "%s/%s", d, f);
217: if (l == -1 || l >= MAXPATHLEN)
218: fatal("cvs_file_get_cf: overflow");
219:
220: for (p = rpath; p[0] == '.' && p[1] == '/';)
221: p += 2;
222:
223: cf = (struct cvs_file *)xmalloc(sizeof(*cf));
224: memset(cf, 0, sizeof(*cf));
225:
226: cf->file_name = xstrdup(f);
227: cf->file_wd = xstrdup(d);
228: cf->file_path = xstrdup(p);
229: cf->fd = fd;
230: cf->repo_fd = -1;
231: cf->file_type = type;
232: cf->file_status = cf->file_flags = 0;
233: cf->file_ent = NULL;
1.68 joris 234:
1.138 joris 235: xfree(rpath);
236: return (cf);
1.9 jfb 237: }
238:
1.138 joris 239: void
240: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9 jfb 241: {
1.138 joris 242: int len, fd, type;
243: struct stat st;
244: struct cvs_file *cf;
245: struct cvs_filelist *l, *nxt;
246: char *d, *f, *repo, *fpath;
247:
248: fpath = xmalloc(MAXPATHLEN);
249: repo = xmalloc(MAXPATHLEN);
250:
251: for (l = TAILQ_FIRST(fl); l != NULL; l = nxt) {
252: if (cvs_quit)
253: fatal("received signal %d", sig_received);
254:
255: cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
256: l->file_path);
257:
258: if ((f = basename(l->file_path)) == NULL)
259: fatal("cvs_file_walklist: basename failed");
260: if ((d = dirname(l->file_path)) == NULL)
261: fatal("cvs_file_walklist: dirname failed");
262:
1.146 joris 263: type = CVS_FILE;
1.138 joris 264: if ((fd = open(l->file_path, O_RDONLY)) != -1) {
265: if (fstat(fd, &st) == -1) {
266: cvs_log(LP_ERRNO, "%s", l->file_path);
267: (void)close(fd);
268: goto next;
269: }
1.26 jfb 270:
1.138 joris 271: if (S_ISDIR(st.st_mode))
272: type = CVS_DIR;
273: else if (S_ISREG(st.st_mode))
274: type = CVS_FILE;
275: else {
276: cvs_log(LP_ERR,
277: "ignoring bad file type for %s",
278: l->file_path);
279: (void)close(fd);
280: goto next;
281: }
1.162 joris 282: } else if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.138 joris 283: if (stat(d, &st) == -1) {
284: cvs_log(LP_ERRNO, "%s", d);
285: goto next;
286: }
1.87 joris 287:
1.146 joris 288: cvs_get_repository_path(d, repo, MAXPATHLEN);
1.138 joris 289: len = snprintf(fpath, MAXPATHLEN, "%s/%s",
290: repo, f);
291: if (len == -1 || len >= MAXPATHLEN)
292: fatal("cvs_file_walklist: overflow");
293:
294: if ((fd = open(fpath, O_RDONLY)) == -1) {
295: strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
296: fd = open(fpath, O_RDONLY);
297: }
1.26 jfb 298:
1.138 joris 299: if (fd != -1) {
300: if (fstat(fd, &st) == -1)
301: fatal("cvs_file_walklist: %s: %s",
302: fpath, strerror(errno));
303:
304: if (S_ISDIR(st.st_mode))
305: type = CVS_DIR;
306: else if (S_ISREG(st.st_mode))
307: type = CVS_FILE;
308: else {
309: cvs_log(LP_ERR,
310: "ignoring bad file type for %s",
311: l->file_path);
312: (void)close(fd);
313: goto next;
314: }
315:
316: /* this file is not in our working copy yet */
317: (void)close(fd);
318: fd = -1;
319: }
320: }
1.73 joris 321:
1.138 joris 322: cf = cvs_file_get_cf(d, f, fd, type);
323: if (cf->file_type == CVS_DIR) {
324: cvs_file_walkdir(cf, cr);
325: } else {
1.161 joris 326: if (cr->fileproc != NULL)
327: cr->fileproc(cf);
1.128 joris 328: }
1.100 joris 329:
1.138 joris 330: cvs_file_free(cf);
1.123 joris 331:
1.138 joris 332: next:
333: nxt = TAILQ_NEXT(l, flist);
334: TAILQ_REMOVE(fl, l, flist);
1.68 joris 335:
1.138 joris 336: xfree(l->file_path);
337: xfree(l);
1.100 joris 338: }
339:
1.138 joris 340: xfree(fpath);
341: xfree(repo);
1.100 joris 342: }
343:
1.138 joris 344: void
345: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100 joris 346: {
1.138 joris 347: int l;
348: FILE *fp;
349: int nbytes;
350: size_t len;
351: long base;
352: size_t bufsize;
353: struct stat st;
354: struct dirent *dp;
1.100 joris 355: struct cvs_ent *ent;
1.138 joris 356: struct cvs_ignpat *ip;
357: struct cvs_ent_line *line;
358: struct cvs_flisthead fl, dl;
359: CVSENTRIES *entlist;
360: char *buf, *ebuf, *cp, *repo, *fpath;
1.100 joris 361:
1.138 joris 362: cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100 joris 363:
1.138 joris 364: if (cr->enterdir != NULL)
365: cr->enterdir(cf);
1.100 joris 366:
1.161 joris 367: if (cr->fileproc != NULL)
368: cr->fileproc(cf);
1.100 joris 369:
1.146 joris 370: if (cf->file_status == FILE_SKIP)
371: return;
372:
1.138 joris 373: fpath = xmalloc(MAXPATHLEN);
1.100 joris 374:
375: /*
1.151 joris 376: * If we do not have a admin directory inside here, dont bother,
377: * unless we are running import.
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);
1.151 joris 385: if (cvs_cmdop != CVS_OP_IMPORT &&
386: (l == -1 || (l == 0 && !S_ISDIR(st.st_mode)))) {
1.138 joris 387: xfree(fpath);
388: return;
1.100 joris 389: }
390:
391: /*
1.138 joris 392: * check for a local .cvsignore file
1.100 joris 393: */
1.138 joris 394: l = snprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
395: if (l == -1 || l >= MAXPATHLEN)
396: fatal("cvs_file_walkdir: overflow");
1.100 joris 397:
1.138 joris 398: if ((fp = fopen(fpath, "r")) != NULL) {
1.163 moritz 399: while (fgets(fpath, MAXPATHLEN, fp) != NULL) {
1.138 joris 400: len = strlen(fpath);
1.163 moritz 401: if (len == 0)
402: continue;
1.138 joris 403: if (fpath[len - 1] == '\n')
404: fpath[len - 1] = '\0';
1.26 jfb 405:
1.138 joris 406: cvs_file_ignore(fpath, &dir_ign_pats);
1.68 joris 407: }
1.26 jfb 408:
1.138 joris 409: (void)fclose(fp);
1.100 joris 410: }
411:
1.138 joris 412: if (fstat(cf->fd, &st) == -1)
413: fatal("cvs_file_walkdir: %s %s", cf->file_path,
414: strerror(errno));
1.100 joris 415:
1.138 joris 416: bufsize = st.st_size;
417: if (bufsize < st.st_blksize)
418: bufsize = st.st_blksize;
1.116 joris 419:
1.138 joris 420: buf = xmalloc(bufsize);
421: TAILQ_INIT(&fl);
422: TAILQ_INIT(&dl);
1.87 joris 423:
1.138 joris 424: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
425: ebuf = buf + nbytes;
426: cp = buf;
1.26 jfb 427:
1.138 joris 428: while (cp < ebuf) {
429: dp = (struct dirent *)cp;
430: if (!strcmp(dp->d_name, ".") ||
431: !strcmp(dp->d_name, "..") ||
432: !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
433: dp->d_reclen == 0) {
434: cp += dp->d_reclen;
435: continue;
436: }
1.13 jfb 437:
1.138 joris 438: if (cvs_file_chkign(dp->d_name)) {
439: cp += dp->d_reclen;
1.13 jfb 440: continue;
1.138 joris 441: }
1.13 jfb 442:
1.140 joris 443: if (!(cr->flags & CR_RECURSE_DIRS) &&
444: dp->d_type == DT_DIR) {
445: cp += dp->d_reclen;
446: continue;
447: }
448:
1.138 joris 449: l = snprintf(fpath, MAXPATHLEN, "%s/%s",
450: cf->file_path, dp->d_name);
451: if (l == -1 || l >= MAXPATHLEN)
452: fatal("cvs_file_walkdir: overflow");
1.13 jfb 453:
1.138 joris 454: /*
455: * Anticipate the file type to sort them,
456: * note that we do not determine the final
457: * type until we actually have the fd floating
458: * around.
459: */
460: if (dp->d_type == DT_DIR)
461: cvs_file_get(fpath, &dl);
462: else if (dp->d_type == DT_REG)
463: cvs_file_get(fpath, &fl);
1.13 jfb 464:
1.138 joris 465: cp += dp->d_reclen;
466: }
1.34 jfb 467: }
468:
1.138 joris 469: if (nbytes == -1)
470: fatal("cvs_file_walkdir: %s %s", cf->file_path,
471: strerror(errno));
1.22 jfb 472:
1.138 joris 473: xfree(buf);
1.22 jfb 474:
1.138 joris 475: while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
476: TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
477: xfree(ip);
1.91 joris 478: }
1.114 xsa 479:
1.138 joris 480: entlist = cvs_ent_open(cf->file_path);
481: TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
482: ent = cvs_ent_parse(line->buf);
1.61 xsa 483:
1.138 joris 484: l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
485: ent->ce_name);
486: if (l == -1 || l >= MAXPATHLEN)
487: fatal("cvs_file_walkdir: overflow");
1.100 joris 488:
1.140 joris 489: if (!(cr->flags & CR_RECURSE_DIRS) &&
490: ent->ce_type == CVS_ENT_DIR)
491: continue;
492:
1.138 joris 493: if (ent->ce_type == CVS_ENT_DIR)
494: cvs_file_get(fpath, &dl);
495: else if (ent->ce_type == CVS_ENT_FILE)
496: cvs_file_get(fpath, &fl);
1.68 joris 497:
1.138 joris 498: cvs_ent_free(ent);
1.128 joris 499: }
500:
1.138 joris 501: cvs_ent_close(entlist, ENT_NOSYNC);
1.91 joris 502:
1.140 joris 503: if (cr->flags & CR_REPO) {
504: repo = xmalloc(MAXPATHLEN);
1.146 joris 505: cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.140 joris 506: cvs_repository_lock(repo);
1.68 joris 507:
1.140 joris 508: cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
509: (cr->flags & CR_RECURSE_DIRS));
510: }
1.68 joris 511:
1.138 joris 512: cvs_file_walklist(&fl, cr);
513: cvs_file_freelist(&fl);
1.68 joris 514:
1.140 joris 515: if (cr->flags & CR_REPO) {
516: cvs_repository_unlock(repo);
517: xfree(repo);
518: }
1.68 joris 519:
1.138 joris 520: cvs_file_walklist(&dl, cr);
521: cvs_file_freelist(&dl);
1.34 jfb 522:
1.138 joris 523: xfree(fpath);
1.26 jfb 524:
1.138 joris 525: if (cr->leavedir != NULL)
526: cr->leavedir(cf);
1.3 jfb 527: }
528:
529: void
1.138 joris 530: cvs_file_freelist(struct cvs_flisthead *fl)
1.3 jfb 531: {
1.138 joris 532: struct cvs_filelist *f;
1.62 jfb 533:
1.138 joris 534: while ((f = TAILQ_FIRST(fl)) != NULL) {
535: TAILQ_REMOVE(fl, f, flist);
536: xfree(f->file_path);
537: xfree(f);
1.62 jfb 538: }
1.5 jfb 539: }
540:
1.138 joris 541: void
1.153 joris 542: cvs_file_classify(struct cvs_file *cf, const char *tag, int loud)
1.3 jfb 543: {
1.138 joris 544: size_t len;
545: time_t mtime;
546: struct stat st;
1.149 joris 547: BUF *b1, *b2;
1.143 joris 548: int rflags, l, ismodified, rcsdead, verbose;
1.138 joris 549: CVSENTRIES *entlist = NULL;
550: const char *state;
551: char *repo, *rcsfile, r1[16], r2[16];
552:
553: cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
554:
555: if (!strcmp(cf->file_path, ".")) {
556: cf->file_status = FILE_UPTODATE;
557: return;
558: }
559:
1.143 joris 560: verbose = (verbosity > 1 && loud == 1);
1.138 joris 561:
562: repo = xmalloc(MAXPATHLEN);
563: rcsfile = xmalloc(MAXPATHLEN);
564:
1.146 joris 565: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.138 joris 566: l = snprintf(rcsfile, MAXPATHLEN, "%s/%s",
567: repo, cf->file_name);
568: if (l == -1 || l >= MAXPATHLEN)
569: fatal("cvs_file_classify: overflow");
570:
571: if (cf->file_type == CVS_FILE) {
572: len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
573: if (len >= MAXPATHLEN)
574: fatal("cvs_file_classify: truncation");
575: }
576:
577: cf->file_rpath = xstrdup(rcsfile);
1.151 joris 578:
579: entlist = cvs_ent_open(cf->file_wd);
1.138 joris 580: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
581:
1.144 joris 582: if (cf->file_ent != NULL) {
583: if (cf->file_ent->ce_type == CVS_ENT_DIR &&
584: cf->file_type != CVS_DIR)
1.148 pedro 585: fatal("%s is supposed to be a directory, but it is not",
1.144 joris 586: cf->file_path);
587: if (cf->file_ent->ce_type == CVS_ENT_FILE &&
588: cf->file_type != CVS_FILE)
1.148 pedro 589: fatal("%s is supposed to be a file, but it is not",
1.144 joris 590: cf->file_path);
591: }
592:
1.138 joris 593: if (cf->file_type == CVS_DIR) {
594: if (cf->fd == -1 && stat(rcsfile, &st) != -1)
595: cf->file_status = DIR_CREATE;
596: else if (cf->file_ent != NULL)
597: cf->file_status = FILE_UPTODATE;
1.144 joris 598: else
599: cf->file_status = FILE_UNKNOWN;
600:
1.138 joris 601: xfree(repo);
602: xfree(rcsfile);
603: cvs_ent_close(entlist, ENT_NOSYNC);
604: return;
605: }
606:
1.147 joris 607: rflags = RCS_READ;
608: switch (cvs_cmdop) {
609: case CVS_OP_COMMIT:
610: rflags = RCS_WRITE;
611: break;
1.156 joris 612: case CVS_OP_IMPORT:
1.147 joris 613: case CVS_OP_LOG:
614: rflags |= RCS_PARSE_FULLY;
615: break;
616: }
617:
1.138 joris 618: cf->repo_fd = open(cf->file_rpath, O_RDONLY);
619: if (cf->repo_fd != -1) {
620: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
621: if (cf->file_rcs == NULL)
1.152 joris 622: fatal("cvs_file_classify: failed to parse RCS");
623: cf->file_rcs->rf_inattic = 0;
1.155 joris 624: } else if (cvs_cmdop != CVS_OP_CHECKOUT) {
1.152 joris 625: l = snprintf(rcsfile, MAXPATHLEN, "%s/%s/%s%s",
626: repo, CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
627: if (l == -1 || l >= MAXPATHLEN)
628: fatal("cvs_file_classify: overflow");
629:
1.155 joris 630: cf->repo_fd = open(rcsfile, O_RDONLY);
1.152 joris 631: if (cf->repo_fd != -1) {
1.155 joris 632: xfree(cf->file_rpath);
633: cf->file_rpath = xstrdup(rcsfile);
1.152 joris 634: cf->file_rcs = rcs_open(cf->file_rpath,
635: cf->repo_fd, rflags);
636: if (cf->file_rcs == NULL)
637: fatal("cvs_file_classify: failed to parse RCS");
638: cf->file_rcs->rf_inattic = 1;
639: } else {
640: cf->file_rcs = NULL;
641: }
1.154 joris 642: } else
643: cf->file_rcs = NULL;
1.3 jfb 644:
1.153 joris 645: if (tag != NULL && cf->file_rcs != NULL)
646: cf->file_rcsrev = rcs_translate_tag(tag, cf->file_rcs);
1.158 joris 647: else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL)
648: cf->file_rcsrev = cf->file_ent->ce_rev;
1.153 joris 649: else if (cf->file_rcs != NULL)
1.156 joris 650: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.153 joris 651: else
652: cf->file_rcsrev = NULL;
653:
1.138 joris 654: if (cf->file_ent != NULL)
655: rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
1.153 joris 656: if (cf->file_rcsrev != NULL)
657: rcsnum_tostr(cf->file_rcsrev, r2, sizeof(r2));
1.6 jfb 658:
1.138 joris 659: ismodified = rcsdead = 0;
660: if (cf->fd != -1 && cf->file_ent != NULL) {
661: if (fstat(cf->fd, &st) == -1)
662: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 663:
1.138 joris 664: mtime = cvs_hack_time(st.st_mtime, 1);
665: if (mtime != cf->file_ent->ce_mtime)
666: ismodified = 1;
1.149 joris 667: }
668:
669: if (ismodified == 1 && cf->fd != -1 && cf->file_rcs != NULL) {
1.153 joris 670: b1 = rcs_getrev(cf->file_rcs, cf->file_rcsrev);
1.149 joris 671: if (b1 == NULL)
672: fatal("failed to get HEAD revision for comparison");
673:
1.153 joris 674: b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcsrev);
1.149 joris 675:
1.159 joris 676: b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT);
1.149 joris 677: if (b2 == NULL)
678: fatal("failed to get file content for comparison");
679:
680: /* b1 and b2 get released in cvs_buf_differ */
681: if (cvs_buf_differ(b1, b2))
682: ismodified = 1;
683: else
684: ismodified = 0;
1.62 jfb 685: }
686:
1.138 joris 687: if (cf->file_rcs != NULL) {
1.153 joris 688: state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138 joris 689: if (state == NULL)
690: fatal("failed to get state for HEAD for %s",
691: cf->file_path);
1.139 joris 692: if (!strcmp(state, RCS_STATE_DEAD))
1.138 joris 693: rcsdead = 1;
1.145 joris 694:
695: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 696: }
697:
1.138 joris 698: /*
699: * 10 Sin
700: * 20 Goto hell
701: * (I welcome you if-else hell)
702: */
703: if (cf->file_ent == NULL) {
704: if (cf->file_rcs == NULL) {
705: if (cf->fd == -1) {
1.143 joris 706: if (verbose)
1.138 joris 707: cvs_log(LP_NOTICE,
708: "nothing known about '%s'",
709: cf->file_path);
710: } else {
1.143 joris 711: if (verbose)
1.138 joris 712: cvs_log(LP_NOTICE,
713: "use add to create an entry for %s",
714: cf->file_path);
715: }
1.130 joris 716:
1.138 joris 717: cf->file_status = FILE_UNKNOWN;
718: } else if (rcsdead == 1) {
719: if (cf->fd == -1) {
720: cf->file_status = FILE_UPTODATE;
721: } else {
1.143 joris 722: if (verbose)
723: cvs_log(LP_NOTICE,
724: "use add to create an entry for %s",
725: cf->file_path);
1.138 joris 726: cf->file_status = FILE_UNKNOWN;
1.44 jfb 727: }
1.138 joris 728: } else {
729: cf->file_status = FILE_CHECKOUT;
1.14 jfb 730: }
1.138 joris 731: } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
732: if (cf->fd == -1) {
1.143 joris 733: if (verbose)
1.138 joris 734: cvs_log(LP_NOTICE,
1.157 david 735: "warning: new-born %s has disappeared",
1.138 joris 736: cf->file_path);
737: cf->file_status = FILE_REMOVE_ENTRY;
738: } else if (cf->file_rcs == NULL || rcsdead == 1) {
739: cf->file_status = FILE_ADDED;
740: } else {
1.143 joris 741: if (verbose)
1.138 joris 742: cvs_log(LP_NOTICE,
743: "conflict: %s already created by others",
744: cf->file_path);
745: cf->file_status = FILE_CONFLICT;
746: }
747: } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
748: if (cf->fd != -1) {
1.143 joris 749: if (verbose)
1.138 joris 750: cvs_log(LP_NOTICE,
751: "%s should be removed but is still there",
752: cf->file_path);
753: cf->file_status = FILE_REMOVED;
754: } else if (cf->file_rcs == NULL || rcsdead == 1) {
755: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 756: } else {
1.138 joris 757: if (strcmp(r1, r2)) {
1.143 joris 758: if (verbose)
1.138 joris 759: cvs_log(LP_NOTICE,
760: "conflict: removed %s was modified"
761: " by a second party",
762: cf->file_path);
763: cf->file_status = FILE_CONFLICT;
764: } else {
765: cf->file_status = FILE_REMOVED;
766: }
1.66 joris 767: }
1.138 joris 768: } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
769: if (cf->file_rcs == NULL || rcsdead == 1) {
770: if (cf->fd == -1) {
1.143 joris 771: if (verbose)
1.138 joris 772: cvs_log(LP_NOTICE,
773: "warning: %s's entry exists but"
774: " there is no longer a file"
775: " in the repository,"
776: " removing entry",
777: cf->file_path);
778: cf->file_status = FILE_REMOVE_ENTRY;
779: } else {
780: if (ismodified) {
1.143 joris 781: if (verbose)
1.138 joris 782: cvs_log(LP_NOTICE,
783: "conflict: %s is no longer "
784: "in the repository but is "
785: "locally modified",
786: cf->file_path);
787: cf->file_status = FILE_CONFLICT;
788: } else {
1.143 joris 789: if (verbose)
1.138 joris 790: cvs_log(LP_NOTICE,
791: "%s is no longer in the "
792: "repository",
793: cf->file_path);
1.128 joris 794:
1.138 joris 795: cf->file_status = FILE_UNLINK;
796: }
797: }
1.128 joris 798: } else {
1.138 joris 799: if (cf->fd == -1) {
1.143 joris 800: if (verbose)
1.138 joris 801: cvs_log(LP_NOTICE,
802: "warning: %s was lost",
803: cf->file_path);
804: cf->file_status = FILE_LOST;
805: } else {
806: if (ismodified == 1)
807: cf->file_status = FILE_MODIFIED;
808: else
809: cf->file_status = FILE_UPTODATE;
810:
811: if (strcmp(r1, r2)) {
812: if (cf->file_status == FILE_MODIFIED)
813: cf->file_status = FILE_MERGE;
814: else
815: cf->file_status = FILE_PATCH;
816: }
817: }
1.128 joris 818: }
819: }
820:
1.138 joris 821: xfree(repo);
822: xfree(rcsfile);
823: cvs_ent_close(entlist, ENT_NOSYNC);
824: }
1.14 jfb 825:
1.138 joris 826: void
827: cvs_file_free(struct cvs_file *cf)
828: {
829: xfree(cf->file_name);
830: xfree(cf->file_wd);
831: xfree(cf->file_path);
832:
833: if (cf->file_rpath != NULL)
834: xfree(cf->file_rpath);
835: if (cf->file_ent != NULL)
836: cvs_ent_free(cf->file_ent);
837: if (cf->file_rcs != NULL)
838: rcs_close(cf->file_rcs);
839: if (cf->fd != -1)
840: (void)close(cf->fd);
841: if (cf->repo_fd != -1)
842: (void)close(cf->repo_fd);
843: xfree(cf);
1.23 jfb 844: }
845:
846: static int
847: cvs_file_cmpname(const char *name1, const char *name2)
848: {
849: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
850: (strcasecmp(name1, name2));
1.164 ! xsa 851: }
! 852:
! 853: int
! 854: cvs_file_cmp(const char *file1, const char *file2)
! 855: {
! 856: struct stat stb1, stb2;
! 857: int fd1, fd2, ret;
! 858:
! 859: ret = 0;
! 860:
! 861: if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
! 862: fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
! 863: if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
! 864: fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
! 865:
! 866: if (fstat(fd1, &stb1) == -1)
! 867: fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
! 868: if (fstat(fd2, &stb2) == -1)
! 869: fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
! 870:
! 871: if (stb1.st_size != stb2.st_size ||
! 872: (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
! 873: ret = 1;
! 874: goto out;
! 875: }
! 876:
! 877: if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
! 878: if (stb1.st_rdev != stb2.st_rdev)
! 879: ret = 1;
! 880: goto out;
! 881: }
! 882:
! 883: if (S_ISREG(stb1.st_mode)) {
! 884: void *p1, *p2;
! 885:
! 886: if (stb1.st_size > SIZE_MAX) {
! 887: ret = 1;
! 888: goto out;
! 889: }
! 890:
! 891: if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
! 892: MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
! 893: fatal("cvs_file_cmp: mmap failed");
! 894:
! 895: if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
! 896: MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
! 897: fatal("cvs_file_cmp: mmap failed");
! 898:
! 899: madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
! 900: madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
! 901:
! 902: ret = memcmp(p1, p2, stb1.st_size);
! 903:
! 904: (void)munmap(p1, stb1.st_size);
! 905: (void)munmap(p2, stb1.st_size);
! 906: }
! 907:
! 908: out:
! 909: (void)close(fd1);
! 910: (void)close(fd2);
! 911:
! 912: return (ret);
1.14 jfb 913: }