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