Re: readfile() improvements
| From: | Alexander Valyalkin | Date: | Mon, 07 Jun 2004 15:55:52 +0000 |
| Subject: | Re: readfile() improvements | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-10299@lists.php.net to get a copy of this message | ||
On Mon, 07 Jun 2004 17:07:06 +0300, Andi Gutmans <andi@zend.com> wrote:
Can you send a unified diff?==================cut================= 1065,1066c1065,1066 < size_t bcount = 0; < int ready = 0; ---
size_t bcount = 0; /* counter of printed out bytes */
int is_mapped = 0;
1067a1068
< #endif 1072d1071 < #ifdef HAVE_MMAP 1077a1077,1078size_t buf_len = sizeof(buf);1070d1070
is_mapped = 1;
buf_len = 1024 * 1024; /* default length of the mapped memory */
1079,1080c1080
< off_t off; < void *p;---
< ---void *p; /* pinter to the mapped part of file */1082c1082
</* get the length of local file connected to descriptor fd */1084,1096c1084,1104
< if (sbuf.st_size > sizeof(buf)) {
< off = php_stream_tell(stream);
< len = sbuf.st_size - off;
< p = mmap(0, len, PROT_READ, MAP_SHARED, fd, off);
< if (p != (void *) MAP_FAILED) {
< BG(mmap_file) = p;
< BG(mmap_len) = len;
< PHPWRITE(p, len);
< BG(mmap_file) = NULL;
< munmap(p, len);
< bcount += len;
< ready = 1;
---
if (errno) {
/* cannot get length of file */
php_error_docref(NULL TSRMLS_CC, E_ERROR, "cannot get length of the file");
return bcount;
}
len = (size_t) sbuf.st_size;
/* print to the output buffer file contents */
while (bcount < len) {
if (len - bcount < buf_len) buf_len = len - bcount;
p = mmap(NULL, buf_len, PROT_READ, MAP_PRIVATE, fd, (off_t) bcount); /* try to map part of the file to memory */
if (p == (void *) MAP_FAILED) {
/* error when mapping part of the file to memory */
php_error_docref(NULL TSRMLS_CC, E_ERROR, "mmap error: cannot map part of the file to memory");
break;
}
PHPWRITE(p, buf_len);
munmap(p, buf_len); /* try to unmap allocated memory */
if (errno) {
/* error when unmapping memory */
php_error_docref(NULL TSRMLS_CC, E_ERROR, "mmap error: cannot unmap allocated memory");
break;
1097a1106
< if(!ready) {bcount += buf_len;1101,1106c1110,1114
< int b;<
< while ((b = php_stream_read(stream, buf, sizeof(buf))) > 0) {
< PHPWRITE(buf, b);
< bcount += b;
---
if (!is_mapped) {
/* print to the output buffer stream contents */
while ((buf_len = php_stream_read(stream, buf, sizeof(buf))) > 0) {
PHPWRITE(buf, buf_len);
bcount += buf_len;
==================cut=================
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/