From 3c9f50efaf4be64a6e824d927c92ee4301bbcde9 Mon Sep 17 00:00:00 2001 From: Sascha Schumann Date: Fri, 22 Dec 2000 16:34:11 +0000 Subject: [PATCH] (php_file_copy) Use mmap to map the source file into our address space and then simply write it out to the target file. That avoids switching between user and kernel land too many times. If that does not work, we fall back to the read/write method. --- ext/standard/file.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/ext/standard/file.c b/ext/standard/file.c index f9fb41f10c..ae0b9ab527 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -1658,6 +1658,7 @@ PHPAPI int php_copy_file(char *src, char *dest) { char buffer[8192]; int fd_s,fd_t,read_bytes; + int ret = FAILURE; #ifdef PHP_WIN32 if ((fd_s=V_OPEN((src,O_RDONLY|_O_BINARY)))==-1) { @@ -1677,18 +1678,36 @@ PHPAPI int php_copy_file(char *src, char *dest) return FAILURE; } +#ifdef HAVE_MMAP + { + void *srcfile; + struct stat sbuf; + + if (fstat(fd_s, &sbuf)) { + goto cleanup; + } + srcfile = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, fd_s, 0); + if (srcfile) { + write(fd_t, srcfile, sbuf.st_size); + ret = SUCCESS; + munmap(srcfile, sbuf.st_size); + goto cleanup; + } + } +#endif + while ((read_bytes=read(fd_s,buffer,8192))!=-1 && read_bytes!=0) { if (write(fd_t,buffer,read_bytes)==-1) { php_error(E_WARNING,"Unable to write to '%s': %s", dest, strerror(errno)); - close(fd_s); - close(fd_t); - return FAILURE; + goto cleanup; } } + ret = SUCCESS; +cleanup: close(fd_s); close(fd_t); - return SUCCESS; + return ret; } -- 2.40.0