Annotation of src/usr.bin/cvs/file.c, Revision 1.196
1.196 ! tobias 1: /* $OpenBSD: file.c,v 1.195 2007/09/07 23:30:30 tobias 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.189 otto 28: #include <sys/types.h>
29: #include <sys/mman.h>
30: #include <sys/stat.h>
1.1 jfb 31:
1.189 otto 32: #include <errno.h>
33: #include <fcntl.h>
34: #include <fnmatch.h>
35: #include <libgen.h>
36: #include <string.h>
37: #include <unistd.h>
1.164 xsa 38:
1.1 jfb 39: #include "cvs.h"
40:
1.104 xsa 41: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
1.1 jfb 42:
1.104 xsa 43: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
1.1 jfb 44:
1.196 ! tobias 45: extern int print_stdout;
! 46:
1.1 jfb 47: /*
48: * Standard patterns to ignore.
49: */
50: static const char *cvs_ign_std[] = {
51: ".",
52: "..",
53: "*.o",
1.45 xsa 54: "*.a",
1.1 jfb 55: "*.bak",
56: "*.orig",
57: "*.rej",
1.45 xsa 58: "*.old",
1.1 jfb 59: "*.exe",
60: "*.depend",
1.45 xsa 61: "*.obj",
62: "*.elc",
63: "*.ln",
64: "*.olb",
1.1 jfb 65: "CVS",
66: "core",
1.102 joris 67: "cvslog*",
1.49 jfb 68: "*.core",
1.8 jfb 69: ".#*",
1.45 xsa 70: "*~",
71: "_$*",
72: "*$",
1.1 jfb 73: };
74:
1.138 joris 75: struct ignore_head cvs_ign_pats;
76: struct ignore_head dir_ign_pats;
1.1 jfb 77:
1.138 joris 78: void
1.1 jfb 79: cvs_file_init(void)
80: {
1.183 xsa 81: int i;
1.138 joris 82: FILE *ifp;
1.1 jfb 83: size_t len;
1.176 otto 84: char path[MAXPATHLEN], buf[MAXNAMLEN];
1.1 jfb 85:
86: TAILQ_INIT(&cvs_ign_pats);
1.138 joris 87: TAILQ_INIT(&dir_ign_pats);
1.4 jfb 88:
1.1 jfb 89: /* standard patterns to ignore */
1.10 jfb 90: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.138 joris 91: cvs_file_ignore(cvs_ign_std[i], &cvs_ign_pats);
1.195 tobias 92:
93: if (cvs_homedir == NULL)
94: return;
1.1 jfb 95:
96: /* read the cvsignore file in the user's home directory, if any */
1.183 xsa 97: (void)xsnprintf(path, MAXPATHLEN, "%s/.cvsignore", cvs_homedir);
1.61 xsa 98:
1.111 xsa 99: ifp = fopen(path, "r");
100: if (ifp == NULL) {
101: if (errno != ENOENT)
102: cvs_log(LP_ERRNO,
103: "failed to open user's cvsignore file `%s'", path);
104: } else {
1.138 joris 105: while (fgets(buf, MAXNAMLEN, ifp) != NULL) {
1.111 xsa 106: len = strlen(buf);
107: if (len == 0)
108: continue;
1.163 moritz 109: if (buf[len - 1] == '\n')
110: buf[len - 1] = '\0';
1.138 joris 111:
112: cvs_file_ignore(buf, &cvs_ign_pats);
1.1 jfb 113: }
1.138 joris 114:
1.111 xsa 115: (void)fclose(ifp);
1.1 jfb 116: }
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: struct cvs_file *cf;
1.176 otto 211: char *p, rpath[MAXPATHLEN];
1.3 jfb 212:
1.183 xsa 213: (void)xsnprintf(rpath, MAXPATHLEN, "%s/%s", d, f);
1.138 joris 214:
215: for (p = rpath; p[0] == '.' && p[1] == '/';)
216: p += 2;
217:
218: cf = (struct cvs_file *)xmalloc(sizeof(*cf));
219: memset(cf, 0, sizeof(*cf));
220:
221: cf->file_name = xstrdup(f);
222: cf->file_wd = xstrdup(d);
223: cf->file_path = xstrdup(p);
224: cf->fd = fd;
225: cf->repo_fd = -1;
226: cf->file_type = type;
227: cf->file_status = cf->file_flags = 0;
228: cf->file_ent = NULL;
1.68 joris 229:
1.138 joris 230: return (cf);
1.9 jfb 231: }
232:
1.138 joris 233: void
234: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9 jfb 235: {
1.183 xsa 236: int fd, type;
1.138 joris 237: struct stat st;
238: struct cvs_file *cf;
239: struct cvs_filelist *l, *nxt;
1.176 otto 240: char *d, *f, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.138 joris 241:
242: for (l = TAILQ_FIRST(fl); l != NULL; l = nxt) {
243: if (cvs_quit)
244: fatal("received signal %d", sig_received);
245:
246: cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
247: l->file_path);
248:
249: if ((f = basename(l->file_path)) == NULL)
250: fatal("cvs_file_walklist: basename failed");
251: if ((d = dirname(l->file_path)) == NULL)
252: fatal("cvs_file_walklist: dirname failed");
253:
1.146 joris 254: type = CVS_FILE;
1.138 joris 255: if ((fd = open(l->file_path, O_RDONLY)) != -1) {
256: if (fstat(fd, &st) == -1) {
257: cvs_log(LP_ERRNO, "%s", l->file_path);
258: (void)close(fd);
259: goto next;
260: }
1.26 jfb 261:
1.138 joris 262: if (S_ISDIR(st.st_mode))
263: type = CVS_DIR;
264: else if (S_ISREG(st.st_mode))
265: type = CVS_FILE;
266: else {
267: cvs_log(LP_ERR,
268: "ignoring bad file type for %s",
269: l->file_path);
270: (void)close(fd);
271: goto next;
272: }
1.162 joris 273: } else if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.196 ! tobias 274: /*
! 275: * During checkout -p, do not use any locally
! 276: * available directories.
! 277: */
! 278: if (cvs_cmdop != CVS_OP_CHECKOUT || !print_stdout)
! 279: if (stat(d, &st) == -1) {
! 280: cvs_log(LP_ERRNO, "%s", d);
! 281: goto next;
! 282: }
1.87 joris 283:
1.146 joris 284: cvs_get_repository_path(d, repo, MAXPATHLEN);
1.183 xsa 285: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
1.138 joris 286: repo, f);
287:
288: if ((fd = open(fpath, O_RDONLY)) == -1) {
289: strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
290: fd = open(fpath, O_RDONLY);
291: }
1.26 jfb 292:
1.138 joris 293: if (fd != -1) {
294: if (fstat(fd, &st) == -1)
295: fatal("cvs_file_walklist: %s: %s",
296: fpath, strerror(errno));
297:
298: if (S_ISDIR(st.st_mode))
299: type = CVS_DIR;
300: else if (S_ISREG(st.st_mode))
301: type = CVS_FILE;
302: else {
303: cvs_log(LP_ERR,
304: "ignoring bad file type for %s",
305: l->file_path);
306: (void)close(fd);
307: goto next;
308: }
309:
310: /* this file is not in our working copy yet */
311: (void)close(fd);
312: fd = -1;
313: }
314: }
1.73 joris 315:
1.138 joris 316: cf = cvs_file_get_cf(d, f, fd, type);
317: if (cf->file_type == CVS_DIR) {
318: cvs_file_walkdir(cf, cr);
319: } else {
1.161 joris 320: if (cr->fileproc != NULL)
321: cr->fileproc(cf);
1.128 joris 322: }
1.100 joris 323:
1.138 joris 324: cvs_file_free(cf);
1.123 joris 325:
1.138 joris 326: next:
327: nxt = TAILQ_NEXT(l, flist);
328: TAILQ_REMOVE(fl, l, flist);
1.68 joris 329:
1.138 joris 330: xfree(l->file_path);
331: xfree(l);
1.100 joris 332: }
333: }
334:
1.138 joris 335: void
336: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100 joris 337: {
1.171 joris 338: int l, type;
1.138 joris 339: FILE *fp;
340: int nbytes;
341: size_t len;
342: long base;
343: size_t bufsize;
344: struct stat st;
345: struct dirent *dp;
1.100 joris 346: struct cvs_ent *ent;
1.138 joris 347: struct cvs_ignpat *ip;
348: struct cvs_ent_line *line;
349: struct cvs_flisthead fl, dl;
350: CVSENTRIES *entlist;
1.194 joris 351: char *buf, *ebuf, *cp, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.100 joris 352:
1.138 joris 353: cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100 joris 354:
1.138 joris 355: if (cr->enterdir != NULL)
356: cr->enterdir(cf);
1.100 joris 357:
1.161 joris 358: if (cr->fileproc != NULL)
359: cr->fileproc(cf);
1.100 joris 360:
1.146 joris 361: if (cf->file_status == FILE_SKIP)
362: return;
363:
1.100 joris 364: /*
1.151 joris 365: * If we do not have a admin directory inside here, dont bother,
366: * unless we are running import.
1.100 joris 367: */
1.183 xsa 368: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138 joris 369: CVS_PATH_CVSDIR);
1.100 joris 370:
1.146 joris 371: l = stat(fpath, &st);
1.192 niallo 372: if (cvs_cmdop != CVS_OP_IMPORT && cvs_cmdop != CVS_OP_RLOG &&
1.151 joris 373: (l == -1 || (l == 0 && !S_ISDIR(st.st_mode)))) {
1.138 joris 374: return;
1.100 joris 375: }
376:
377: /*
1.138 joris 378: * check for a local .cvsignore file
1.100 joris 379: */
1.183 xsa 380: (void)xsnprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
1.100 joris 381:
1.138 joris 382: if ((fp = fopen(fpath, "r")) != NULL) {
1.163 moritz 383: while (fgets(fpath, MAXPATHLEN, fp) != NULL) {
1.138 joris 384: len = strlen(fpath);
1.163 moritz 385: if (len == 0)
386: continue;
1.138 joris 387: if (fpath[len - 1] == '\n')
388: fpath[len - 1] = '\0';
1.26 jfb 389:
1.138 joris 390: cvs_file_ignore(fpath, &dir_ign_pats);
1.68 joris 391: }
1.26 jfb 392:
1.138 joris 393: (void)fclose(fp);
1.100 joris 394: }
395:
1.138 joris 396: if (fstat(cf->fd, &st) == -1)
397: fatal("cvs_file_walkdir: %s %s", cf->file_path,
398: strerror(errno));
1.100 joris 399:
1.138 joris 400: bufsize = st.st_size;
401: if (bufsize < st.st_blksize)
402: bufsize = st.st_blksize;
1.116 joris 403:
1.138 joris 404: buf = xmalloc(bufsize);
405: TAILQ_INIT(&fl);
406: TAILQ_INIT(&dl);
1.87 joris 407:
1.138 joris 408: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
409: ebuf = buf + nbytes;
410: cp = buf;
1.26 jfb 411:
1.138 joris 412: while (cp < ebuf) {
413: dp = (struct dirent *)cp;
414: if (!strcmp(dp->d_name, ".") ||
415: !strcmp(dp->d_name, "..") ||
416: !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
1.186 joris 417: dp->d_fileno == 0) {
1.138 joris 418: cp += dp->d_reclen;
419: continue;
420: }
1.13 jfb 421:
1.138 joris 422: if (cvs_file_chkign(dp->d_name)) {
423: cp += dp->d_reclen;
1.13 jfb 424: continue;
1.138 joris 425: }
1.13 jfb 426:
1.192 niallo 427: len = xsnprintf(fpath, MAXPATHLEN, "%s/%s",
1.138 joris 428: cf->file_path, dp->d_name);
1.13 jfb 429:
1.138 joris 430: /*
1.171 joris 431: * nfs and afs will show d_type as DT_UNKNOWN
432: * for files and/or directories so when we encounter
1.184 todd 433: * this we call lstat() on the path to be sure.
1.138 joris 434: */
1.171 joris 435: if (dp->d_type == DT_UNKNOWN) {
1.184 todd 436: if (lstat(fpath, &st) == -1)
1.171 joris 437: fatal("'%s': %s", fpath,
438: strerror(errno));
439:
440: switch (st.st_mode & S_IFMT) {
441: case S_IFDIR:
442: type = CVS_DIR;
443: break;
444: case S_IFREG:
445: type = CVS_FILE;
446: break;
447: default:
1.175 ray 448: type = FILE_SKIP;
449: break;
1.171 joris 450: }
451: } else {
452: switch (dp->d_type) {
453: case DT_DIR:
454: type = CVS_DIR;
455: break;
456: case DT_REG:
457: type = CVS_FILE;
458: break;
459: default:
1.175 ray 460: type = FILE_SKIP;
461: break;
1.171 joris 462: }
1.175 ray 463: }
464:
465: if (type == FILE_SKIP) {
466: if (verbosity > 1) {
467: cvs_log(LP_NOTICE, "ignoring `%s'",
468: dp->d_name);
469: }
470: cp += dp->d_reclen;
471: continue;
1.171 joris 472: }
473:
474: switch (type) {
475: case CVS_DIR:
1.192 niallo 476: if (cr->flags & CR_RECURSE_DIRS)
477: cvs_file_get(fpath, &dl);
1.171 joris 478: break;
479: case CVS_FILE:
1.138 joris 480: cvs_file_get(fpath, &fl);
1.171 joris 481: break;
482: default:
483: fatal("type %d unknown, shouldn't happen",
484: type);
485: }
1.13 jfb 486:
1.138 joris 487: cp += dp->d_reclen;
488: }
1.34 jfb 489: }
490:
1.138 joris 491: if (nbytes == -1)
492: fatal("cvs_file_walkdir: %s %s", cf->file_path,
493: strerror(errno));
1.22 jfb 494:
1.138 joris 495: xfree(buf);
1.22 jfb 496:
1.138 joris 497: while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
498: TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
499: xfree(ip);
1.91 joris 500: }
1.114 xsa 501:
1.138 joris 502: entlist = cvs_ent_open(cf->file_path);
503: TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
504: ent = cvs_ent_parse(line->buf);
1.61 xsa 505:
1.183 xsa 506: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138 joris 507: ent->ce_name);
1.100 joris 508:
1.140 joris 509: if (!(cr->flags & CR_RECURSE_DIRS) &&
510: ent->ce_type == CVS_ENT_DIR)
511: continue;
1.138 joris 512: if (ent->ce_type == CVS_ENT_DIR)
513: cvs_file_get(fpath, &dl);
514: else if (ent->ce_type == CVS_ENT_FILE)
515: cvs_file_get(fpath, &fl);
1.68 joris 516:
1.138 joris 517: cvs_ent_free(ent);
1.128 joris 518: }
519:
1.138 joris 520: cvs_ent_close(entlist, ENT_NOSYNC);
1.91 joris 521:
1.140 joris 522: if (cr->flags & CR_REPO) {
1.146 joris 523: cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.140 joris 524: cvs_repository_lock(repo);
1.68 joris 525:
1.140 joris 526: cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
527: (cr->flags & CR_RECURSE_DIRS));
528: }
1.68 joris 529:
1.138 joris 530: cvs_file_walklist(&fl, cr);
531: cvs_file_freelist(&fl);
1.68 joris 532:
1.176 otto 533: if (cr->flags & CR_REPO)
1.140 joris 534: cvs_repository_unlock(repo);
1.68 joris 535:
1.138 joris 536: cvs_file_walklist(&dl, cr);
537: cvs_file_freelist(&dl);
1.34 jfb 538:
1.138 joris 539: if (cr->leavedir != NULL)
540: cr->leavedir(cf);
1.3 jfb 541: }
542:
543: void
1.138 joris 544: cvs_file_freelist(struct cvs_flisthead *fl)
1.3 jfb 545: {
1.138 joris 546: struct cvs_filelist *f;
1.62 jfb 547:
1.138 joris 548: while ((f = TAILQ_FIRST(fl)) != NULL) {
549: TAILQ_REMOVE(fl, f, flist);
550: xfree(f->file_path);
551: xfree(f);
1.62 jfb 552: }
1.5 jfb 553: }
554:
1.138 joris 555: void
1.185 joris 556: cvs_file_classify(struct cvs_file *cf, const char *tag)
1.3 jfb 557: {
1.138 joris 558: size_t len;
559: struct stat st;
1.149 joris 560: BUF *b1, *b2;
1.183 xsa 561: int rflags, ismodified, rcsdead;
1.138 joris 562: CVSENTRIES *entlist = NULL;
563: const char *state;
1.193 xsa 564: char repo[MAXPATHLEN], rcsfile[MAXPATHLEN];
1.194 joris 565: char r1[CVS_REV_BUFSZ], r2[CVS_REV_BUFSZ];
1.138 joris 566:
567: cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
568:
569: if (!strcmp(cf->file_path, ".")) {
570: cf->file_status = FILE_UPTODATE;
571: return;
572: }
573:
1.146 joris 574: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.183 xsa 575: (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s",
1.138 joris 576: repo, cf->file_name);
577:
578: if (cf->file_type == CVS_FILE) {
579: len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
580: if (len >= MAXPATHLEN)
581: fatal("cvs_file_classify: truncation");
582: }
583:
584: cf->file_rpath = xstrdup(rcsfile);
1.151 joris 585: entlist = cvs_ent_open(cf->file_wd);
1.138 joris 586: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
587:
1.144 joris 588: if (cf->file_ent != NULL) {
589: if (cf->file_ent->ce_type == CVS_ENT_DIR &&
590: cf->file_type != CVS_DIR)
1.148 pedro 591: fatal("%s is supposed to be a directory, but it is not",
1.144 joris 592: cf->file_path);
593: if (cf->file_ent->ce_type == CVS_ENT_FILE &&
594: cf->file_type != CVS_FILE)
1.148 pedro 595: fatal("%s is supposed to be a file, but it is not",
1.144 joris 596: cf->file_path);
597: }
598:
1.138 joris 599: if (cf->file_type == CVS_DIR) {
600: if (cf->fd == -1 && stat(rcsfile, &st) != -1)
601: cf->file_status = DIR_CREATE;
1.192 niallo 602: else if (cf->file_ent != NULL || cvs_cmdop == CVS_OP_RLOG)
1.138 joris 603: cf->file_status = FILE_UPTODATE;
1.144 joris 604: else
605: cf->file_status = FILE_UNKNOWN;
606:
1.138 joris 607: cvs_ent_close(entlist, ENT_NOSYNC);
608: return;
609: }
610:
1.147 joris 611: rflags = RCS_READ;
612: switch (cvs_cmdop) {
613: case CVS_OP_COMMIT:
614: rflags = RCS_WRITE;
615: break;
1.156 joris 616: case CVS_OP_IMPORT:
1.147 joris 617: case CVS_OP_LOG:
1.192 niallo 618: case CVS_OP_RLOG:
1.147 joris 619: rflags |= RCS_PARSE_FULLY;
620: break;
621: }
622:
1.138 joris 623: cf->repo_fd = open(cf->file_rpath, O_RDONLY);
624: if (cf->repo_fd != -1) {
625: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
626: if (cf->file_rcs == NULL)
1.152 joris 627: fatal("cvs_file_classify: failed to parse RCS");
1.194 joris 628: } else {
629: (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s/%s%s",
630: repo, CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
631:
632: cf->repo_fd = open(rcsfile, O_RDONLY);
633: if (cf->repo_fd != -1) {
634: xfree(cf->file_rpath);
635: cf->file_rpath = xstrdup(rcsfile);
636: cf->file_rcs = rcs_open(cf->file_rpath,
637: cf->repo_fd, rflags);
638: if (cf->file_rcs == NULL)
639: fatal("cvs_file_classify: failed to parse RCS");
640: cf->in_attic = 1;
641: } else {
642: cf->file_rcs = NULL;
643: }
1.190 niallo 644: }
645:
646: if (tag != NULL && cf->file_rcs != NULL) {
647: /* if we could not translate tag, means that we should
648: * skip this file. */
649: if ((cf->file_rcsrev = rcs_translate_tag(tag, cf->file_rcs)) == NULL) {
650: cf->file_status = FILE_SKIP;
651: cvs_ent_close(entlist, ENT_NOSYNC);
652: return;
1.152 joris 653: }
1.3 jfb 654:
1.190 niallo 655: rcsnum_tostr(cf->file_rcsrev, r1, sizeof(r1));
656:
1.172 niallo 657: } else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL) {
1.169 joris 658: cf->file_rcsrev = rcsnum_alloc();
659: rcsnum_cpy(cf->file_ent->ce_rev, cf->file_rcsrev, 0);
1.190 niallo 660: } else if (cf->file_rcs != NULL) {
1.156 joris 661: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.190 niallo 662: } else {
1.153 joris 663: cf->file_rcsrev = NULL;
1.190 niallo 664: }
1.153 joris 665:
1.138 joris 666: if (cf->file_ent != NULL)
667: rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
1.190 niallo 668: if (cf->file_rcsrev != NULL) {
1.153 joris 669: rcsnum_tostr(cf->file_rcsrev, r2, sizeof(r2));
1.190 niallo 670: }
1.6 jfb 671:
1.138 joris 672: ismodified = rcsdead = 0;
673: if (cf->fd != -1 && cf->file_ent != NULL) {
674: if (fstat(cf->fd, &st) == -1)
675: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 676:
1.177 joris 677: if (st.st_mtime != cf->file_ent->ce_mtime)
1.138 joris 678: ismodified = 1;
1.149 joris 679: }
680:
681: if (ismodified == 1 && cf->fd != -1 && cf->file_rcs != NULL) {
1.170 joris 682: b1 = rcs_rev_getbuf(cf->file_rcs, cf->file_rcsrev, 0);
1.149 joris 683: if (b1 == NULL)
684: fatal("failed to get HEAD revision for comparison");
685:
1.159 joris 686: b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT);
1.149 joris 687: if (b2 == NULL)
688: fatal("failed to get file content for comparison");
689:
690: if (cvs_buf_differ(b1, b2))
691: ismodified = 1;
692: else
693: ismodified = 0;
1.188 ray 694: cvs_buf_free(b1);
695: cvs_buf_free(b2);
1.62 jfb 696: }
697:
1.173 joris 698: if (cf->file_rcs != NULL && cf->file_rcsrev != NULL) {
1.153 joris 699: state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138 joris 700: if (state == NULL)
701: fatal("failed to get state for HEAD for %s",
702: cf->file_path);
1.139 joris 703: if (!strcmp(state, RCS_STATE_DEAD))
1.138 joris 704: rcsdead = 1;
1.145 joris 705:
706: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 707: }
708:
1.138 joris 709: /*
710: * 10 Sin
711: * 20 Goto hell
712: * (I welcome you if-else hell)
713: */
714: if (cf->file_ent == NULL) {
715: if (cf->file_rcs == NULL) {
716: if (cf->fd == -1) {
1.178 joris 717: cvs_log(LP_NOTICE,
718: "nothing known about '%s'",
719: cf->file_path);
1.138 joris 720: }
1.130 joris 721:
1.138 joris 722: cf->file_status = FILE_UNKNOWN;
723: } else if (rcsdead == 1) {
724: if (cf->fd == -1) {
725: cf->file_status = FILE_UPTODATE;
1.179 joris 726: } else if (cvs_cmdop != CVS_OP_ADD) {
1.138 joris 727: cf->file_status = FILE_UNKNOWN;
1.44 jfb 728: }
1.138 joris 729: } else {
730: cf->file_status = FILE_CHECKOUT;
1.14 jfb 731: }
1.138 joris 732: } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
733: if (cf->fd == -1) {
1.178 joris 734: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 735: cvs_log(LP_NOTICE,
1.157 david 736: "warning: new-born %s has disappeared",
1.138 joris 737: cf->file_path);
1.178 joris 738: }
1.138 joris 739: cf->file_status = FILE_REMOVE_ENTRY;
740: } else if (cf->file_rcs == NULL || rcsdead == 1) {
741: cf->file_status = FILE_ADDED;
742: } else {
1.178 joris 743: cvs_log(LP_NOTICE,
744: "conflict: %s already created by others",
745: cf->file_path);
1.138 joris 746: cf->file_status = FILE_CONFLICT;
747: }
748: } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
749: if (cf->fd != -1) {
1.178 joris 750: cvs_log(LP_NOTICE,
751: "%s should be removed but is still there",
752: cf->file_path);
1.138 joris 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.178 joris 758: cvs_log(LP_NOTICE,
759: "conflict: removed %s was modified"
760: " by a second party",
761: cf->file_path);
1.138 joris 762: cf->file_status = FILE_CONFLICT;
763: } else {
764: cf->file_status = FILE_REMOVED;
765: }
1.66 joris 766: }
1.138 joris 767: } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
1.194 joris 768: if (cf->file_rcs == NULL || rcsdead == 1 ||
769: (reset_stickies == 1 && cf->in_attic == 1)) {
1.138 joris 770: if (cf->fd == -1) {
1.178 joris 771: cvs_log(LP_NOTICE,
772: "warning: %s's entry exists but"
773: " there is no longer a file"
774: " in the repository,"
775: " removing entry",
776: cf->file_path);
1.138 joris 777: cf->file_status = FILE_REMOVE_ENTRY;
778: } else {
779: if (ismodified) {
1.178 joris 780: cvs_log(LP_NOTICE,
781: "conflict: %s is no longer "
782: "in the repository but is "
783: "locally modified",
784: cf->file_path);
1.138 joris 785: cf->file_status = FILE_CONFLICT;
786: } else {
1.178 joris 787: cvs_log(LP_NOTICE,
788: "%s is no longer in the "
789: "repository",
790: cf->file_path);
1.128 joris 791:
1.138 joris 792: cf->file_status = FILE_UNLINK;
793: }
794: }
1.128 joris 795: } else {
1.138 joris 796: if (cf->fd == -1) {
1.178 joris 797: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 798: cvs_log(LP_NOTICE,
799: "warning: %s was lost",
800: cf->file_path);
1.178 joris 801: }
1.138 joris 802: cf->file_status = FILE_LOST;
803: } else {
804: if (ismodified == 1)
805: cf->file_status = FILE_MODIFIED;
806: else
807: cf->file_status = FILE_UPTODATE;
808:
809: if (strcmp(r1, r2)) {
810: if (cf->file_status == FILE_MODIFIED)
811: cf->file_status = FILE_MERGE;
812: else
813: cf->file_status = FILE_PATCH;
814: }
815: }
1.128 joris 816: }
817: }
818:
1.138 joris 819: cvs_ent_close(entlist, ENT_NOSYNC);
820: }
1.14 jfb 821:
1.138 joris 822: void
823: cvs_file_free(struct cvs_file *cf)
824: {
825: xfree(cf->file_name);
826: xfree(cf->file_wd);
827: xfree(cf->file_path);
828:
1.167 joris 829: if (cf->file_rcsrev != NULL)
830: rcsnum_free(cf->file_rcsrev);
1.138 joris 831: if (cf->file_rpath != NULL)
832: xfree(cf->file_rpath);
833: if (cf->file_ent != NULL)
834: cvs_ent_free(cf->file_ent);
835: if (cf->file_rcs != NULL)
836: rcs_close(cf->file_rcs);
837: if (cf->fd != -1)
838: (void)close(cf->fd);
839: if (cf->repo_fd != -1)
840: (void)close(cf->repo_fd);
841: xfree(cf);
1.23 jfb 842: }
843:
1.165 xsa 844: int
1.23 jfb 845: cvs_file_cmpname(const char *name1, const char *name2)
846: {
847: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
848: (strcasecmp(name1, name2));
1.164 xsa 849: }
850:
851: int
852: cvs_file_cmp(const char *file1, const char *file2)
853: {
854: struct stat stb1, stb2;
855: int fd1, fd2, ret;
856:
857: ret = 0;
858:
859: if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
860: fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
861: if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
862: fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
863:
864: if (fstat(fd1, &stb1) == -1)
865: fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
866: if (fstat(fd2, &stb2) == -1)
867: fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
868:
869: if (stb1.st_size != stb2.st_size ||
870: (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
871: ret = 1;
872: goto out;
873: }
874:
875: if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
876: if (stb1.st_rdev != stb2.st_rdev)
877: ret = 1;
878: goto out;
879: }
880:
881: if (S_ISREG(stb1.st_mode)) {
882: void *p1, *p2;
883:
1.187 otto 884: if (stb1.st_size > (off_t)SIZE_MAX) {
1.164 xsa 885: ret = 1;
886: goto out;
887: }
888:
889: if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
890: MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
891: fatal("cvs_file_cmp: mmap failed");
892:
893: if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
894: MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
895: fatal("cvs_file_cmp: mmap failed");
896:
897: madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
898: madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
899:
900: ret = memcmp(p1, p2, stb1.st_size);
901:
902: (void)munmap(p1, stb1.st_size);
903: (void)munmap(p2, stb1.st_size);
904: }
905:
906: out:
907: (void)close(fd1);
908: (void)close(fd2);
1.166 xsa 909:
910: return (ret);
911: }
912:
913: int
914: cvs_file_copy(const char *from, const char *to)
915: {
916: struct stat st;
917: struct timeval tv[2];
918: time_t atime, mtime;
919: int src, dst, ret;
920:
921: ret = 0;
922:
923: cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
924:
925: if (cvs_noexec == 1)
926: return (0);
927:
928: if ((src = open(from, O_RDONLY, 0)) == -1)
929: fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
930:
931: if (fstat(src, &st) == -1)
932: fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
933:
934: atime = st.st_atimespec.tv_sec;
935: mtime = st.st_mtimespec.tv_sec;
936:
937: if (S_ISREG(st.st_mode)) {
938: size_t sz;
939: ssize_t nw;
940: char *p, *buf;
941: int saved_errno;
942:
1.187 otto 943: if (st.st_size > (off_t)SIZE_MAX) {
1.166 xsa 944: ret = -1;
945: goto out;
946: }
947:
948: if ((dst = open(to, O_CREAT|O_TRUNC|O_WRONLY,
949: st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) == -1)
950: fatal("cvs_file_copy: open `%s': %s",
951: to, strerror(errno));
952:
953: if ((p = mmap(NULL, st.st_size, PROT_READ,
954: MAP_FILE, src, (off_t)0)) == MAP_FAILED) {
955: saved_errno = errno;
956: (void)unlink(to);
957: fatal("cvs_file_copy: mmap: %s", strerror(saved_errno));
958: }
959:
960: madvise(p, st.st_size, MADV_SEQUENTIAL);
961:
962: sz = st.st_size;
963: buf = p;
964:
965: while (sz > 0) {
966: if ((nw = write(dst, p, sz)) == -1) {
967: saved_errno = errno;
968: (void)unlink(to);
969: fatal("cvs_file_copy: `%s': %s",
970: from, strerror(saved_errno));
971: }
972: buf += nw;
973: sz -= nw;
974: }
975:
976: (void)munmap(p, st.st_size);
977:
978: tv[0].tv_sec = atime;
979: tv[1].tv_sec = mtime;
980:
981: if (futimes(dst, tv) == -1) {
982: saved_errno = errno;
983: (void)unlink(to);
984: fatal("cvs_file_copy: futimes: %s",
985: strerror(saved_errno));
986: }
987: (void)close(dst);
988: }
989: out:
990: (void)close(src);
1.164 xsa 991:
992: return (ret);
1.14 jfb 993: }