Skip to content

Commit

Permalink
Extract common flock code
Browse files Browse the repository at this point in the history
As SPL is currently a copie of the code in file.c

Closes GH-6069
  • Loading branch information
Girgias committed Sep 4, 2020
1 parent 2e21818 commit 7805b97
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
26 changes: 2 additions & 24 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
/* }}} */

Expand Down
40 changes: 23 additions & 17 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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 ".\\+*?[^]$() "
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7805b97

Please sign in to comment.