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