blob: d28dc0d42d08a9da50cc445fc033e6e27e04f812 [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
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
46#include "winerror.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000047#include "windef.h"
Michael Vekslerf935c591999-02-09 15:49:39 +000048#include "winbase.h"
Alexandre Julliard014099c2004-03-12 01:56:49 +000049#include "winreg.h"
50#include "winternl.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000051
Alexandre Julliard863637b2003-01-30 00:26:44 +000052#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000053#include "handle.h"
54#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000055#include "request.h"
Alexandre Julliard338e7571998-12-27 15:28:54 +000056
57struct file
58{
Alexandre Julliard57e11311999-05-16 16:59:38 +000059 struct object obj; /* object header */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000060 struct fd *fd; /* file descriptor for this file */
Alexandre Julliard57e11311999-05-16 16:59:38 +000061 unsigned int access; /* file access (GENERIC_READ/WRITE) */
Alexandre Julliard014099c2004-03-12 01:56:49 +000062 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
Alexandre Julliarddd81ac52005-02-24 17:06:31 +000063 struct list read_q;
64 struct list write_q;
Alexandre Julliard338e7571998-12-27 15:28:54 +000065};
66
67static void file_dump( struct object *obj, int verbose );
Alexandre Julliarde66207e2003-02-19 00:33:32 +000068static struct fd *file_get_fd( struct object *obj );
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 );
72static void file_poll_event( struct fd *fd, int event );
Mike McCormackef8b9462003-05-15 04:22:45 +000073static int file_flush( struct fd *fd, struct event **event );
Alexandre Julliard6a27b482004-08-18 00:04:58 +000074static int file_get_info( struct fd *fd );
Eric Pouech46344472005-01-14 19:54:38 +000075static void file_queue_async( struct fd *fd, void *apc, void *user, void* iosb, int type, int count );
76static void file_cancel_async( struct fd *fd );
Alexandre Julliard338e7571998-12-27 15:28:54 +000077
78static const struct object_ops file_ops =
79{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000080 sizeof(struct file), /* size */
81 file_dump, /* dump */
Alexandre Julliard863637b2003-01-30 00:26:44 +000082 default_fd_add_queue, /* add_queue */
83 default_fd_remove_queue, /* remove_queue */
84 default_fd_signaled, /* signaled */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000085 no_satisfied, /* satisfied */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000086 file_get_fd, /* get_fd */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000087 file_destroy /* destroy */
Alexandre Julliard338e7571998-12-27 15:28:54 +000088};
89
Alexandre Julliard863637b2003-01-30 00:26:44 +000090static const struct fd_ops file_fd_ops =
91{
92 file_get_poll_events, /* get_poll_events */
93 file_poll_event, /* poll_event */
94 file_flush, /* flush */
95 file_get_info, /* get_file_info */
Eric Pouech46344472005-01-14 19:54:38 +000096 file_queue_async, /* queue_async */
97 file_cancel_async /* cancel_async */
Alexandre Julliard863637b2003-01-30 00:26:44 +000098};
Alexandre Julliard05625391999-01-03 11:55:56 +000099
Alexandre Julliard014099c2004-03-12 01:56:49 +0000100static inline int is_overlapped( const struct file *file )
101{
102 return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
103}
104
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000105/* create a file from a file descriptor */
106/* if the function fails the fd is closed */
Alexandre Julliard014099c2004-03-12 01:56:49 +0000107static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
Alexandre Julliard05625391999-01-03 11:55:56 +0000108{
109 struct file *file;
Alexandre Julliard863637b2003-01-30 00:26:44 +0000110
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000111 if ((file = alloc_object( &file_ops )))
Alexandre Julliard05625391999-01-03 11:55:56 +0000112 {
Ove Kaaven708a8462001-10-24 00:23:25 +0000113 file->access = access;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000114 file->options = FILE_SYNCHRONOUS_IO_NONALERT;
Alexandre Julliard580da242003-03-12 22:38:14 +0000115 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000116 {
117 release_object( file );
118 return NULL;
119 }
Alexandre Julliard05625391999-01-03 11:55:56 +0000120 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000121 return file;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000122}
123
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000124
Alexandre Julliardadc06102004-03-18 04:08:48 +0000125static struct object *create_file( const char *nameptr, size_t len, unsigned int access,
126 unsigned int sharing, int create, unsigned int options,
Alexandre Julliard49b2f6d2004-04-06 23:41:01 +0000127 unsigned int attrs )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000128{
129 struct file *file;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000130 int flags;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000131 char *name;
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000132 mode_t mode;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000133
134 if (!(name = mem_alloc( len + 1 ))) return NULL;
135 memcpy( name, nameptr, len );
136 name[len] = 0;
137
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000138 switch(create)
139 {
Alexandre Julliard014099c2004-03-12 01:56:49 +0000140 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
141 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
142 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
143 case FILE_OPEN: flags = 0; break;
144 case FILE_OPEN_IF: flags = O_CREAT; break;
145 case FILE_OVERWRITE: flags = O_TRUNC; break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000146 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000147 }
Eric Pouech46344472005-01-14 19:54:38 +0000148
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000149 switch(access & (GENERIC_READ | GENERIC_WRITE))
150 {
151 case 0: break;
152 case GENERIC_READ: flags |= O_RDONLY; break;
153 case GENERIC_WRITE: flags |= O_WRONLY; break;
154 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
155 }
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000156 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
157
158 if (len >= 4 &&
159 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
160 mode |= 0111;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000161
Alexandre Julliard580da242003-03-12 22:38:14 +0000162 if (!(file = alloc_object( &file_ops ))) goto error;
163
164 file->access = access;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000165 file->options = options;
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000166 list_init( &file->read_q );
167 list_init( &file->write_q );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000168
Alexandre Julliard580da242003-03-12 22:38:14 +0000169 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
170 if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) ||
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000171 !(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE,
Alexandre Julliard684b65c2004-04-16 04:31:35 +0000172 &mode, access, sharing, options )))
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000173 {
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000174 free( name );
Alexandre Julliard580da242003-03-12 22:38:14 +0000175 release_object( file );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000176 return NULL;
177 }
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000178 free( name );
179
Alexandre Julliardadc06102004-03-18 04:08:48 +0000180 /* check for serial port */
181 if (S_ISCHR(mode) && is_serial_fd( file->fd ))
182 {
183 struct object *obj = create_serial( file->fd, file->options );
184 release_object( file );
185 return obj;
186 }
187
188 return &file->obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000189
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000190 error:
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000191 free( name );
192 return NULL;
193}
194
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000195/* check if two file objects point to the same file */
196int is_same_file( struct file *file1, struct file *file2 )
197{
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000198 return is_same_file_fd( file1->fd, file2->fd );
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000199}
200
Alexandre Julliard580da242003-03-12 22:38:14 +0000201/* create a temp file for anonymous mappings */
202struct file *create_temp_file( int access )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000203{
Alexandre Julliard580da242003-03-12 22:38:14 +0000204 char tmpfn[16];
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000205 int fd;
206
Alexandre Julliard580da242003-03-12 22:38:14 +0000207 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
Alexandre Julliard2ee8b5b2003-03-20 21:07:49 +0000208 fd = mkstemps( tmpfn, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000209 if (fd == -1)
210 {
211 file_set_error();
Alexandre Julliard580da242003-03-12 22:38:14 +0000212 return NULL;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000213 }
Marcus Meissner786d2492002-07-29 23:55:39 +0000214 unlink( tmpfn );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000215 return create_file_for_fd( fd, access, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000216}
217
Alexandre Julliard338e7571998-12-27 15:28:54 +0000218static void file_dump( struct object *obj, int verbose )
219{
220 struct file *file = (struct file *)obj;
221 assert( obj->ops == &file_ops );
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000222 fprintf( stderr, "File fd=%p options=%08x\n", file->fd, file->options );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000223}
224
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000225static int file_get_poll_events( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000226{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000227 struct file *file = get_fd_user( fd );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000228 int events = 0;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000229 assert( file->obj.ops == &file_ops );
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000230 if (file->access & GENERIC_READ) events |= POLLIN;
231 if (file->access & GENERIC_WRITE) events |= POLLOUT;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000232 return events;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000233}
234
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000235static void file_poll_event( struct fd *fd, int event )
Martin Wilck718b1b72002-01-07 21:02:15 +0000236{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000237 struct file *file = get_fd_user( fd );
238 assert( file->obj.ops == &file_ops );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000239 if (is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000240 {
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000241 if (!list_empty( &file->read_q ) && (POLLIN & event) )
Martin Wilck718b1b72002-01-07 21:02:15 +0000242 {
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000243 async_terminate_head( &file->read_q, STATUS_ALERTED );
Martin Wilck718b1b72002-01-07 21:02:15 +0000244 return;
245 }
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000246 if (!list_empty( &file->write_q ) && (POLLOUT & event) )
Martin Wilck718b1b72002-01-07 21:02:15 +0000247 {
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000248 async_terminate_head( &file->write_q, STATUS_ALERTED );
Martin Wilck718b1b72002-01-07 21:02:15 +0000249 return;
250 }
251 }
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000252 default_poll_event( fd, event );
Martin Wilck718b1b72002-01-07 21:02:15 +0000253}
254
255
Mike McCormackef8b9462003-05-15 04:22:45 +0000256static int file_flush( struct fd *fd, struct event **event )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000257{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000258 int ret = (fsync( get_unix_fd(fd) ) != -1);
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000259 if (!ret) file_set_error();
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000260 return ret;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000261}
262
Alexandre Julliard6a27b482004-08-18 00:04:58 +0000263static int file_get_info( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000264{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000265 struct file *file = get_fd_user( fd );
Alexandre Julliard05625391999-01-03 11:55:56 +0000266
Alexandre Julliard6a27b482004-08-18 00:04:58 +0000267 if (is_overlapped( file )) return FD_FLAG_OVERLAPPED;
268 else return 0;
Alexandre Julliard05625391999-01-03 11:55:56 +0000269}
270
Eric Pouech46344472005-01-14 19:54:38 +0000271static void file_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
272 int type, int count )
Martin Wilck718b1b72002-01-07 21:02:15 +0000273{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000274 struct file *file = get_fd_user( fd );
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000275 struct list *queue;
Eric Pouech46344472005-01-14 19:54:38 +0000276 int events;
Martin Wilck718b1b72002-01-07 21:02:15 +0000277
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000278 assert( file->obj.ops == &file_ops );
Martin Wilck718b1b72002-01-07 21:02:15 +0000279
Alexandre Julliard014099c2004-03-12 01:56:49 +0000280 if (!is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000281 {
Eric Pouech46344472005-01-14 19:54:38 +0000282 set_error( STATUS_INVALID_HANDLE );
Martin Wilck54ba2722002-04-24 21:29:54 +0000283 return;
Martin Wilck718b1b72002-01-07 21:02:15 +0000284 }
285
Eric Pouech46344472005-01-14 19:54:38 +0000286 switch (type)
Martin Wilck718b1b72002-01-07 21:02:15 +0000287 {
288 case ASYNC_TYPE_READ:
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000289 queue = &file->read_q;
Martin Wilck718b1b72002-01-07 21:02:15 +0000290 break;
291 case ASYNC_TYPE_WRITE:
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000292 queue = &file->write_q;
Martin Wilck718b1b72002-01-07 21:02:15 +0000293 break;
294 default:
295 set_error( STATUS_INVALID_PARAMETER );
Martin Wilck54ba2722002-04-24 21:29:54 +0000296 return;
Martin Wilck718b1b72002-01-07 21:02:15 +0000297 }
298
Eric Pouech7001d6e2005-03-29 11:40:10 +0000299 if (!create_async( current, 0, queue, apc, user, iosb ))
Eric Pouech46344472005-01-14 19:54:38 +0000300 return;
Martin Wilck718b1b72002-01-07 21:02:15 +0000301
Eric Pouech46344472005-01-14 19:54:38 +0000302 /* Check if the new pending request can be served immediately */
303 events = check_fd_events( fd, file_get_poll_events( fd ) );
304 if (events) file_poll_event( fd, events );
Martin Wilck54ba2722002-04-24 21:29:54 +0000305
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000306 set_fd_events( fd, file_get_poll_events( fd ));
307}
308
Eric Pouech46344472005-01-14 19:54:38 +0000309static void file_cancel_async( struct fd *fd )
310{
311 struct file *file = get_fd_user( fd );
312 assert( file->obj.ops == &file_ops );
313
314 async_terminate_queue( &file->read_q, STATUS_CANCELLED );
315 async_terminate_queue( &file->write_q, STATUS_CANCELLED );
316}
317
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000318static struct fd *file_get_fd( struct object *obj )
319{
320 struct file *file = (struct file *)obj;
321 assert( obj->ops == &file_ops );
322 return (struct fd *)grab_object( file->fd );
Martin Wilck718b1b72002-01-07 21:02:15 +0000323}
324
Alexandre Julliard05625391999-01-03 11:55:56 +0000325static void file_destroy( struct object *obj )
326{
Alexandre Julliard05625391999-01-03 11:55:56 +0000327 struct file *file = (struct file *)obj;
328 assert( obj->ops == &file_ops );
329
Alexandre Julliard014099c2004-03-12 01:56:49 +0000330 if (is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000331 {
Eric Pouech46344472005-01-14 19:54:38 +0000332 async_terminate_queue( &file->read_q, STATUS_CANCELLED );
333 async_terminate_queue( &file->write_q, STATUS_CANCELLED );
Martin Wilck718b1b72002-01-07 21:02:15 +0000334 }
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000335 if (file->fd) release_object( file->fd );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000336}
337
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000338/* set the last error depending on errno */
339void file_set_error(void)
Alexandre Julliard338e7571998-12-27 15:28:54 +0000340{
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000341 switch (errno)
342 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000343 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
344 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
345 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000346 case EACCES:
Alexandre Julliard5f258c62001-07-14 00:50:30 +0000347 case ESRCH:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000348 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
349 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
350 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
351 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
Alexandre Julliard716878c2004-04-16 23:32:40 +0000352 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000353 case ENFILE:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000354 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
355 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
356 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
Alexandre Julliardd5b42322003-12-10 01:12:18 +0000357 case ESPIPE: set_win32_error( ERROR_SEEK ); break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000358 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
359 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000360 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000361 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
Mike McCormackea782b62004-07-06 19:42:09 +0000362 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
363 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000364#ifdef EOVERFLOW
Alexandre Julliardce613492003-03-18 05:04:33 +0000365 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000366#endif
Filip Navara2392a362004-04-12 23:15:11 +0000367 default: perror("file_set_error"); set_error( STATUS_UNSUCCESSFUL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000368 }
Alexandre Julliard338e7571998-12-27 15:28:54 +0000369}
370
Alexandre Julliard51885742002-05-30 20:12:58 +0000371struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000372{
Alexandre Julliard61ec6c11999-11-29 02:17:08 +0000373 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000374}
Alexandre Julliard338e7571998-12-27 15:28:54 +0000375
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000376int get_file_unix_fd( struct file *file )
377{
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000378 return get_unix_fd( file->fd );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000379}
380
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000381/* extend a file beyond the current end of file */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000382static int extend_file( struct file *file, file_pos_t new_size )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000383{
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000384 static const char zero;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000385 int unix_fd = get_file_unix_fd( file );
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000386 off_t size = new_size;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000387
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000388 if (sizeof(new_size) > sizeof(size) && size != new_size)
389 {
390 set_error( STATUS_INVALID_PARAMETER );
391 return 0;
392 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000393 /* extend the file one byte beyond the requested size and then truncate it */
394 /* this should work around ftruncate implementations that can't extend files */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000395 if (pwrite( unix_fd, &zero, 1, size ) != -1)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000396 {
Alexandre Julliard863637b2003-01-30 00:26:44 +0000397 ftruncate( unix_fd, size );
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000398 return 1;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000399 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000400 file_set_error();
401 return 0;
402}
403
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000404/* try to grow the file to the specified size */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000405int grow_file( struct file *file, file_pos_t size )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000406{
Marcus Meissner6bb990f2001-05-29 20:55:21 +0000407 struct stat st;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000408 int unix_fd = get_file_unix_fd( file );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000409
Alexandre Julliard863637b2003-01-30 00:26:44 +0000410 if (fstat( unix_fd, &st ) == -1)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000411 {
412 file_set_error();
413 return 0;
414 }
Marcus Meissner3f1ed522001-05-14 20:09:37 +0000415 if (st.st_size >= size) return 1; /* already large enough */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000416 return extend_file( file, size );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000417}
418
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000419/* create a file */
420DECL_HANDLER(create_file)
421{
Alexandre Julliardadc06102004-03-18 04:08:48 +0000422 struct object *file;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000423
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000424 reply->handle = 0;
425 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
Alexandre Julliard49b2f6d2004-04-06 23:41:01 +0000426 req->sharing, req->create, req->options, req->attrs )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000427 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000428 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000429 release_object( file );
430 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000431}
432
433/* allocate a file handle for a Unix fd */
434DECL_HANDLER(alloc_file_handle)
435{
436 struct file *file;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000437 int fd;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000438
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000439 reply->handle = 0;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000440 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000441 {
Alexandre Julliardf5242402001-02-28 21:45:23 +0000442 set_error( STATUS_INVALID_HANDLE );
443 return;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000444 }
Alexandre Julliard014099c2004-03-12 01:56:49 +0000445 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
Alexandre Julliardf5242402001-02-28 21:45:23 +0000446 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000447 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000448 release_object( file );
449 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000450}
451
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000452/* lock a region of a file */
453DECL_HANDLER(lock_file)
454{
455 struct file *file;
Alexandre Julliardce613492003-03-18 05:04:33 +0000456 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
457 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000458
459 if ((file = get_file_obj( current->process, req->handle, 0 )))
460 {
Alexandre Julliardce613492003-03-18 05:04:33 +0000461 reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000462 reply->overlapped = is_overlapped( file );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000463 release_object( file );
464 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000465}
466
467/* unlock a region of a file */
468DECL_HANDLER(unlock_file)
469{
470 struct file *file;
Alexandre Julliardce613492003-03-18 05:04:33 +0000471 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
472 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000473
474 if ((file = get_file_obj( current->process, req->handle, 0 )))
475 {
Alexandre Julliardce613492003-03-18 05:04:33 +0000476 unlock_fd( file->fd, offset, count );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000477 release_object( file );
478 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000479}