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