From: George Peter Banyard Date: Thu, 3 Sep 2020 13:49:28 +0000 (+0200) Subject: Extract common flock code X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7805b97720d03b5f3b9f0296c26a56cce4c77cea;p=php Extract common flock code As SPL is currently a copie of the code in file.c Closes GH-6069 --- diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index e782b1e70a..3f3391334a 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -2498,15 +2498,11 @@ PHP_METHOD(SplFileObject, getCsvControl) } /* }}} */ -static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN }; - -/* {{{ Portable file locking, copy pasted from ext/standard/file.c flock() function. - * This is done to prevent this to fail if flock is disabled via disable_functions */ +/* {{{ Portable file locking */ PHP_METHOD(SplFileObject, flock) { spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS); zval *wouldblock = NULL; - int act; zend_long operation = 0; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|z", &operation, &wouldblock) == FAILURE) { @@ -2515,25 +2511,7 @@ PHP_METHOD(SplFileObject, flock) CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern); - act = operation & PHP_LOCK_UN; - if (act < 1 || act > 3) { - zend_argument_value_error(1, "must be either LOCK_SH, LOCK_EX, or LOCK_UN"); - RETURN_THROWS(); - } - - if (wouldblock) { - ZEND_TRY_ASSIGN_REF_LONG(wouldblock, 0); - } - - /* flock_values contains all possible actions if (operation & PHP_LOCK_NB) we won't block on the lock */ - act = flock_values[act - 1] | (operation & PHP_LOCK_NB ? LOCK_NB : 0); - if (php_stream_lock(intern->u.file.stream, act)) { - if (operation && errno == EWOULDBLOCK && wouldblock) { - ZEND_TRY_ASSIGN_REF_LONG(wouldblock, 1); - } - RETURN_FALSE; - } - RETURN_TRUE; + php_flock_common(intern->u.file.stream, operation, 1, wouldblock, return_value); } /* }}} */ diff --git a/ext/standard/file.c b/ext/standard/file.c index d402c60acf..9883973c5d 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -325,28 +325,15 @@ PHP_MSHUTDOWN_FUNCTION(file) /* {{{ */ } /* }}} */ -static int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN }; - -/* {{{ Portable file locking */ -PHP_FUNCTION(flock) +PHPAPI void php_flock_common(php_stream *stream, zend_long operation, + uint32_t operation_arg_num, zval *wouldblock, zval *return_value) { - zval *res, *wouldblock = NULL; + int flock_values[] = { LOCK_SH, LOCK_EX, LOCK_UN }; int act; - php_stream *stream; - zend_long operation = 0; - - ZEND_PARSE_PARAMETERS_START(2, 3) - Z_PARAM_RESOURCE(res) - Z_PARAM_LONG(operation) - Z_PARAM_OPTIONAL - Z_PARAM_ZVAL(wouldblock) - ZEND_PARSE_PARAMETERS_END(); - - PHP_STREAM_TO_ZVAL(stream, res); act = operation & PHP_LOCK_UN; if (act < 1 || act > 3) { - zend_argument_value_error(2, "must be either LOCK_SH, LOCK_EX, or LOCK_UN"); + zend_argument_value_error(operation_arg_num, "must be either LOCK_SH, LOCK_EX, or LOCK_UN"); RETURN_THROWS(); } @@ -364,6 +351,25 @@ PHP_FUNCTION(flock) } RETURN_TRUE; } + +/* {{{ Portable file locking */ +PHP_FUNCTION(flock) +{ + zval *res, *wouldblock = NULL; + php_stream *stream; + zend_long operation = 0; + + ZEND_PARSE_PARAMETERS_START(2, 3) + Z_PARAM_RESOURCE(res) + Z_PARAM_LONG(operation) + Z_PARAM_OPTIONAL + Z_PARAM_ZVAL(wouldblock) + ZEND_PARSE_PARAMETERS_END(); + + PHP_STREAM_TO_ZVAL(stream, res); + + php_flock_common(stream, operation, 2, wouldblock, return_value); +} /* }}} */ #define PHP_META_UNSAFE ".\\+*?[^]$() " diff --git a/ext/standard/file.h b/ext/standard/file.h index 1faf667a00..c51a953086 100644 --- a/ext/standard/file.h +++ b/ext/standard/file.h @@ -44,6 +44,8 @@ PHPAPI int php_copy_file_ctx(const char *src, const char *dest, int src_chk, php PHPAPI int php_mkdir_ex(const char *dir, zend_long mode, int options); PHPAPI int php_mkdir(const char *dir, zend_long mode); PHPAPI void php_fstat(php_stream *stream, zval *return_value); +PHPAPI void php_flock_common(php_stream *stream, zend_long operation, uint32_t operation_arg_num, + zval *wouldblock, zval *return_value); #define PHP_CSV_NO_ESCAPE EOF PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf, zval *return_value);