1998-12-27 16:28:54 +01:00
|
|
|
/*
|
|
|
|
* Server-side file management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1998-12-27 16:28:54 +01:00
|
|
|
*/
|
|
|
|
|
1999-10-25 00:13:47 +02:00
|
|
|
#include "config.h"
|
2001-05-14 22:09:37 +02:00
|
|
|
#include "wine/port.h"
|
1999-10-25 00:13:47 +02:00
|
|
|
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
1999-02-28 13:27:56 +01:00
|
|
|
#include <string.h>
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <stdlib.h>
|
1999-01-01 19:42:17 +01:00
|
|
|
#include <errno.h>
|
1999-07-10 15:16:29 +02:00
|
|
|
#ifdef HAVE_SYS_ERRNO_H
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <sys/errno.h>
|
1999-07-10 15:16:29 +02:00
|
|
|
#endif
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1999-01-03 12:55:56 +01:00
|
|
|
#include <utime.h>
|
1998-12-27 16:28:54 +01:00
|
|
|
|
|
|
|
#include "winerror.h"
|
1999-02-09 16:49:39 +01:00
|
|
|
#include "winbase.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
|
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 19:26:53 +02:00
|
|
|
#include "request.h"
|
2002-01-07 22:02:15 +01:00
|
|
|
#include "async.h"
|
1998-12-27 16:28:54 +01:00
|
|
|
|
|
|
|
struct file
|
|
|
|
{
|
1999-05-16 18:59:38 +02:00
|
|
|
struct object obj; /* object header */
|
|
|
|
struct file *next; /* next file in hashing list */
|
|
|
|
char *name; /* file name */
|
|
|
|
unsigned int access; /* file access (GENERIC_READ/WRITE) */
|
|
|
|
unsigned int flags; /* flags (FILE_FLAG_*) */
|
|
|
|
unsigned int sharing; /* file sharing mode */
|
2001-10-24 02:23:25 +02:00
|
|
|
int drive_type; /* type of drive the file is on */
|
2002-01-07 22:02:15 +01:00
|
|
|
struct async_queue read_q;
|
|
|
|
struct async_queue write_q;
|
1998-12-27 16:28:54 +01:00
|
|
|
};
|
|
|
|
|
1999-01-03 12:55:56 +01:00
|
|
|
#define NAME_HASH_SIZE 37
|
|
|
|
|
|
|
|
static struct file *file_hash[NAME_HASH_SIZE];
|
|
|
|
|
1998-12-27 16:28:54 +01:00
|
|
|
static void file_dump( struct object *obj, int verbose );
|
2000-01-01 01:56:27 +01:00
|
|
|
static int file_get_poll_events( struct object *obj );
|
2002-01-07 22:02:15 +01:00
|
|
|
static void file_poll_event( struct object *obj, int event );
|
2000-12-19 03:12:45 +01:00
|
|
|
static int file_get_fd( struct object *obj );
|
1998-12-30 13:06:45 +01:00
|
|
|
static int file_flush( struct object *obj );
|
2002-01-09 21:30:51 +01:00
|
|
|
static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
|
1998-12-27 16:28:54 +01:00
|
|
|
static void file_destroy( struct object *obj );
|
2002-04-24 23:29:54 +02:00
|
|
|
static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
|
1998-12-27 16:28:54 +01:00
|
|
|
|
|
|
|
static const struct object_ops file_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
sizeof(struct file), /* size */
|
|
|
|
file_dump, /* dump */
|
|
|
|
default_poll_add_queue, /* add_queue */
|
|
|
|
default_poll_remove_queue, /* remove_queue */
|
|
|
|
default_poll_signaled, /* signaled */
|
|
|
|
no_satisfied, /* satisfied */
|
|
|
|
file_get_poll_events, /* get_poll_events */
|
2002-01-07 22:02:15 +01:00
|
|
|
file_poll_event, /* poll_event */
|
2000-12-19 03:12:45 +01:00
|
|
|
file_get_fd, /* get_fd */
|
2000-01-01 01:56:27 +01:00
|
|
|
file_flush, /* flush */
|
|
|
|
file_get_info, /* get_file_info */
|
2002-01-07 22:02:15 +01:00
|
|
|
file_queue_async, /* queue_async */
|
2000-01-01 01:56:27 +01:00
|
|
|
file_destroy /* destroy */
|
1998-12-27 16:28:54 +01:00
|
|
|
};
|
|
|
|
|
1999-01-03 12:55:56 +01:00
|
|
|
|
|
|
|
static int get_name_hash( const char *name )
|
|
|
|
{
|
|
|
|
int hash = 0;
|
1999-11-15 01:07:30 +01:00
|
|
|
while (*name) hash ^= (unsigned char)*name++;
|
1999-01-03 12:55:56 +01:00
|
|
|
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;
|
|
|
|
unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
|
|
|
unsigned int existing_access = 0;
|
|
|
|
|
|
|
|
for (file = file_hash[hash]; file; file = file->next)
|
|
|
|
{
|
|
|
|
if (strcmp( file->name, name )) continue;
|
|
|
|
existing_sharing &= file->sharing;
|
|
|
|
existing_access |= file->access;
|
|
|
|
}
|
1999-06-22 19:26:53 +02:00
|
|
|
if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error;
|
|
|
|
if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
|
|
|
|
if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
|
|
|
|
if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
|
1999-01-03 12:55:56 +01:00
|
|
|
return 1;
|
1999-06-22 19:26:53 +02:00
|
|
|
error:
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_SHARING_VIOLATION );
|
1999-06-22 19:26:53 +02:00
|
|
|
return 0;
|
1999-01-03 12:55:56 +01:00
|
|
|
}
|
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
/* create a file from a file descriptor */
|
|
|
|
/* if the function fails the fd is closed */
|
1999-06-22 19:26:53 +02:00
|
|
|
static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
|
2001-10-24 02:23:25 +02:00
|
|
|
unsigned int attrs, int drive_type )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
|
|
|
struct file *file;
|
2000-01-01 01:56:27 +01:00
|
|
|
if ((file = alloc_object( &file_ops, fd )))
|
1999-01-03 12:55:56 +01:00
|
|
|
{
|
2001-10-24 02:23:25 +02:00
|
|
|
file->name = NULL;
|
|
|
|
file->next = NULL;
|
|
|
|
file->access = access;
|
|
|
|
file->flags = attrs;
|
|
|
|
file->sharing = sharing;
|
|
|
|
file->drive_type = drive_type;
|
2002-01-07 22:02:15 +01:00
|
|
|
if (file->flags & FILE_FLAG_OVERLAPPED)
|
|
|
|
{
|
|
|
|
init_async_queue (&file->read_q);
|
|
|
|
init_async_queue (&file->write_q);
|
|
|
|
}
|
1999-06-22 19:26:53 +02:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
1999-01-03 12:55:56 +01:00
|
|
|
|
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
|
2001-10-24 02:23:25 +02:00
|
|
|
unsigned int sharing, int create, unsigned int attrs,
|
|
|
|
int drive_type )
|
1999-06-22 19:26:53 +02:00
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
int hash, flags;
|
|
|
|
struct stat st;
|
|
|
|
char *name;
|
|
|
|
int fd = -1;
|
2001-07-23 20:09:41 +02:00
|
|
|
mode_t mode;
|
1999-01-03 12:55:56 +01:00
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
if (!(name = mem_alloc( len + 1 ))) return NULL;
|
|
|
|
memcpy( name, nameptr, len );
|
|
|
|
name[len] = 0;
|
1999-01-03 12:55:56 +01:00
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
/* check sharing mode */
|
|
|
|
hash = get_name_hash( name );
|
|
|
|
if (!check_sharing( name, hash, access, sharing )) goto error;
|
1998-12-27 16:28:54 +01:00
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
switch(create)
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
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;
|
2000-01-24 22:58:06 +01:00
|
|
|
default: set_error( STATUS_INVALID_PARAMETER ); goto error;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
1999-06-22 19:26:53 +02:00
|
|
|
switch(access & (GENERIC_READ | GENERIC_WRITE))
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
1999-06-22 19:26:53 +02:00
|
|
|
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;
|
1999-01-03 12:55:56 +01:00
|
|
|
}
|
2001-07-23 20:09:41 +02:00
|
|
|
mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
|
|
|
|
|
|
|
|
if (len >= 4 &&
|
|
|
|
(!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
|
|
|
|
mode |= 0111;
|
1999-06-22 19:26:53 +02:00
|
|
|
|
2000-01-24 22:58:06 +01:00
|
|
|
/* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
|
2001-07-23 20:09:41 +02:00
|
|
|
if ((fd = open( name, flags | O_NONBLOCK | O_LARGEFILE, mode )) == -1 )
|
1999-06-22 19:26:53 +02:00
|
|
|
goto file_error;
|
|
|
|
/* refuse to open a directory */
|
|
|
|
if (fstat( fd, &st ) == -1) goto file_error;
|
|
|
|
if (S_ISDIR(st.st_mode))
|
1999-01-03 12:55:56 +01:00
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_ACCESS_DENIED );
|
1999-06-22 19:26:53 +02:00
|
|
|
goto error;
|
2001-05-14 22:09:37 +02:00
|
|
|
}
|
1999-06-22 19:26:53 +02:00
|
|
|
|
2001-10-24 02:23:25 +02:00
|
|
|
if (!(file = create_file_for_fd( fd, access, sharing, attrs, drive_type )))
|
2000-01-01 01:56:27 +01:00
|
|
|
{
|
|
|
|
free( name );
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-22 19:26:53 +02:00
|
|
|
file->name = name;
|
|
|
|
file->next = file_hash[hash];
|
|
|
|
file_hash[hash] = file;
|
|
|
|
return file;
|
|
|
|
|
|
|
|
file_error:
|
|
|
|
file_set_error();
|
|
|
|
error:
|
|
|
|
if (fd != -1) close( fd );
|
|
|
|
free( name );
|
|
|
|
return NULL;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2001-04-14 00:38:39 +02:00
|
|
|
/* check if two file objects point to the same file */
|
|
|
|
int is_same_file( struct file *file1, struct file *file2 )
|
|
|
|
{
|
|
|
|
return !strcmp( file1->name, file2->name );
|
|
|
|
}
|
|
|
|
|
2001-10-24 02:23:25 +02:00
|
|
|
/* get the type of drive the file is on */
|
|
|
|
int get_file_drive_type( struct file *file )
|
|
|
|
{
|
|
|
|
return file->drive_type;
|
|
|
|
}
|
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
/* Create an anonymous Unix file */
|
|
|
|
int create_anonymous_file(void)
|
1999-02-28 11:13:59 +01:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!(name = tmpnam(NULL)))
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_TOO_MANY_OPENED_FILES );
|
1999-06-22 19:26:53 +02:00
|
|
|
return -1;
|
1999-02-28 11:13:59 +01:00
|
|
|
}
|
|
|
|
fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
|
|
|
|
} while ((fd == -1) && (errno == EEXIST));
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
1999-06-22 19:26:53 +02:00
|
|
|
return -1;
|
1999-02-28 11:13:59 +01:00
|
|
|
}
|
|
|
|
unlink( name );
|
1999-06-22 19:26:53 +02:00
|
|
|
return fd;
|
|
|
|
}
|
1999-02-28 11:13:59 +01:00
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
/* Create a temp file for anonymous mappings */
|
|
|
|
struct file *create_temp_file( int access )
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
1999-06-26 10:43:26 +02:00
|
|
|
if ((fd = create_anonymous_file()) == -1) return NULL;
|
2001-10-24 02:23:25 +02:00
|
|
|
return create_file_for_fd( fd, access, 0, 0, DRIVE_FIXED );
|
1999-02-28 11:13:59 +01:00
|
|
|
}
|
|
|
|
|
1998-12-27 16:28:54 +01:00
|
|
|
static void file_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
2000-01-01 01:56:27 +01:00
|
|
|
fprintf( stderr, "File fd=%d flags=%08x name='%s'\n", file->obj.fd, file->flags, file->name );
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
static int file_get_poll_events( struct object *obj )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
1999-05-16 18:59:38 +02:00
|
|
|
int events = 0;
|
1998-12-27 16:28:54 +01:00
|
|
|
assert( obj->ops == &file_ops );
|
1999-12-13 01:16:44 +01:00
|
|
|
if (file->access & GENERIC_READ) events |= POLLIN;
|
|
|
|
if (file->access & GENERIC_WRITE) events |= POLLOUT;
|
2000-01-01 01:56:27 +01:00
|
|
|
return events;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2002-01-07 22:02:15 +01:00
|
|
|
static void file_poll_event( struct object *obj, int event )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
if ( file->flags & FILE_FLAG_OVERLAPPED )
|
|
|
|
{
|
|
|
|
if( IS_READY(file->read_q) && (POLLIN & event) )
|
|
|
|
{
|
|
|
|
async_notify(file->read_q.head, STATUS_ALERTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( IS_READY(file->write_q) && (POLLOUT & event) )
|
|
|
|
{
|
|
|
|
async_notify(file->write_q.head, STATUS_ALERTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default_poll_event( obj, event );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-19 03:12:45 +01:00
|
|
|
static int file_get_fd( struct object *obj )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
2000-12-22 22:12:36 +01:00
|
|
|
return file->obj.fd;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
1998-12-30 13:06:45 +01:00
|
|
|
static int file_flush( struct object *obj )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
1998-12-30 13:06:45 +01:00
|
|
|
int ret;
|
|
|
|
struct file *file = (struct file *)grab_object(obj);
|
|
|
|
assert( obj->ops == &file_ops );
|
1998-12-27 16:28:54 +01:00
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
ret = (fsync( file->obj.fd ) != -1);
|
1998-12-30 13:06:45 +01:00
|
|
|
if (!ret) file_set_error();
|
|
|
|
release_object( file );
|
|
|
|
return ret;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2002-01-09 21:30:51 +01:00
|
|
|
static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
|
1999-01-03 12:55:56 +01:00
|
|
|
{
|
2001-05-29 22:55:21 +02:00
|
|
|
struct stat st;
|
1999-01-03 12:55:56 +01:00
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
if (reply)
|
1999-01-03 12:55:56 +01:00
|
|
|
{
|
2001-10-04 18:18:15 +02:00
|
|
|
if (fstat( file->obj.fd, &st ) == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return FD_TYPE_INVALID;
|
|
|
|
}
|
|
|
|
if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
|
2001-11-30 19:46:42 +01:00
|
|
|
S_ISSOCK(st.st_mode) || isatty(file->obj.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;
|
2001-10-04 18:18:15 +02:00
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->size_high = 0;
|
|
|
|
reply->size_low = 0;
|
2001-10-04 18:18:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->size_high = st.st_size >> 32;
|
|
|
|
reply->size_low = st.st_size & 0xffffffff;
|
2001-10-04 18:18:15 +02:00
|
|
|
}
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->links = st.st_nlink;
|
|
|
|
reply->index_high = st.st_dev;
|
|
|
|
reply->index_low = st.st_ino;
|
|
|
|
reply->serial = 0; /* FIXME */
|
2001-05-14 22:09:37 +02:00
|
|
|
}
|
2002-01-09 21:30:51 +01:00
|
|
|
*flags = 0;
|
|
|
|
if (file->flags & FILE_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
|
2001-10-04 18:18:15 +02:00
|
|
|
return FD_TYPE_DEFAULT;
|
1999-01-03 12:55:56 +01:00
|
|
|
}
|
|
|
|
|
2002-04-24 23:29:54 +02:00
|
|
|
static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
|
2002-01-07 22:02:15 +01:00
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
2002-04-24 23:29:54 +02:00
|
|
|
struct async *async;
|
2002-01-07 22:02:15 +01:00
|
|
|
struct async_queue *q;
|
|
|
|
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
|
|
|
if ( !(file->flags & FILE_FLAG_OVERLAPPED) )
|
|
|
|
{
|
|
|
|
set_error ( STATUS_INVALID_HANDLE );
|
2002-04-24 23:29:54 +02:00
|
|
|
return;
|
2002-01-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case ASYNC_TYPE_READ:
|
|
|
|
q = &file->read_q;
|
|
|
|
break;
|
|
|
|
case ASYNC_TYPE_WRITE:
|
|
|
|
q = &file->write_q;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
2002-04-24 23:29:54 +02:00
|
|
|
return;
|
2002-01-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
2002-04-24 23:29:54 +02:00
|
|
|
async = find_async ( q, current, ptr );
|
|
|
|
|
|
|
|
if ( status == STATUS_PENDING )
|
|
|
|
{
|
2002-04-26 20:31:19 +02:00
|
|
|
struct pollfd pfd;
|
|
|
|
|
2002-04-24 23:29:54 +02:00
|
|
|
if ( !async )
|
|
|
|
async = create_async ( obj, current, ptr );
|
|
|
|
if ( !async )
|
|
|
|
return;
|
|
|
|
|
|
|
|
async->status = STATUS_PENDING;
|
|
|
|
if ( !async->q )
|
|
|
|
async_insert( q, async );
|
2002-04-26 20:31:19 +02:00
|
|
|
|
|
|
|
/* Check if the new pending request can be served immediately */
|
|
|
|
pfd.fd = obj->fd;
|
|
|
|
pfd.events = file_get_poll_events ( obj );
|
|
|
|
pfd.revents = 0;
|
|
|
|
poll ( &pfd, 1, 0 );
|
|
|
|
|
|
|
|
if ( pfd.revents )
|
|
|
|
file_poll_event ( obj, pfd.revents );
|
2002-04-24 23:29:54 +02:00
|
|
|
}
|
|
|
|
else if ( async ) destroy_async ( async );
|
|
|
|
else set_error ( STATUS_INVALID_PARAMETER );
|
2002-01-07 22:02:15 +01:00
|
|
|
|
2002-04-24 23:29:54 +02:00
|
|
|
set_select_events ( obj, file_get_poll_events ( obj ));
|
2002-01-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
1998-12-30 13:06:45 +01:00
|
|
|
static void file_destroy( struct object *obj )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
1998-12-30 13:06:45 +01:00
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
1999-01-03 12:55:56 +01:00
|
|
|
|
1999-01-17 17:32:32 +01:00
|
|
|
if (file->name)
|
|
|
|
{
|
|
|
|
/* remove it from the hashing list */
|
|
|
|
struct file **pptr = &file_hash[get_name_hash( file->name )];
|
|
|
|
while (*pptr && *pptr != file) pptr = &(*pptr)->next;
|
|
|
|
assert( *pptr );
|
|
|
|
*pptr = (*pptr)->next;
|
1999-02-14 12:20:07 +01:00
|
|
|
if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
|
1999-01-17 17:32:32 +01:00
|
|
|
free( file->name );
|
|
|
|
}
|
2002-01-07 22:02:15 +01:00
|
|
|
if (file->flags & FILE_FLAG_OVERLAPPED)
|
|
|
|
{
|
|
|
|
destroy_async_queue (&file->read_q);
|
|
|
|
destroy_async_queue (&file->write_q);
|
|
|
|
}
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the last error depending on errno */
|
|
|
|
void file_set_error(void)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
|
|
|
|
case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
|
|
|
|
case ENOSPC: set_error( STATUS_DISK_FULL ); break;
|
1998-12-30 13:06:45 +01:00
|
|
|
case EACCES:
|
2001-07-14 02:50:30 +02:00
|
|
|
case ESRCH:
|
2000-01-24 22:58:06 +01:00
|
|
|
case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
|
|
|
|
case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
|
|
|
|
case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
|
|
|
|
case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
|
|
|
|
case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
|
1998-12-30 13:06:45 +01:00
|
|
|
case ENFILE:
|
2000-01-24 22:58:06 +01:00
|
|
|
case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
|
|
|
|
case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
|
|
|
|
case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
|
|
|
|
case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
|
|
|
|
case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
|
|
|
|
case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
|
|
|
|
default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2001-01-05 05:08:07 +01:00
|
|
|
struct file *get_file_obj( struct process *process, handle_t handle, unsigned int access )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
1999-11-29 03:17:08 +01:00
|
|
|
return (struct file *)get_handle_obj( process, handle, access, &file_ops );
|
1999-01-01 17:59:27 +01:00
|
|
|
}
|
1998-12-27 16:28:54 +01:00
|
|
|
|
2001-05-14 22:09:37 +02:00
|
|
|
static int set_file_pointer( handle_t handle, unsigned int *low, int *high, int whence )
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
|
|
|
struct file *file;
|
2001-05-29 22:55:21 +02:00
|
|
|
off_t result,xto;
|
1998-12-30 13:06:45 +01:00
|
|
|
|
2001-05-29 22:55:21 +02:00
|
|
|
xto = *low+((off_t)*high<<32);
|
1999-01-01 17:59:27 +01:00
|
|
|
if (!(file = get_file_obj( current->process, handle, 0 )))
|
1998-12-30 13:06:45 +01:00
|
|
|
return 0;
|
2001-05-29 22:55:21 +02:00
|
|
|
if ((result = lseek(file->obj.fd,xto,whence))==-1)
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
|
|
|
/* Check for seek before start of file */
|
2001-01-13 00:02:39 +01:00
|
|
|
|
|
|
|
/* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
|
|
|
|
if (((errno == EINVAL) || (errno == EPERM))
|
2001-06-19 05:30:53 +02:00
|
|
|
&& (whence != SEEK_SET) && (*high < 0))
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
|
1998-12-30 13:06:45 +01:00
|
|
|
else
|
|
|
|
file_set_error();
|
|
|
|
release_object( file );
|
|
|
|
return 0;
|
|
|
|
}
|
2001-05-14 22:09:37 +02:00
|
|
|
*low = result & 0xffffffff;
|
|
|
|
*high = result >> 32;
|
1998-12-30 13:06:45 +01:00
|
|
|
release_object( file );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-01-29 03:51:12 +01:00
|
|
|
/* extend a file beyond the current end of file */
|
|
|
|
static int extend_file( struct file *file, off_t size )
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
2002-01-29 03:51:12 +01:00
|
|
|
static const char zero;
|
1998-12-30 13:06:45 +01:00
|
|
|
|
2002-01-29 03:51:12 +01:00
|
|
|
/* extend the file one byte beyond the requested size and then truncate it */
|
|
|
|
/* this should work around ftruncate implementations that can't extend files */
|
|
|
|
if ((lseek( file->obj.fd, size, SEEK_SET ) != -1) &&
|
|
|
|
(write( file->obj.fd, &zero, 1 ) != -1))
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
2002-01-29 03:51:12 +01:00
|
|
|
ftruncate( file->obj.fd, size );
|
|
|
|
return 1;
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
2002-01-29 03:51:12 +01:00
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* truncate file at current position */
|
|
|
|
static int truncate_file( struct file *file )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
off_t pos = lseek( file->obj.fd, 0, SEEK_CUR );
|
|
|
|
off_t eof = lseek( file->obj.fd, 0, SEEK_END );
|
|
|
|
|
|
|
|
if (eof < pos) ret = extend_file( file, pos );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ftruncate( file->obj.fd, pos ) != -1) ret = 1;
|
|
|
|
else file_set_error();
|
|
|
|
}
|
|
|
|
lseek( file->obj.fd, pos, SEEK_SET ); /* restore file pos */
|
|
|
|
return ret;
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
1999-02-28 11:13:59 +01:00
|
|
|
/* try to grow the file to the specified size */
|
|
|
|
int grow_file( struct file *file, int size_high, int size_low )
|
|
|
|
{
|
2002-01-29 03:51:12 +01:00
|
|
|
int ret = 0;
|
2001-05-29 22:55:21 +02:00
|
|
|
struct stat st;
|
2002-01-29 03:51:12 +01:00
|
|
|
off_t old_pos, size = size_low + (((off_t)size_high)<<32);
|
1999-02-28 11:13:59 +01:00
|
|
|
|
2001-05-29 22:55:21 +02:00
|
|
|
if (fstat( file->obj.fd, &st ) == -1)
|
1999-02-28 11:13:59 +01:00
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
2001-05-14 22:09:37 +02:00
|
|
|
if (st.st_size >= size) return 1; /* already large enough */
|
2002-01-29 03:51:12 +01:00
|
|
|
old_pos = lseek( file->obj.fd, 0, SEEK_CUR ); /* save old pos */
|
|
|
|
ret = extend_file( file, size );
|
|
|
|
lseek( file->obj.fd, old_pos, SEEK_SET ); /* restore file pos */
|
|
|
|
return ret;
|
1999-02-28 11:13:59 +01:00
|
|
|
}
|
|
|
|
|
2001-01-05 05:08:07 +01:00
|
|
|
static int set_file_time( handle_t handle, time_t access_time, time_t write_time )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
|
|
|
struct file *file;
|
1999-01-03 12:55:56 +01:00
|
|
|
struct utimbuf utimbuf;
|
1998-12-27 16:28:54 +01:00
|
|
|
|
1999-01-03 12:55:56 +01:00
|
|
|
if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
|
1998-12-27 16:28:54 +01:00
|
|
|
return 0;
|
2001-02-28 22:45:23 +01:00
|
|
|
if (!file->name)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_HANDLE );
|
|
|
|
release_object( file );
|
|
|
|
return 0;
|
|
|
|
}
|
1999-01-31 16:08:31 +01:00
|
|
|
if (!access_time || !write_time)
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
1999-01-31 16:08:31 +01:00
|
|
|
struct stat st;
|
|
|
|
if (stat( file->name, &st ) == -1) goto error;
|
|
|
|
if (!access_time) access_time = st.st_atime;
|
|
|
|
if (!write_time) write_time = st.st_mtime;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
1999-01-31 16:08:31 +01:00
|
|
|
utimbuf.actime = access_time;
|
|
|
|
utimbuf.modtime = write_time;
|
|
|
|
if (utime( file->name, &utimbuf ) == -1) goto error;
|
1998-12-27 16:28:54 +01:00
|
|
|
release_object( file );
|
|
|
|
return 1;
|
1999-01-31 16:08:31 +01:00
|
|
|
error:
|
|
|
|
file_set_error();
|
|
|
|
release_object( file );
|
|
|
|
return 0;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
1999-01-19 18:48:23 +01:00
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
static int file_lock( struct file *file, int offset_high, int offset_low,
|
|
|
|
int count_high, int count_low )
|
1999-01-19 18:48:23 +01:00
|
|
|
{
|
|
|
|
/* FIXME: implement this */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
static int file_unlock( struct file *file, int offset_high, int offset_low,
|
|
|
|
int count_high, int count_low )
|
1999-01-19 18:48:23 +01:00
|
|
|
{
|
|
|
|
/* FIXME: implement this */
|
|
|
|
return 1;
|
|
|
|
}
|
1999-06-26 10:43:26 +02:00
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
/* create a file */
|
|
|
|
DECL_HANDLER(create_file)
|
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
struct file *file;
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->handle = 0;
|
|
|
|
if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
|
2001-10-24 02:23:25 +02:00
|
|
|
req->sharing, req->create, req->attrs, req->drive_type )))
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
|
1999-06-26 10:43:26 +02:00
|
|
|
release_object( file );
|
1999-06-22 19:26:53 +02:00
|
|
|
}
|
1999-06-26 10:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a file handle for a Unix fd */
|
|
|
|
DECL_HANDLER(alloc_file_handle)
|
|
|
|
{
|
|
|
|
struct file *file;
|
2001-02-28 22:45:23 +01:00
|
|
|
int fd;
|
1999-06-26 10:43:26 +02:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->handle = 0;
|
2001-02-28 22:45:23 +01:00
|
|
|
if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
|
1999-06-22 19:26:53 +02:00
|
|
|
{
|
2001-02-28 22:45:23 +01:00
|
|
|
set_error( STATUS_INVALID_HANDLE );
|
|
|
|
return;
|
|
|
|
}
|
2001-10-24 02:23:25 +02:00
|
|
|
if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
0, DRIVE_UNKNOWN )))
|
2001-02-28 22:45:23 +01:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
|
2001-02-28 22:45:23 +01:00
|
|
|
release_object( file );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-19 03:12:45 +01:00
|
|
|
/* get a Unix fd to access a file */
|
|
|
|
DECL_HANDLER(get_handle_fd)
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->fd = -1;
|
|
|
|
reply->type = FD_TYPE_INVALID;
|
2000-12-19 03:12:45 +01:00
|
|
|
if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
2000-12-22 22:12:36 +01:00
|
|
|
int fd = get_handle_fd( current->process, req->handle, req->access );
|
2001-11-30 19:46:42 +01:00
|
|
|
if (fd != -1) reply->fd = fd;
|
2000-12-22 22:12:36 +01:00
|
|
|
else if (!get_error())
|
|
|
|
{
|
|
|
|
if ((fd = obj->ops->get_fd( obj )) != -1)
|
2001-03-01 23:13:49 +01:00
|
|
|
send_client_fd( current->process, fd, req->handle );
|
2000-12-22 22:12:36 +01:00
|
|
|
}
|
2002-01-09 21:30:51 +01:00
|
|
|
reply->type = obj->ops->get_file_info( obj, NULL, &reply->flags );
|
1999-05-15 12:48:19 +02:00
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a file current position */
|
|
|
|
DECL_HANDLER(set_file_pointer)
|
|
|
|
{
|
1999-06-26 10:43:26 +02:00
|
|
|
int high = req->high;
|
|
|
|
int low = req->low;
|
|
|
|
set_file_pointer( req->handle, &low, &high, req->whence );
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->new_low = low;
|
|
|
|
reply->new_high = high;
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* truncate (or extend) a file */
|
|
|
|
DECL_HANDLER(truncate_file)
|
|
|
|
{
|
2002-01-29 03:51:12 +01:00
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
|
|
|
|
{
|
|
|
|
truncate_file( file );
|
|
|
|
release_object( file );
|
|
|
|
}
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* flush a file buffers */
|
|
|
|
DECL_HANDLER(flush_file)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
1999-12-04 05:00:16 +01:00
|
|
|
if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
|
|
|
obj->ops->flush( obj );
|
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a file access and modification times */
|
|
|
|
DECL_HANDLER(set_file_time)
|
|
|
|
{
|
|
|
|
set_file_time( req->handle, req->access_time, req->write_time );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a file information */
|
|
|
|
DECL_HANDLER(get_file_info)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
|
|
|
if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
|
|
|
|
{
|
2002-01-09 21:30:51 +01:00
|
|
|
int flags;
|
|
|
|
obj->ops->get_file_info( obj, reply, &flags );
|
1999-05-15 12:48:19 +02:00
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lock a region of a file */
|
|
|
|
DECL_HANDLER(lock_file)
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
if ((file = get_file_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
|
|
|
file_lock( file, req->offset_high, req->offset_low,
|
|
|
|
req->count_high, req->count_low );
|
|
|
|
release_object( file );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unlock a region of a file */
|
|
|
|
DECL_HANDLER(unlock_file)
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
if ((file = get_file_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
|
|
|
file_unlock( file, req->offset_high, req->offset_low,
|
|
|
|
req->count_high, req->count_low );
|
|
|
|
release_object( file );
|
|
|
|
}
|
|
|
|
}
|