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