]> granicus.if.org Git - php/commitdiff
- Fix Bug #26304 Unexpected data loss when opening dba file
authorMarcus Boerger <helly@php.net>
Sun, 14 Dec 2003 22:08:18 +0000 (22:08 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 14 Dec 2003 22:08:18 +0000 (22:08 +0000)
- Correct handling for flatfile handler

ext/dba/dba.c
ext/dba/dba_flatfile.c

index 72adca6f52aab8782fef74693b174f36ebdeac4d..0e996cd3a188b6ecedfe433984815b3519760217 100644 (file)
@@ -648,15 +648,23 @@ 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 || !lock_dbf) {
-                               file_mode = "a+b";
+                       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 {
-                               /* the create/append check will be done on the lock
-                                * when the lib opens the file it is already created
-                                */
-                               file_mode = "w+b";
-                               lock_file_mode = "a+b";
+                               file_mode = "a+b";
                        }
+                       /* 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;
index feba2c8da8143cb540afe65c6989435809be0172..fa1232779ad7d9566b33adf1b19cad46383c7b18 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));