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