blob: 7ddcd3553c212450d9003a2605ed3d7dbfce0f8b [file] [log] [blame]
Alexandre Julliard338e7571998-12-27 15:28:54 +00001/*
2 * Server-side file management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard338e7571998-12-27 15:28:54 +000019 */
20
Patrik Stridvall96336321999-10-24 22:13:47 +000021#include "config.h"
Marcus Meissner3f1ed522001-05-14 20:09:37 +000022#include "wine/port.h"
Patrik Stridvall96336321999-10-24 22:13:47 +000023
Alexandre Julliard338e7571998-12-27 15:28:54 +000024#include <assert.h>
25#include <fcntl.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000026#include <stdarg.h>
Alexandre Julliard338e7571998-12-27 15:28:54 +000027#include <stdio.h>
David Luyeree517e81999-02-28 12:27:56 +000028#include <string.h>
Alexandre Julliard338e7571998-12-27 15:28:54 +000029#include <stdlib.h>
Marcus Meissner8ba68fb1999-01-01 18:42:17 +000030#include <errno.h>
Howard Abrams13277481999-07-10 13:16:29 +000031#ifdef HAVE_SYS_ERRNO_H
Alexandre Julliard338e7571998-12-27 15:28:54 +000032#include <sys/errno.h>
Howard Abrams13277481999-07-10 13:16:29 +000033#endif
Alexandre Julliard338e7571998-12-27 15:28:54 +000034#include <sys/stat.h>
35#include <sys/time.h>
36#include <sys/types.h>
37#include <time.h>
38#include <unistd.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000039#ifdef HAVE_UTIME_H
Alexandre Julliard05625391999-01-03 11:55:56 +000040#include <utime.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000041#endif
Steven Edwards57279182005-03-04 12:38:36 +000042#ifdef HAVE_POLL_H
43#include <poll.h>
44#endif
Alexandre Julliard338e7571998-12-27 15:28:54 +000045
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010046#include "ntstatus.h"
47#define WIN32_NO_STATUS
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000048#include "windef.h"
Alexandre Julliard014099c2004-03-12 01:56:49 +000049#include "winternl.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000050
Alexandre Julliard863637b2003-01-30 00:26:44 +000051#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000052#include "handle.h"
53#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000054#include "request.h"
Alexandre Julliard338e7571998-12-27 15:28:54 +000055
56struct file
57{
Alexandre Julliard57e11311999-05-16 16:59:38 +000058 struct object obj; /* object header */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000059 struct fd *fd; /* file descriptor for this file */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +010060 unsigned int access; /* file access (FILE_READ_DATA etc.) */
Alexandre Julliard014099c2004-03-12 01:56:49 +000061 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
Alexandre Julliard338e7571998-12-27 15:28:54 +000062};
63
Mike McCormack9a7124e2006-01-24 13:30:55 +010064static unsigned int generic_file_map_access( unsigned int access );
65
Alexandre Julliard338e7571998-12-27 15:28:54 +000066static void file_dump( struct object *obj, int verbose );
Alexandre Julliarde66207e2003-02-19 00:33:32 +000067static struct fd *file_get_fd( struct object *obj );
Alexandre Julliarda510a7e2005-12-12 16:46:17 +010068static unsigned int file_map_access( struct object *obj, unsigned int access );
Alexandre Julliard338e7571998-12-27 15:28:54 +000069static void file_destroy( struct object *obj );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000070
71static int file_get_poll_events( struct fd *fd );
Mike McCormackef8b9462003-05-15 04:22:45 +000072static int file_flush( struct fd *fd, struct event **event );
Alexandre Julliard6a27b482004-08-18 00:04:58 +000073static int file_get_info( struct fd *fd );
Alexandre Julliard338e7571998-12-27 15:28:54 +000074
75static const struct object_ops file_ops =
76{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000077 sizeof(struct file), /* size */
78 file_dump, /* dump */
Alexandre Julliard863637b2003-01-30 00:26:44 +000079 default_fd_add_queue, /* add_queue */
80 default_fd_remove_queue, /* remove_queue */
81 default_fd_signaled, /* signaled */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000082 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000083 no_signal, /* signal */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000084 file_get_fd, /* get_fd */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +010085 file_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000086 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000087 no_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000088 file_destroy /* destroy */
Alexandre Julliard338e7571998-12-27 15:28:54 +000089};
90
Alexandre Julliard863637b2003-01-30 00:26:44 +000091static const struct fd_ops file_fd_ops =
92{
93 file_get_poll_events, /* get_poll_events */
Robert Shearmand6a4e342005-06-07 20:09:01 +000094 default_poll_event, /* poll_event */
Alexandre Julliard863637b2003-01-30 00:26:44 +000095 file_flush, /* flush */
96 file_get_info, /* get_file_info */
Robert Shearmand6a4e342005-06-07 20:09:01 +000097 default_fd_queue_async, /* queue_async */
98 default_fd_cancel_async /* cancel_async */
Alexandre Julliard863637b2003-01-30 00:26:44 +000099};
Alexandre Julliard05625391999-01-03 11:55:56 +0000100
Alexandre Julliard014099c2004-03-12 01:56:49 +0000101static inline int is_overlapped( const struct file *file )
102{
103 return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
104}
105
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000106/* create a file from a file descriptor */
107/* if the function fails the fd is closed */
Alexandre Julliard014099c2004-03-12 01:56:49 +0000108static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
Alexandre Julliard05625391999-01-03 11:55:56 +0000109{
110 struct file *file;
Alexandre Julliard863637b2003-01-30 00:26:44 +0000111
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000112 if ((file = alloc_object( &file_ops )))
Alexandre Julliard05625391999-01-03 11:55:56 +0000113 {
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100114 file->access = file_map_access( &file->obj, access );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000115 file->options = FILE_SYNCHRONOUS_IO_NONALERT;
Alexandre Julliard580da242003-03-12 22:38:14 +0000116 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000117 {
118 release_object( file );
119 return NULL;
120 }
Alexandre Julliard05625391999-01-03 11:55:56 +0000121 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000122 return file;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000123}
124
Mike McCormack9a7124e2006-01-24 13:30:55 +0100125static struct object *create_file_obj( struct fd *fd, unsigned int access, unsigned int options )
126{
127 struct file *file = alloc_object( &file_ops );
128
129 if (!file) return NULL;
130 file->access = access;
131 file->options = options;
132 file->fd = fd;
133 grab_object( fd );
134 set_fd_user( fd, &file_fd_ops, &file->obj );
135 return &file->obj;
136}
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000137
Alexandre Julliardadc06102004-03-18 04:08:48 +0000138static struct object *create_file( const char *nameptr, size_t len, unsigned int access,
139 unsigned int sharing, int create, unsigned int options,
Alexandre Julliard49b2f6d2004-04-06 23:41:01 +0000140 unsigned int attrs )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000141{
Mike McCormack9a7124e2006-01-24 13:30:55 +0100142 struct object *obj = NULL;
143 struct fd *fd;
Alexandre Julliard471782a2006-01-25 15:06:48 +0100144 int flags;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000145 char *name;
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000146 mode_t mode;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000147
148 if (!(name = mem_alloc( len + 1 ))) return NULL;
149 memcpy( name, nameptr, len );
150 name[len] = 0;
151
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000152 switch(create)
153 {
Alexandre Julliard014099c2004-03-12 01:56:49 +0000154 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
155 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
156 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
157 case FILE_OPEN: flags = 0; break;
158 case FILE_OPEN_IF: flags = O_CREAT; break;
159 case FILE_OVERWRITE: flags = O_TRUNC; break;
Mike McCormack9a7124e2006-01-24 13:30:55 +0100160 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000161 }
Eric Pouech46344472005-01-14 19:54:38 +0000162
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000163 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
164
165 if (len >= 4 &&
166 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
167 mode |= 0111;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000168
Mike McCormack9a7124e2006-01-24 13:30:55 +0100169 access = generic_file_map_access( access );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000170
Alexandre Julliard580da242003-03-12 22:38:14 +0000171 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
Mike McCormack9a7124e2006-01-24 13:30:55 +0100172 fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
173 if (!fd) goto done;
174
Mike McCormack08351072006-01-27 12:13:56 +0100175 if (S_ISDIR(mode))
176 obj = create_dir_obj( fd );
177 else if (S_ISCHR(mode) && is_serial_fd( fd ))
Mike McCormack9a7124e2006-01-24 13:30:55 +0100178 obj = create_serial( fd, options );
179 else
180 obj = create_file_obj( fd, access, options );
181
182 release_object( fd );
183
184done:
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000185 free( name );
Mike McCormack9a7124e2006-01-24 13:30:55 +0100186 return obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000187}
188
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000189/* check if two file objects point to the same file */
190int is_same_file( struct file *file1, struct file *file2 )
191{
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000192 return is_same_file_fd( file1->fd, file2->fd );
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000193}
194
Alexandre Julliard580da242003-03-12 22:38:14 +0000195/* create a temp file for anonymous mappings */
196struct file *create_temp_file( int access )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000197{
Alexandre Julliard580da242003-03-12 22:38:14 +0000198 char tmpfn[16];
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000199 int fd;
200
Alexandre Julliard580da242003-03-12 22:38:14 +0000201 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
Alexandre Julliard2ee8b5b2003-03-20 21:07:49 +0000202 fd = mkstemps( tmpfn, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000203 if (fd == -1)
204 {
205 file_set_error();
Alexandre Julliard580da242003-03-12 22:38:14 +0000206 return NULL;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000207 }
Marcus Meissner786d2492002-07-29 23:55:39 +0000208 unlink( tmpfn );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000209 return create_file_for_fd( fd, access, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000210}
211
Alexandre Julliard338e7571998-12-27 15:28:54 +0000212static void file_dump( struct object *obj, int verbose )
213{
214 struct file *file = (struct file *)obj;
215 assert( obj->ops == &file_ops );
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000216 fprintf( stderr, "File fd=%p options=%08x\n", file->fd, file->options );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000217}
218
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000219static int file_get_poll_events( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000220{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000221 struct file *file = get_fd_user( fd );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000222 int events = 0;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000223 assert( file->obj.ops == &file_ops );
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100224 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
225 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000226 return events;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000227}
228
Mike McCormackef8b9462003-05-15 04:22:45 +0000229static int file_flush( struct fd *fd, struct event **event )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000230{
Alexandre Julliard964815b2005-08-08 15:11:03 +0000231 int ret = 0, unix_fd = get_unix_fd( fd );
232
233 if (unix_fd != -1)
234 {
235 ret = (fsync( unix_fd ) != -1);
236 if (!ret) file_set_error();
237 }
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000238 return ret;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000239}
240
Alexandre Julliard6a27b482004-08-18 00:04:58 +0000241static int file_get_info( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000242{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000243 struct file *file = get_fd_user( fd );
Alexandre Julliard05625391999-01-03 11:55:56 +0000244
Alexandre Julliard6a27b482004-08-18 00:04:58 +0000245 if (is_overlapped( file )) return FD_FLAG_OVERLAPPED;
246 else return 0;
Alexandre Julliard05625391999-01-03 11:55:56 +0000247}
248
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000249static struct fd *file_get_fd( struct object *obj )
250{
251 struct file *file = (struct file *)obj;
252 assert( obj->ops == &file_ops );
253 return (struct fd *)grab_object( file->fd );
Martin Wilck718b1b72002-01-07 21:02:15 +0000254}
255
Mike McCormack9a7124e2006-01-24 13:30:55 +0100256static unsigned int generic_file_map_access( unsigned int access )
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100257{
258 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
259 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
260 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
261 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
262 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
263}
264
Mike McCormack9a7124e2006-01-24 13:30:55 +0100265static unsigned int file_map_access( struct object *obj, unsigned int access )
266{
267 return generic_file_map_access( access );
268}
269
Alexandre Julliard05625391999-01-03 11:55:56 +0000270static void file_destroy( struct object *obj )
271{
Alexandre Julliard05625391999-01-03 11:55:56 +0000272 struct file *file = (struct file *)obj;
273 assert( obj->ops == &file_ops );
274
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000275 if (file->fd) release_object( file->fd );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000276}
277
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000278/* set the last error depending on errno */
279void file_set_error(void)
Alexandre Julliard338e7571998-12-27 15:28:54 +0000280{
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000281 switch (errno)
282 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000283 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
284 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
285 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000286 case EACCES:
Alexandre Julliard5f258c62001-07-14 00:50:30 +0000287 case ESRCH:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000288 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
289 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
290 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
291 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
Alexandre Julliard716878c2004-04-16 23:32:40 +0000292 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000293 case ENFILE:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000294 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
295 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
296 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
Alexandre Julliardd5b42322003-12-10 01:12:18 +0000297 case ESPIPE: set_win32_error( ERROR_SEEK ); break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000298 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
299 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000300 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000301 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
Mike McCormackea782b62004-07-06 19:42:09 +0000302 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
303 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000304#ifdef EOVERFLOW
Alexandre Julliardce613492003-03-18 05:04:33 +0000305 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000306#endif
Filip Navara2392a362004-04-12 23:15:11 +0000307 default: perror("file_set_error"); set_error( STATUS_UNSUCCESSFUL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000308 }
Alexandre Julliard338e7571998-12-27 15:28:54 +0000309}
310
Alexandre Julliard51885742002-05-30 20:12:58 +0000311struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000312{
Alexandre Julliard61ec6c11999-11-29 02:17:08 +0000313 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000314}
Alexandre Julliard338e7571998-12-27 15:28:54 +0000315
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000316int get_file_unix_fd( struct file *file )
317{
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000318 return get_unix_fd( file->fd );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000319}
320
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000321/* extend a file beyond the current end of file */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000322static int extend_file( struct file *file, file_pos_t new_size )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000323{
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000324 static const char zero;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000325 int unix_fd = get_file_unix_fd( file );
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000326 off_t size = new_size;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000327
Alexandre Julliard964815b2005-08-08 15:11:03 +0000328 if (unix_fd == -1) return 0;
329
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000330 if (sizeof(new_size) > sizeof(size) && size != new_size)
331 {
332 set_error( STATUS_INVALID_PARAMETER );
333 return 0;
334 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000335 /* extend the file one byte beyond the requested size and then truncate it */
336 /* this should work around ftruncate implementations that can't extend files */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000337 if (pwrite( unix_fd, &zero, 1, size ) != -1)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000338 {
Alexandre Julliard863637b2003-01-30 00:26:44 +0000339 ftruncate( unix_fd, size );
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000340 return 1;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000341 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000342 file_set_error();
343 return 0;
344}
345
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000346/* try to grow the file to the specified size */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000347int grow_file( struct file *file, file_pos_t size )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000348{
Marcus Meissner6bb990f2001-05-29 20:55:21 +0000349 struct stat st;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000350 int unix_fd = get_file_unix_fd( file );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000351
Alexandre Julliard964815b2005-08-08 15:11:03 +0000352 if (unix_fd == -1) return 0;
353
Alexandre Julliard863637b2003-01-30 00:26:44 +0000354 if (fstat( unix_fd, &st ) == -1)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000355 {
356 file_set_error();
357 return 0;
358 }
Marcus Meissner3f1ed522001-05-14 20:09:37 +0000359 if (st.st_size >= size) return 1; /* already large enough */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000360 return extend_file( file, size );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000361}
362
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000363/* create a file */
364DECL_HANDLER(create_file)
365{
Alexandre Julliardadc06102004-03-18 04:08:48 +0000366 struct object *file;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000367
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000368 reply->handle = 0;
369 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
Alexandre Julliard49b2f6d2004-04-06 23:41:01 +0000370 req->sharing, req->create, req->options, req->attrs )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000371 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100372 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000373 release_object( file );
374 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000375}
376
377/* allocate a file handle for a Unix fd */
378DECL_HANDLER(alloc_file_handle)
379{
380 struct file *file;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000381 int fd;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000382
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000383 reply->handle = 0;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000384 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000385 {
Alexandre Julliardf5242402001-02-28 21:45:23 +0000386 set_error( STATUS_INVALID_HANDLE );
387 return;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000388 }
Alexandre Julliard014099c2004-03-12 01:56:49 +0000389 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
Alexandre Julliardf5242402001-02-28 21:45:23 +0000390 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100391 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000392 release_object( file );
393 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000394}
395
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000396/* lock a region of a file */
397DECL_HANDLER(lock_file)
398{
399 struct file *file;
Alexandre Julliardce613492003-03-18 05:04:33 +0000400 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
401 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000402
403 if ((file = get_file_obj( current->process, req->handle, 0 )))
404 {
Alexandre Julliardce613492003-03-18 05:04:33 +0000405 reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000406 reply->overlapped = is_overlapped( file );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000407 release_object( file );
408 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000409}
410
411/* unlock a region of a file */
412DECL_HANDLER(unlock_file)
413{
414 struct file *file;
Alexandre Julliardce613492003-03-18 05:04:33 +0000415 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
416 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000417
418 if ((file = get_file_obj( current->process, req->handle, 0 )))
419 {
Alexandre Julliardce613492003-03-18 05:04:33 +0000420 unlock_fd( file->fd, offset, count );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000421 release_object( file );
422 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000423}