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