From 046da9b9068a1f0c3bdaba2553d4548af3a4e40d Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Sun, 14 Dec 2003 22:20:05 +0000 Subject: [PATCH] MFH: Synch (as far as possible) and fix #26304 --- ext/dba/dba.c | 39 +++++++++++++++++++-------------- ext/dba/dba_flatfile.c | 41 +++++++++++++++++++++++++---------- ext/dba/tests/dba_handler.inc | 2 +- 3 files changed, 54 insertions(+), 28 deletions(-) diff --git a/ext/dba/dba.c b/ext/dba/dba.c index b14d3b6765..ea918fcc44 100644 --- a/ext/dba/dba.c +++ b/ext/dba/dba.c @@ -27,7 +27,6 @@ #if HAVE_DBA -#include "ext/standard/flock_compat.h" #include "php_ini.h" #include #include @@ -38,6 +37,7 @@ #include "php_dba.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" +#include "ext/standard/flock_compat.h" #include "php_gdbm.h" #include "php_ndbm.h" @@ -651,22 +651,36 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent) case 'c': modenr = DBA_CREAT; lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0; + if (lock_mode) { + if (lock_dbf) { + /* the create/append check will be done on the lock + * when the lib opens the file it is already created + */ + file_mode = "r+b"; /* read & write, seek 0 */ + lock_file_mode = "a+b"; /* append */ + } else { + file_mode = "a+b"; /* append */ + lock_file_mode = "w+b"; /* create/truncate */ + } + } else { file_mode = "a+b"; - if (!lock_mode || !lock_dbf) { - break; } - /* When we lock the db file it will be created before the handler - * even tries to open it, hence we must change to truncate mode. + /* In case of the 'a+b' append mode, the handler is responsible + * to handle any rewind problems (see flatfile handler). */ + break; case 'n': modenr = DBA_TRUNC; lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0; file_mode = "w+b"; break; default: - modenr = 0; - lock_mode = 0; - file_mode = ""; + php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Illegal DBA mode"); + FREENOW; + RETURN_FALSE; + } + if (!lock_file_mode) { + lock_file_mode = file_mode; } if (*pmode=='d' || *pmode=='l' || *pmode=='-') { pmode++; /* done already - skip here */ @@ -692,7 +706,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent) lock_mode |= LOCK_NB; /* test =: non blocking */ } } - if (*pmode || !modenr) { + if (*pmode) { php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Illegal DBA mode"); FREENOW; RETURN_FALSE; @@ -724,7 +738,6 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent) if (!error && lock_mode) { if (lock_dbf) { info->lock.name = pestrdup(info->path, persistent); - lock_file_mode = file_mode; } else { spprintf(&info->lock.name, 0, "%s.lck", info->path); if (!strcmp(file_mode, "r")) { @@ -747,12 +760,6 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent) FREENOW; RETURN_FALSE; } - if (php_stream_cast(info->lock.fp, PHP_STREAM_AS_FD, (void*)&info->lock.fd, 1) == FAILURE) { - dba_close(info TSRMLS_CC); - /* stream operation already wrote an error message */ - FREENOW; - RETURN_FALSE; - } if (php_flock(info->lock.fd, lock_mode)) { error = "Unable to establish lock"; /* force failure exit */ } diff --git a/ext/dba/dba_flatfile.c b/ext/dba/dba_flatfile.c index 3134603259..4444f2c6e5 100644 --- a/ext/dba/dba_flatfile.c +++ b/ext/dba/dba_flatfile.c @@ -41,6 +41,17 @@ DBA_OPEN_FUNC(flatfile) { + int fd, flags; + + if (info->mode != DBA_READER) { + if (SUCCESS != php_stream_cast(info->fp, PHP_STREAM_AS_FD, (void*)&fd, 1)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not cast stream"); + return FAILURE; + } + flags = fcntl(fd, F_SETFL); + fcntl(fd, F_SETFL, flags & ~O_APPEND); + } + info->dbf = pemalloc(sizeof(flatfile), info->flags&DBA_PERSISTENT); memset(info->dbf, 0, sizeof(flatfile)); @@ -53,8 +64,9 @@ DBA_CLOSE_FUNC(flatfile) { FLATFILE_DATA; - if (dba->nextkey.dptr) + if (dba->nextkey.dptr) { efree(dba->nextkey.dptr); + } pefree(dba, info->flags&DBA_PERSISTENT); } @@ -67,8 +79,10 @@ DBA_FETCH_FUNC(flatfile) FLATFILE_GKEY; gval = flatfile_fetch(dba, gkey TSRMLS_CC); - if(gval.dptr) { - if(newlen) *newlen = gval.dsize; + if (gval.dptr) { + if (newlen) { + *newlen = gval.dsize; + } new = estrndup(gval.dptr, gval.dsize); efree(gval.dptr); } @@ -104,7 +118,7 @@ DBA_EXISTS_FUNC(flatfile) FLATFILE_GKEY; gval = flatfile_fetch(dba, gkey TSRMLS_CC); - if(gval.dptr) { + if (gval.dptr) { efree(gval.dptr); return SUCCESS; } @@ -122,12 +136,14 @@ DBA_FIRSTKEY_FUNC(flatfile) { FLATFILE_DATA; - if (dba->nextkey.dptr) + if (dba->nextkey.dptr) { efree(dba->nextkey.dptr); + } dba->nextkey = flatfile_firstkey(dba TSRMLS_CC); - if(dba->nextkey.dptr) { - if(newlen) + if (dba->nextkey.dptr) { + if (newlen) { *newlen = dba->nextkey.dsize; + } return estrndup(dba->nextkey.dptr, dba->nextkey.dsize); } return NULL; @@ -137,15 +153,18 @@ DBA_NEXTKEY_FUNC(flatfile) { FLATFILE_DATA; - if(!dba->nextkey.dptr) + if (!dba->nextkey.dptr) { return NULL; + } - if (dba->nextkey.dptr) + if (dba->nextkey.dptr) { efree(dba->nextkey.dptr); + } dba->nextkey = flatfile_nextkey(dba TSRMLS_CC); - if(dba->nextkey.dptr) { - if(newlen) + if (dba->nextkey.dptr) { + if (newlen) { *newlen = dba->nextkey.dsize; + } return estrndup(dba->nextkey.dptr, dba->nextkey.dsize); } return NULL; diff --git a/ext/dba/tests/dba_handler.inc b/ext/dba/tests/dba_handler.inc index b2e73d6237..674fd9f463 100644 --- a/ext/dba/tests/dba_handler.inc +++ b/ext/dba/tests/dba_handler.inc @@ -36,7 +36,7 @@ echo "Error creating database\n"; } if ($handler != 'cdb') { - $db_writer = dba_open($db_filename, 'w'.$lock_flag, $handler); + $db_writer = dba_open($db_filename, 'c'.$lock_flag, $handler); if (($dba_reader = @dba_open($db_filename, 'r'.$lock_flag.($lock_flag ? 't' : ''), $handler))===false) { echo "Read during write: not allowed\n"; } else { -- 2.50.1