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