]> granicus.if.org Git - php/commitdiff
MFH: Synch (as far as possible) and fix #26304
authorMarcus Boerger <helly@php.net>
Sun, 14 Dec 2003 22:20:05 +0000 (22:20 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 14 Dec 2003 22:20:05 +0000 (22:20 +0000)
ext/dba/dba.c
ext/dba/dba_flatfile.c
ext/dba/tests/dba_handler.inc

index b14d3b676501db0943812210f398a979efdf97e5..ea918fcc44202d93dae571ef265b25006071db80 100644 (file)
@@ -27,7 +27,6 @@
 
 #if HAVE_DBA
 
-#include "ext/standard/flock_compat.h" 
 #include "php_ini.h"
 #include <stdio.h> 
 #include <fcntl.h>
@@ -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 */
                }
index 3134603259dd1af4891a5cf9e0ca24a4df587ea4..4444f2c6e58fddb840b224cacca7f2694c44e9be 100644 (file)
 
 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;
index b2e73d62370aee39112c9c5854643e61f2948281..674fd9f46328ee7a62b9b4537f69b92854b0a6fe 100644 (file)
@@ -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 {