Skip to content

Commit 5df341f

Browse files
committed
Implement basic database locking
We're using advisory file locking via php_flock(), so the usual caveats regarding multithreaded Windows SAPIs apply. We're applying blocking locks automatically on creation and opening of databases, so that existing code doesn't have to be changed to make use of the locking. If users want to check whether an operation would block, flock() with LOCK_NB can be used (leaving room for possible race-conditions, though). git-svn-id: http://svn.php.net/repository/pecl/dbase/trunk@340803 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent fb312fa commit 5df341f

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

dbase.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "fopen_wrappers.h"
2727
#include "php_globals.h"
2828
#include "ext/standard/php_math.h"
29+
#include "ext/standard/flock_compat.h"
2930

3031
#include <stdlib.h>
3132

@@ -47,6 +48,7 @@ static void _close_dbase(zend_resource *rsrc)
4748
{
4849
dbhead_t *dbhead = (dbhead_t *)rsrc->ptr;
4950

51+
php_flock(dbhead->db_fd, LOCK_UN);
5052
close(dbhead->db_fd);
5153
free_dbf_head(dbhead);
5254
}
@@ -516,6 +518,12 @@ PHP_FUNCTION(dbase_create)
516518
RETURN_FALSE;
517519
}
518520

521+
if (php_flock(fd, LOCK_EX)) {
522+
php_error_docref(NULL, E_WARNING, "unable to lock database");
523+
close(fd);
524+
RETURN_FALSE;
525+
}
526+
519527
num_fields = zend_hash_num_elements(fields);
520528

521529
if (num_fields <= 0) {

dbf_head.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <fcntl.h>
99

1010
#include "php.h"
11+
#include "ext/standard/flock_compat.h"
1112
#include "dbf.h"
1213

1314
void free_dbf_head(dbhead_t *dbh);
@@ -278,6 +279,11 @@ dbhead_t *dbf_open(char *dp, int o_flags)
278279
return NULL;
279280
}
280281

282+
if (php_flock(fd, (o_flags == O_RDWR ? LOCK_EX : LOCK_SH))) {
283+
close(fd);
284+
return NULL;
285+
}
286+
281287
if ((dbh = get_dbf_head(fd)) == NULL) {
282288
return NULL;
283289
}

package.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
1010
in dBase-format (dbf) databases.
1111

1212
There is no support for indexes or memo fields.
13-
There is no support for locking, too.
14-
Two concurrent webserver processes modifying the
15-
same dBase file will very likely ruin your database.
13+
The databases are automatically locked with blocking flock()s.
1614

1715
dBase files are simple sequential files of fixed length records.
1816
Records are appended to the end of the file and deleted records

0 commit comments

Comments
 (0)