Annotation of src/usr.bin/cvs/file.c, Revision 1.185
1.185 ! joris 1: /* $OpenBSD: file.c,v 1.184 2007/02/07 23:47:56 todd 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) ||
402: dp->d_reclen == 0) {
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: /* b1 and b2 get released in cvs_buf_differ */
673: if (cvs_buf_differ(b1, b2))
674: ismodified = 1;
675: else
676: ismodified = 0;
1.62 jfb 677: }
678:
1.173 joris 679: if (cf->file_rcs != NULL && cf->file_rcsrev != NULL) {
1.153 joris 680: state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138 joris 681: if (state == NULL)
682: fatal("failed to get state for HEAD for %s",
683: cf->file_path);
1.139 joris 684: if (!strcmp(state, RCS_STATE_DEAD))
1.138 joris 685: rcsdead = 1;
1.145 joris 686:
687: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 688: }
689:
1.138 joris 690: /*
691: * 10 Sin
692: * 20 Goto hell
693: * (I welcome you if-else hell)
694: */
695: if (cf->file_ent == NULL) {
696: if (cf->file_rcs == NULL) {
697: if (cf->fd == -1) {
1.178 joris 698: cvs_log(LP_NOTICE,
699: "nothing known about '%s'",
700: cf->file_path);
1.138 joris 701: }
1.130 joris 702:
1.138 joris 703: cf->file_status = FILE_UNKNOWN;
704: } else if (rcsdead == 1) {
705: if (cf->fd == -1) {
706: cf->file_status = FILE_UPTODATE;
1.179 joris 707: } else if (cvs_cmdop != CVS_OP_ADD) {
1.138 joris 708: cf->file_status = FILE_UNKNOWN;
1.44 jfb 709: }
1.138 joris 710: } else {
711: cf->file_status = FILE_CHECKOUT;
1.14 jfb 712: }
1.138 joris 713: } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
714: if (cf->fd == -1) {
1.178 joris 715: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 716: cvs_log(LP_NOTICE,
1.157 david 717: "warning: new-born %s has disappeared",
1.138 joris 718: cf->file_path);
1.178 joris 719: }
1.138 joris 720: cf->file_status = FILE_REMOVE_ENTRY;
721: } else if (cf->file_rcs == NULL || rcsdead == 1) {
722: cf->file_status = FILE_ADDED;
723: } else {
1.178 joris 724: cvs_log(LP_NOTICE,
725: "conflict: %s already created by others",
726: cf->file_path);
1.138 joris 727: cf->file_status = FILE_CONFLICT;
728: }
729: } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
730: if (cf->fd != -1) {
1.178 joris 731: cvs_log(LP_NOTICE,
732: "%s should be removed but is still there",
733: cf->file_path);
1.138 joris 734: cf->file_status = FILE_REMOVED;
735: } else if (cf->file_rcs == NULL || rcsdead == 1) {
736: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 737: } else {
1.138 joris 738: if (strcmp(r1, r2)) {
1.178 joris 739: cvs_log(LP_NOTICE,
740: "conflict: removed %s was modified"
741: " by a second party",
742: cf->file_path);
1.138 joris 743: cf->file_status = FILE_CONFLICT;
744: } else {
745: cf->file_status = FILE_REMOVED;
746: }
1.66 joris 747: }
1.138 joris 748: } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
749: if (cf->file_rcs == NULL || rcsdead == 1) {
750: if (cf->fd == -1) {
1.178 joris 751: cvs_log(LP_NOTICE,
752: "warning: %s's entry exists but"
753: " there is no longer a file"
754: " in the repository,"
755: " removing entry",
756: cf->file_path);
1.138 joris 757: cf->file_status = FILE_REMOVE_ENTRY;
758: } else {
759: if (ismodified) {
1.178 joris 760: cvs_log(LP_NOTICE,
761: "conflict: %s is no longer "
762: "in the repository but is "
763: "locally modified",
764: cf->file_path);
1.138 joris 765: cf->file_status = FILE_CONFLICT;
766: } else {
1.178 joris 767: cvs_log(LP_NOTICE,
768: "%s is no longer in the "
769: "repository",
770: cf->file_path);
1.128 joris 771:
1.138 joris 772: cf->file_status = FILE_UNLINK;
773: }
774: }
1.128 joris 775: } else {
1.138 joris 776: if (cf->fd == -1) {
1.178 joris 777: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 778: cvs_log(LP_NOTICE,
779: "warning: %s was lost",
780: cf->file_path);
1.178 joris 781: }
1.138 joris 782: cf->file_status = FILE_LOST;
783: } else {
784: if (ismodified == 1)
785: cf->file_status = FILE_MODIFIED;
786: else
787: cf->file_status = FILE_UPTODATE;
788:
789: if (strcmp(r1, r2)) {
790: if (cf->file_status == FILE_MODIFIED)
791: cf->file_status = FILE_MERGE;
792: else
793: cf->file_status = FILE_PATCH;
794: }
795: }
1.128 joris 796: }
797: }
798:
1.138 joris 799: cvs_ent_close(entlist, ENT_NOSYNC);
800: }
1.14 jfb 801:
1.138 joris 802: void
803: cvs_file_free(struct cvs_file *cf)
804: {
805: xfree(cf->file_name);
806: xfree(cf->file_wd);
807: xfree(cf->file_path);
808:
1.167 joris 809: if (cf->file_rcsrev != NULL)
810: rcsnum_free(cf->file_rcsrev);
1.138 joris 811: if (cf->file_rpath != NULL)
812: xfree(cf->file_rpath);
813: if (cf->file_ent != NULL)
814: cvs_ent_free(cf->file_ent);
815: if (cf->file_rcs != NULL)
816: rcs_close(cf->file_rcs);
817: if (cf->fd != -1)
818: (void)close(cf->fd);
819: if (cf->repo_fd != -1)
820: (void)close(cf->repo_fd);
821: xfree(cf);
1.23 jfb 822: }
823:
1.165 xsa 824: int
1.23 jfb 825: cvs_file_cmpname(const char *name1, const char *name2)
826: {
827: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
828: (strcasecmp(name1, name2));
1.164 xsa 829: }
830:
831: int
832: cvs_file_cmp(const char *file1, const char *file2)
833: {
834: struct stat stb1, stb2;
835: int fd1, fd2, ret;
836:
837: ret = 0;
838:
839: if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
840: fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
841: if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
842: fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
843:
844: if (fstat(fd1, &stb1) == -1)
845: fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
846: if (fstat(fd2, &stb2) == -1)
847: fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
848:
849: if (stb1.st_size != stb2.st_size ||
850: (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
851: ret = 1;
852: goto out;
853: }
854:
855: if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
856: if (stb1.st_rdev != stb2.st_rdev)
857: ret = 1;
858: goto out;
859: }
860:
861: if (S_ISREG(stb1.st_mode)) {
862: void *p1, *p2;
863:
864: if (stb1.st_size > SIZE_MAX) {
865: ret = 1;
866: goto out;
867: }
868:
869: if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
870: MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
871: fatal("cvs_file_cmp: mmap failed");
872:
873: if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
874: MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
875: fatal("cvs_file_cmp: mmap failed");
876:
877: madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
878: madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
879:
880: ret = memcmp(p1, p2, stb1.st_size);
881:
882: (void)munmap(p1, stb1.st_size);
883: (void)munmap(p2, stb1.st_size);
884: }
885:
886: out:
887: (void)close(fd1);
888: (void)close(fd2);
1.166 xsa 889:
890: return (ret);
891: }
892:
893: int
894: cvs_file_copy(const char *from, const char *to)
895: {
896: struct stat st;
897: struct timeval tv[2];
898: time_t atime, mtime;
899: int src, dst, ret;
900:
901: ret = 0;
902:
903: cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
904:
905: if (cvs_noexec == 1)
906: return (0);
907:
908: if ((src = open(from, O_RDONLY, 0)) == -1)
909: fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
910:
911: if (fstat(src, &st) == -1)
912: fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
913:
914: atime = st.st_atimespec.tv_sec;
915: mtime = st.st_mtimespec.tv_sec;
916:
917: if (S_ISREG(st.st_mode)) {
918: size_t sz;
919: ssize_t nw;
920: char *p, *buf;
921: int saved_errno;
922:
923: if (st.st_size > SIZE_MAX) {
924: ret = -1;
925: goto out;
926: }
927:
928: if ((dst = open(to, O_CREAT|O_TRUNC|O_WRONLY,
929: st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) == -1)
930: fatal("cvs_file_copy: open `%s': %s",
931: to, strerror(errno));
932:
933: if ((p = mmap(NULL, st.st_size, PROT_READ,
934: MAP_FILE, src, (off_t)0)) == MAP_FAILED) {
935: saved_errno = errno;
936: (void)unlink(to);
937: fatal("cvs_file_copy: mmap: %s", strerror(saved_errno));
938: }
939:
940: madvise(p, st.st_size, MADV_SEQUENTIAL);
941:
942: sz = st.st_size;
943: buf = p;
944:
945: while (sz > 0) {
946: if ((nw = write(dst, p, sz)) == -1) {
947: saved_errno = errno;
948: (void)unlink(to);
949: fatal("cvs_file_copy: `%s': %s",
950: from, strerror(saved_errno));
951: }
952: buf += nw;
953: sz -= nw;
954: }
955:
956: (void)munmap(p, st.st_size);
957:
958: tv[0].tv_sec = atime;
959: tv[1].tv_sec = mtime;
960:
961: if (futimes(dst, tv) == -1) {
962: saved_errno = errno;
963: (void)unlink(to);
964: fatal("cvs_file_copy: futimes: %s",
965: strerror(saved_errno));
966: }
967: (void)close(dst);
968: }
969: out:
970: (void)close(src);
1.164 xsa 971:
972: return (ret);
1.14 jfb 973: }