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