}
/* }}} */
-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) {
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);
}
/* }}} */
}
/* }}} */
-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();
}
}
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 ".\\+*?[^]$() "
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);