utils.c (184342) | utils.c (184471) |
---|---|
1/*- 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 19 unchanged lines hidden (view full) --- 28 */ 29 30#ifndef lint 31#if 0 32static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94"; 33#endif 34#endif /* not lint */ 35#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 19 unchanged lines hidden (view full) --- 28 */ 29 30#ifndef lint 31#if 0 32static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94"; 33#endif 34#endif /* not lint */ 35#include <sys/cdefs.h> |
36__FBSDID("$FreeBSD: head/bin/cp/utils.c 184342 2008-10-27 15:21:15Z dds $"); | 36__FBSDID("$FreeBSD: head/bin/cp/utils.c 184471 2008-10-30 14:05:57Z ivoras $"); |
37 38#include <sys/types.h> 39#include <sys/acl.h> 40#include <sys/param.h> 41#include <sys/stat.h> 42#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 43#include <sys/mman.h> 44#endif --- 7 unchanged lines hidden (view full) --- 52#include <stdlib.h> 53#include <sysexits.h> 54#include <unistd.h> 55 56#include "extern.h" 57 58#define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y))) 59 | 37 38#include <sys/types.h> 39#include <sys/acl.h> 40#include <sys/param.h> 41#include <sys/stat.h> 42#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 43#include <sys/mman.h> 44#endif --- 7 unchanged lines hidden (view full) --- 52#include <stdlib.h> 53#include <sysexits.h> 54#include <unistd.h> 55 56#include "extern.h" 57 58#define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y))) 59 |
60/* Memory strategy threshold, in pages: if physmem is larger then this, use a 61 * large buffer */ 62#define PHYSPAGES_THRESHOLD (32*1024) 63 64/* Maximum buffer size in bytes - do not allow it to grow larger than this */ 65#define BUFSIZE_MAX (2*1024*1024) 66 67/* Small (default) buffer size in bytes. It's inefficient for this to be 68 * smaller than MAXPHYS */ 69#define BUFSIZE_SMALL (MAXPHYS) 70 |
|
60int 61copy_file(const FTSENT *entp, int dne) 62{ | 71int 72copy_file(const FTSENT *entp, int dne) 73{ |
63 static char buf[MAXBSIZE]; | 74 static char *buf = NULL; 75 static size_t bufsize; |
64 struct stat *fs; 65 ssize_t wcount; 66 size_t wresid; 67 off_t wtotal; 68 int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0; 69 char *bufp; 70#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 71 char *p; --- 97 unchanged lines hidden (view full) --- 169 /* Some systems don't unmap on close(2). */ 170 if (munmap(p, fs->st_size) < 0) { 171 warn("%s", entp->fts_path); 172 rval = 1; 173 } 174 } else 175#endif 176 { | 76 struct stat *fs; 77 ssize_t wcount; 78 size_t wresid; 79 off_t wtotal; 80 int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0; 81 char *bufp; 82#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 83 char *p; --- 97 unchanged lines hidden (view full) --- 181 /* Some systems don't unmap on close(2). */ 182 if (munmap(p, fs->st_size) < 0) { 183 warn("%s", entp->fts_path); 184 rval = 1; 185 } 186 } else 187#endif 188 { |
189 if (buf == NULL) { 190 /* 191 * Note that buf and bufsize are static. If 192 * malloc() fails, it will fail at the start 193 * and not copy only some files. 194 */ 195 if (sysconf(_SC_PHYS_PAGES) > 196 PHYSPAGES_THRESHOLD) 197 bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8); 198 else 199 bufsize = BUFSIZE_SMALL; 200 buf = malloc(bufsize); 201 if (buf == NULL) 202 err(1, "Not enough memory"); 203 } |
|
177 wtotal = 0; | 204 wtotal = 0; |
178 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { | 205 while ((rcount = read(from_fd, buf, bufsize)) > 0) { |
179 for (bufp = buf, wresid = rcount; ; 180 bufp += wcount, wresid -= wcount) { 181 wcount = write(to_fd, bufp, wresid); 182 if (wcount <= 0) 183 break; 184 wtotal += wcount; 185 if (info) { 186 info = 0; --- 249 unchanged lines hidden --- | 206 for (bufp = buf, wresid = rcount; ; 207 bufp += wcount, wresid -= wcount) { 208 wcount = write(to_fd, bufp, wresid); 209 if (wcount <= 0) 210 break; 211 wtotal += wcount; 212 if (info) { 213 info = 0; --- 249 unchanged lines hidden --- |