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