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