blob: 73363dd13bf502e937b2b6b75a4d2d16ad8213e7 [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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
Alexandre Julliard338e7571998-12-27 15:28:54 +000042
43#include "winerror.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000044#include "windef.h"
Michael Vekslerf935c591999-02-09 15:49:39 +000045#include "winbase.h"
Alexandre Julliard014099c2004-03-12 01:56:49 +000046#include "winreg.h"
47#include "winternl.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000048
Alexandre Julliard863637b2003-01-30 00:26:44 +000049#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000050#include "handle.h"
51#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000052#include "request.h"
Martin Wilck718b1b72002-01-07 21:02:15 +000053#include "async.h"
Alexandre Julliard338e7571998-12-27 15:28:54 +000054
55struct file
56{
Alexandre Julliard57e11311999-05-16 16:59:38 +000057 struct object obj; /* object header */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000058 struct fd *fd; /* file descriptor for this file */
Alexandre Julliard57e11311999-05-16 16:59:38 +000059 unsigned int access; /* file access (GENERIC_READ/WRITE) */
Alexandre Julliard014099c2004-03-12 01:56:49 +000060 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
Martin Wilck718b1b72002-01-07 21:02:15 +000061 struct async_queue read_q;
62 struct async_queue write_q;
Alexandre Julliard338e7571998-12-27 15:28:54 +000063};
64
65static void file_dump( struct object *obj, int verbose );
Alexandre Julliarde66207e2003-02-19 00:33:32 +000066static struct fd *file_get_fd( struct object *obj );
Alexandre Julliard338e7571998-12-27 15:28:54 +000067static void file_destroy( struct object *obj );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000068
69static int file_get_poll_events( struct fd *fd );
70static void file_poll_event( struct fd *fd, int event );
Mike McCormackef8b9462003-05-15 04:22:45 +000071static int file_flush( struct fd *fd, struct event **event );
Alexandre Julliard18c08d32004-04-08 19:09:04 +000072static int file_get_info( struct fd *fd, int *flags );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000073static void file_queue_async( struct fd *fd, void *ptr, unsigned int status, int type, int count );
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 */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000083 file_get_fd, /* get_fd */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000084 file_destroy /* destroy */
Alexandre Julliard338e7571998-12-27 15:28:54 +000085};
86
Alexandre Julliard863637b2003-01-30 00:26:44 +000087static const struct fd_ops file_fd_ops =
88{
89 file_get_poll_events, /* get_poll_events */
90 file_poll_event, /* poll_event */
91 file_flush, /* flush */
92 file_get_info, /* get_file_info */
93 file_queue_async /* queue_async */
94};
Alexandre Julliard05625391999-01-03 11:55:56 +000095
Alexandre Julliard014099c2004-03-12 01:56:49 +000096static inline int is_overlapped( const struct file *file )
97{
98 return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
99}
100
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000101/* create a file from a file descriptor */
102/* if the function fails the fd is closed */
Alexandre Julliard014099c2004-03-12 01:56:49 +0000103static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
Alexandre Julliard05625391999-01-03 11:55:56 +0000104{
105 struct file *file;
Alexandre Julliard863637b2003-01-30 00:26:44 +0000106
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000107 if ((file = alloc_object( &file_ops )))
Alexandre Julliard05625391999-01-03 11:55:56 +0000108 {
Ove Kaaven708a8462001-10-24 00:23:25 +0000109 file->access = access;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000110 file->options = FILE_SYNCHRONOUS_IO_NONALERT;
Alexandre Julliard580da242003-03-12 22:38:14 +0000111 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000112 {
113 release_object( file );
114 return NULL;
115 }
Alexandre Julliard05625391999-01-03 11:55:56 +0000116 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000117 return file;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000118}
119
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000120
Alexandre Julliardadc06102004-03-18 04:08:48 +0000121static struct object *create_file( const char *nameptr, size_t len, unsigned int access,
122 unsigned int sharing, int create, unsigned int options,
Alexandre Julliard49b2f6d2004-04-06 23:41:01 +0000123 unsigned int attrs )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000124{
125 struct file *file;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000126 int flags;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000127 char *name;
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000128 mode_t mode;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000129
130 if (!(name = mem_alloc( len + 1 ))) return NULL;
131 memcpy( name, nameptr, len );
132 name[len] = 0;
133
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000134 switch(create)
135 {
Alexandre Julliard014099c2004-03-12 01:56:49 +0000136 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
137 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
138 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
139 case FILE_OPEN: flags = 0; break;
140 case FILE_OPEN_IF: flags = O_CREAT; break;
141 case FILE_OVERWRITE: flags = O_TRUNC; break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000142 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000143 }
144 switch(access & (GENERIC_READ | GENERIC_WRITE))
145 {
146 case 0: break;
147 case GENERIC_READ: flags |= O_RDONLY; break;
148 case GENERIC_WRITE: flags |= O_WRONLY; break;
149 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
150 }
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000151 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
152
153 if (len >= 4 &&
154 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
155 mode |= 0111;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000156
Alexandre Julliard580da242003-03-12 22:38:14 +0000157 if (!(file = alloc_object( &file_ops ))) goto error;
158
159 file->access = access;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000160 file->options = options;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000161 if (is_overlapped( file ))
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000162 {
Alexandre Julliard580da242003-03-12 22:38:14 +0000163 init_async_queue (&file->read_q);
164 init_async_queue (&file->write_q);
Marcus Meissner3f1ed522001-05-14 20:09:37 +0000165 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000166
Alexandre Julliard580da242003-03-12 22:38:14 +0000167 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
168 if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) ||
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000169 !(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE,
Alexandre Julliard684b65c2004-04-16 04:31:35 +0000170 &mode, access, sharing, options )))
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000171 {
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000172 free( name );
Alexandre Julliard580da242003-03-12 22:38:14 +0000173 release_object( file );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000174 return NULL;
175 }
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000176 free( name );
177
Alexandre Julliardadc06102004-03-18 04:08:48 +0000178 /* check for serial port */
179 if (S_ISCHR(mode) && is_serial_fd( file->fd ))
180 {
181 struct object *obj = create_serial( file->fd, file->options );
182 release_object( file );
183 return obj;
184 }
185
186 return &file->obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000187
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000188 error:
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000189 free( name );
190 return NULL;
191}
192
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000193/* check if two file objects point to the same file */
194int is_same_file( struct file *file1, struct file *file2 )
195{
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000196 return is_same_file_fd( file1->fd, file2->fd );
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000197}
198
Alexandre Julliard580da242003-03-12 22:38:14 +0000199/* create a temp file for anonymous mappings */
200struct file *create_temp_file( int access )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000201{
Alexandre Julliard580da242003-03-12 22:38:14 +0000202 char tmpfn[16];
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000203 int fd;
204
Alexandre Julliard580da242003-03-12 22:38:14 +0000205 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
Alexandre Julliard2ee8b5b2003-03-20 21:07:49 +0000206 fd = mkstemps( tmpfn, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000207 if (fd == -1)
208 {
209 file_set_error();
Alexandre Julliard580da242003-03-12 22:38:14 +0000210 return NULL;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000211 }
Marcus Meissner786d2492002-07-29 23:55:39 +0000212 unlink( tmpfn );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000213 return create_file_for_fd( fd, access, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000214}
215
Alexandre Julliard338e7571998-12-27 15:28:54 +0000216static void file_dump( struct object *obj, int verbose )
217{
218 struct file *file = (struct file *)obj;
219 assert( obj->ops == &file_ops );
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000220 fprintf( stderr, "File fd=%p options=%08x\n", file->fd, file->options );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000221}
222
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000223static int file_get_poll_events( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000224{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000225 struct file *file = get_fd_user( fd );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000226 int events = 0;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000227 assert( file->obj.ops == &file_ops );
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000228 if (file->access & GENERIC_READ) events |= POLLIN;
229 if (file->access & GENERIC_WRITE) events |= POLLOUT;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000230 return events;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000231}
232
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000233static void file_poll_event( struct fd *fd, int event )
Martin Wilck718b1b72002-01-07 21:02:15 +0000234{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000235 struct file *file = get_fd_user( fd );
236 assert( file->obj.ops == &file_ops );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000237 if (is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000238 {
239 if( IS_READY(file->read_q) && (POLLIN & event) )
240 {
241 async_notify(file->read_q.head, STATUS_ALERTED);
242 return;
243 }
244 if( IS_READY(file->write_q) && (POLLOUT & event) )
245 {
246 async_notify(file->write_q.head, STATUS_ALERTED);
247 return;
248 }
249 }
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000250 default_poll_event( fd, event );
Martin Wilck718b1b72002-01-07 21:02:15 +0000251}
252
253
Mike McCormackef8b9462003-05-15 04:22:45 +0000254static int file_flush( struct fd *fd, struct event **event )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000255{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000256 int ret = (fsync( get_unix_fd(fd) ) != -1);
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000257 if (!ret) file_set_error();
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000258 return ret;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000259}
260
Alexandre Julliard18c08d32004-04-08 19:09:04 +0000261static int file_get_info( struct fd *fd, int *flags )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000262{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000263 struct file *file = get_fd_user( fd );
Alexandre Julliard05625391999-01-03 11:55:56 +0000264
Martin Wilck88cd32b2002-01-09 20:30:51 +0000265 *flags = 0;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000266 if (is_overlapped( file )) *flags |= FD_FLAG_OVERLAPPED;
Mike McCormackff58be52001-10-04 16:18:15 +0000267 return FD_TYPE_DEFAULT;
Alexandre Julliard05625391999-01-03 11:55:56 +0000268}
269
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000270static void file_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
Martin Wilck718b1b72002-01-07 21:02:15 +0000271{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000272 struct file *file = get_fd_user( fd );
Martin Wilck54ba2722002-04-24 21:29:54 +0000273 struct async *async;
Martin Wilck718b1b72002-01-07 21:02:15 +0000274 struct async_queue *q;
275
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000276 assert( file->obj.ops == &file_ops );
Martin Wilck718b1b72002-01-07 21:02:15 +0000277
Alexandre Julliard014099c2004-03-12 01:56:49 +0000278 if (!is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000279 {
280 set_error ( STATUS_INVALID_HANDLE );
Martin Wilck54ba2722002-04-24 21:29:54 +0000281 return;
Martin Wilck718b1b72002-01-07 21:02:15 +0000282 }
283
284 switch(type)
285 {
286 case ASYNC_TYPE_READ:
287 q = &file->read_q;
288 break;
289 case ASYNC_TYPE_WRITE:
290 q = &file->write_q;
291 break;
292 default:
293 set_error( STATUS_INVALID_PARAMETER );
Martin Wilck54ba2722002-04-24 21:29:54 +0000294 return;
Martin Wilck718b1b72002-01-07 21:02:15 +0000295 }
296
Martin Wilck54ba2722002-04-24 21:29:54 +0000297 async = find_async ( q, current, ptr );
Martin Wilck718b1b72002-01-07 21:02:15 +0000298
Martin Wilck54ba2722002-04-24 21:29:54 +0000299 if ( status == STATUS_PENDING )
300 {
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000301 int events;
Martin Wilckff1f3202002-04-26 18:31:19 +0000302
Martin Wilck54ba2722002-04-24 21:29:54 +0000303 if ( !async )
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000304 async = create_async ( &file->obj, current, ptr );
Martin Wilck54ba2722002-04-24 21:29:54 +0000305 if ( !async )
306 return;
307
308 async->status = STATUS_PENDING;
309 if ( !async->q )
310 async_insert( q, async );
Martin Wilckff1f3202002-04-26 18:31:19 +0000311
312 /* Check if the new pending request can be served immediately */
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000313 events = check_fd_events( fd, file_get_poll_events( fd ) );
314 if (events) file_poll_event ( fd, events );
Martin Wilck54ba2722002-04-24 21:29:54 +0000315 }
316 else if ( async ) destroy_async ( async );
317 else set_error ( STATUS_INVALID_PARAMETER );
318
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000319 set_fd_events( fd, file_get_poll_events( fd ));
320}
321
322static struct fd *file_get_fd( struct object *obj )
323{
324 struct file *file = (struct file *)obj;
325 assert( obj->ops == &file_ops );
326 return (struct fd *)grab_object( file->fd );
Martin Wilck718b1b72002-01-07 21:02:15 +0000327}
328
Alexandre Julliard05625391999-01-03 11:55:56 +0000329static void file_destroy( struct object *obj )
330{
Alexandre Julliard05625391999-01-03 11:55:56 +0000331 struct file *file = (struct file *)obj;
332 assert( obj->ops == &file_ops );
333
Alexandre Julliard014099c2004-03-12 01:56:49 +0000334 if (is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000335 {
336 destroy_async_queue (&file->read_q);
337 destroy_async_queue (&file->write_q);
338 }
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000339 if (file->fd) release_object( file->fd );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000340}
341
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000342/* set the last error depending on errno */
343void file_set_error(void)
Alexandre Julliard338e7571998-12-27 15:28:54 +0000344{
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000345 switch (errno)
346 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000347 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
348 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
349 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000350 case EACCES:
Alexandre Julliard5f258c62001-07-14 00:50:30 +0000351 case ESRCH:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000352 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
353 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
354 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
355 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
Alexandre Julliard716878c2004-04-16 23:32:40 +0000356 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000357 case ENFILE:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000358 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
359 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
360 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
Alexandre Julliardd5b42322003-12-10 01:12:18 +0000361 case ESPIPE: set_win32_error( ERROR_SEEK ); break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000362 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
363 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000364 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000365 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000366#ifdef EOVERFLOW
Alexandre Julliardce613492003-03-18 05:04:33 +0000367 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000368#endif
Filip Navara2392a362004-04-12 23:15:11 +0000369 default: perror("file_set_error"); set_error( STATUS_UNSUCCESSFUL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000370 }
Alexandre Julliard338e7571998-12-27 15:28:54 +0000371}
372
Alexandre Julliard51885742002-05-30 20:12:58 +0000373struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000374{
Alexandre Julliard61ec6c11999-11-29 02:17:08 +0000375 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000376}
Alexandre Julliard338e7571998-12-27 15:28:54 +0000377
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000378int get_file_unix_fd( struct file *file )
379{
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000380 return get_unix_fd( file->fd );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000381}
382
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000383/* extend a file beyond the current end of file */
384static int extend_file( struct file *file, off_t size )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000385{
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000386 static const char zero;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000387 int unix_fd = get_file_unix_fd( file );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000388
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000389 /* extend the file one byte beyond the requested size and then truncate it */
390 /* this should work around ftruncate implementations that can't extend files */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000391 if (pwrite( unix_fd, &zero, 1, size ) != -1)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000392 {
Alexandre Julliard863637b2003-01-30 00:26:44 +0000393 ftruncate( unix_fd, size );
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000394 return 1;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000395 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000396 file_set_error();
397 return 0;
398}
399
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000400/* try to grow the file to the specified size */
401int grow_file( struct file *file, int size_high, int size_low )
402{
Marcus Meissner6bb990f2001-05-29 20:55:21 +0000403 struct stat st;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000404 int unix_fd = get_file_unix_fd( file );
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000405 off_t size = size_low + (((off_t)size_high)<<32);
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000406
Alexandre Julliard863637b2003-01-30 00:26:44 +0000407 if (fstat( unix_fd, &st ) == -1)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000408 {
409 file_set_error();
410 return 0;
411 }
Marcus Meissner3f1ed522001-05-14 20:09:37 +0000412 if (st.st_size >= size) return 1; /* already large enough */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000413 return extend_file( file, size );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000414}
415
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000416/* create a file */
417DECL_HANDLER(create_file)
418{
Alexandre Julliardadc06102004-03-18 04:08:48 +0000419 struct object *file;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000420
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000421 reply->handle = 0;
422 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
Alexandre Julliard49b2f6d2004-04-06 23:41:01 +0000423 req->sharing, req->create, req->options, req->attrs )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000424 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000425 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000426 release_object( file );
427 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000428}
429
430/* allocate a file handle for a Unix fd */
431DECL_HANDLER(alloc_file_handle)
432{
433 struct file *file;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000434 int fd;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000435
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000436 reply->handle = 0;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000437 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000438 {
Alexandre Julliardf5242402001-02-28 21:45:23 +0000439 set_error( STATUS_INVALID_HANDLE );
440 return;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000441 }
Alexandre Julliard014099c2004-03-12 01:56:49 +0000442 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
Alexandre Julliardf5242402001-02-28 21:45:23 +0000443 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000444 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000445 release_object( file );
446 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000447}
448
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000449/* lock a region of a file */
450DECL_HANDLER(lock_file)
451{
452 struct file *file;
Alexandre Julliardce613492003-03-18 05:04:33 +0000453 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
454 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000455
456 if ((file = get_file_obj( current->process, req->handle, 0 )))
457 {
Alexandre Julliardce613492003-03-18 05:04:33 +0000458 reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000459 reply->overlapped = is_overlapped( file );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000460 release_object( file );
461 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000462}
463
464/* unlock a region of a file */
465DECL_HANDLER(unlock_file)
466{
467 struct file *file;
Alexandre Julliardce613492003-03-18 05:04:33 +0000468 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
469 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000470
471 if ((file = get_file_obj( current->process, req->handle, 0 )))
472 {
Alexandre Julliardce613492003-03-18 05:04:33 +0000473 unlock_fd( file->fd, offset, count );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000474 release_object( file );
475 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000476}