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