Annotation of src/usr.bin/cvs/file.c, Revision 1.259
1.259 ! nicm 1: /* $OpenBSD: file.c,v 1.258 2009/03/28 16:47:33 joris Exp $ */
1.1 jfb 2: /*
1.138 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26 jfb 5: * All rights reserved.
1.1 jfb 6: *
1.26 jfb 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
1.1 jfb 10: *
1.26 jfb 11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
1.1 jfb 13: * 2. The name of the author may not be used to endorse or promote products
1.26 jfb 14: * derived from this software without specific prior written permission.
1.1 jfb 15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.26 jfb 25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 26: */
27:
1.189 otto 28: #include <sys/types.h>
29: #include <sys/mman.h>
30: #include <sys/stat.h>
1.213 tobias 31: #include <sys/time.h>
1.1 jfb 32:
1.189 otto 33: #include <errno.h>
34: #include <fcntl.h>
35: #include <fnmatch.h>
36: #include <libgen.h>
37: #include <string.h>
38: #include <unistd.h>
1.164 xsa 39:
1.197 tobias 40: #include "atomicio.h"
1.1 jfb 41: #include "cvs.h"
1.198 joris 42: #include "remote.h"
1.1 jfb 43:
1.104 xsa 44: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
1.1 jfb 45:
1.104 xsa 46: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
1.1 jfb 47:
1.196 tobias 48: extern int print_stdout;
1.243 joris 49: extern int build_dirs;
1.196 tobias 50:
1.1 jfb 51: /*
52: * Standard patterns to ignore.
53: */
54: static const char *cvs_ign_std[] = {
55: ".",
56: "..",
57: "*.o",
1.45 xsa 58: "*.a",
1.1 jfb 59: "*.bak",
60: "*.orig",
61: "*.rej",
1.45 xsa 62: "*.old",
1.1 jfb 63: "*.exe",
64: "*.depend",
1.45 xsa 65: "*.obj",
66: "*.elc",
67: "*.ln",
68: "*.olb",
1.1 jfb 69: "CVS",
70: "core",
1.102 joris 71: "cvslog*",
1.49 jfb 72: "*.core",
1.8 jfb 73: ".#*",
1.45 xsa 74: "*~",
75: "_$*",
76: "*$",
1.1 jfb 77: };
78:
1.199 joris 79: char *cvs_directory_tag = NULL;
1.138 joris 80: struct ignore_head cvs_ign_pats;
81: struct ignore_head dir_ign_pats;
1.214 joris 82: struct ignore_head checkout_ign_pats;
1.1 jfb 83:
1.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;
396: long base;
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.243 joris 599: if (stat(fpath, &st) == -1 || build_dirs == 1)
1.212 tobias 600: cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
1.251 joris 601: (cr->flags & CR_RECURSE_DIRS) ?
602: REPOSITORY_DODIRS : 0);
1.140 joris 603: }
1.68 joris 604:
1.138 joris 605: cvs_file_walklist(&fl, cr);
606: cvs_file_freelist(&fl);
1.68 joris 607:
1.234 joris 608: if (current_cvsroot->cr_method == CVS_METHOD_LOCAL &&
609: (cmdp->cmd_flags & CVS_LOCK_REPO))
1.140 joris 610: cvs_repository_unlock(repo);
1.68 joris 611:
1.241 tobias 612: if (cvs_directory_tag != NULL && cmdp->cmd_flags & CVS_USE_WDIR) {
613: cvs_write_tagfile(cf->file_path, cvs_directory_tag, NULL);
614: xfree(cvs_directory_tag);
615: cvs_directory_tag = NULL;
616: }
617:
1.138 joris 618: cvs_file_walklist(&dl, cr);
619: cvs_file_freelist(&dl);
1.34 jfb 620:
1.138 joris 621: if (cr->leavedir != NULL)
622: cr->leavedir(cf);
1.3 jfb 623: }
624:
625: void
1.138 joris 626: cvs_file_freelist(struct cvs_flisthead *fl)
1.3 jfb 627: {
1.256 joris 628: struct cvs_filelist *f, *nxt;
1.62 jfb 629:
1.256 joris 630: for (f = RB_MIN(cvs_flisthead, fl); f != NULL; f = nxt) {
631: nxt = RB_NEXT(cvs_flisthead, fl, f);
632: RB_REMOVE(cvs_flisthead, fl, f);
1.138 joris 633: xfree(f->file_path);
634: xfree(f);
1.62 jfb 635: }
1.5 jfb 636: }
637:
1.138 joris 638: void
1.185 joris 639: cvs_file_classify(struct cvs_file *cf, const char *tag)
1.3 jfb 640: {
1.138 joris 641: size_t len;
642: struct stat st;
1.149 joris 643: BUF *b1, *b2;
1.199 joris 644: int server_has_file, notag;
1.183 xsa 645: int rflags, ismodified, rcsdead;
1.138 joris 646: CVSENTRIES *entlist = NULL;
647: const char *state;
1.193 xsa 648: char repo[MAXPATHLEN], rcsfile[MAXPATHLEN];
1.138 joris 649:
1.198 joris 650: cvs_log(LP_TRACE, "cvs_file_classify(%s, %s)", cf->file_path,
651: (tag != NULL) ? tag : "none");
1.138 joris 652:
1.232 tobias 653: if (!strcmp(cf->file_path, ".")) {
1.138 joris 654: cf->file_status = FILE_UPTODATE;
655: return;
656: }
657:
1.146 joris 658: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.183 xsa 659: (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s",
1.138 joris 660: repo, cf->file_name);
661:
1.211 tobias 662: if (cf->file_type == CVS_FILE) {
1.138 joris 663: len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
664: if (len >= MAXPATHLEN)
665: fatal("cvs_file_classify: truncation");
666: }
667:
668: cf->file_rpath = xstrdup(rcsfile);
1.211 tobias 669:
670: if (cmdp->cmd_flags & CVS_USE_WDIR) {
671: entlist = cvs_ent_open(cf->file_wd);
672: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
673: } else
674: cf->file_ent = NULL;
1.138 joris 675:
1.144 joris 676: if (cf->file_ent != NULL) {
1.257 joris 677: if (cf->file_ent->ce_tag != NULL && cvs_specified_tag == NULL)
678: tag = cf->file_ent->ce_tag;
679:
680: if (cf->file_flags & FILE_ON_DISK &&
681: cf->file_ent->ce_type == CVS_ENT_FILE &&
682: cf->file_type == CVS_DIR && tag != NULL) {
683: cf->file_status = FILE_SKIP;
684: return;
685: }
686:
687: if (cf->file_flags & FILE_ON_DISK &&
688: cf->file_ent->ce_type == CVS_ENT_DIR &&
689: cf->file_type == CVS_FILE && tag != NULL) {
1.258 joris 690: cf->file_status = FILE_SKIP;
691: return;
692: }
693:
694: if (cf->file_flags & FILE_INSIDE_ATTIC &&
695: cf->file_ent->ce_type == CVS_ENT_DIR &&
696: cf->file_type != CVS_DIR) {
1.257 joris 697: cf->file_status = FILE_SKIP;
698: return;
699: }
700:
1.252 joris 701: if (cf->file_flags & FILE_ON_DISK &&
702: cf->file_ent->ce_type == CVS_ENT_DIR &&
1.144 joris 703: cf->file_type != CVS_DIR)
1.148 pedro 704: fatal("%s is supposed to be a directory, but it is not",
1.144 joris 705: cf->file_path);
1.252 joris 706: if (cf->file_flags & FILE_ON_DISK &&
707: cf->file_ent->ce_type == CVS_ENT_FILE &&
1.144 joris 708: cf->file_type != CVS_FILE)
1.148 pedro 709: fatal("%s is supposed to be a file, but it is not",
1.144 joris 710: cf->file_path);
711: }
712:
1.138 joris 713: if (cf->file_type == CVS_DIR) {
1.211 tobias 714: if (!(cmdp->cmd_flags & CVS_USE_WDIR))
715: cf->file_status = FILE_UPTODATE;
716: else if (cf->fd == -1 && stat(rcsfile, &st) != -1)
1.138 joris 717: cf->file_status = DIR_CREATE;
1.209 tobias 718: else if (cf->file_ent != NULL || cvs_cmdop == CVS_OP_RLOG ||
719: cvs_cmdop == CVS_OP_RTAG)
1.138 joris 720: cf->file_status = FILE_UPTODATE;
1.144 joris 721: else
722: cf->file_status = FILE_UNKNOWN;
723:
1.138 joris 724: return;
725: }
726:
1.147 joris 727: rflags = RCS_READ;
728: switch (cvs_cmdop) {
729: case CVS_OP_COMMIT:
1.255 joris 730: case CVS_OP_TAG:
731: case CVS_OP_RTAG:
1.147 joris 732: rflags = RCS_WRITE;
733: break;
1.236 joris 734: case CVS_OP_ADMIN:
1.156 joris 735: case CVS_OP_IMPORT:
1.147 joris 736: case CVS_OP_LOG:
1.192 niallo 737: case CVS_OP_RLOG:
1.147 joris 738: rflags |= RCS_PARSE_FULLY;
739: break;
1.208 tobias 740: default:
741: break;
1.147 joris 742: }
743:
1.138 joris 744: cf->repo_fd = open(cf->file_rpath, O_RDONLY);
745: if (cf->repo_fd != -1) {
746: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
747: if (cf->file_rcs == NULL)
1.152 joris 748: fatal("cvs_file_classify: failed to parse RCS");
1.194 joris 749: } else {
750: (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s/%s%s",
751: repo, CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
752:
753: cf->repo_fd = open(rcsfile, O_RDONLY);
754: if (cf->repo_fd != -1) {
755: xfree(cf->file_rpath);
756: cf->file_rpath = xstrdup(rcsfile);
757: cf->file_rcs = rcs_open(cf->file_rpath,
758: cf->repo_fd, rflags);
759: if (cf->file_rcs == NULL)
760: fatal("cvs_file_classify: failed to parse RCS");
761: cf->in_attic = 1;
762: } else {
763: cf->file_rcs = NULL;
764: }
1.190 niallo 765: }
766:
1.199 joris 767: notag = 0;
1.200 joris 768: cf->file_flags |= FILE_HAS_TAG;
1.190 niallo 769: if (tag != NULL && cf->file_rcs != NULL) {
1.218 tobias 770: if ((cf->file_rcsrev = rcs_translate_tag(tag, cf->file_rcs))
1.238 tobias 771: == NULL) {
1.218 tobias 772: cf->file_rcsrev = rcs_translate_tag(NULL, cf->file_rcs);
1.206 tobias 773: if (cf->file_rcsrev != NULL) {
774: notag = 1;
775: cf->file_flags &= ~FILE_HAS_TAG;
776: }
1.152 joris 777: }
1.172 niallo 778: } else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL) {
1.169 joris 779: cf->file_rcsrev = rcsnum_alloc();
780: rcsnum_cpy(cf->file_ent->ce_rev, cf->file_rcsrev, 0);
1.190 niallo 781: } else if (cf->file_rcs != NULL) {
1.218 tobias 782: cf->file_rcsrev = rcs_translate_tag(NULL, cf->file_rcs);
1.190 niallo 783: } else {
1.153 joris 784: cf->file_rcsrev = NULL;
1.190 niallo 785: }
1.153 joris 786:
1.138 joris 787: ismodified = rcsdead = 0;
1.252 joris 788: if ((cf->file_flags & FILE_ON_DISK) && cf->file_ent != NULL) {
1.138 joris 789: if (fstat(cf->fd, &st) == -1)
790: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 791:
1.177 joris 792: if (st.st_mtime != cf->file_ent->ce_mtime)
1.138 joris 793: ismodified = 1;
1.149 joris 794: }
795:
1.198 joris 796: server_has_file = 0;
797: if (cvs_server_active == 1 && cf->file_ent != NULL &&
798: cf->file_ent->ce_mtime == CVS_SERVER_UPTODATE) {
799: server_has_file = 1;
800: ismodified = 0;
801: }
1.251 joris 802:
803: if ((server_has_file == 1) || (cf->fd != -1))
804: cf->file_flags |= FILE_ON_DISK;
1.198 joris 805:
1.252 joris 806: if (ismodified == 1 &&
807: (cf->file_flags & FILE_ON_DISK) && cf->file_rcs != NULL &&
1.227 joris 808: cf->file_ent != NULL && !RCSNUM_ISBRANCH(cf->file_ent->ce_rev) &&
809: cf->file_ent->ce_status != CVS_ENT_ADDED) {
1.225 joris 810: b1 = rcs_rev_getbuf(cf->file_rcs, cf->file_ent->ce_rev, 0);
1.222 tobias 811: b2 = cvs_buf_load_fd(cf->fd);
1.149 joris 812:
813: if (cvs_buf_differ(b1, b2))
814: ismodified = 1;
815: else
816: ismodified = 0;
1.188 ray 817: cvs_buf_free(b1);
818: cvs_buf_free(b2);
1.62 jfb 819: }
820:
1.202 joris 821: if (cf->file_rcs != NULL && cf->file_rcsrev != NULL &&
822: !RCSNUM_ISBRANCH(cf->file_rcsrev)) {
1.153 joris 823: state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138 joris 824: if (state == NULL)
825: fatal("failed to get state for HEAD for %s",
826: cf->file_path);
1.139 joris 827: if (!strcmp(state, RCS_STATE_DEAD))
1.205 tobias 828: rcsdead = 1;
829:
1.248 tobias 830: if (cvs_specified_date == -1 && cvs_directory_date == -1 &&
831: tag == NULL && cf->in_attic &&
1.205 tobias 832: !RCSNUM_ISBRANCHREV(cf->file_rcsrev))
1.138 joris 833: rcsdead = 1;
1.145 joris 834:
835: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 836: }
837:
1.138 joris 838: /*
839: * 10 Sin
840: * 20 Goto hell
841: * (I welcome you if-else hell)
842: */
843: if (cf->file_ent == NULL) {
844: if (cf->file_rcs == NULL) {
1.252 joris 845: if (!(cf->file_flags & FILE_ON_DISK)) {
1.178 joris 846: cvs_log(LP_NOTICE,
847: "nothing known about '%s'",
848: cf->file_path);
1.138 joris 849: }
1.130 joris 850:
1.138 joris 851: cf->file_status = FILE_UNKNOWN;
1.249 tobias 852: } else if (rcsdead == 1 || !(cf->file_flags & FILE_HAS_TAG)) {
1.252 joris 853: if (!(cf->file_flags & FILE_ON_DISK)) {
1.138 joris 854: cf->file_status = FILE_UPTODATE;
1.179 joris 855: } else if (cvs_cmdop != CVS_OP_ADD) {
1.138 joris 856: cf->file_status = FILE_UNKNOWN;
1.44 jfb 857: }
1.218 tobias 858: } else if (notag == 0 && cf->file_rcsrev != NULL) {
1.199 joris 859: cf->file_status = FILE_CHECKOUT;
1.138 joris 860: } else {
1.199 joris 861: cf->file_status = FILE_UPTODATE;
1.14 jfb 862: }
1.208 tobias 863:
864: return;
865: }
866:
867: switch (cf->file_ent->ce_status) {
868: case CVS_ENT_ADDED:
1.252 joris 869: if (!(cf->file_flags & FILE_ON_DISK)) {
1.178 joris 870: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 871: cvs_log(LP_NOTICE,
1.157 david 872: "warning: new-born %s has disappeared",
1.138 joris 873: cf->file_path);
1.178 joris 874: }
1.138 joris 875: cf->file_status = FILE_REMOVE_ENTRY;
1.249 tobias 876: } else if (cf->file_rcs == NULL || rcsdead == 1 ||
877: !(cf->file_flags & FILE_HAS_TAG)) {
1.138 joris 878: cf->file_status = FILE_ADDED;
879: } else {
1.178 joris 880: cvs_log(LP_NOTICE,
881: "conflict: %s already created by others",
882: cf->file_path);
1.138 joris 883: cf->file_status = FILE_CONFLICT;
884: }
1.208 tobias 885: break;
886: case CVS_ENT_REMOVED:
1.252 joris 887: if (cf->file_flags & FILE_ON_DISK) {
1.178 joris 888: cvs_log(LP_NOTICE,
889: "%s should be removed but is still there",
890: cf->file_path);
1.138 joris 891: cf->file_status = FILE_REMOVED;
892: } else if (cf->file_rcs == NULL || rcsdead == 1) {
893: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 894: } else {
1.238 tobias 895: if (rcsnum_differ(cf->file_ent->ce_rev,
896: cf->file_rcsrev) && cvs_cmdop != CVS_OP_ADD) {
1.178 joris 897: cvs_log(LP_NOTICE,
898: "conflict: removed %s was modified"
899: " by a second party",
900: cf->file_path);
1.138 joris 901: cf->file_status = FILE_CONFLICT;
902: } else {
903: cf->file_status = FILE_REMOVED;
904: }
1.66 joris 905: }
1.208 tobias 906: break;
907: case CVS_ENT_REG:
1.218 tobias 908: if (cf->file_rcs == NULL || cf->file_rcsrev == NULL ||
1.219 tobias 909: rcsdead == 1 || (reset_tag == 1 && cf->in_attic == 1) ||
910: (notag == 1 && tag != NULL)) {
1.252 joris 911: if (!(cf->file_flags & FILE_ON_DISK)) {
1.178 joris 912: cvs_log(LP_NOTICE,
913: "warning: %s's entry exists but"
1.235 joris 914: " is no longer in the repository,"
1.178 joris 915: " removing entry",
916: cf->file_path);
1.138 joris 917: cf->file_status = FILE_REMOVE_ENTRY;
918: } else {
919: if (ismodified) {
1.178 joris 920: cvs_log(LP_NOTICE,
921: "conflict: %s is no longer "
922: "in the repository but is "
923: "locally modified",
924: cf->file_path);
1.242 tobias 925: if (cvs_cmdop == CVS_OP_COMMIT)
926: cf->file_status = FILE_UNLINK;
927: else
928: cf->file_status = FILE_CONFLICT;
1.204 tobias 929: } else if (cvs_cmdop != CVS_OP_IMPORT) {
1.178 joris 930: cvs_log(LP_NOTICE,
931: "%s is no longer in the "
932: "repository",
933: cf->file_path);
1.128 joris 934:
1.138 joris 935: cf->file_status = FILE_UNLINK;
936: }
937: }
1.218 tobias 938: } else if (cf->file_rcsrev == NULL) {
939: cf->file_status = FILE_UNLINK;
1.237 xsa 940: } else {
1.252 joris 941: if (!(cf->file_flags & FILE_ON_DISK)) {
1.178 joris 942: if (cvs_cmdop != CVS_OP_REMOVE) {
1.138 joris 943: cvs_log(LP_NOTICE,
944: "warning: %s was lost",
945: cf->file_path);
1.178 joris 946: }
1.138 joris 947: cf->file_status = FILE_LOST;
948: } else {
949: if (ismodified == 1)
950: cf->file_status = FILE_MODIFIED;
951: else
952: cf->file_status = FILE_UPTODATE;
1.238 tobias 953: if (rcsnum_differ(cf->file_ent->ce_rev,
954: cf->file_rcsrev)) {
1.138 joris 955: if (cf->file_status == FILE_MODIFIED)
956: cf->file_status = FILE_MERGE;
957: else
958: cf->file_status = FILE_PATCH;
959: }
960: }
1.128 joris 961: }
1.254 joris 962: break;
963: case CVS_ENT_UNKNOWN:
964: if (cvs_server_active != 1)
965: fatal("server-side questionable in local mode?");
966: cf->file_status = FILE_UNKNOWN;
1.208 tobias 967: break;
968: default:
969: break;
1.128 joris 970: }
1.138 joris 971: }
1.14 jfb 972:
1.138 joris 973: void
974: cvs_file_free(struct cvs_file *cf)
975: {
976: xfree(cf->file_name);
977: xfree(cf->file_wd);
978: xfree(cf->file_path);
979:
1.167 joris 980: if (cf->file_rcsrev != NULL)
981: rcsnum_free(cf->file_rcsrev);
1.138 joris 982: if (cf->file_rpath != NULL)
983: xfree(cf->file_rpath);
984: if (cf->file_ent != NULL)
985: cvs_ent_free(cf->file_ent);
986: if (cf->file_rcs != NULL)
987: rcs_close(cf->file_rcs);
988: if (cf->fd != -1)
989: (void)close(cf->fd);
990: if (cf->repo_fd != -1)
991: (void)close(cf->repo_fd);
992: xfree(cf);
1.23 jfb 993: }
994:
1.165 xsa 995: int
1.23 jfb 996: cvs_file_cmpname(const char *name1, const char *name2)
997: {
998: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
999: (strcasecmp(name1, name2));
1.164 xsa 1000: }
1001:
1002: int
1003: cvs_file_cmp(const char *file1, const char *file2)
1004: {
1005: struct stat stb1, stb2;
1006: int fd1, fd2, ret;
1007:
1008: ret = 0;
1009:
1010: if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
1011: fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
1012: if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
1013: fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
1014:
1015: if (fstat(fd1, &stb1) == -1)
1016: fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
1017: if (fstat(fd2, &stb2) == -1)
1018: fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
1019:
1020: if (stb1.st_size != stb2.st_size ||
1021: (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
1022: ret = 1;
1023: goto out;
1024: }
1025:
1026: if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
1027: if (stb1.st_rdev != stb2.st_rdev)
1028: ret = 1;
1029: goto out;
1030: }
1031:
1032: if (S_ISREG(stb1.st_mode)) {
1033: void *p1, *p2;
1034:
1.228 tobias 1035: if (stb1.st_size > SIZE_MAX) {
1.164 xsa 1036: ret = 1;
1037: goto out;
1.226 deraadt 1038: }
1.164 xsa 1039:
1040: if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
1041: MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
1042: fatal("cvs_file_cmp: mmap failed");
1043:
1044: if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
1045: MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
1046: fatal("cvs_file_cmp: mmap failed");
1047:
1048: madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
1049: madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
1050:
1051: ret = memcmp(p1, p2, stb1.st_size);
1052:
1053: (void)munmap(p1, stb1.st_size);
1054: (void)munmap(p2, stb1.st_size);
1055: }
1056:
1057: out:
1058: (void)close(fd1);
1059: (void)close(fd2);
1.166 xsa 1060:
1061: return (ret);
1062: }
1063:
1064: int
1065: cvs_file_copy(const char *from, const char *to)
1066: {
1067: struct stat st;
1068: struct timeval tv[2];
1069: time_t atime, mtime;
1070: int src, dst, ret;
1071:
1072: ret = 0;
1073:
1074: cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
1075:
1076: if (cvs_noexec == 1)
1077: return (0);
1078:
1079: if ((src = open(from, O_RDONLY, 0)) == -1)
1080: fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
1081:
1082: if (fstat(src, &st) == -1)
1083: fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
1084:
1085: atime = st.st_atimespec.tv_sec;
1086: mtime = st.st_mtimespec.tv_sec;
1087:
1088: if (S_ISREG(st.st_mode)) {
1.197 tobias 1089: char *p;
1.166 xsa 1090: int saved_errno;
1091:
1.228 tobias 1092: if (st.st_size > SIZE_MAX) {
1.166 xsa 1093: ret = -1;
1094: goto out;
1.226 deraadt 1095: }
1.166 xsa 1096:
1097: if ((dst = open(to, O_CREAT|O_TRUNC|O_WRONLY,
1098: st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) == -1)
1099: fatal("cvs_file_copy: open `%s': %s",
1100: to, strerror(errno));
1101:
1102: if ((p = mmap(NULL, st.st_size, PROT_READ,
1103: MAP_FILE, src, (off_t)0)) == MAP_FAILED) {
1104: saved_errno = errno;
1105: (void)unlink(to);
1106: fatal("cvs_file_copy: mmap: %s", strerror(saved_errno));
1107: }
1108:
1109: madvise(p, st.st_size, MADV_SEQUENTIAL);
1110:
1.197 tobias 1111: if (atomicio(vwrite, dst, p, st.st_size) != st.st_size) {
1112: saved_errno = errno;
1113: (void)unlink(to);
1114: fatal("cvs_file_copy: `%s': %s", from,
1115: strerror(saved_errno));
1.166 xsa 1116: }
1117:
1118: (void)munmap(p, st.st_size);
1119:
1120: tv[0].tv_sec = atime;
1121: tv[1].tv_sec = mtime;
1122:
1123: if (futimes(dst, tv) == -1) {
1124: saved_errno = errno;
1125: (void)unlink(to);
1126: fatal("cvs_file_copy: futimes: %s",
1127: strerror(saved_errno));
1128: }
1129: (void)close(dst);
1130: }
1131: out:
1132: (void)close(src);
1.164 xsa 1133:
1134: return (ret);
1.256 joris 1135: }
1136:
1137: int
1138: cvs_filelist_cmp(struct cvs_filelist *f1, struct cvs_filelist *f2)
1139: {
1140: return (strcmp(f1->file_path, f2->file_path));
1.14 jfb 1141: }