Annotation of src/usr.bin/cvs/file.c, Revision 1.206
1.206 ! tobias 1: /* $OpenBSD: file.c,v 1.205 2007/11/09 16:21:24 tobias 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: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.206 ! tobias 674: if (cf->file_rcsrev != NULL) {
! 675: notag = 1;
! 676: cf->file_flags &= ~FILE_HAS_TAG;
! 677: }
1.152 joris 678: }
1.172 niallo 679: } else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL) {
1.169 joris 680: cf->file_rcsrev = rcsnum_alloc();
681: rcsnum_cpy(cf->file_ent->ce_rev, cf->file_rcsrev, 0);
1.190 niallo 682: } else if (cf->file_rcs != NULL) {
1.156 joris 683: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.190 niallo 684: } else {
1.153 joris 685: cf->file_rcsrev = NULL;
1.190 niallo 686: }
1.153 joris 687:
1.138 joris 688: if (cf->file_ent != NULL)
689: rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
1.190 niallo 690: if (cf->file_rcsrev != NULL) {
1.153 joris 691: rcsnum_tostr(cf->file_rcsrev, r2, sizeof(r2));
1.190 niallo 692: }
1.6 jfb 693:
1.138 joris 694: ismodified = rcsdead = 0;
695: if (cf->fd != -1 && cf->file_ent != NULL) {
696: if (fstat(cf->fd, &st) == -1)
697: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 698:
1.177 joris 699: if (st.st_mtime != cf->file_ent->ce_mtime)
1.138 joris 700: ismodified = 1;
1.149 joris 701: }
702:
1.198 joris 703: server_has_file = 0;
704: if (cvs_server_active == 1 && cf->file_ent != NULL &&
705: cf->file_ent->ce_mtime == CVS_SERVER_UPTODATE) {
706: server_has_file = 1;
707: ismodified = 0;
708: }
709:
1.202 joris 710: if (ismodified == 1 && cf->fd != -1 && cf->file_rcs != NULL &&
711: !RCSNUM_ISBRANCH(cf->file_rcsrev)) {
1.170 joris 712: b1 = rcs_rev_getbuf(cf->file_rcs, cf->file_rcsrev, 0);
1.149 joris 713: if (b1 == NULL)
714: fatal("failed to get HEAD revision for comparison");
715:
1.159 joris 716: b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT);
1.149 joris 717: if (b2 == NULL)
718: fatal("failed to get file content for comparison");
719:
720: if (cvs_buf_differ(b1, b2))
721: ismodified = 1;
722: else
723: ismodified = 0;
1.188 ray 724: cvs_buf_free(b1);
725: cvs_buf_free(b2);
1.62 jfb 726: }
727:
1.202 joris 728: if (cf->file_rcs != NULL && cf->file_rcsrev != NULL &&
729: !RCSNUM_ISBRANCH(cf->file_rcsrev)) {
1.153 joris 730: state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138 joris 731: if (state == NULL)
732: fatal("failed to get state for HEAD for %s",
733: cf->file_path);
1.139 joris 734: if (!strcmp(state, RCS_STATE_DEAD))
1.205 tobias 735: rcsdead = 1;
736:
737: if (cvs_specified_tag == NULL && cf->in_attic &&
738: !RCSNUM_ISBRANCHREV(cf->file_rcsrev))
1.138 joris 739: rcsdead = 1;
1.145 joris 740:
741: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 742: }
743:
1.138 joris 744: /*
745: * 10 Sin
746: * 20 Goto hell
747: * (I welcome you if-else hell)
748: */
749: if (cf->file_ent == NULL) {
750: if (cf->file_rcs == NULL) {
751: if (cf->fd == -1) {
1.178 joris 752: cvs_log(LP_NOTICE,
753: "nothing known about '%s'",
754: cf->file_path);
1.138 joris 755: }
1.130 joris 756:
1.138 joris 757: cf->file_status = FILE_UNKNOWN;
758: } else if (rcsdead == 1) {
759: if (cf->fd == -1) {
760: cf->file_status = FILE_UPTODATE;
1.179 joris 761: } else if (cvs_cmdop != CVS_OP_ADD) {
1.138 joris 762: cf->file_status = FILE_UNKNOWN;
1.44 jfb 763: }
1.199 joris 764: } else if (notag == 0) {
765: cf->file_status = FILE_CHECKOUT;
1.138 joris 766: } else {
1.199 joris 767: cf->file_status = FILE_UPTODATE;
1.14 jfb 768: }
1.138 joris 769: } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
770: if (cf->fd == -1) {
1.178 joris 771: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 772: cvs_log(LP_NOTICE,
1.157 david 773: "warning: new-born %s has disappeared",
1.138 joris 774: cf->file_path);
1.178 joris 775: }
1.138 joris 776: cf->file_status = FILE_REMOVE_ENTRY;
777: } else if (cf->file_rcs == NULL || rcsdead == 1) {
778: cf->file_status = FILE_ADDED;
779: } else {
1.178 joris 780: cvs_log(LP_NOTICE,
781: "conflict: %s already created by others",
782: cf->file_path);
1.138 joris 783: cf->file_status = FILE_CONFLICT;
784: }
785: } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
786: if (cf->fd != -1) {
1.178 joris 787: cvs_log(LP_NOTICE,
788: "%s should be removed but is still there",
789: cf->file_path);
1.138 joris 790: cf->file_status = FILE_REMOVED;
791: } else if (cf->file_rcs == NULL || rcsdead == 1) {
792: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 793: } else {
1.138 joris 794: if (strcmp(r1, r2)) {
1.178 joris 795: cvs_log(LP_NOTICE,
796: "conflict: removed %s was modified"
797: " by a second party",
798: cf->file_path);
1.138 joris 799: cf->file_status = FILE_CONFLICT;
800: } else {
801: cf->file_status = FILE_REMOVED;
802: }
1.66 joris 803: }
1.138 joris 804: } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
1.194 joris 805: if (cf->file_rcs == NULL || rcsdead == 1 ||
1.199 joris 806: (reset_stickies == 1 && cf->in_attic == 1) ||
807: (notag == 1 && tag != NULL)) {
1.198 joris 808: if (cf->fd == -1 && server_has_file == 0) {
1.178 joris 809: cvs_log(LP_NOTICE,
810: "warning: %s's entry exists but"
811: " there is no longer a file"
812: " in the repository,"
813: " removing entry",
814: cf->file_path);
1.138 joris 815: cf->file_status = FILE_REMOVE_ENTRY;
816: } else {
817: if (ismodified) {
1.178 joris 818: cvs_log(LP_NOTICE,
819: "conflict: %s is no longer "
820: "in the repository but is "
821: "locally modified",
822: cf->file_path);
1.138 joris 823: cf->file_status = FILE_CONFLICT;
1.204 tobias 824: } else if (cvs_cmdop != CVS_OP_IMPORT) {
1.178 joris 825: cvs_log(LP_NOTICE,
826: "%s is no longer in the "
827: "repository",
828: cf->file_path);
1.128 joris 829:
1.138 joris 830: cf->file_status = FILE_UNLINK;
831: }
832: }
1.128 joris 833: } else {
1.198 joris 834: if (cf->fd == -1 && server_has_file == 0) {
1.178 joris 835: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 836: cvs_log(LP_NOTICE,
837: "warning: %s was lost",
838: cf->file_path);
1.178 joris 839: }
1.138 joris 840: cf->file_status = FILE_LOST;
841: } else {
842: if (ismodified == 1)
843: cf->file_status = FILE_MODIFIED;
844: else
845: cf->file_status = FILE_UPTODATE;
846:
847: if (strcmp(r1, r2)) {
848: if (cf->file_status == FILE_MODIFIED)
849: cf->file_status = FILE_MERGE;
850: else
851: cf->file_status = FILE_PATCH;
852: }
853: }
1.128 joris 854: }
855: }
856:
1.138 joris 857: cvs_ent_close(entlist, ENT_NOSYNC);
858: }
1.14 jfb 859:
1.138 joris 860: void
861: cvs_file_free(struct cvs_file *cf)
862: {
863: xfree(cf->file_name);
864: xfree(cf->file_wd);
865: xfree(cf->file_path);
866:
1.167 joris 867: if (cf->file_rcsrev != NULL)
868: rcsnum_free(cf->file_rcsrev);
1.138 joris 869: if (cf->file_rpath != NULL)
870: xfree(cf->file_rpath);
871: if (cf->file_ent != NULL)
872: cvs_ent_free(cf->file_ent);
873: if (cf->file_rcs != NULL)
874: rcs_close(cf->file_rcs);
875: if (cf->fd != -1)
876: (void)close(cf->fd);
877: if (cf->repo_fd != -1)
878: (void)close(cf->repo_fd);
879: xfree(cf);
1.23 jfb 880: }
881:
1.165 xsa 882: int
1.23 jfb 883: cvs_file_cmpname(const char *name1, const char *name2)
884: {
885: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
886: (strcasecmp(name1, name2));
1.164 xsa 887: }
888:
889: int
890: cvs_file_cmp(const char *file1, const char *file2)
891: {
892: struct stat stb1, stb2;
893: int fd1, fd2, ret;
894:
895: ret = 0;
896:
897: if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
898: fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
899: if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
900: fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
901:
902: if (fstat(fd1, &stb1) == -1)
903: fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
904: if (fstat(fd2, &stb2) == -1)
905: fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
906:
907: if (stb1.st_size != stb2.st_size ||
908: (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
909: ret = 1;
910: goto out;
911: }
912:
913: if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
914: if (stb1.st_rdev != stb2.st_rdev)
915: ret = 1;
916: goto out;
917: }
918:
919: if (S_ISREG(stb1.st_mode)) {
920: void *p1, *p2;
921:
1.187 otto 922: if (stb1.st_size > (off_t)SIZE_MAX) {
1.164 xsa 923: ret = 1;
924: goto out;
925: }
926:
927: if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
928: MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
929: fatal("cvs_file_cmp: mmap failed");
930:
931: if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
932: MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
933: fatal("cvs_file_cmp: mmap failed");
934:
935: madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
936: madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
937:
938: ret = memcmp(p1, p2, stb1.st_size);
939:
940: (void)munmap(p1, stb1.st_size);
941: (void)munmap(p2, stb1.st_size);
942: }
943:
944: out:
945: (void)close(fd1);
946: (void)close(fd2);
1.166 xsa 947:
948: return (ret);
949: }
950:
951: int
952: cvs_file_copy(const char *from, const char *to)
953: {
954: struct stat st;
955: struct timeval tv[2];
956: time_t atime, mtime;
957: int src, dst, ret;
958:
959: ret = 0;
960:
961: cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
962:
963: if (cvs_noexec == 1)
964: return (0);
965:
966: if ((src = open(from, O_RDONLY, 0)) == -1)
967: fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
968:
969: if (fstat(src, &st) == -1)
970: fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
971:
972: atime = st.st_atimespec.tv_sec;
973: mtime = st.st_mtimespec.tv_sec;
974:
975: if (S_ISREG(st.st_mode)) {
1.197 tobias 976: char *p;
1.166 xsa 977: int saved_errno;
978:
1.187 otto 979: if (st.st_size > (off_t)SIZE_MAX) {
1.166 xsa 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:
1.197 tobias 998: if (atomicio(vwrite, dst, p, st.st_size) != st.st_size) {
999: saved_errno = errno;
1000: (void)unlink(to);
1001: fatal("cvs_file_copy: `%s': %s", from,
1002: strerror(saved_errno));
1.166 xsa 1003: }
1004:
1005: (void)munmap(p, st.st_size);
1006:
1007: tv[0].tv_sec = atime;
1008: tv[1].tv_sec = mtime;
1009:
1010: if (futimes(dst, tv) == -1) {
1011: saved_errno = errno;
1012: (void)unlink(to);
1013: fatal("cvs_file_copy: futimes: %s",
1014: strerror(saved_errno));
1015: }
1016: (void)close(dst);
1017: }
1018: out:
1019: (void)close(src);
1.164 xsa 1020:
1021: return (ret);
1.14 jfb 1022: }