blob: 45f2bc0a8a67c49a83761992f5f89d59cdd6515c [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 */
Mike McCormackf92fff62005-04-24 17:35:52 +000086 no_signal, /* signal */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000087 file_get_fd, /* get_fd */
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 */
94 file_poll_event, /* poll_event */
95 file_flush, /* flush */
96 file_get_info, /* get_file_info */
Eric Pouech46344472005-01-14 19:54:38 +000097 file_queue_async, /* queue_async */
98 file_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 {
Ove Kaaven708a8462001-10-24 00:23:25 +0000114 file->access = 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
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000125
Alexandre Julliardadc06102004-03-18 04:08:48 +0000126static struct object *create_file( const char *nameptr, size_t len, unsigned int access,
127 unsigned int sharing, int create, unsigned int options,
Alexandre Julliard49b2f6d2004-04-06 23:41:01 +0000128 unsigned int attrs )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000129{
130 struct file *file;
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000131 int flags;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000132 char *name;
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000133 mode_t mode;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000134
135 if (!(name = mem_alloc( len + 1 ))) return NULL;
136 memcpy( name, nameptr, len );
137 name[len] = 0;
138
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000139 switch(create)
140 {
Alexandre Julliard014099c2004-03-12 01:56:49 +0000141 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
142 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
143 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
144 case FILE_OPEN: flags = 0; break;
145 case FILE_OPEN_IF: flags = O_CREAT; break;
146 case FILE_OVERWRITE: flags = O_TRUNC; break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000147 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000148 }
Eric Pouech46344472005-01-14 19:54:38 +0000149
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000150 switch(access & (GENERIC_READ | GENERIC_WRITE))
151 {
152 case 0: break;
153 case GENERIC_READ: flags |= O_RDONLY; break;
154 case GENERIC_WRITE: flags |= O_WRONLY; break;
155 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
156 }
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000157 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
158
159 if (len >= 4 &&
160 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
161 mode |= 0111;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000162
Alexandre Julliard580da242003-03-12 22:38:14 +0000163 if (!(file = alloc_object( &file_ops ))) goto error;
164
165 file->access = access;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000166 file->options = options;
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000167 list_init( &file->read_q );
168 list_init( &file->write_q );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000169
Alexandre Julliard580da242003-03-12 22:38:14 +0000170 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
171 if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) ||
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000172 !(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE,
Alexandre Julliard684b65c2004-04-16 04:31:35 +0000173 &mode, access, sharing, options )))
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000174 {
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000175 free( name );
Alexandre Julliard580da242003-03-12 22:38:14 +0000176 release_object( file );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000177 return NULL;
178 }
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000179 free( name );
180
Alexandre Julliardadc06102004-03-18 04:08:48 +0000181 /* check for serial port */
182 if (S_ISCHR(mode) && is_serial_fd( file->fd ))
183 {
184 struct object *obj = create_serial( file->fd, file->options );
185 release_object( file );
186 return obj;
187 }
188
189 return &file->obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000190
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000191 error:
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000192 free( name );
193 return NULL;
194}
195
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000196/* check if two file objects point to the same file */
197int is_same_file( struct file *file1, struct file *file2 )
198{
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000199 return is_same_file_fd( file1->fd, file2->fd );
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000200}
201
Alexandre Julliard580da242003-03-12 22:38:14 +0000202/* create a temp file for anonymous mappings */
203struct file *create_temp_file( int access )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000204{
Alexandre Julliard580da242003-03-12 22:38:14 +0000205 char tmpfn[16];
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000206 int fd;
207
Alexandre Julliard580da242003-03-12 22:38:14 +0000208 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
Alexandre Julliard2ee8b5b2003-03-20 21:07:49 +0000209 fd = mkstemps( tmpfn, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000210 if (fd == -1)
211 {
212 file_set_error();
Alexandre Julliard580da242003-03-12 22:38:14 +0000213 return NULL;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000214 }
Marcus Meissner786d2492002-07-29 23:55:39 +0000215 unlink( tmpfn );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000216 return create_file_for_fd( fd, access, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000217}
218
Alexandre Julliard338e7571998-12-27 15:28:54 +0000219static void file_dump( struct object *obj, int verbose )
220{
221 struct file *file = (struct file *)obj;
222 assert( obj->ops == &file_ops );
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000223 fprintf( stderr, "File fd=%p options=%08x\n", file->fd, file->options );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000224}
225
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000226static int file_get_poll_events( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000227{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000228 struct file *file = get_fd_user( fd );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000229 int events = 0;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000230 assert( file->obj.ops == &file_ops );
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000231 if (file->access & GENERIC_READ) events |= POLLIN;
232 if (file->access & GENERIC_WRITE) events |= POLLOUT;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000233 return events;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000234}
235
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000236static void file_poll_event( struct fd *fd, int event )
Martin Wilck718b1b72002-01-07 21:02:15 +0000237{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000238 struct file *file = get_fd_user( fd );
239 assert( file->obj.ops == &file_ops );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000240 if (is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000241 {
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000242 if (!list_empty( &file->read_q ) && (POLLIN & event) )
Martin Wilck718b1b72002-01-07 21:02:15 +0000243 {
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000244 async_terminate_head( &file->read_q, STATUS_ALERTED );
Martin Wilck718b1b72002-01-07 21:02:15 +0000245 return;
246 }
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000247 if (!list_empty( &file->write_q ) && (POLLOUT & event) )
Martin Wilck718b1b72002-01-07 21:02:15 +0000248 {
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000249 async_terminate_head( &file->write_q, STATUS_ALERTED );
Martin Wilck718b1b72002-01-07 21:02:15 +0000250 return;
251 }
252 }
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000253 default_poll_event( fd, event );
Martin Wilck718b1b72002-01-07 21:02:15 +0000254}
255
256
Mike McCormackef8b9462003-05-15 04:22:45 +0000257static int file_flush( struct fd *fd, struct event **event )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000258{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000259 int ret = (fsync( get_unix_fd(fd) ) != -1);
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000260 if (!ret) file_set_error();
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000261 return ret;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000262}
263
Alexandre Julliard6a27b482004-08-18 00:04:58 +0000264static int file_get_info( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000265{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000266 struct file *file = get_fd_user( fd );
Alexandre Julliard05625391999-01-03 11:55:56 +0000267
Alexandre Julliard6a27b482004-08-18 00:04:58 +0000268 if (is_overlapped( file )) return FD_FLAG_OVERLAPPED;
269 else return 0;
Alexandre Julliard05625391999-01-03 11:55:56 +0000270}
271
Eric Pouech46344472005-01-14 19:54:38 +0000272static void file_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
273 int type, int count )
Martin Wilck718b1b72002-01-07 21:02:15 +0000274{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000275 struct file *file = get_fd_user( fd );
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000276 struct list *queue;
Eric Pouech46344472005-01-14 19:54:38 +0000277 int events;
Martin Wilck718b1b72002-01-07 21:02:15 +0000278
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000279 assert( file->obj.ops == &file_ops );
Martin Wilck718b1b72002-01-07 21:02:15 +0000280
Alexandre Julliard014099c2004-03-12 01:56:49 +0000281 if (!is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000282 {
Eric Pouech46344472005-01-14 19:54:38 +0000283 set_error( STATUS_INVALID_HANDLE );
Martin Wilck54ba2722002-04-24 21:29:54 +0000284 return;
Martin Wilck718b1b72002-01-07 21:02:15 +0000285 }
286
Eric Pouech46344472005-01-14 19:54:38 +0000287 switch (type)
Martin Wilck718b1b72002-01-07 21:02:15 +0000288 {
289 case ASYNC_TYPE_READ:
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000290 queue = &file->read_q;
Martin Wilck718b1b72002-01-07 21:02:15 +0000291 break;
292 case ASYNC_TYPE_WRITE:
Alexandre Julliarddd81ac52005-02-24 17:06:31 +0000293 queue = &file->write_q;
Martin Wilck718b1b72002-01-07 21:02:15 +0000294 break;
295 default:
296 set_error( STATUS_INVALID_PARAMETER );
Martin Wilck54ba2722002-04-24 21:29:54 +0000297 return;
Martin Wilck718b1b72002-01-07 21:02:15 +0000298 }
299
Eric Pouech7001d6e2005-03-29 11:40:10 +0000300 if (!create_async( current, 0, queue, apc, user, iosb ))
Eric Pouech46344472005-01-14 19:54:38 +0000301 return;
Martin Wilck718b1b72002-01-07 21:02:15 +0000302
Eric Pouech46344472005-01-14 19:54:38 +0000303 /* Check if the new pending request can be served immediately */
304 events = check_fd_events( fd, file_get_poll_events( fd ) );
305 if (events) file_poll_event( fd, events );
Martin Wilck54ba2722002-04-24 21:29:54 +0000306
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000307 set_fd_events( fd, file_get_poll_events( fd ));
308}
309
Eric Pouech46344472005-01-14 19:54:38 +0000310static void file_cancel_async( struct fd *fd )
311{
312 struct file *file = get_fd_user( fd );
313 assert( file->obj.ops == &file_ops );
314
315 async_terminate_queue( &file->read_q, STATUS_CANCELLED );
316 async_terminate_queue( &file->write_q, STATUS_CANCELLED );
317}
318
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000319static struct fd *file_get_fd( struct object *obj )
320{
321 struct file *file = (struct file *)obj;
322 assert( obj->ops == &file_ops );
323 return (struct fd *)grab_object( file->fd );
Martin Wilck718b1b72002-01-07 21:02:15 +0000324}
325
Alexandre Julliard05625391999-01-03 11:55:56 +0000326static void file_destroy( struct object *obj )
327{
Alexandre Julliard05625391999-01-03 11:55:56 +0000328 struct file *file = (struct file *)obj;
329 assert( obj->ops == &file_ops );
330
Alexandre Julliard014099c2004-03-12 01:56:49 +0000331 if (is_overlapped( file ))
Martin Wilck718b1b72002-01-07 21:02:15 +0000332 {
Eric Pouech46344472005-01-14 19:54:38 +0000333 async_terminate_queue( &file->read_q, STATUS_CANCELLED );
334 async_terminate_queue( &file->write_q, STATUS_CANCELLED );
Martin Wilck718b1b72002-01-07 21:02:15 +0000335 }
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000336 if (file->fd) release_object( file->fd );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000337}
338
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000339/* set the last error depending on errno */
340void file_set_error(void)
Alexandre Julliard338e7571998-12-27 15:28:54 +0000341{
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000342 switch (errno)
343 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000344 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
345 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
346 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000347 case EACCES:
Alexandre Julliard5f258c62001-07-14 00:50:30 +0000348 case ESRCH:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000349 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
350 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
351 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
352 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
Alexandre Julliard716878c2004-04-16 23:32:40 +0000353 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000354 case ENFILE:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000355 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
356 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
357 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
Alexandre Julliardd5b42322003-12-10 01:12:18 +0000358 case ESPIPE: set_win32_error( ERROR_SEEK ); break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000359 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
360 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000361 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000362 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
Mike McCormackea782b62004-07-06 19:42:09 +0000363 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
364 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000365#ifdef EOVERFLOW
Alexandre Julliardce613492003-03-18 05:04:33 +0000366 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000367#endif
Filip Navara2392a362004-04-12 23:15:11 +0000368 default: perror("file_set_error"); set_error( STATUS_UNSUCCESSFUL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000369 }
Alexandre Julliard338e7571998-12-27 15:28:54 +0000370}
371
Alexandre Julliard51885742002-05-30 20:12:58 +0000372struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000373{
Alexandre Julliard61ec6c11999-11-29 02:17:08 +0000374 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000375}
Alexandre Julliard338e7571998-12-27 15:28:54 +0000376
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000377int get_file_unix_fd( struct file *file )
378{
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000379 return get_unix_fd( file->fd );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000380}
381
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000382/* extend a file beyond the current end of file */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000383static int extend_file( struct file *file, file_pos_t new_size )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000384{
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000385 static const char zero;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000386 int unix_fd = get_file_unix_fd( file );
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000387 off_t size = new_size;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000388
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000389 if (sizeof(new_size) > sizeof(size) && size != new_size)
390 {
391 set_error( STATUS_INVALID_PARAMETER );
392 return 0;
393 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000394 /* extend the file one byte beyond the requested size and then truncate it */
395 /* this should work around ftruncate implementations that can't extend files */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000396 if (pwrite( unix_fd, &zero, 1, size ) != -1)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000397 {
Alexandre Julliard863637b2003-01-30 00:26:44 +0000398 ftruncate( unix_fd, size );
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000399 return 1;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000400 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000401 file_set_error();
402 return 0;
403}
404
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000405/* try to grow the file to the specified size */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000406int grow_file( struct file *file, file_pos_t size )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000407{
Marcus Meissner6bb990f2001-05-29 20:55:21 +0000408 struct stat st;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000409 int unix_fd = get_file_unix_fd( file );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000410
Alexandre Julliard863637b2003-01-30 00:26:44 +0000411 if (fstat( unix_fd, &st ) == -1)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000412 {
413 file_set_error();
414 return 0;
415 }
Marcus Meissner3f1ed522001-05-14 20:09:37 +0000416 if (st.st_size >= size) return 1; /* already large enough */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000417 return extend_file( file, size );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000418}
419
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000420/* create a file */
421DECL_HANDLER(create_file)
422{
Alexandre Julliardadc06102004-03-18 04:08:48 +0000423 struct object *file;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000424
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000425 reply->handle = 0;
426 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
Alexandre Julliard49b2f6d2004-04-06 23:41:01 +0000427 req->sharing, req->create, req->options, req->attrs )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000428 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000429 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000430 release_object( file );
431 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000432}
433
434/* allocate a file handle for a Unix fd */
435DECL_HANDLER(alloc_file_handle)
436{
437 struct file *file;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000438 int fd;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000439
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000440 reply->handle = 0;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000441 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000442 {
Alexandre Julliardf5242402001-02-28 21:45:23 +0000443 set_error( STATUS_INVALID_HANDLE );
444 return;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000445 }
Alexandre Julliard014099c2004-03-12 01:56:49 +0000446 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
Alexandre Julliardf5242402001-02-28 21:45:23 +0000447 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000448 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000449 release_object( file );
450 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000451}
452
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000453/* lock a region of a file */
454DECL_HANDLER(lock_file)
455{
456 struct file *file;
Alexandre Julliardce613492003-03-18 05:04:33 +0000457 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
458 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000459
460 if ((file = get_file_obj( current->process, req->handle, 0 )))
461 {
Alexandre Julliardce613492003-03-18 05:04:33 +0000462 reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000463 reply->overlapped = is_overlapped( file );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000464 release_object( file );
465 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000466}
467
468/* unlock a region of a file */
469DECL_HANDLER(unlock_file)
470{
471 struct file *file;
Alexandre Julliardce613492003-03-18 05:04:33 +0000472 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
473 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000474
475 if ((file = get_file_obj( current->process, req->handle, 0 )))
476 {
Alexandre Julliardce613492003-03-18 05:04:33 +0000477 unlock_fd( file->fd, offset, count );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000478 release_object( file );
479 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000480}