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