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