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