com php-src: fixed file_put_contents(): ext/standar d/file.c

From: Date: Wed, 16 Oct 2013 10:09:48 +0000
Subject: com php-src: fixed file_put_contents(): ext/standar d/file.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-73479@lists.php.net to get a copy of this message
Commit: 469b9e44fb9f0c2871b041beae3c8ea744ab4af3 Author: Anatol Belski <ab@php.net> Thu, 22 Aug 2013 01:27:08 -0700 Parents: b7f87230652bf23c3676394f0ce3497e1f8fa703 Branches: str_size_and_int64 Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=469b9e44fb9f0c2871b041beae3c8ea744ab4af3 Log: fixed file_put_contents() Changed paths: M ext/standard/file.c Diff: diff --git a/ext/standard/file.c b/ext/standard/file.c index e3cee07..144270f 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -576,6 +576,7 @@ PHP_FUNCTION(file_put_contents) php_stream_context *context = NULL; php_stream *srcstream = NULL; char mode[3] = "wb"; + char ret_ok = 1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Pz/|ir!", &filename, &filename_len, &data, &flags, &zcontext) == FAILURE) { return; @@ -616,11 +617,13 @@ PHP_FUNCTION(file_put_contents) php_stream_truncate_set_size(stream, 0); } + /* In case of php_stream_write() it returns size_t, so we only can check if the full data was written. + XXX maybe the write should be retried once more */ switch (Z_TYPE_P(data)) { case IS_RESOURCE: { zend_str_size len; if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &len) != SUCCESS) { - numbytes = -1; + ret_ok = 0; } else { numbytes = len; } @@ -638,14 +641,13 @@ PHP_FUNCTION(file_put_contents) numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRSIZE_P(data)); if (numbytes != Z_STRSIZE_P(data)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, Z_STRSIZE_P(data)); - numbytes = -1; + ret_ok = 0; } } break; case IS_ARRAY: if (zend_hash_num_elements(Z_ARRVAL_P(data))) { - zend_str_size bytes_written; zval **tmp; HashPosition pos; @@ -656,15 +658,13 @@ PHP_FUNCTION(file_put_contents) convert_to_string(*tmp); } if (Z_STRSIZE_PP(tmp)) { + zend_str_size bytes_written; + numbytes += Z_STRSIZE_PP(tmp); bytes_written = php_stream_write(stream, Z_STRVAL_PP(tmp), Z_STRSIZE_PP(tmp)); - if (bytes_written < 0 || bytes_written != Z_STRSIZE_PP(tmp)) { - if (bytes_written < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write %d bytes to %s", Z_STRSIZE_PP(tmp), filename); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", bytes_written, Z_STRSIZE_PP(tmp)); - } - numbytes = -1; + if (bytes_written != Z_STRSIZE_PP(tmp)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write %d bytes to %s", Z_STRSIZE_PP(tmp), filename); + ret_ok = 0; break; } } @@ -681,19 +681,19 @@ PHP_FUNCTION(file_put_contents) numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRSIZE(out)); if (numbytes != Z_STRSIZE(out)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, Z_STRSIZE(out)); - numbytes = -1; + ret_ok = 0; } zval_dtor(&out); break; } } default: - numbytes = -1; + ret_ok = 0; break; } php_stream_close(stream); - if (numbytes < 0) { + if (!ret_ok) { RETURN_FALSE; }

« previous php.cvs (#73479) next »