iliaa           Mon Feb 24 20:39:07 2003 EDT

  Modified files:              
    /php4/main  php_streams.h 
    /php4/main/streams  plain_wrapper.c 
    /php4/ext/standard  file.c 
  Log:
  Added locking to streams.
  Allow PHP to automatically release locks on files when terminating the 
  stream.
  Fixed bugs in the handling of the 3rd optional parameter to flock().
  
  
Index: php4/main/php_streams.h
diff -u php4/main/php_streams.h:1.72 php4/main/php_streams.h:1.73
--- php4/main/php_streams.h:1.72        Mon Feb 24 16:40:23 2003
+++ php4/main/php_streams.h     Mon Feb 24 20:39:06 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_streams.h,v 1.72 2003/02/24 21:40:23 wez Exp $ */
+/* $Id: php_streams.h,v 1.73 2003/02/25 01:39:06 iliaa Exp $ */
 
 #ifndef PHP_STREAMS_H
 #define PHP_STREAMS_H
@@ -325,6 +325,15 @@
 /* set the timeout duration for reads on the stream. ptrparam is a pointer to a 
struct timeval * */
 #define PHP_STREAM_OPTION_READ_TIMEOUT 4
 #define PHP_STREAM_OPTION_SET_CHUNK_SIZE       5
+
+/* set or release lock on a stream */
+#define PHP_STREAM_OPTION_LOCKING              6
+
+/* whether or not locking is supported */
+#define PHP_STREAM_LOCK_SUPPORTED              1       
+
+#define php_stream_supports_lock(stream)       php_stream_set_option((stream), 
PHP_STREAM_OPTION_LOCKING, 0, (void *) PHP_STREAM_LOCK_SUPPORTED TSRMLS_CC) == 0 ? 1 : 0
+#define php_stream_lock(stream, mode)          php_stream_set_option((stream), 
PHP_STREAM_OPTION_LOCKING, (mode), (void *) NULL TSRMLS_CC)
 
 #define PHP_STREAM_OPTION_RETURN_OK                     0 /* option set OK */
 #define PHP_STREAM_OPTION_RETURN_ERR           -1 /* problem setting option */
Index: php4/main/streams/plain_wrapper.c
diff -u php4/main/streams/plain_wrapper.c:1.3 php4/main/streams/plain_wrapper.c:1.4
--- php4/main/streams/plain_wrapper.c:1.3       Mon Feb 24 16:40:23 2003
+++ php4/main/streams/plain_wrapper.c   Mon Feb 24 20:39:06 2003
@@ -16,13 +16,14 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: plain_wrapper.c,v 1.3 2003/02/24 21:40:23 wez Exp $ */
+/* $Id: plain_wrapper.c,v 1.4 2003/02/25 01:39:06 iliaa Exp $ */
 
 #include "php.h"
 #include "php_globals.h"
 #include "php_network.h"
 #include "php_open_temporary_file.h"
 #include "ext/standard/file.h"
+#include "ext/standard/flock_compat.h"
 #include <stddef.h>
 #include <fcntl.h>
 #if HAVE_SYS_WAIT_H
@@ -131,6 +132,7 @@
        int fd;                                 /* underlying file descriptor */
        int is_process_pipe;    /* use pclose instead of fclose */
        int is_pipe;                    /* don't try and seek */
+       int lock_flag;          /* stores the lock state */
        char *temp_file_name;   /* if non-null, this is the path to a temporary file 
that
                                                         * is to be deleted when the 
stream is closed */
 #if HAVE_FLUSHIO
@@ -167,6 +169,8 @@
                        php_stdio_stream_data *self = 
(php_stdio_stream_data*)stream->abstract;
 
                        self->temp_file_name = opened_path;
+                       self->lock_flag = LOCK_UN;
+                       
                        return stream;
                }
                fclose(fp);
@@ -186,6 +190,7 @@
        self = emalloc_rel_orig(sizeof(*self));
        self->file = NULL;
        self->is_pipe = 0;
+       self->lock_flag = LOCK_UN;
        self->is_process_pipe = 0;
        self->temp_file_name = NULL;
        self->fd = fd;
@@ -228,6 +233,7 @@
        self = emalloc_rel_orig(sizeof(*self));
        self->file = file;
        self->is_pipe = 0;
+       self->lock_flag = LOCK_UN;
        self->is_process_pipe = 0;
        self->temp_file_name = NULL;
        self->fd = fileno(file);
@@ -270,6 +276,7 @@
        self = emalloc_rel_orig(sizeof(*self));
        self->file = file;
        self->is_pipe = 1;
+       self->lock_flag = LOCK_UN;
        self->is_process_pipe = 1;
        self->fd = fileno(file);
        self->temp_file_name = NULL;
@@ -340,6 +347,9 @@
        assert(data != NULL);
        
        if (close_handle) {
+               if (data->lock_flag != LOCK_UN) {
+                       php_stream_lock(stream, LOCK_UN);
+               }
                if (data->file) {
                        if (data->is_process_pipe) {
                                errno = 0;
@@ -541,6 +551,24 @@
                                        return -1;
                        }
                        break;
+               
+               case PHP_STREAM_OPTION_LOCKING:
+                       if (fd == -1) {
+                               return -1;
+                       }
+
+                       if ((int) ptrparam == PHP_STREAM_LOCK_SUPPORTED) {
+                               return 0;
+                       }
+
+                       if (!php_flock(fd, value) || (errno == EWOULDBLOCK && value & 
LOCK_NB)) {
+                               data->lock_flag = value;
+                               return 0;
+                       } else {
+                               return -1;
+                       }
+                       break;
+
                default:
                        return -1;
        }
Index: php4/ext/standard/file.c
diff -u php4/ext/standard/file.c:1.312 php4/ext/standard/file.c:1.313
--- php4/ext/standard/file.c:1.312      Mon Feb 24 17:39:47 2003
+++ php4/ext/standard/file.c    Mon Feb 24 20:39:06 2003
@@ -21,7 +21,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: file.c,v 1.312 2003/02/24 22:39:47 moriyoshi Exp $ */
+/* $Id: file.c,v 1.313 2003/02/25 01:39:06 iliaa Exp $ */
 
 /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
 
@@ -218,8 +218,8 @@
 
 PHP_FUNCTION(flock)
 {
-       zval *arg1, *arg3;
-       int fd, act, ret;
+       zval *arg1, *arg3 = NULL;
+       int fd, act;
        php_stream *stream;
        long operation = 0;
 
@@ -241,15 +241,14 @@
 
        /* flock_values contains all possible actions if (operation & 4) we won't 
block on the lock */
        act = flock_values[act - 1] | (operation & 4 ? LOCK_NB : 0);
-       if ((ret = flock(fd, act)) == -1) {
-               if (errno == EWOULDBLOCK && operation) {
-                       convert_to_long(arg3);
-                       Z_LVAL_P(arg3) = 1;
-                       RETURN_TRUE;
+       if (!php_stream_lock(stream, act)) {
+               if (operation && errno == EWOULDBLOCK && arg3 && PZVAL_IS_REF(arg3)) {
+                       convert_to_long_ex(&arg3);
+                       ZVAL_LONG(arg3, 1);
                }
-               RETURN_FALSE;
+               RETURN_TRUE;
        }
-       RETURN_TRUE;
+       RETURN_FALSE;
 }
 
 /* }}} */



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to