]> granicus.if.org Git - php/commitdiff
Extract common flock code
authorGeorge Peter Banyard <girgias@php.net>
Thu, 3 Sep 2020 13:49:28 +0000 (15:49 +0200)
committerGeorge Peter Banyard <girgias@php.net>
Fri, 4 Sep 2020 12:29:31 +0000 (14:29 +0200)
As SPL is currently a copie of the code in file.c

Closes GH-6069

ext/spl/spl_directory.c
ext/standard/file.c
ext/standard/file.h

index e782b1e70a462e857fc9a5919a715486ec08ea18..3f3391334aa7fbc5c7efde614fc3f6113673b215 100644 (file)
@@ -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);
 }
 /* }}} */
 
index d402c60acf48ea677045d4b811f5807081aa911a..9883973c5dc335415117804041fbb2eb1d9522f9 100644 (file)
@@ -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 ".\\+*?[^]$() "
index 1faf667a00be58ac448bf056462978ebe8d229d9..c51a953086eeedabf5f99df99db8ae19cb05c7f7 100644 (file)
@@ -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);