Annotation of src/usr.bin/cvs/file.c, Revision 1.190
1.190 ! niallo 1: /* $OpenBSD: file.c,v 1.189 2007/02/22 06:42:09 otto 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.176 otto 341: char *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.151 joris 362: if (cvs_cmdop != CVS_OP_IMPORT &&
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.183 xsa 417: (void)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: if (!(cr->flags & CR_RECURSE_DIRS) &&
465: type == CVS_DIR) {
466: cp += dp->d_reclen;
467: continue;
468: }
469:
470: switch (type) {
471: case CVS_DIR:
1.138 joris 472: cvs_file_get(fpath, &dl);
1.171 joris 473: break;
474: case CVS_FILE:
1.138 joris 475: cvs_file_get(fpath, &fl);
1.171 joris 476: break;
477: default:
478: fatal("type %d unknown, shouldn't happen",
479: type);
480: }
1.13 jfb 481:
1.138 joris 482: cp += dp->d_reclen;
483: }
1.34 jfb 484: }
485:
1.138 joris 486: if (nbytes == -1)
487: fatal("cvs_file_walkdir: %s %s", cf->file_path,
488: strerror(errno));
1.22 jfb 489:
1.138 joris 490: xfree(buf);
1.22 jfb 491:
1.138 joris 492: while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
493: TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
494: xfree(ip);
1.91 joris 495: }
1.114 xsa 496:
1.138 joris 497: entlist = cvs_ent_open(cf->file_path);
498: TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
499: ent = cvs_ent_parse(line->buf);
1.61 xsa 500:
1.183 xsa 501: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138 joris 502: ent->ce_name);
1.100 joris 503:
1.140 joris 504: if (!(cr->flags & CR_RECURSE_DIRS) &&
505: ent->ce_type == CVS_ENT_DIR)
506: continue;
1.138 joris 507: if (ent->ce_type == CVS_ENT_DIR)
508: cvs_file_get(fpath, &dl);
509: else if (ent->ce_type == CVS_ENT_FILE)
510: cvs_file_get(fpath, &fl);
1.68 joris 511:
1.138 joris 512: cvs_ent_free(ent);
1.128 joris 513: }
514:
1.138 joris 515: cvs_ent_close(entlist, ENT_NOSYNC);
1.91 joris 516:
1.140 joris 517: if (cr->flags & CR_REPO) {
1.146 joris 518: cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.140 joris 519: cvs_repository_lock(repo);
1.68 joris 520:
1.140 joris 521: cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
522: (cr->flags & CR_RECURSE_DIRS));
523: }
1.68 joris 524:
1.138 joris 525: cvs_file_walklist(&fl, cr);
526: cvs_file_freelist(&fl);
1.68 joris 527:
1.176 otto 528: if (cr->flags & CR_REPO)
1.140 joris 529: cvs_repository_unlock(repo);
1.68 joris 530:
1.138 joris 531: cvs_file_walklist(&dl, cr);
532: cvs_file_freelist(&dl);
1.34 jfb 533:
1.138 joris 534: if (cr->leavedir != NULL)
535: cr->leavedir(cf);
1.3 jfb 536: }
537:
538: void
1.138 joris 539: cvs_file_freelist(struct cvs_flisthead *fl)
1.3 jfb 540: {
1.138 joris 541: struct cvs_filelist *f;
1.62 jfb 542:
1.138 joris 543: while ((f = TAILQ_FIRST(fl)) != NULL) {
544: TAILQ_REMOVE(fl, f, flist);
545: xfree(f->file_path);
546: xfree(f);
1.62 jfb 547: }
1.5 jfb 548: }
549:
1.138 joris 550: void
1.185 joris 551: cvs_file_classify(struct cvs_file *cf, const char *tag)
1.3 jfb 552: {
1.138 joris 553: size_t len;
554: struct stat st;
1.149 joris 555: BUF *b1, *b2;
1.183 xsa 556: int rflags, ismodified, rcsdead;
1.138 joris 557: CVSENTRIES *entlist = NULL;
558: const char *state;
1.176 otto 559: char repo[MAXPATHLEN], rcsfile[MAXPATHLEN], r1[16], r2[16];
1.190 ! niallo 560: char *tfname, *tpath, *p;
1.138 joris 561:
562: cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
563:
564: if (!strcmp(cf->file_path, ".")) {
565: cf->file_status = FILE_UPTODATE;
566: return;
567: }
568:
1.190 ! niallo 569:
1.146 joris 570: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.183 xsa 571: (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s",
1.138 joris 572: repo, cf->file_name);
573:
574: if (cf->file_type == CVS_FILE) {
575: len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
576: if (len >= MAXPATHLEN)
577: fatal("cvs_file_classify: truncation");
578: }
579:
580: cf->file_rpath = xstrdup(rcsfile);
1.190 ! niallo 581: /* XXX: likely wrong place for this shit */
! 582: /* is this file in the Attic? */
! 583: if (cf->file_type == CVS_FILE
! 584: && strstr(cf->file_rpath, CVS_PATH_ATTIC) != NULL) {
! 585: cf->in_attic = 1;
! 586: /* chop the 'Attic' out of cf->file_path */
! 587: if ((tfname = basename(cf->file_path)) == NULL)
! 588: fatal("cvs_file_classify: basename failure");
! 589: if ((tpath = dirname(cf->file_path)) == NULL)
! 590: fatal("cvs_file_classify: dirname failure");
! 591: if ((p = strstr(tpath, CVS_PATH_ATTIC)) == NULL)
! 592: fatal("cvs_file_classify: strstr failure");
! 593: strlcpy(cf->file_path, tpath, p - tpath + 1);
! 594: strlcat(cf->file_path, tfname, MAXPATHLEN);
! 595: }
1.151 joris 596:
597: entlist = cvs_ent_open(cf->file_wd);
1.138 joris 598: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
599:
1.144 joris 600: if (cf->file_ent != NULL) {
601: if (cf->file_ent->ce_type == CVS_ENT_DIR &&
602: cf->file_type != CVS_DIR)
1.148 pedro 603: fatal("%s is supposed to be a directory, but it is not",
1.144 joris 604: cf->file_path);
605: if (cf->file_ent->ce_type == CVS_ENT_FILE &&
606: cf->file_type != CVS_FILE)
1.148 pedro 607: fatal("%s is supposed to be a file, but it is not",
1.144 joris 608: cf->file_path);
609: }
610:
1.138 joris 611: if (cf->file_type == CVS_DIR) {
612: if (cf->fd == -1 && stat(rcsfile, &st) != -1)
613: cf->file_status = DIR_CREATE;
614: else if (cf->file_ent != NULL)
615: cf->file_status = FILE_UPTODATE;
1.144 joris 616: else
617: cf->file_status = FILE_UNKNOWN;
618:
1.138 joris 619: cvs_ent_close(entlist, ENT_NOSYNC);
620: return;
621: }
622:
1.147 joris 623: rflags = RCS_READ;
624: switch (cvs_cmdop) {
625: case CVS_OP_COMMIT:
626: rflags = RCS_WRITE;
627: break;
1.156 joris 628: case CVS_OP_IMPORT:
1.147 joris 629: case CVS_OP_LOG:
630: rflags |= RCS_PARSE_FULLY;
631: break;
632: }
633:
1.190 ! niallo 634: if (strncmp(cf->file_path, CVS_JUNK, strlen(CVS_JUNK)) == 0) {
! 635: cf->file_status = FILE_UNKNOWN;
! 636: return;
! 637: }
1.138 joris 638: cf->repo_fd = open(cf->file_rpath, O_RDONLY);
639: if (cf->repo_fd != -1) {
640: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
641: if (cf->file_rcs == NULL)
1.152 joris 642: fatal("cvs_file_classify: failed to parse RCS");
1.190 ! niallo 643: } else {
! 644:
! 645: fatal("cvs_file_classify: failed to open file `%s' of type %d",
! 646: cf->file_rpath, cf->file_type);
! 647: }
! 648:
! 649: if (tag != NULL && cf->file_rcs != NULL) {
! 650: /* if we could not translate tag, means that we should
! 651: * skip this file. */
! 652: if ((cf->file_rcsrev = rcs_translate_tag(tag, cf->file_rcs)) == NULL) {
! 653: cf->file_status = FILE_SKIP;
! 654: cvs_ent_close(entlist, ENT_NOSYNC);
! 655: return;
1.152 joris 656: }
1.3 jfb 657:
1.190 ! niallo 658: rcsnum_tostr(cf->file_rcsrev, r1, sizeof(r1));
! 659:
1.172 niallo 660: } else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL) {
1.169 joris 661: cf->file_rcsrev = rcsnum_alloc();
662: rcsnum_cpy(cf->file_ent->ce_rev, cf->file_rcsrev, 0);
1.190 ! niallo 663: } else if (cf->file_rcs != NULL) {
1.156 joris 664: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.190 ! niallo 665: } else {
1.153 joris 666: cf->file_rcsrev = NULL;
1.190 ! niallo 667: }
1.153 joris 668:
1.138 joris 669: if (cf->file_ent != NULL)
670: rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
1.190 ! niallo 671: if (cf->file_rcsrev != NULL) {
1.153 joris 672: rcsnum_tostr(cf->file_rcsrev, r2, sizeof(r2));
1.190 ! niallo 673: }
1.6 jfb 674:
1.138 joris 675: ismodified = rcsdead = 0;
676: if (cf->fd != -1 && cf->file_ent != NULL) {
677: if (fstat(cf->fd, &st) == -1)
678: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 679:
1.177 joris 680: if (st.st_mtime != cf->file_ent->ce_mtime)
1.138 joris 681: ismodified = 1;
1.149 joris 682: }
683:
684: if (ismodified == 1 && cf->fd != -1 && cf->file_rcs != NULL) {
1.170 joris 685: b1 = rcs_rev_getbuf(cf->file_rcs, cf->file_rcsrev, 0);
1.149 joris 686: if (b1 == NULL)
687: fatal("failed to get HEAD revision for comparison");
688:
1.159 joris 689: b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT);
1.149 joris 690: if (b2 == NULL)
691: fatal("failed to get file content for comparison");
692:
693: if (cvs_buf_differ(b1, b2))
694: ismodified = 1;
695: else
696: ismodified = 0;
1.188 ray 697: cvs_buf_free(b1);
698: cvs_buf_free(b2);
1.62 jfb 699: }
700:
1.173 joris 701: if (cf->file_rcs != NULL && cf->file_rcsrev != NULL) {
1.153 joris 702: state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138 joris 703: if (state == NULL)
704: fatal("failed to get state for HEAD for %s",
705: cf->file_path);
1.139 joris 706: if (!strcmp(state, RCS_STATE_DEAD))
1.138 joris 707: rcsdead = 1;
1.145 joris 708:
709: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 710: }
711:
1.138 joris 712: /*
713: * 10 Sin
714: * 20 Goto hell
715: * (I welcome you if-else hell)
716: */
717: if (cf->file_ent == NULL) {
718: if (cf->file_rcs == NULL) {
719: if (cf->fd == -1) {
1.178 joris 720: cvs_log(LP_NOTICE,
721: "nothing known about '%s'",
722: cf->file_path);
1.138 joris 723: }
1.130 joris 724:
1.138 joris 725: cf->file_status = FILE_UNKNOWN;
726: } else if (rcsdead == 1) {
727: if (cf->fd == -1) {
728: cf->file_status = FILE_UPTODATE;
1.179 joris 729: } else if (cvs_cmdop != CVS_OP_ADD) {
1.138 joris 730: cf->file_status = FILE_UNKNOWN;
1.44 jfb 731: }
1.138 joris 732: } else {
733: cf->file_status = FILE_CHECKOUT;
1.14 jfb 734: }
1.138 joris 735: } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
736: if (cf->fd == -1) {
1.178 joris 737: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 738: cvs_log(LP_NOTICE,
1.157 david 739: "warning: new-born %s has disappeared",
1.138 joris 740: cf->file_path);
1.178 joris 741: }
1.138 joris 742: cf->file_status = FILE_REMOVE_ENTRY;
743: } else if (cf->file_rcs == NULL || rcsdead == 1) {
744: cf->file_status = FILE_ADDED;
745: } else {
1.178 joris 746: cvs_log(LP_NOTICE,
747: "conflict: %s already created by others",
748: cf->file_path);
1.138 joris 749: cf->file_status = FILE_CONFLICT;
750: }
751: } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
752: if (cf->fd != -1) {
1.178 joris 753: cvs_log(LP_NOTICE,
754: "%s should be removed but is still there",
755: cf->file_path);
1.138 joris 756: cf->file_status = FILE_REMOVED;
757: } else if (cf->file_rcs == NULL || rcsdead == 1) {
758: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 759: } else {
1.138 joris 760: if (strcmp(r1, r2)) {
1.178 joris 761: cvs_log(LP_NOTICE,
762: "conflict: removed %s was modified"
763: " by a second party",
764: cf->file_path);
1.138 joris 765: cf->file_status = FILE_CONFLICT;
766: } else {
767: cf->file_status = FILE_REMOVED;
768: }
1.66 joris 769: }
1.138 joris 770: } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
771: if (cf->file_rcs == NULL || rcsdead == 1) {
772: if (cf->fd == -1) {
1.178 joris 773: cvs_log(LP_NOTICE,
774: "warning: %s's entry exists but"
775: " there is no longer a file"
776: " in the repository,"
777: " removing entry",
778: cf->file_path);
1.138 joris 779: cf->file_status = FILE_REMOVE_ENTRY;
780: } else {
781: if (ismodified) {
1.178 joris 782: cvs_log(LP_NOTICE,
783: "conflict: %s is no longer "
784: "in the repository but is "
785: "locally modified",
786: cf->file_path);
1.138 joris 787: cf->file_status = FILE_CONFLICT;
788: } else {
1.178 joris 789: cvs_log(LP_NOTICE,
790: "%s is no longer in the "
791: "repository",
792: cf->file_path);
1.128 joris 793:
1.138 joris 794: cf->file_status = FILE_UNLINK;
795: }
796: }
1.128 joris 797: } else {
1.138 joris 798: if (cf->fd == -1) {
1.178 joris 799: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 800: cvs_log(LP_NOTICE,
801: "warning: %s was lost",
802: cf->file_path);
1.178 joris 803: }
1.138 joris 804: cf->file_status = FILE_LOST;
805: } else {
806: if (ismodified == 1)
807: cf->file_status = FILE_MODIFIED;
808: else
809: cf->file_status = FILE_UPTODATE;
810:
811: if (strcmp(r1, r2)) {
812: if (cf->file_status == FILE_MODIFIED)
813: cf->file_status = FILE_MERGE;
814: else
815: cf->file_status = FILE_PATCH;
816: }
817: }
1.128 joris 818: }
819: }
820:
1.138 joris 821: cvs_ent_close(entlist, ENT_NOSYNC);
822: }
1.14 jfb 823:
1.138 joris 824: void
825: cvs_file_free(struct cvs_file *cf)
826: {
827: xfree(cf->file_name);
828: xfree(cf->file_wd);
829: xfree(cf->file_path);
830:
1.167 joris 831: if (cf->file_rcsrev != NULL)
832: rcsnum_free(cf->file_rcsrev);
1.138 joris 833: if (cf->file_rpath != NULL)
834: xfree(cf->file_rpath);
835: if (cf->file_ent != NULL)
836: cvs_ent_free(cf->file_ent);
837: if (cf->file_rcs != NULL)
838: rcs_close(cf->file_rcs);
839: if (cf->fd != -1)
840: (void)close(cf->fd);
841: if (cf->repo_fd != -1)
842: (void)close(cf->repo_fd);
843: xfree(cf);
1.23 jfb 844: }
845:
1.165 xsa 846: int
1.23 jfb 847: cvs_file_cmpname(const char *name1, const char *name2)
848: {
849: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
850: (strcasecmp(name1, name2));
1.164 xsa 851: }
852:
853: int
854: cvs_file_cmp(const char *file1, const char *file2)
855: {
856: struct stat stb1, stb2;
857: int fd1, fd2, ret;
858:
859: ret = 0;
860:
861: if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
862: fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
863: if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
864: fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
865:
866: if (fstat(fd1, &stb1) == -1)
867: fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
868: if (fstat(fd2, &stb2) == -1)
869: fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
870:
871: if (stb1.st_size != stb2.st_size ||
872: (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
873: ret = 1;
874: goto out;
875: }
876:
877: if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
878: if (stb1.st_rdev != stb2.st_rdev)
879: ret = 1;
880: goto out;
881: }
882:
883: if (S_ISREG(stb1.st_mode)) {
884: void *p1, *p2;
885:
1.187 otto 886: if (stb1.st_size > (off_t)SIZE_MAX) {
1.164 xsa 887: ret = 1;
888: goto out;
889: }
890:
891: if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
892: MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
893: fatal("cvs_file_cmp: mmap failed");
894:
895: if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
896: MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
897: fatal("cvs_file_cmp: mmap failed");
898:
899: madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
900: madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
901:
902: ret = memcmp(p1, p2, stb1.st_size);
903:
904: (void)munmap(p1, stb1.st_size);
905: (void)munmap(p2, stb1.st_size);
906: }
907:
908: out:
909: (void)close(fd1);
910: (void)close(fd2);
1.166 xsa 911:
912: return (ret);
913: }
914:
915: int
916: cvs_file_copy(const char *from, const char *to)
917: {
918: struct stat st;
919: struct timeval tv[2];
920: time_t atime, mtime;
921: int src, dst, ret;
922:
923: ret = 0;
924:
925: cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
926:
927: if (cvs_noexec == 1)
928: return (0);
929:
930: if ((src = open(from, O_RDONLY, 0)) == -1)
931: fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
932:
933: if (fstat(src, &st) == -1)
934: fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
935:
936: atime = st.st_atimespec.tv_sec;
937: mtime = st.st_mtimespec.tv_sec;
938:
939: if (S_ISREG(st.st_mode)) {
940: size_t sz;
941: ssize_t nw;
942: char *p, *buf;
943: int saved_errno;
944:
1.187 otto 945: if (st.st_size > (off_t)SIZE_MAX) {
1.166 xsa 946: ret = -1;
947: goto out;
948: }
949:
950: if ((dst = open(to, O_CREAT|O_TRUNC|O_WRONLY,
951: st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) == -1)
952: fatal("cvs_file_copy: open `%s': %s",
953: to, strerror(errno));
954:
955: if ((p = mmap(NULL, st.st_size, PROT_READ,
956: MAP_FILE, src, (off_t)0)) == MAP_FAILED) {
957: saved_errno = errno;
958: (void)unlink(to);
959: fatal("cvs_file_copy: mmap: %s", strerror(saved_errno));
960: }
961:
962: madvise(p, st.st_size, MADV_SEQUENTIAL);
963:
964: sz = st.st_size;
965: buf = p;
966:
967: while (sz > 0) {
968: if ((nw = write(dst, p, sz)) == -1) {
969: saved_errno = errno;
970: (void)unlink(to);
971: fatal("cvs_file_copy: `%s': %s",
972: from, strerror(saved_errno));
973: }
974: buf += nw;
975: sz -= nw;
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: }