Annotation of src/usr.bin/cvs/file.c, Revision 1.209
1.209 ! tobias 1: /* $OpenBSD: file.c,v 1.208 2008/01/10 10:12:09 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:
1.207 tobias 219: cf = (struct cvs_file *)xcalloc(1, sizeof(*cf));
1.138 joris 220:
221: cf->file_name = xstrdup(f);
222: cf->file_wd = xstrdup(d);
223: cf->file_path = xstrdup(p);
224: cf->fd = fd;
225: cf->repo_fd = -1;
226: cf->file_type = type;
227: cf->file_status = cf->file_flags = 0;
228: cf->file_ent = NULL;
1.68 joris 229:
1.138 joris 230: return (cf);
1.9 jfb 231: }
232:
1.138 joris 233: void
234: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9 jfb 235: {
1.183 xsa 236: int fd, type;
1.138 joris 237: struct stat st;
238: struct cvs_file *cf;
239: struct cvs_filelist *l, *nxt;
1.176 otto 240: char *d, *f, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.138 joris 241:
242: for (l = TAILQ_FIRST(fl); l != NULL; l = nxt) {
243: if (cvs_quit)
244: fatal("received signal %d", sig_received);
245:
246: cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
247: l->file_path);
248:
249: if ((f = basename(l->file_path)) == NULL)
250: fatal("cvs_file_walklist: basename failed");
251: if ((d = dirname(l->file_path)) == NULL)
252: fatal("cvs_file_walklist: dirname failed");
253:
1.146 joris 254: type = CVS_FILE;
1.138 joris 255: if ((fd = open(l->file_path, O_RDONLY)) != -1) {
256: if (fstat(fd, &st) == -1) {
257: cvs_log(LP_ERRNO, "%s", l->file_path);
258: (void)close(fd);
259: goto next;
260: }
1.26 jfb 261:
1.138 joris 262: if (S_ISDIR(st.st_mode))
263: type = CVS_DIR;
264: else if (S_ISREG(st.st_mode))
265: type = CVS_FILE;
266: else {
267: cvs_log(LP_ERR,
268: "ignoring bad file type for %s",
269: l->file_path);
270: (void)close(fd);
271: goto next;
272: }
1.162 joris 273: } else if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.196 tobias 274: /*
275: * During checkout -p, do not use any locally
276: * available directories.
277: */
278: if (cvs_cmdop != CVS_OP_CHECKOUT || !print_stdout)
279: if (stat(d, &st) == -1) {
280: cvs_log(LP_ERRNO, "%s", d);
281: goto next;
282: }
1.87 joris 283:
1.146 joris 284: cvs_get_repository_path(d, repo, MAXPATHLEN);
1.183 xsa 285: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
1.138 joris 286: repo, f);
287:
288: if ((fd = open(fpath, O_RDONLY)) == -1) {
289: strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
290: fd = open(fpath, O_RDONLY);
291: }
1.26 jfb 292:
1.138 joris 293: if (fd != -1) {
294: if (fstat(fd, &st) == -1)
295: fatal("cvs_file_walklist: %s: %s",
296: fpath, strerror(errno));
297:
298: if (S_ISDIR(st.st_mode))
299: type = CVS_DIR;
300: else if (S_ISREG(st.st_mode))
301: type = CVS_FILE;
302: else {
303: cvs_log(LP_ERR,
304: "ignoring bad file type for %s",
305: l->file_path);
306: (void)close(fd);
307: goto next;
308: }
309:
310: /* this file is not in our working copy yet */
311: (void)close(fd);
312: fd = -1;
313: }
314: }
1.73 joris 315:
1.138 joris 316: cf = cvs_file_get_cf(d, f, fd, type);
317: if (cf->file_type == CVS_DIR) {
318: cvs_file_walkdir(cf, cr);
319: } else {
1.199 joris 320: if (l->check_dir_tag) {
321: cvs_parse_tagfile(cf->file_wd,
322: &cvs_directory_tag, NULL, NULL);
323:
324: if (cvs_directory_tag == NULL &&
325: cvs_specified_tag != NULL)
326: cvs_directory_tag = cvs_specified_tag;
327: }
328:
1.161 joris 329: if (cr->fileproc != NULL)
330: cr->fileproc(cf);
1.128 joris 331: }
1.100 joris 332:
1.138 joris 333: cvs_file_free(cf);
1.123 joris 334:
1.138 joris 335: next:
336: nxt = TAILQ_NEXT(l, flist);
337: TAILQ_REMOVE(fl, l, flist);
1.68 joris 338:
1.138 joris 339: xfree(l->file_path);
340: xfree(l);
1.100 joris 341: }
342: }
343:
1.138 joris 344: void
345: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100 joris 346: {
1.171 joris 347: int l, type;
1.138 joris 348: FILE *fp;
349: int nbytes;
350: long base;
351: size_t bufsize;
352: struct stat st;
353: struct dirent *dp;
1.100 joris 354: struct cvs_ent *ent;
1.138 joris 355: struct cvs_ignpat *ip;
356: struct cvs_ent_line *line;
357: struct cvs_flisthead fl, dl;
358: CVSENTRIES *entlist;
1.194 joris 359: char *buf, *ebuf, *cp, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.100 joris 360:
1.138 joris 361: cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100 joris 362:
1.138 joris 363: if (cr->enterdir != NULL)
364: cr->enterdir(cf);
1.100 joris 365:
1.161 joris 366: if (cr->fileproc != NULL)
367: cr->fileproc(cf);
1.100 joris 368:
1.146 joris 369: if (cf->file_status == FILE_SKIP)
370: return;
371:
1.100 joris 372: /*
1.151 joris 373: * If we do not have a admin directory inside here, dont bother,
374: * unless we are running import.
1.100 joris 375: */
1.183 xsa 376: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138 joris 377: CVS_PATH_CVSDIR);
1.100 joris 378:
1.146 joris 379: l = stat(fpath, &st);
1.192 niallo 380: if (cvs_cmdop != CVS_OP_IMPORT && cvs_cmdop != CVS_OP_RLOG &&
1.209 ! tobias 381: cvs_cmdop != CVS_OP_RTAG &&
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.209 ! tobias 431: if (cvs_file_chkign(dp->d_name) &&
! 432: cvs_cmdop != CVS_OP_RLOG &&
! 433: cvs_cmdop != CVS_OP_RTAG) {
1.138 joris 434: cp += dp->d_reclen;
1.13 jfb 435: continue;
1.138 joris 436: }
1.13 jfb 437:
1.203 gilles 438: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
1.138 joris 439: cf->file_path, dp->d_name);
1.13 jfb 440:
1.138 joris 441: /*
1.171 joris 442: * nfs and afs will show d_type as DT_UNKNOWN
443: * for files and/or directories so when we encounter
1.184 todd 444: * this we call lstat() on the path to be sure.
1.138 joris 445: */
1.171 joris 446: if (dp->d_type == DT_UNKNOWN) {
1.184 todd 447: if (lstat(fpath, &st) == -1)
1.171 joris 448: fatal("'%s': %s", fpath,
449: strerror(errno));
450:
451: switch (st.st_mode & S_IFMT) {
452: case S_IFDIR:
453: type = CVS_DIR;
454: break;
455: case S_IFREG:
456: type = CVS_FILE;
457: break;
458: default:
1.175 ray 459: type = FILE_SKIP;
460: break;
1.171 joris 461: }
462: } else {
463: switch (dp->d_type) {
464: case DT_DIR:
465: type = CVS_DIR;
466: break;
467: case DT_REG:
468: type = CVS_FILE;
469: break;
470: default:
1.175 ray 471: type = FILE_SKIP;
472: break;
1.171 joris 473: }
1.175 ray 474: }
475:
476: if (type == FILE_SKIP) {
477: if (verbosity > 1) {
478: cvs_log(LP_NOTICE, "ignoring `%s'",
479: dp->d_name);
480: }
481: cp += dp->d_reclen;
482: continue;
1.171 joris 483: }
484:
485: switch (type) {
486: case CVS_DIR:
1.192 niallo 487: if (cr->flags & CR_RECURSE_DIRS)
1.199 joris 488: cvs_file_get(fpath, 0, &dl);
1.171 joris 489: break;
490: case CVS_FILE:
1.199 joris 491: cvs_file_get(fpath, 0, &fl);
1.171 joris 492: break;
493: default:
494: fatal("type %d unknown, shouldn't happen",
495: type);
496: }
1.13 jfb 497:
1.138 joris 498: cp += dp->d_reclen;
499: }
1.34 jfb 500: }
501:
1.138 joris 502: if (nbytes == -1)
503: fatal("cvs_file_walkdir: %s %s", cf->file_path,
504: strerror(errno));
1.22 jfb 505:
1.138 joris 506: xfree(buf);
1.22 jfb 507:
1.138 joris 508: while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
509: TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
510: xfree(ip);
1.91 joris 511: }
1.114 xsa 512:
1.138 joris 513: entlist = cvs_ent_open(cf->file_path);
514: TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
515: ent = cvs_ent_parse(line->buf);
1.61 xsa 516:
1.183 xsa 517: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138 joris 518: ent->ce_name);
1.100 joris 519:
1.140 joris 520: if (!(cr->flags & CR_RECURSE_DIRS) &&
521: ent->ce_type == CVS_ENT_DIR)
522: continue;
1.138 joris 523: if (ent->ce_type == CVS_ENT_DIR)
1.199 joris 524: cvs_file_get(fpath, 0, &dl);
1.138 joris 525: else if (ent->ce_type == CVS_ENT_FILE)
1.199 joris 526: cvs_file_get(fpath, 0, &fl);
1.68 joris 527:
1.138 joris 528: cvs_ent_free(ent);
1.128 joris 529: }
530:
1.138 joris 531: cvs_ent_close(entlist, ENT_NOSYNC);
1.91 joris 532:
1.140 joris 533: if (cr->flags & CR_REPO) {
1.146 joris 534: cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.140 joris 535: cvs_repository_lock(repo);
1.68 joris 536:
1.140 joris 537: cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
538: (cr->flags & CR_RECURSE_DIRS));
539: }
1.68 joris 540:
1.138 joris 541: cvs_file_walklist(&fl, cr);
542: cvs_file_freelist(&fl);
1.68 joris 543:
1.176 otto 544: if (cr->flags & CR_REPO)
1.140 joris 545: cvs_repository_unlock(repo);
1.68 joris 546:
1.138 joris 547: cvs_file_walklist(&dl, cr);
548: cvs_file_freelist(&dl);
1.34 jfb 549:
1.138 joris 550: if (cr->leavedir != NULL)
551: cr->leavedir(cf);
1.199 joris 552:
553: if (cvs_directory_tag != NULL) {
554: cvs_write_tagfile(cf->file_path, cvs_directory_tag, NULL, 0);
555: xfree(cvs_directory_tag);
556: cvs_directory_tag = NULL;
557: }
1.3 jfb 558: }
559:
560: void
1.138 joris 561: cvs_file_freelist(struct cvs_flisthead *fl)
1.3 jfb 562: {
1.138 joris 563: struct cvs_filelist *f;
1.62 jfb 564:
1.138 joris 565: while ((f = TAILQ_FIRST(fl)) != NULL) {
566: TAILQ_REMOVE(fl, f, flist);
567: xfree(f->file_path);
568: xfree(f);
1.62 jfb 569: }
1.5 jfb 570: }
571:
1.138 joris 572: void
1.185 joris 573: cvs_file_classify(struct cvs_file *cf, const char *tag)
1.3 jfb 574: {
1.138 joris 575: size_t len;
576: struct stat st;
1.149 joris 577: BUF *b1, *b2;
1.199 joris 578: int server_has_file, notag;
1.183 xsa 579: int rflags, ismodified, rcsdead;
1.138 joris 580: CVSENTRIES *entlist = NULL;
581: const char *state;
1.193 xsa 582: char repo[MAXPATHLEN], rcsfile[MAXPATHLEN];
1.194 joris 583: char r1[CVS_REV_BUFSZ], r2[CVS_REV_BUFSZ];
1.138 joris 584:
1.198 joris 585: cvs_log(LP_TRACE, "cvs_file_classify(%s, %s)", cf->file_path,
586: (tag != NULL) ? tag : "none");
1.138 joris 587:
588: if (!strcmp(cf->file_path, ".")) {
589: cf->file_status = FILE_UPTODATE;
590: return;
591: }
592:
1.146 joris 593: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.183 xsa 594: (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s",
1.138 joris 595: repo, cf->file_name);
596:
1.209 ! tobias 597: if (cvs_cmdop != CVS_OP_RLOG && cvs_cmdop != CVS_OP_RTAG &&
! 598: cf->file_type == CVS_FILE) {
1.138 joris 599: len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
600: if (len >= MAXPATHLEN)
601: fatal("cvs_file_classify: truncation");
602: }
603:
604: cf->file_rpath = xstrdup(rcsfile);
1.151 joris 605: entlist = cvs_ent_open(cf->file_wd);
1.138 joris 606: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
1.208 tobias 607: cvs_ent_close(entlist, ENT_NOSYNC);
1.138 joris 608:
1.144 joris 609: if (cf->file_ent != NULL) {
610: if (cf->file_ent->ce_type == CVS_ENT_DIR &&
611: cf->file_type != CVS_DIR)
1.148 pedro 612: fatal("%s is supposed to be a directory, but it is not",
1.144 joris 613: cf->file_path);
614: if (cf->file_ent->ce_type == CVS_ENT_FILE &&
615: cf->file_type != CVS_FILE)
1.148 pedro 616: fatal("%s is supposed to be a file, but it is not",
1.144 joris 617: cf->file_path);
1.201 joris 618:
619: if (cf->file_ent->ce_tag != NULL)
620: tag = cf->file_ent->ce_tag;
1.144 joris 621: }
622:
1.138 joris 623: if (cf->file_type == CVS_DIR) {
624: if (cf->fd == -1 && stat(rcsfile, &st) != -1)
625: cf->file_status = DIR_CREATE;
1.209 ! tobias 626: else if (cf->file_ent != NULL || cvs_cmdop == CVS_OP_RLOG ||
! 627: cvs_cmdop == CVS_OP_RTAG)
1.138 joris 628: cf->file_status = FILE_UPTODATE;
1.144 joris 629: else
630: cf->file_status = FILE_UNKNOWN;
631:
1.138 joris 632: return;
633: }
634:
1.147 joris 635: rflags = RCS_READ;
636: switch (cvs_cmdop) {
637: case CVS_OP_COMMIT:
638: rflags = RCS_WRITE;
639: break;
1.156 joris 640: case CVS_OP_IMPORT:
1.147 joris 641: case CVS_OP_LOG:
1.192 niallo 642: case CVS_OP_RLOG:
1.209 ! tobias 643: case CVS_OP_RTAG:
1.147 joris 644: rflags |= RCS_PARSE_FULLY;
645: break;
1.208 tobias 646: default:
647: break;
1.147 joris 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");
1.194 joris 655: } else {
656: (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s/%s%s",
657: repo, CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
658:
659: cf->repo_fd = open(rcsfile, O_RDONLY);
660: if (cf->repo_fd != -1) {
661: xfree(cf->file_rpath);
662: cf->file_rpath = xstrdup(rcsfile);
663: cf->file_rcs = rcs_open(cf->file_rpath,
664: cf->repo_fd, rflags);
665: if (cf->file_rcs == NULL)
666: fatal("cvs_file_classify: failed to parse RCS");
667: cf->in_attic = 1;
668: } else {
669: cf->file_rcs = NULL;
670: }
1.190 niallo 671: }
672:
1.199 joris 673: notag = 0;
1.200 joris 674: cf->file_flags |= FILE_HAS_TAG;
1.190 niallo 675: if (tag != NULL && cf->file_rcs != NULL) {
1.199 joris 676: if ((cf->file_rcsrev = rcs_translate_tag(tag,
677: cf->file_rcs)) != NULL) {
678: rcsnum_tostr(cf->file_rcsrev, r1, sizeof(r1));
679: } else {
680: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.206 tobias 681: if (cf->file_rcsrev != NULL) {
682: notag = 1;
683: cf->file_flags &= ~FILE_HAS_TAG;
684: }
1.152 joris 685: }
1.172 niallo 686: } else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL) {
1.169 joris 687: cf->file_rcsrev = rcsnum_alloc();
688: rcsnum_cpy(cf->file_ent->ce_rev, cf->file_rcsrev, 0);
1.190 niallo 689: } else if (cf->file_rcs != NULL) {
1.156 joris 690: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.190 niallo 691: } else {
1.153 joris 692: cf->file_rcsrev = NULL;
1.190 niallo 693: }
1.153 joris 694:
1.138 joris 695: if (cf->file_ent != NULL)
696: rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
1.190 niallo 697: if (cf->file_rcsrev != NULL) {
1.153 joris 698: rcsnum_tostr(cf->file_rcsrev, r2, sizeof(r2));
1.190 niallo 699: }
1.6 jfb 700:
1.138 joris 701: ismodified = rcsdead = 0;
702: if (cf->fd != -1 && cf->file_ent != NULL) {
703: if (fstat(cf->fd, &st) == -1)
704: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 705:
1.177 joris 706: if (st.st_mtime != cf->file_ent->ce_mtime)
1.138 joris 707: ismodified = 1;
1.149 joris 708: }
709:
1.198 joris 710: server_has_file = 0;
711: if (cvs_server_active == 1 && cf->file_ent != NULL &&
712: cf->file_ent->ce_mtime == CVS_SERVER_UPTODATE) {
713: server_has_file = 1;
714: ismodified = 0;
715: }
716:
1.202 joris 717: if (ismodified == 1 && cf->fd != -1 && cf->file_rcs != NULL &&
718: !RCSNUM_ISBRANCH(cf->file_rcsrev)) {
1.170 joris 719: b1 = rcs_rev_getbuf(cf->file_rcs, cf->file_rcsrev, 0);
1.149 joris 720: if (b1 == NULL)
721: fatal("failed to get HEAD revision for comparison");
722:
1.159 joris 723: b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT);
1.149 joris 724: if (b2 == NULL)
725: fatal("failed to get file content for comparison");
726:
727: if (cvs_buf_differ(b1, b2))
728: ismodified = 1;
729: else
730: ismodified = 0;
1.188 ray 731: cvs_buf_free(b1);
732: cvs_buf_free(b2);
1.62 jfb 733: }
734:
1.202 joris 735: if (cf->file_rcs != NULL && cf->file_rcsrev != NULL &&
736: !RCSNUM_ISBRANCH(cf->file_rcsrev)) {
1.153 joris 737: state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138 joris 738: if (state == NULL)
739: fatal("failed to get state for HEAD for %s",
740: cf->file_path);
1.139 joris 741: if (!strcmp(state, RCS_STATE_DEAD))
1.205 tobias 742: rcsdead = 1;
743:
744: if (cvs_specified_tag == NULL && cf->in_attic &&
745: !RCSNUM_ISBRANCHREV(cf->file_rcsrev))
1.138 joris 746: rcsdead = 1;
1.145 joris 747:
748: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 749: }
750:
1.138 joris 751: /*
752: * 10 Sin
753: * 20 Goto hell
754: * (I welcome you if-else hell)
755: */
756: if (cf->file_ent == NULL) {
757: if (cf->file_rcs == NULL) {
758: if (cf->fd == -1) {
1.178 joris 759: cvs_log(LP_NOTICE,
760: "nothing known about '%s'",
761: cf->file_path);
1.138 joris 762: }
1.130 joris 763:
1.138 joris 764: cf->file_status = FILE_UNKNOWN;
765: } else if (rcsdead == 1) {
766: if (cf->fd == -1) {
767: cf->file_status = FILE_UPTODATE;
1.179 joris 768: } else if (cvs_cmdop != CVS_OP_ADD) {
1.138 joris 769: cf->file_status = FILE_UNKNOWN;
1.44 jfb 770: }
1.199 joris 771: } else if (notag == 0) {
772: cf->file_status = FILE_CHECKOUT;
1.138 joris 773: } else {
1.199 joris 774: cf->file_status = FILE_UPTODATE;
1.14 jfb 775: }
1.208 tobias 776:
777: return;
778: }
779:
780: switch (cf->file_ent->ce_status) {
781: case CVS_ENT_ADDED:
1.138 joris 782: if (cf->fd == -1) {
1.178 joris 783: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 784: cvs_log(LP_NOTICE,
1.157 david 785: "warning: new-born %s has disappeared",
1.138 joris 786: cf->file_path);
1.178 joris 787: }
1.138 joris 788: cf->file_status = FILE_REMOVE_ENTRY;
789: } else if (cf->file_rcs == NULL || rcsdead == 1) {
790: cf->file_status = FILE_ADDED;
791: } else {
1.178 joris 792: cvs_log(LP_NOTICE,
793: "conflict: %s already created by others",
794: cf->file_path);
1.138 joris 795: cf->file_status = FILE_CONFLICT;
796: }
1.208 tobias 797: break;
798: case CVS_ENT_REMOVED:
1.138 joris 799: if (cf->fd != -1) {
1.178 joris 800: cvs_log(LP_NOTICE,
801: "%s should be removed but is still there",
802: cf->file_path);
1.138 joris 803: cf->file_status = FILE_REMOVED;
804: } else if (cf->file_rcs == NULL || rcsdead == 1) {
805: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 806: } else {
1.138 joris 807: if (strcmp(r1, r2)) {
1.178 joris 808: cvs_log(LP_NOTICE,
809: "conflict: removed %s was modified"
810: " by a second party",
811: cf->file_path);
1.138 joris 812: cf->file_status = FILE_CONFLICT;
813: } else {
814: cf->file_status = FILE_REMOVED;
815: }
1.66 joris 816: }
1.208 tobias 817: break;
818: case CVS_ENT_REG:
1.194 joris 819: if (cf->file_rcs == NULL || rcsdead == 1 ||
1.199 joris 820: (reset_stickies == 1 && cf->in_attic == 1) ||
821: (notag == 1 && tag != NULL)) {
1.198 joris 822: if (cf->fd == -1 && server_has_file == 0) {
1.178 joris 823: cvs_log(LP_NOTICE,
824: "warning: %s's entry exists but"
825: " there is no longer a file"
826: " in the repository,"
827: " removing entry",
828: cf->file_path);
1.138 joris 829: cf->file_status = FILE_REMOVE_ENTRY;
830: } else {
831: if (ismodified) {
1.178 joris 832: cvs_log(LP_NOTICE,
833: "conflict: %s is no longer "
834: "in the repository but is "
835: "locally modified",
836: cf->file_path);
1.138 joris 837: cf->file_status = FILE_CONFLICT;
1.204 tobias 838: } else if (cvs_cmdop != CVS_OP_IMPORT) {
1.178 joris 839: cvs_log(LP_NOTICE,
840: "%s is no longer in the "
841: "repository",
842: cf->file_path);
1.128 joris 843:
1.138 joris 844: cf->file_status = FILE_UNLINK;
845: }
846: }
1.128 joris 847: } else {
1.198 joris 848: if (cf->fd == -1 && server_has_file == 0) {
1.178 joris 849: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 850: cvs_log(LP_NOTICE,
851: "warning: %s was lost",
852: cf->file_path);
1.178 joris 853: }
1.138 joris 854: cf->file_status = FILE_LOST;
855: } else {
856: if (ismodified == 1)
857: cf->file_status = FILE_MODIFIED;
858: else
859: cf->file_status = FILE_UPTODATE;
860:
861: if (strcmp(r1, r2)) {
862: if (cf->file_status == FILE_MODIFIED)
863: cf->file_status = FILE_MERGE;
864: else
865: cf->file_status = FILE_PATCH;
866: }
867: }
1.128 joris 868: }
1.208 tobias 869: break;
870: default:
871: break;
1.128 joris 872: }
1.138 joris 873: }
1.14 jfb 874:
1.138 joris 875: void
876: cvs_file_free(struct cvs_file *cf)
877: {
878: xfree(cf->file_name);
879: xfree(cf->file_wd);
880: xfree(cf->file_path);
881:
1.167 joris 882: if (cf->file_rcsrev != NULL)
883: rcsnum_free(cf->file_rcsrev);
1.138 joris 884: if (cf->file_rpath != NULL)
885: xfree(cf->file_rpath);
886: if (cf->file_ent != NULL)
887: cvs_ent_free(cf->file_ent);
888: if (cf->file_rcs != NULL)
889: rcs_close(cf->file_rcs);
890: if (cf->fd != -1)
891: (void)close(cf->fd);
892: if (cf->repo_fd != -1)
893: (void)close(cf->repo_fd);
894: xfree(cf);
1.23 jfb 895: }
896:
1.165 xsa 897: int
1.23 jfb 898: cvs_file_cmpname(const char *name1, const char *name2)
899: {
900: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
901: (strcasecmp(name1, name2));
1.164 xsa 902: }
903:
904: int
905: cvs_file_cmp(const char *file1, const char *file2)
906: {
907: struct stat stb1, stb2;
908: int fd1, fd2, ret;
909:
910: ret = 0;
911:
912: if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
913: fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
914: if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
915: fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
916:
917: if (fstat(fd1, &stb1) == -1)
918: fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
919: if (fstat(fd2, &stb2) == -1)
920: fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
921:
922: if (stb1.st_size != stb2.st_size ||
923: (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
924: ret = 1;
925: goto out;
926: }
927:
928: if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
929: if (stb1.st_rdev != stb2.st_rdev)
930: ret = 1;
931: goto out;
932: }
933:
934: if (S_ISREG(stb1.st_mode)) {
935: void *p1, *p2;
936:
1.187 otto 937: if (stb1.st_size > (off_t)SIZE_MAX) {
1.164 xsa 938: ret = 1;
939: goto out;
940: }
941:
942: if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
943: MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
944: fatal("cvs_file_cmp: mmap failed");
945:
946: if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
947: MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
948: fatal("cvs_file_cmp: mmap failed");
949:
950: madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
951: madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
952:
953: ret = memcmp(p1, p2, stb1.st_size);
954:
955: (void)munmap(p1, stb1.st_size);
956: (void)munmap(p2, stb1.st_size);
957: }
958:
959: out:
960: (void)close(fd1);
961: (void)close(fd2);
1.166 xsa 962:
963: return (ret);
964: }
965:
966: int
967: cvs_file_copy(const char *from, const char *to)
968: {
969: struct stat st;
970: struct timeval tv[2];
971: time_t atime, mtime;
972: int src, dst, ret;
973:
974: ret = 0;
975:
976: cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
977:
978: if (cvs_noexec == 1)
979: return (0);
980:
981: if ((src = open(from, O_RDONLY, 0)) == -1)
982: fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
983:
984: if (fstat(src, &st) == -1)
985: fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
986:
987: atime = st.st_atimespec.tv_sec;
988: mtime = st.st_mtimespec.tv_sec;
989:
990: if (S_ISREG(st.st_mode)) {
1.197 tobias 991: char *p;
1.166 xsa 992: int saved_errno;
993:
1.187 otto 994: if (st.st_size > (off_t)SIZE_MAX) {
1.166 xsa 995: ret = -1;
996: goto out;
997: }
998:
999: if ((dst = open(to, O_CREAT|O_TRUNC|O_WRONLY,
1000: st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) == -1)
1001: fatal("cvs_file_copy: open `%s': %s",
1002: to, strerror(errno));
1003:
1004: if ((p = mmap(NULL, st.st_size, PROT_READ,
1005: MAP_FILE, src, (off_t)0)) == MAP_FAILED) {
1006: saved_errno = errno;
1007: (void)unlink(to);
1008: fatal("cvs_file_copy: mmap: %s", strerror(saved_errno));
1009: }
1010:
1011: madvise(p, st.st_size, MADV_SEQUENTIAL);
1012:
1.197 tobias 1013: if (atomicio(vwrite, dst, p, st.st_size) != st.st_size) {
1014: saved_errno = errno;
1015: (void)unlink(to);
1016: fatal("cvs_file_copy: `%s': %s", from,
1017: strerror(saved_errno));
1.166 xsa 1018: }
1019:
1020: (void)munmap(p, st.st_size);
1021:
1022: tv[0].tv_sec = atime;
1023: tv[1].tv_sec = mtime;
1024:
1025: if (futimes(dst, tv) == -1) {
1026: saved_errno = errno;
1027: (void)unlink(to);
1028: fatal("cvs_file_copy: futimes: %s",
1029: strerror(saved_errno));
1030: }
1031: (void)close(dst);
1032: }
1033: out:
1034: (void)close(src);
1.164 xsa 1035:
1036: return (ret);
1.14 jfb 1037: }