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