1998-12-27 15:28:54 +00:00
|
|
|
/*
|
|
|
|
* Server-side file management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2002-03-09 23:29:33 +00: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 15:28:54 +00:00
|
|
|
*/
|
|
|
|
|
1999-10-24 22:13:47 +00:00
|
|
|
#include "config.h"
|
2001-05-14 20:09:37 +00:00
|
|
|
#include "wine/port.h"
|
1999-10-24 22:13:47 +00:00
|
|
|
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2003-09-05 23:08:26 +00:00
|
|
|
#include <stdarg.h>
|
1998-12-27 15:28:54 +00:00
|
|
|
#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>
|
2003-02-11 22:27:13 +00:00
|
|
|
#ifdef HAVE_UTIME_H
|
1999-01-03 11:55:56 +00:00
|
|
|
#include <utime.h>
|
2003-02-11 22:27:13 +00:00
|
|
|
#endif
|
2005-03-04 12:38:36 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
1998-12-27 15:28:54 +00:00
|
|
|
|
2005-11-28 16:32:54 +00:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-05 23:08:26 +00:00
|
|
|
#include "windef.h"
|
2004-03-12 01:56:49 +00:00
|
|
|
#include "winternl.h"
|
1999-05-15 10:48:19 +00:00
|
|
|
|
2003-01-30 00:26:44 +00:00
|
|
|
#include "file.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 */
|
2003-02-19 00:33:32 +00:00
|
|
|
struct fd *fd; /* file descriptor for this file */
|
2005-12-12 15:46:17 +00:00
|
|
|
unsigned int access; /* file access (FILE_READ_DATA etc.) */
|
2004-03-12 01:56:49 +00:00
|
|
|
unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
|
1998-12-27 15:28:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void file_dump( struct object *obj, int verbose );
|
2003-02-19 00:33:32 +00:00
|
|
|
static struct fd *file_get_fd( struct object *obj );
|
2005-12-12 15:46:17 +00:00
|
|
|
static unsigned int file_map_access( struct object *obj, unsigned int access );
|
1998-12-27 15:28:54 +00:00
|
|
|
static void file_destroy( struct object *obj );
|
2003-02-14 20:27:09 +00:00
|
|
|
|
|
|
|
static int file_get_poll_events( struct fd *fd );
|
2003-05-15 04:22:45 +00:00
|
|
|
static int file_flush( struct fd *fd, struct event **event );
|
2004-08-18 00:04:58 +00:00
|
|
|
static int file_get_info( struct fd *fd );
|
1998-12-27 15:28:54 +00:00
|
|
|
|
|
|
|
static const struct object_ops file_ops =
|
|
|
|
{
|
2000-01-01 00:56:27 +00:00
|
|
|
sizeof(struct file), /* size */
|
|
|
|
file_dump, /* dump */
|
2003-01-30 00:26:44 +00:00
|
|
|
default_fd_add_queue, /* add_queue */
|
|
|
|
default_fd_remove_queue, /* remove_queue */
|
|
|
|
default_fd_signaled, /* signaled */
|
2000-01-01 00:56:27 +00:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 17:35:52 +00:00
|
|
|
no_signal, /* signal */
|
2003-02-19 00:33:32 +00:00
|
|
|
file_get_fd, /* get_fd */
|
2005-12-12 15:46:17 +00:00
|
|
|
file_map_access, /* map_access */
|
2005-11-22 14:55:42 +00:00
|
|
|
no_lookup_name, /* lookup_name */
|
2005-06-09 15:39:52 +00:00
|
|
|
no_close_handle, /* close_handle */
|
2003-01-30 00:26:44 +00:00
|
|
|
file_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fd_ops file_fd_ops =
|
|
|
|
{
|
2000-01-01 00:56:27 +00:00
|
|
|
file_get_poll_events, /* get_poll_events */
|
2005-06-07 20:09:01 +00:00
|
|
|
default_poll_event, /* poll_event */
|
2000-01-01 00:56:27 +00:00
|
|
|
file_flush, /* flush */
|
|
|
|
file_get_info, /* get_file_info */
|
2005-06-07 20:09:01 +00:00
|
|
|
default_fd_queue_async, /* queue_async */
|
|
|
|
default_fd_cancel_async /* cancel_async */
|
1998-12-27 15:28:54 +00:00
|
|
|
};
|
|
|
|
|
2004-03-12 01:56:49 +00:00
|
|
|
static inline int is_overlapped( const struct file *file )
|
|
|
|
{
|
|
|
|
return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
|
|
|
|
}
|
|
|
|
|
2000-01-01 00:56:27 +00:00
|
|
|
/* create a file from a file descriptor */
|
|
|
|
/* if the function fails the fd is closed */
|
2004-03-12 01:56:49 +00:00
|
|
|
static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
|
|
|
struct file *file;
|
2003-01-30 00:26:44 +00:00
|
|
|
|
2003-02-19 00:33:32 +00:00
|
|
|
if ((file = alloc_object( &file_ops )))
|
1999-01-03 11:55:56 +00:00
|
|
|
{
|
2005-12-12 15:46:17 +00:00
|
|
|
file->access = file_map_access( &file->obj, access );
|
2004-03-12 01:56:49 +00:00
|
|
|
file->options = FILE_SYNCHRONOUS_IO_NONALERT;
|
2003-03-12 22:38:14 +00:00
|
|
|
if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
|
2003-02-19 00:33:32 +00:00
|
|
|
{
|
|
|
|
release_object( file );
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-22 17:26:53 +00:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
1999-01-03 11:55:56 +00:00
|
|
|
|
|
|
|
|
2004-03-18 04:08:48 +00:00
|
|
|
static struct object *create_file( const char *nameptr, size_t len, unsigned int access,
|
|
|
|
unsigned int sharing, int create, unsigned int options,
|
2004-04-06 23:41:01 +00:00
|
|
|
unsigned int attrs )
|
1999-06-22 17:26:53 +00:00
|
|
|
{
|
|
|
|
struct file *file;
|
2005-12-12 15:46:17 +00:00
|
|
|
int flags, rw_mode;
|
1999-06-22 17:26:53 +00:00
|
|
|
char *name;
|
2001-07-23 18:09:41 +00:00
|
|
|
mode_t mode;
|
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
|
|
|
switch(create)
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
2004-03-12 01:56:49 +00:00
|
|
|
case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
|
|
|
|
case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
|
|
|
|
case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
|
|
|
|
case FILE_OPEN: flags = 0; break;
|
|
|
|
case FILE_OPEN_IF: flags = O_CREAT; break;
|
|
|
|
case FILE_OVERWRITE: flags = O_TRUNC; break;
|
2000-01-24 21:58:06 +00:00
|
|
|
default: set_error( STATUS_INVALID_PARAMETER ); goto error;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
2005-01-14 19:54:38 +00:00
|
|
|
|
2001-07-23 18:09:41 +00: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 17:26:53 +00:00
|
|
|
|
2003-03-12 22:38:14 +00:00
|
|
|
if (!(file = alloc_object( &file_ops ))) goto error;
|
|
|
|
|
2005-12-12 15:46:17 +00:00
|
|
|
file->access = file_map_access( &file->obj, access );
|
2004-03-12 01:56:49 +00:00
|
|
|
file->options = options;
|
1999-06-22 17:26:53 +00:00
|
|
|
|
2005-12-12 15:46:17 +00:00
|
|
|
rw_mode = 0;
|
|
|
|
if (file->access & FILE_UNIX_READ_ACCESS) rw_mode |= FILE_READ_DATA;
|
|
|
|
if (file->access & FILE_UNIX_WRITE_ACCESS) rw_mode |= FILE_WRITE_DATA;
|
|
|
|
switch(rw_mode)
|
|
|
|
{
|
|
|
|
case 0: break;
|
|
|
|
case FILE_READ_DATA: flags |= O_RDONLY; break;
|
|
|
|
case FILE_WRITE_DATA: flags |= O_WRONLY; break;
|
|
|
|
case FILE_READ_DATA|FILE_WRITE_DATA: flags |= O_RDWR; break;
|
|
|
|
}
|
|
|
|
|
2003-03-12 22:38:14 +00:00
|
|
|
/* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
|
|
|
|
if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) ||
|
2004-03-27 20:48:42 +00:00
|
|
|
!(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE,
|
2005-12-12 15:46:17 +00:00
|
|
|
&mode, file->access, sharing, options )))
|
2003-03-12 22:38:14 +00:00
|
|
|
{
|
2004-04-02 19:50:49 +00:00
|
|
|
free( name );
|
2003-03-12 22:38:14 +00:00
|
|
|
release_object( file );
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-04-02 19:50:49 +00:00
|
|
|
free( name );
|
|
|
|
|
2004-03-18 04:08:48 +00:00
|
|
|
/* check for serial port */
|
|
|
|
if (S_ISCHR(mode) && is_serial_fd( file->fd ))
|
|
|
|
{
|
|
|
|
struct object *obj = create_serial( file->fd, file->options );
|
|
|
|
release_object( file );
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &file->obj;
|
1999-06-22 17:26:53 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
free( name );
|
|
|
|
return NULL;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2001-04-13 22:38:39 +00:00
|
|
|
/* check if two file objects point to the same file */
|
|
|
|
int is_same_file( struct file *file1, struct file *file2 )
|
|
|
|
{
|
2004-03-27 20:48:42 +00:00
|
|
|
return is_same_file_fd( file1->fd, file2->fd );
|
2001-04-13 22:38:39 +00:00
|
|
|
}
|
|
|
|
|
2003-03-12 22:38:14 +00:00
|
|
|
/* create a temp file for anonymous mappings */
|
|
|
|
struct file *create_temp_file( int access )
|
1999-02-28 10:13:59 +00:00
|
|
|
{
|
2003-03-12 22:38:14 +00:00
|
|
|
char tmpfn[16];
|
1999-02-28 10:13:59 +00:00
|
|
|
int fd;
|
|
|
|
|
2003-03-12 22:38:14 +00:00
|
|
|
sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
|
2003-03-20 21:07:49 +00:00
|
|
|
fd = mkstemps( tmpfn, 0 );
|
1999-02-28 10:13:59 +00:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
2003-03-12 22:38:14 +00:00
|
|
|
return NULL;
|
1999-02-28 10:13:59 +00:00
|
|
|
}
|
2002-07-29 23:55:39 +00:00
|
|
|
unlink( tmpfn );
|
2004-03-12 01:56:49 +00:00
|
|
|
return create_file_for_fd( fd, access, 0 );
|
1999-02-28 10:13:59 +00:00
|
|
|
}
|
|
|
|
|
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 );
|
2004-04-02 19:50:49 +00:00
|
|
|
fprintf( stderr, "File fd=%p options=%08x\n", file->fd, file->options );
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2003-02-14 20:27:09 +00:00
|
|
|
static int file_get_poll_events( struct fd *fd )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
2003-02-14 20:27:09 +00:00
|
|
|
struct file *file = get_fd_user( fd );
|
1999-05-16 16:59:38 +00:00
|
|
|
int events = 0;
|
2003-02-14 20:27:09 +00:00
|
|
|
assert( file->obj.ops == &file_ops );
|
2005-12-12 15:46:17 +00:00
|
|
|
if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
|
|
|
|
if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
|
2000-01-01 00:56:27 +00:00
|
|
|
return events;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2003-05-15 04:22:45 +00:00
|
|
|
static int file_flush( struct fd *fd, struct event **event )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
2005-08-08 15:11:03 +00:00
|
|
|
int ret = 0, unix_fd = get_unix_fd( fd );
|
|
|
|
|
|
|
|
if (unix_fd != -1)
|
|
|
|
{
|
|
|
|
ret = (fsync( unix_fd ) != -1);
|
|
|
|
if (!ret) file_set_error();
|
|
|
|
}
|
1998-12-30 12:06:45 +00:00
|
|
|
return ret;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2004-08-18 00:04:58 +00:00
|
|
|
static int file_get_info( struct fd *fd )
|
1999-01-03 11:55:56 +00:00
|
|
|
{
|
2003-02-14 20:27:09 +00:00
|
|
|
struct file *file = get_fd_user( fd );
|
1999-01-03 11:55:56 +00:00
|
|
|
|
2004-08-18 00:04:58 +00:00
|
|
|
if (is_overlapped( file )) return FD_FLAG_OVERLAPPED;
|
|
|
|
else return 0;
|
1999-01-03 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2003-02-19 00:33:32 +00:00
|
|
|
static struct fd *file_get_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
return (struct fd *)grab_object( file->fd );
|
2002-01-07 21:02:15 +00:00
|
|
|
}
|
|
|
|
|
2005-12-12 15:46:17 +00:00
|
|
|
static unsigned int file_map_access( struct object *obj, unsigned int access )
|
|
|
|
{
|
|
|
|
if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
|
|
|
|
if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
|
|
|
|
if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
|
|
|
|
if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
|
|
|
|
return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2003-02-19 00:33:32 +00:00
|
|
|
if (file->fd) release_object( file->fd );
|
1998-12-30 12:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the last error depending on errno */
|
|
|
|
void file_set_error(void)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
2000-01-24 21:58:06 +00: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 12:06:45 +00:00
|
|
|
case EACCES:
|
2001-07-14 00:50:30 +00:00
|
|
|
case ESRCH:
|
2000-01-24 21:58:06 +00: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;
|
2004-04-16 23:32:40 +00:00
|
|
|
case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
|
1998-12-30 12:06:45 +00:00
|
|
|
case ENFILE:
|
2000-01-24 21:58:06 +00: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;
|
2003-12-10 01:12:18 +00:00
|
|
|
case ESPIPE: set_win32_error( ERROR_SEEK ); break;
|
2000-01-24 21:58:06 +00:00
|
|
|
case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
|
|
|
|
case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
|
2004-03-12 01:56:49 +00:00
|
|
|
case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
|
2004-05-01 02:50:06 +00:00
|
|
|
case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
|
2004-07-06 19:42:09 +00:00
|
|
|
case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
|
|
|
|
case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
|
2004-01-02 20:11:35 +00:00
|
|
|
#ifdef EOVERFLOW
|
2003-03-18 05:04:33 +00:00
|
|
|
case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
|
2004-01-02 20:11:35 +00:00
|
|
|
#endif
|
2004-04-12 23:15:11 +00:00
|
|
|
default: perror("file_set_error"); set_error( STATUS_UNSUCCESSFUL ); break;
|
1998-12-30 12:06:45 +00:00
|
|
|
}
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2002-05-30 20:12:58 +00:00
|
|
|
struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1999-11-29 02:17:08 +00:00
|
|
|
return (struct file *)get_handle_obj( process, handle, access, &file_ops );
|
1999-01-01 16:59:27 +00:00
|
|
|
}
|
1998-12-27 15:28:54 +00:00
|
|
|
|
2003-02-14 20:27:09 +00:00
|
|
|
int get_file_unix_fd( struct file *file )
|
|
|
|
{
|
2003-02-19 00:33:32 +00:00
|
|
|
return get_unix_fd( file->fd );
|
2003-02-14 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2002-01-29 02:51:12 +00:00
|
|
|
/* extend a file beyond the current end of file */
|
2005-04-19 11:59:13 +00:00
|
|
|
static int extend_file( struct file *file, file_pos_t new_size )
|
1998-12-30 12:06:45 +00:00
|
|
|
{
|
2002-01-29 02:51:12 +00:00
|
|
|
static const char zero;
|
2003-02-14 20:27:09 +00:00
|
|
|
int unix_fd = get_file_unix_fd( file );
|
2005-04-19 11:59:13 +00:00
|
|
|
off_t size = new_size;
|
1998-12-30 12:06:45 +00:00
|
|
|
|
2005-08-08 15:11:03 +00:00
|
|
|
if (unix_fd == -1) return 0;
|
|
|
|
|
2005-04-19 11:59:13 +00:00
|
|
|
if (sizeof(new_size) > sizeof(size) && size != new_size)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
2002-01-29 02:51:12 +00: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 */
|
2004-05-01 02:50:06 +00:00
|
|
|
if (pwrite( unix_fd, &zero, 1, size ) != -1)
|
1998-12-30 12:06:45 +00:00
|
|
|
{
|
2003-01-30 00:26:44 +00:00
|
|
|
ftruncate( unix_fd, size );
|
2002-01-29 02:51:12 +00:00
|
|
|
return 1;
|
1998-12-30 12:06:45 +00:00
|
|
|
}
|
2002-01-29 02:51:12 +00:00
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-02-28 10:13:59 +00:00
|
|
|
/* try to grow the file to the specified size */
|
2005-04-19 11:59:13 +00:00
|
|
|
int grow_file( struct file *file, file_pos_t size )
|
1999-02-28 10:13:59 +00:00
|
|
|
{
|
2001-05-29 20:55:21 +00:00
|
|
|
struct stat st;
|
2003-02-14 20:27:09 +00:00
|
|
|
int unix_fd = get_file_unix_fd( file );
|
1999-02-28 10:13:59 +00:00
|
|
|
|
2005-08-08 15:11:03 +00:00
|
|
|
if (unix_fd == -1) return 0;
|
|
|
|
|
2003-01-30 00:26:44 +00:00
|
|
|
if (fstat( unix_fd, &st ) == -1)
|
1999-02-28 10:13:59 +00:00
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
2001-05-14 20:09:37 +00:00
|
|
|
if (st.st_size >= size) return 1; /* already large enough */
|
2004-05-01 02:50:06 +00:00
|
|
|
return extend_file( file, size );
|
1999-02-28 10:13:59 +00:00
|
|
|
}
|
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
/* create a file */
|
|
|
|
DECL_HANDLER(create_file)
|
|
|
|
{
|
2004-03-18 04:08:48 +00:00
|
|
|
struct object *file;
|
1999-05-15 10:48:19 +00:00
|
|
|
|
2001-11-30 18:46:42 +00:00
|
|
|
reply->handle = 0;
|
|
|
|
if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
|
2004-04-06 23:41:01 +00:00
|
|
|
req->sharing, req->create, req->options, req->attrs )))
|
1999-05-15 10:48:19 +00:00
|
|
|
{
|
2005-12-09 12:58:25 +00:00
|
|
|
reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
|
1999-06-26 08:43:26 +00:00
|
|
|
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;
|
2001-02-28 21:45:23 +00:00
|
|
|
int fd;
|
1999-06-26 08:43:26 +00:00
|
|
|
|
2001-11-30 18:46:42 +00:00
|
|
|
reply->handle = 0;
|
2001-02-28 21:45:23 +00:00
|
|
|
if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
|
1999-06-22 17:26:53 +00:00
|
|
|
{
|
2001-02-28 21:45:23 +00:00
|
|
|
set_error( STATUS_INVALID_HANDLE );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-12 01:56:49 +00:00
|
|
|
if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
|
2001-02-28 21:45:23 +00:00
|
|
|
{
|
2005-12-09 12:58:25 +00:00
|
|
|
reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
|
2001-02-28 21:45:23 +00:00
|
|
|
release_object( file );
|
1999-05-15 10:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lock a region of a file */
|
|
|
|
DECL_HANDLER(lock_file)
|
|
|
|
{
|
|
|
|
struct file *file;
|
2003-03-18 05:04:33 +00:00
|
|
|
file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
|
|
|
|
file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
|
1999-05-15 10:48:19 +00:00
|
|
|
|
|
|
|
if ((file = get_file_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
2003-03-18 05:04:33 +00:00
|
|
|
reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
|
2004-03-12 01:56:49 +00:00
|
|
|
reply->overlapped = is_overlapped( file );
|
1999-05-15 10:48:19 +00:00
|
|
|
release_object( file );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unlock a region of a file */
|
|
|
|
DECL_HANDLER(unlock_file)
|
|
|
|
{
|
|
|
|
struct file *file;
|
2003-03-18 05:04:33 +00:00
|
|
|
file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
|
|
|
|
file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
|
1999-05-15 10:48:19 +00:00
|
|
|
|
|
|
|
if ((file = get_file_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
2003-03-18 05:04:33 +00:00
|
|
|
unlock_fd( file->fd, offset, count );
|
1999-05-15 10:48:19 +00:00
|
|
|
release_object( file );
|
|
|
|
}
|
|
|
|
}
|