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