Implemented file sharing checks in the server.
Added set file time server request.
Overall clean up of the file handling (DOS device handling is now
broken, should be redone).

diff --git a/server/file.c b/server/file.c
index 2274fb6..760902d 100644
--- a/server/file.c
+++ b/server/file.c
@@ -15,6 +15,7 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#include <utime.h>
 
 #include "winerror.h"
 #include "winnt.h"
@@ -23,10 +24,18 @@
 struct file
 {
     struct object  obj;             /* object header */
+    struct file   *next;            /* next file in hashing list */
+    char          *name;            /* file name */
     int            fd;              /* Unix file descriptor */
-    int            event;           /* possible events on this file */
+    unsigned int   access;          /* file access (GENERIC_READ/WRITE) */
+    unsigned int   flags;           /* flags (FILE_FLAG_*) */
+    unsigned int   sharing;         /* file sharing mode */
 };
 
+#define NAME_HASH_SIZE 37
+
+static struct file *file_hash[NAME_HASH_SIZE];
+
 static void file_dump( struct object *obj, int verbose );
 static int file_add_queue( struct object *obj, struct wait_queue_entry *entry );
 static void file_remove_queue( struct object *obj, struct wait_queue_entry *entry );
@@ -34,6 +43,7 @@
 static int file_get_read_fd( struct object *obj );
 static int file_get_write_fd( struct object *obj );
 static int file_flush( struct object *obj );
+static int file_get_info( struct object *obj, struct get_file_info_reply *reply );
 static void file_destroy( struct object *obj );
 
 static const struct object_ops file_ops =
@@ -46,6 +56,7 @@
     file_get_read_fd,
     file_get_write_fd,
     file_flush,
+    file_get_info,
     file_destroy
 };
 
@@ -55,31 +66,134 @@
     NULL   /* we never set a timeout on a file */
 };
 
-struct object *create_file( int fd )
+
+static int get_name_hash( const char *name )
+{
+    int hash = 0;
+    while (*name) hash ^= *name++;
+    return hash % NAME_HASH_SIZE;
+}
+
+/* check if the desired access is possible without violating */
+/* the sharing mode of other opens of the same file */
+static int check_sharing( const char *name, int hash, unsigned int access,
+                          unsigned int sharing )
 {
     struct file *file;
-    int flags;
+    unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
+    unsigned int existing_access = 0;
 
-    if ((flags = fcntl( fd, F_GETFL )) == -1)
+    for (file = file_hash[hash]; file; file = file->next)
     {
-        perror( "fcntl" );
+        if (strcmp( file->name, name )) continue;
+        existing_sharing &= file->sharing;
+        existing_access |= file->access;
+    }
+    if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) return 0;
+    if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
+    if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) return 0;
+    if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) return 0;
+    return 1;
+}
+
+struct object *create_file( int fd, const char *name, unsigned int access,
+                            unsigned int sharing, int create, unsigned int attrs )
+{
+    struct file *file;
+    int hash = 0;
+
+    if (fd == -1)
+    {
+        int flags;
+        struct stat st;
+
+        if (!name)
+        {
+            SET_ERROR( ERROR_INVALID_PARAMETER );
+            return NULL;
+        }
+
+        /* check sharing mode */
+        hash = get_name_hash( name );
+        if (!check_sharing( name, hash, access, sharing ))
+        {
+            SET_ERROR( ERROR_SHARING_VIOLATION );
+            return NULL;
+        }
+
+        switch(create)
+        {
+        case CREATE_NEW:        flags = O_CREAT | O_EXCL; break;
+        case CREATE_ALWAYS:     flags = O_CREAT | O_TRUNC; break;
+        case OPEN_ALWAYS:       flags = O_CREAT; break;
+        case TRUNCATE_EXISTING: flags = O_TRUNC; break;
+        case OPEN_EXISTING:     flags = 0; break;
+        default:                SET_ERROR( ERROR_INVALID_PARAMETER ); return NULL;
+        }
+        switch(access & (GENERIC_READ | GENERIC_WRITE))
+        {
+        case 0: break;
+        case GENERIC_READ:  flags |= O_RDONLY; break;
+        case GENERIC_WRITE: flags |= O_WRONLY; break;
+        case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
+        }
+
+        if ((fd = open( name, flags | O_NONBLOCK,
+                        (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666 )) == -1)
+        {
+            file_set_error();
+            return NULL;
+        }
+        /* Refuse to open a directory */
+        if (fstat( fd, &st ) == -1)
+        {
+            file_set_error();
+            close( fd );
+            return NULL;
+        }
+        if (S_ISDIR(st.st_mode))
+        {
+            SET_ERROR( ERROR_ACCESS_DENIED );
+            close( fd );
+            return NULL;
+        }            
+    }
+    else
+    {
+        if ((fd = dup(fd)) == -1)
+        {
+            file_set_error();
+            return NULL;
+        }
+    }
+
+    if (!(file = mem_alloc( sizeof(*file) )))
+    {
+        close( fd );
         return NULL;
     }
-    if (!(file = mem_alloc( sizeof(*file) ))) return NULL;
-    init_object( &file->obj, &file_ops, NULL );
-    file->fd = fd;
-    switch(flags & 3)
+    if (name)
     {
-    case O_RDONLY:
-        file->event = READ_EVENT;
-        break;
-    case O_WRONLY:
-        file->event = WRITE_EVENT;
-        break;
-    case O_RDWR:
-        file->event = READ_EVENT | WRITE_EVENT;
-        break;
+        if (!(file->name = mem_alloc( strlen(name) + 1 )))
+        {
+            close( fd );
+            free( file );
+            return NULL;
+        }
+        strcpy( file->name, name );
+        file->next = file_hash[hash];
+        file_hash[hash] = file;
     }
+    else
+    {
+        file->name = NULL;
+        file->next = NULL;
+    }
+    init_object( &file->obj, &file_ops, NULL );
+    file->fd      = fd;
+    file->access  = access;
+    file->flags   = attrs;
+    file->sharing = sharing;
     CLEAR_ERROR();
     return &file->obj;
 }
@@ -88,7 +202,8 @@
 {
     struct file *file = (struct file *)obj;
     assert( obj->ops == &file_ops );
-    printf( "File fd=%d\n", file->fd );
+    printf( "File fd=%d flags=%08x name='%s'\n",
+            file->fd, file->flags, file->name );
 }
 
 static int file_add_queue( struct object *obj, struct wait_queue_entry *entry )
@@ -128,8 +243,8 @@
 
     FD_ZERO( &read_fds );
     FD_ZERO( &write_fds );
-    if (file->event & READ_EVENT) FD_SET( file->fd, &read_fds );
-    if (file->event & WRITE_EVENT) FD_SET( file->fd, &write_fds );
+    if (file->access & GENERIC_READ) FD_SET( file->fd, &read_fds );
+    if (file->access & GENERIC_WRITE) FD_SET( file->fd, &write_fds );
     return select( file->fd + 1, &read_fds, &write_fds, NULL, &tv ) > 0;
 }
 
@@ -137,12 +252,6 @@
 {
     struct file *file = (struct file *)obj;
     assert( obj->ops == &file_ops );
-
-    if (!(file->event & READ_EVENT))  /* FIXME: should not be necessary */
-    {
-        SET_ERROR( ERROR_ACCESS_DENIED );
-        return -1;
-    }
     return dup( file->fd );
 }
 
@@ -150,12 +259,6 @@
 {
     struct file *file = (struct file *)obj;
     assert( obj->ops == &file_ops );
-
-    if (!(file->event & WRITE_EVENT))  /* FIXME: should not be necessary */
-    {
-        SET_ERROR( ERROR_ACCESS_DENIED );
-        return -1;
-    }
     return dup( file->fd );
 }
 
@@ -171,11 +274,48 @@
     return ret;
 }
 
-static void file_destroy( struct object *obj )
+static int file_get_info( struct object *obj, struct get_file_info_reply *reply )
 {
+    struct stat st;
     struct file *file = (struct file *)obj;
     assert( obj->ops == &file_ops );
+
+    if (fstat( file->fd, &st ) == -1)
+    {
+        file_set_error();
+        return 0;
+    }
+    if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
+        S_ISSOCK(st.st_mode) || isatty(file->fd)) reply->type = FILE_TYPE_CHAR;
+    else reply->type = FILE_TYPE_DISK;
+    if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
+    else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
+    if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
+    reply->access_time = st.st_atime;
+    reply->write_time  = st.st_mtime;
+    reply->size_high   = 0;
+    reply->size_low    = S_ISDIR(st.st_mode) ? 0 : st.st_size;
+    reply->links       = st.st_nlink;
+    reply->index_high  = st.st_dev;
+    reply->index_low   = st.st_ino;
+    reply->serial      = 0; /* FIXME */
+    return 1;
+}
+
+static void file_destroy( struct object *obj )
+{
+    struct file **pptr;
+    struct file *file = (struct file *)obj;
+    assert( obj->ops == &file_ops );
+
+    /* remove it from the hashing list */
+    pptr = &file_hash[get_name_hash( file->name )];
+    while (*pptr && *pptr != file) pptr = &(*pptr)->next;
+    assert( *pptr );
+    *pptr = (*pptr)->next;
+
     close( file->fd );
+    free( file->name );
     free( file );
 }
 
@@ -263,31 +403,22 @@
     
 }
 
-int get_file_info( int handle, struct get_file_info_reply *reply )
+int set_file_time( int handle, time_t access_time, time_t write_time )
 {
     struct file *file;
-    struct stat st;
+    struct utimbuf utimbuf;
 
-    if (!(file = get_file_obj( current->process, handle, 0 )))
+    if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
         return 0;
-    if (fstat( file->fd, &st ) == -1)
+    utimbuf.actime  = access_time;
+    utimbuf.modtime = write_time;
+    if (utime( file->name, &utimbuf ) == -1)
     {
         file_set_error();
         release_object( file );
         return 0;
     }
-    if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
-    else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
-    if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
-    reply->access_time = st.st_atime;
-    reply->write_time  = st.st_mtime;
-    reply->size_high   = 0;
-    reply->size_low    = S_ISDIR(st.st_mode) ? 0 : st.st_size;
-    reply->links       = st.st_nlink;
-    reply->index_high  = st.st_dev;
-    reply->index_low   = st.st_ino;
-    reply->serial      = 0; /* FIXME */
-    
     release_object( file );
     return 1;
+    
 }