diff options
author | Ilia Alshanetsky <iliaa@php.net> | 2007-11-12 18:44:18 +0000 |
---|---|---|
committer | Ilia Alshanetsky <iliaa@php.net> | 2007-11-12 18:44:18 +0000 |
commit | 6a792bed75aa767edd0fdbd63b5772019917db21 (patch) | |
tree | 3b73f4365bfe741fca979dc5966313d1ae5803a1 /ext/standard/file.c | |
parent | 727a35f1cabb85afa13b6c0b3ce54b2ba1786836 (diff) | |
download | php-git-6a792bed75aa767edd0fdbd63b5772019917db21.tar.gz |
Fixed bug #43182 (file_put_contents() LOCK_EX does not work properly on file
truncation).
Diffstat (limited to 'ext/standard/file.c')
-rw-r--r-- | ext/standard/file.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/ext/standard/file.c b/ext/standard/file.c index b55fd49b5b..45c31c7201 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -581,6 +581,7 @@ PHP_FUNCTION(file_put_contents) zval *zcontext = NULL; php_stream_context *context = NULL; php_stream *srcstream = NULL; + char mode[3] = "wb"; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|lr!", &filename, &filename_len, &data, &flags, &zcontext) == FAILURE) { return; @@ -592,8 +593,14 @@ PHP_FUNCTION(file_put_contents) context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); - stream = php_stream_open_wrapper_ex(filename, (flags & PHP_FILE_APPEND) ? "ab" : "wb", - ((flags & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); + if (flags & PHP_FILE_APPEND) { + mode[0] = 'a'; + } else if (flags & LOCK_EX) { + mode[0] = 'c'; + } + mode[2] = '\0'; + + stream = php_stream_open_wrapper_ex(filename, mode, ((flags & PHP_FILE_USE_INCLUDE_PATH) ? USE_PATH : 0) | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); if (stream == NULL) { RETURN_FALSE; } @@ -603,6 +610,10 @@ PHP_FUNCTION(file_put_contents) RETURN_FALSE; } + if (mode[0] = 'c') { + php_stream_truncate_set_size(stream, 0); + } + switch (Z_TYPE_P(data)) { case IS_RESOURCE: numbytes = php_stream_copy_to_stream(srcstream, stream, PHP_STREAM_COPY_ALL); |