1998-12-27 15:28:54 +00:00
|
|
|
/*
|
|
|
|
* Server-side file management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
|
|
|
*/
|
|
|
|
|
1999-10-24 22:13:47 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
1999-02-28 12:27:56 +00:00
|
|
|
#include <string.h>
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <stdlib.h>
|
1999-01-01 18:42:17 +00:00
|
|
|
#include <errno.h>
|
1999-07-10 13:16:29 +00:00
|
|
|
#ifdef HAVE_SYS_ERRNO_H
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <sys/errno.h>
|
1999-07-10 13:16:29 +00:00
|
|
|
#endif
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1999-01-03 11:55:56 +00:00
|
|
|
#include <utime.h>
|
1998-12-27 15:28:54 +00:00
|
|
|
|
|
|
|
#include "winerror.h"
|
1999-02-09 15:49:39 +00:00
|
|
|
#include "winbase.h"
|
1999-05-15 10:48:19 +00:00
|
|
|
|
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 17:26:53 +00:00
|
|
|
#include "request.h"
|
1998-12-27 15:28:54 +00:00
|
|
|
|
|
|
|
struct file
|
|
|
|
{
|
1999-05-16 16:59:38 +00:00
|
|
|
struct object obj; /* object header */
|
|
|
|
struct select_user select; /* select user */
|
|
|
|
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 */
|
1998-12-27 15:28:54 +00:00
|
|
|
};
|
|
|
|
|
1999-01-03 11:55:56 +00:00
|
|
|
#define NAME_HASH_SIZE 37
|
|
|
|
|
|
|
|
static struct file *file_hash[NAME_HASH_SIZE];
|
|
|
|
|
1998-12-27 15:28:54 +00:00
|
|
|
static void file_dump( struct object *obj, int verbose );
|
1999-01-01 16:59:27 +00:00
|
|
|
static int file_add_queue( struct object *obj, struct wait_queue_entry *entry );
|
1998-12-27 15:28:54 +00:00
|
|
|
static void file_remove_queue( struct object *obj, struct wait_queue_entry *entry );
|
|
|
|
static int file_signaled( struct object *obj, struct thread *thread );
|
1998-12-30 12:06:45 +00:00
|
|
|
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 );
|
1999-06-26 08:43:26 +00:00
|
|
|
static int file_get_info( struct object *obj, struct get_file_info_request *req );
|
1998-12-27 15:28:54 +00:00
|
|
|
static void file_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops file_ops =
|
|
|
|
{
|
1999-06-22 17:26:53 +00:00
|
|
|
sizeof(struct file),
|
1998-12-27 15:28:54 +00:00
|
|
|
file_dump,
|
|
|
|
file_add_queue,
|
|
|
|
file_remove_queue,
|
|
|
|
file_signaled,
|
1998-12-30 12:06:45 +00:00
|
|
|
no_satisfied,
|
|
|
|
file_get_read_fd,
|
|
|
|
file_get_write_fd,
|
|
|
|
file_flush,
|
1999-01-03 11:55:56 +00:00
|
|
|
file_get_info,
|
1998-12-27 15:28:54 +00:00
|
|
|
file_destroy
|
|
|
|
};
|
|
|
|
|
1999-01-03 11:55:56 +00:00
|
|
|
|
|
|
|
static int get_name_hash( const char *name )
|
|
|
|
{
|
|
|
|
int hash = 0;
|
1999-11-15 00:07:30 +00:00
|
|
|
while (*name) hash ^= (unsigned char)*name++;
|
1999-01-03 11:55:56 +00: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 17:26:53 +00: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 11:55:56 +00:00
|
|
|
return 1;
|
1999-06-22 17:26:53 +00:00
|
|
|
error:
|
|
|
|
set_error( ERROR_SHARING_VIOLATION );
|
|
|
|
return 0;
|
1999-01-03 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
1999-06-22 17:26:53 +00:00
|
|
|
static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
|
|
|
|
unsigned int attrs )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
|
|
|
struct file *file;
|
1999-06-22 17:26:53 +00:00
|
|
|
if ((file = alloc_object( &file_ops )))
|
1999-01-03 11:55:56 +00:00
|
|
|
{
|
1999-06-22 17:26:53 +00:00
|
|
|
file->name = NULL;
|
|
|
|
file->next = NULL;
|
|
|
|
file->select.fd = fd;
|
|
|
|
file->select.func = default_select_event;
|
|
|
|
file->select.private = file;
|
|
|
|
file->access = access;
|
|
|
|
file->flags = attrs;
|
|
|
|
file->sharing = sharing;
|
|
|
|
register_select_user( &file->select );
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
1999-01-03 11:55:56 +00:00
|
|
|
|
|
|
|
|
1999-06-22 17:26:53 +00:00
|
|
|
static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
|
|
|
|
unsigned int sharing, int create, unsigned int attrs )
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
int hash, flags;
|
|
|
|
struct stat st;
|
|
|
|
char *name;
|
|
|
|
int fd = -1;
|
1999-01-03 11:55:56 +00:00
|
|
|
|
1999-06-22 17:26:53 +00:00
|
|
|
if (!(name = mem_alloc( len + 1 ))) return NULL;
|
|
|
|
memcpy( name, nameptr, len );
|
|
|
|
name[len] = 0;
|
1999-01-03 11:55:56 +00:00
|
|
|
|
1999-06-22 17:26:53 +00:00
|
|
|
/* check sharing mode */
|
|
|
|
hash = get_name_hash( name );
|
|
|
|
if (!check_sharing( name, hash, access, sharing )) goto error;
|
1998-12-27 15:28:54 +00:00
|
|
|
|
1999-06-22 17:26:53 +00:00
|
|
|
switch(create)
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1999-06-22 17:26:53 +00: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;
|
|
|
|
default: set_error( ERROR_INVALID_PARAMETER ); goto error;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
1999-06-22 17:26:53 +00:00
|
|
|
switch(access & (GENERIC_READ | GENERIC_WRITE))
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1999-06-22 17:26:53 +00: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 11:55:56 +00:00
|
|
|
}
|
1999-06-22 17:26:53 +00:00
|
|
|
|
|
|
|
/* FIXME: should set error to ERROR_ALREADY_EXISTS if file existed before */
|
|
|
|
if ((fd = open( name, flags | O_NONBLOCK,
|
|
|
|
(attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666 )) == -1)
|
|
|
|
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 11:55:56 +00:00
|
|
|
{
|
1999-06-22 17:26:53 +00:00
|
|
|
set_error( ERROR_ACCESS_DENIED );
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(file = create_file_for_fd( fd, access, sharing, attrs ))) goto error;
|
|
|
|
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 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
1999-06-22 17:26:53 +00:00
|
|
|
/* Create an anonymous Unix file */
|
|
|
|
int create_anonymous_file(void)
|
1999-02-28 10:13:59 +00:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!(name = tmpnam(NULL)))
|
|
|
|
{
|
1999-06-22 17:26:53 +00:00
|
|
|
set_error( ERROR_TOO_MANY_OPEN_FILES );
|
|
|
|
return -1;
|
1999-02-28 10:13:59 +00: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 17:26:53 +00:00
|
|
|
return -1;
|
1999-02-28 10:13:59 +00:00
|
|
|
}
|
|
|
|
unlink( name );
|
1999-06-22 17:26:53 +00:00
|
|
|
return fd;
|
|
|
|
}
|
1999-02-28 10:13:59 +00:00
|
|
|
|
1999-06-22 17:26:53 +00:00
|
|
|
/* Create a temp file for anonymous mappings */
|
|
|
|
struct file *create_temp_file( int access )
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
int fd;
|
|
|
|
|
1999-06-26 08:43:26 +00:00
|
|
|
if ((fd = create_anonymous_file()) == -1) return NULL;
|
1999-06-22 17:26:53 +00:00
|
|
|
if (!(file = create_file_for_fd( fd, access, 0, 0 ))) close( fd );
|
1999-02-28 10:13:59 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
1998-12-27 15:28:54 +00:00
|
|
|
static void file_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
1999-06-22 17:26:53 +00:00
|
|
|
fprintf( stderr, "File fd=%d flags=%08x name='%s'\n",
|
|
|
|
file->select.fd, file->flags, file->name );
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
1999-01-01 16:59:27 +00:00
|
|
|
static int file_add_queue( struct object *obj, struct wait_queue_entry *entry )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
if (!obj->head) /* first on the queue */
|
1999-01-01 16:59:27 +00:00
|
|
|
{
|
1999-05-16 16:59:38 +00:00
|
|
|
int events = 0;
|
|
|
|
if (file->access & GENERIC_READ) events |= READ_EVENT;
|
|
|
|
if (file->access & GENERIC_WRITE) events |= WRITE_EVENT;
|
|
|
|
set_select_events( &file->select, events );
|
1999-01-01 16:59:27 +00:00
|
|
|
}
|
1998-12-27 15:28:54 +00:00
|
|
|
add_queue( obj, entry );
|
1999-01-01 16:59:27 +00:00
|
|
|
return 1;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void file_remove_queue( struct object *obj, struct wait_queue_entry *entry )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)grab_object(obj);
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
|
|
|
remove_queue( obj, entry );
|
|
|
|
if (!obj->head) /* last on the queue is gone */
|
1999-05-16 16:59:38 +00:00
|
|
|
set_select_events( &file->select, 0 );
|
1998-12-27 15:28:54 +00:00
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_signaled( struct object *obj, struct thread *thread )
|
|
|
|
{
|
1999-05-16 16:59:38 +00:00
|
|
|
int events = 0;
|
1998-12-27 15:28:54 +00:00
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
1999-05-16 16:59:38 +00:00
|
|
|
if (file->access & GENERIC_READ) events |= READ_EVENT;
|
|
|
|
if (file->access & GENERIC_WRITE) events |= WRITE_EVENT;
|
|
|
|
if (check_select_events( &file->select, events ))
|
|
|
|
{
|
|
|
|
/* stop waiting on select() if we are signaled */
|
|
|
|
set_select_events( &file->select, 0 );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* restart waiting on select() if we are no longer signaled */
|
|
|
|
if (obj->head) set_select_events( &file->select, events );
|
|
|
|
return 0;
|
|
|
|
}
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
1998-12-30 12:06:45 +00:00
|
|
|
static int file_get_read_fd( struct object *obj )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1998-12-30 12:06:45 +00:00
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
1999-05-16 16:59:38 +00:00
|
|
|
return dup( file->select.fd );
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
1998-12-30 12:06:45 +00:00
|
|
|
static int file_get_write_fd( struct object *obj )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
1999-05-16 16:59:38 +00:00
|
|
|
return dup( file->select.fd );
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
1998-12-30 12:06:45 +00:00
|
|
|
static int file_flush( struct object *obj )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1998-12-30 12:06:45 +00:00
|
|
|
int ret;
|
|
|
|
struct file *file = (struct file *)grab_object(obj);
|
|
|
|
assert( obj->ops == &file_ops );
|
1998-12-27 15:28:54 +00:00
|
|
|
|
1999-05-16 16:59:38 +00:00
|
|
|
ret = (fsync( file->select.fd ) != -1);
|
1998-12-30 12:06:45 +00:00
|
|
|
if (!ret) file_set_error();
|
|
|
|
release_object( file );
|
|
|
|
return ret;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
1999-06-26 08:43:26 +00:00
|
|
|
static int file_get_info( struct object *obj, struct get_file_info_request *req )
|
1999-01-03 11:55:56 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
1999-05-16 16:59:38 +00:00
|
|
|
if (fstat( file->select.fd, &st ) == -1)
|
1999-01-03 11:55:56 +00:00
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
|
1999-06-26 08:43:26 +00:00
|
|
|
S_ISSOCK(st.st_mode) || isatty(file->select.fd)) req->type = FILE_TYPE_CHAR;
|
|
|
|
else req->type = FILE_TYPE_DISK;
|
|
|
|
if (S_ISDIR(st.st_mode)) req->attr = FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
else req->attr = FILE_ATTRIBUTE_ARCHIVE;
|
|
|
|
if (!(st.st_mode & S_IWUSR)) req->attr |= FILE_ATTRIBUTE_READONLY;
|
|
|
|
req->access_time = st.st_atime;
|
|
|
|
req->write_time = st.st_mtime;
|
|
|
|
req->size_high = 0;
|
|
|
|
req->size_low = S_ISDIR(st.st_mode) ? 0 : st.st_size;
|
|
|
|
req->links = st.st_nlink;
|
|
|
|
req->index_high = st.st_dev;
|
|
|
|
req->index_low = st.st_ino;
|
|
|
|
req->serial = 0; /* FIXME */
|
1999-01-03 11:55:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-12-30 12:06:45 +00:00
|
|
|
static void file_destroy( struct object *obj )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1998-12-30 12:06:45 +00:00
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
1999-01-03 11:55:56 +00:00
|
|
|
|
1999-01-17 16:32:32 +00: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 11:20:07 +00:00
|
|
|
if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
|
1999-01-17 16:32:32 +00:00
|
|
|
free( file->name );
|
|
|
|
}
|
1999-05-16 16:59:38 +00:00
|
|
|
unregister_select_user( &file->select );
|
|
|
|
close( file->select.fd );
|
1998-12-30 12:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the last error depending on errno */
|
|
|
|
void file_set_error(void)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
1999-06-22 17:26:53 +00:00
|
|
|
case EAGAIN: set_error( ERROR_SHARING_VIOLATION ); break;
|
|
|
|
case EBADF: set_error( ERROR_INVALID_HANDLE ); break;
|
|
|
|
case ENOSPC: set_error( ERROR_HANDLE_DISK_FULL ); break;
|
1998-12-30 12:06:45 +00:00
|
|
|
case EACCES:
|
1999-06-22 17:26:53 +00:00
|
|
|
case EPERM: set_error( ERROR_ACCESS_DENIED ); break;
|
|
|
|
case EROFS: set_error( ERROR_WRITE_PROTECT ); break;
|
|
|
|
case EBUSY: set_error( ERROR_LOCK_VIOLATION ); break;
|
|
|
|
case ENOENT: set_error( ERROR_FILE_NOT_FOUND ); break;
|
|
|
|
case EISDIR: set_error( ERROR_CANNOT_MAKE ); break;
|
1998-12-30 12:06:45 +00:00
|
|
|
case ENFILE:
|
1999-06-22 17:26:53 +00:00
|
|
|
case EMFILE: set_error( ERROR_NO_MORE_FILES ); break;
|
|
|
|
case EEXIST: set_error( ERROR_FILE_EXISTS ); break;
|
|
|
|
case EINVAL: set_error( ERROR_INVALID_PARAMETER ); break;
|
|
|
|
case ESPIPE: set_error( ERROR_SEEK ); break;
|
|
|
|
case ENOTEMPTY: set_error( ERROR_DIR_NOT_EMPTY ); break;
|
1999-11-12 21:39:14 +00:00
|
|
|
case EIO: set_error( ERROR_NOACCESS ); break;
|
1999-06-22 17:26:53 +00:00
|
|
|
default: perror("file_set_error"); set_error( ERROR_UNKNOWN ); break;
|
1998-12-30 12:06:45 +00:00
|
|
|
}
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
1999-01-01 16:59:27 +00:00
|
|
|
struct file *get_file_obj( struct process *process, int handle,
|
|
|
|
unsigned int access )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1999-01-01 16:59:27 +00:00
|
|
|
return (struct file *)get_handle_obj( current->process, handle,
|
|
|
|
access, &file_ops );
|
|
|
|
}
|
1998-12-27 15:28:54 +00:00
|
|
|
|
1999-01-01 16:59:27 +00:00
|
|
|
int file_get_mmap_fd( struct file *file )
|
|
|
|
{
|
1999-05-16 16:59:38 +00:00
|
|
|
return dup( file->select.fd );
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
static int set_file_pointer( int handle, int *low, int *high, int whence )
|
1998-12-30 12:06:45 +00:00
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (*high)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "set_file_pointer: offset > 4Gb not supported yet\n" );
|
1999-06-22 17:26:53 +00:00
|
|
|
set_error( ERROR_INVALID_PARAMETER );
|
1998-12-30 12:06:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-01-01 16:59:27 +00:00
|
|
|
if (!(file = get_file_obj( current->process, handle, 0 )))
|
1998-12-30 12:06:45 +00:00
|
|
|
return 0;
|
1999-05-16 16:59:38 +00:00
|
|
|
if ((result = lseek( file->select.fd, *low, whence )) == -1)
|
1998-12-30 12:06:45 +00:00
|
|
|
{
|
|
|
|
/* Check for seek before start of file */
|
|
|
|
if ((errno == EINVAL) && (whence != SEEK_SET) && (*low < 0))
|
1999-06-22 17:26:53 +00:00
|
|
|
set_error( ERROR_NEGATIVE_SEEK );
|
1998-12-30 12:06:45 +00:00
|
|
|
else
|
|
|
|
file_set_error();
|
|
|
|
release_object( file );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*low = result;
|
|
|
|
release_object( file );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
static int truncate_file( int handle )
|
1998-12-30 12:06:45 +00:00
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
int result;
|
|
|
|
|
1999-01-01 16:59:27 +00:00
|
|
|
if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
|
1998-12-30 12:06:45 +00:00
|
|
|
return 0;
|
1999-05-16 16:59:38 +00:00
|
|
|
if (((result = lseek( file->select.fd, 0, SEEK_CUR )) == -1) ||
|
|
|
|
(ftruncate( file->select.fd, result ) == -1))
|
1998-12-30 12:06:45 +00:00
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
release_object( file );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
release_object( file );
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1999-02-28 10:13:59 +00:00
|
|
|
/* try to grow the file to the specified size */
|
|
|
|
int grow_file( struct file *file, int size_high, int size_low )
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (size_high)
|
|
|
|
{
|
1999-06-22 17:26:53 +00:00
|
|
|
set_error( ERROR_INVALID_PARAMETER );
|
1999-02-28 10:13:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-05-16 16:59:38 +00:00
|
|
|
if (fstat( file->select.fd, &st ) == -1)
|
1999-02-28 10:13:59 +00:00
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (st.st_size >= size_low) return 1; /* already large enough */
|
1999-05-16 16:59:38 +00:00
|
|
|
if (ftruncate( file->select.fd, size_low ) != -1) return 1;
|
1999-02-28 10:13:59 +00:00
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
static int set_file_time( int handle, time_t access_time, time_t write_time )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
|
|
|
struct file *file;
|
1999-01-03 11:55:56 +00:00
|
|
|
struct utimbuf utimbuf;
|
1998-12-27 15:28:54 +00:00
|
|
|
|
1999-01-03 11:55:56 +00:00
|
|
|
if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
|
1998-12-27 15:28:54 +00:00
|
|
|
return 0;
|
1999-01-31 15:08:31 +00:00
|
|
|
if (!access_time || !write_time)
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1999-01-31 15:08:31 +00: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 15:28:54 +00:00
|
|
|
}
|
1999-01-31 15:08:31 +00:00
|
|
|
utimbuf.actime = access_time;
|
|
|
|
utimbuf.modtime = write_time;
|
|
|
|
if (utime( file->name, &utimbuf ) == -1) goto error;
|
1998-12-27 15:28:54 +00:00
|
|
|
release_object( file );
|
|
|
|
return 1;
|
1999-01-31 15:08:31 +00:00
|
|
|
error:
|
|
|
|
file_set_error();
|
|
|
|
release_object( file );
|
|
|
|
return 0;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
1999-01-19 17:48:23 +00:00
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
static int file_lock( struct file *file, int offset_high, int offset_low,
|
|
|
|
int count_high, int count_low )
|
1999-01-19 17:48:23 +00:00
|
|
|
{
|
|
|
|
/* FIXME: implement this */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
static int file_unlock( struct file *file, int offset_high, int offset_low,
|
|
|
|
int count_high, int count_low )
|
1999-01-19 17:48:23 +00:00
|
|
|
{
|
|
|
|
/* FIXME: implement this */
|
|
|
|
return 1;
|
|
|
|
}
|
1999-06-26 08:43:26 +00:00
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
/* create a file */
|
|
|
|
DECL_HANDLER(create_file)
|
|
|
|
{
|
1999-06-26 08:43:26 +00:00
|
|
|
size_t len = get_req_strlen( req->name );
|
|
|
|
struct file *file;
|
1999-05-15 10:48:19 +00:00
|
|
|
|
1999-06-26 08:43:26 +00:00
|
|
|
req->handle = -1;
|
|
|
|
if ((file = create_file( req->name, len, req->access,
|
|
|
|
req->sharing, req->create, req->attrs )))
|
1999-05-15 10:48:19 +00:00
|
|
|
{
|
1999-06-26 08:43:26 +00:00
|
|
|
req->handle = alloc_handle( current->process, file, req->access, req->inherit );
|
|
|
|
release_object( file );
|
1999-06-22 17:26:53 +00:00
|
|
|
}
|
1999-06-26 08:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a file handle for a Unix fd */
|
|
|
|
DECL_HANDLER(alloc_file_handle)
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
req->handle = -1;
|
|
|
|
if ((fd = dup(fd)) != -1)
|
1999-06-22 17:26:53 +00:00
|
|
|
{
|
1999-06-26 08:43:26 +00:00
|
|
|
if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE, 0 )))
|
|
|
|
{
|
|
|
|
req->handle = alloc_handle( current->process, file, req->access, 0 );
|
|
|
|
release_object( file );
|
|
|
|
}
|
|
|
|
else close( fd );
|
1999-05-15 10:48:19 +00:00
|
|
|
}
|
1999-06-26 08:43:26 +00:00
|
|
|
else file_set_error();
|
1999-05-15 10:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get a Unix fd to read from a file */
|
|
|
|
DECL_HANDLER(get_read_fd)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
|
|
|
if ((obj = get_handle_obj( current->process, req->handle, GENERIC_READ, NULL )))
|
|
|
|
{
|
1999-06-26 08:43:26 +00:00
|
|
|
set_reply_fd( current, obj->ops->get_read_fd( obj ) );
|
1999-05-15 10:48:19 +00:00
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a Unix fd to write to a file */
|
|
|
|
DECL_HANDLER(get_write_fd)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
|
|
|
if ((obj = get_handle_obj( current->process, req->handle, GENERIC_WRITE, NULL )))
|
|
|
|
{
|
1999-06-26 08:43:26 +00:00
|
|
|
set_reply_fd( current, obj->ops->get_write_fd( obj ) );
|
1999-05-15 10:48:19 +00:00
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a file current position */
|
|
|
|
DECL_HANDLER(set_file_pointer)
|
|
|
|
{
|
1999-06-26 08:43:26 +00:00
|
|
|
int high = req->high;
|
|
|
|
int low = req->low;
|
|
|
|
set_file_pointer( req->handle, &low, &high, req->whence );
|
|
|
|
req->new_low = low;
|
|
|
|
req->new_high = high;
|
1999-05-15 10:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* truncate (or extend) a file */
|
|
|
|
DECL_HANDLER(truncate_file)
|
|
|
|
{
|
|
|
|
truncate_file( req->handle );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flush a file buffers */
|
|
|
|
DECL_HANDLER(flush_file)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
|
|
|
if ((obj = get_handle_obj( current->process, req->handle, GENERIC_WRITE, NULL )))
|
|
|
|
{
|
|
|
|
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 )))
|
|
|
|
{
|
1999-06-26 08:43:26 +00:00
|
|
|
obj->ops->get_file_info( obj, req );
|
1999-05-15 10:48:19 +00: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 );
|
|
|
|
}
|
|
|
|
}
|