- Use NULL instead of 0 for all non-handle pointers.
[wine] / server / file.c
1 /*
2  * Server-side file management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
33 #endif
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_UTIME_H
40 #include <utime.h>
41 #endif
42 #ifdef HAVE_POLL_H
43 #include <poll.h>
44 #endif
45
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winreg.h"
50 #include "winternl.h"
51
52 #include "file.h"
53 #include "handle.h"
54 #include "thread.h"
55 #include "request.h"
56
57 struct file
58 {
59     struct object       obj;        /* object header */
60     struct fd          *fd;         /* file descriptor for this file */
61     unsigned int        access;     /* file access (GENERIC_READ/WRITE) */
62     unsigned int        options;    /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
63     struct list         read_q;
64     struct list         write_q;
65 };
66
67 static void file_dump( struct object *obj, int verbose );
68 static struct fd *file_get_fd( struct object *obj );
69 static void file_destroy( struct object *obj );
70
71 static int file_get_poll_events( struct fd *fd );
72 static void file_poll_event( struct fd *fd, int event );
73 static int file_flush( struct fd *fd, struct event **event );
74 static int file_get_info( struct fd *fd );
75 static void file_queue_async( struct fd *fd, void *apc, void *user, void* iosb, int type, int count );
76 static void file_cancel_async( struct fd *fd );
77
78 static const struct object_ops file_ops =
79 {
80     sizeof(struct file),          /* size */
81     file_dump,                    /* dump */
82     default_fd_add_queue,         /* add_queue */
83     default_fd_remove_queue,      /* remove_queue */
84     default_fd_signaled,          /* signaled */
85     no_satisfied,                 /* satisfied */
86     no_signal,                    /* signal */
87     file_get_fd,                  /* get_fd */
88     file_destroy                  /* destroy */
89 };
90
91 static 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 */
97     file_queue_async,             /* queue_async */
98     file_cancel_async             /* cancel_async */
99 };
100
101 static inline int is_overlapped( const struct file *file )
102 {
103     return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
104 }
105
106 /* create a file from a file descriptor */
107 /* if the function fails the fd is closed */
108 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
109 {
110     struct file *file;
111
112     if ((file = alloc_object( &file_ops )))
113     {
114         file->access     = access;
115         file->options    = FILE_SYNCHRONOUS_IO_NONALERT;
116         if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
117         {
118             release_object( file );
119             return NULL;
120         }
121     }
122     return file;
123 }
124
125
126 static struct object *create_file( const char *nameptr, size_t len, unsigned int access,
127                                    unsigned int sharing, int create, unsigned int options,
128                                    unsigned int attrs )
129 {
130     struct file *file;
131     int flags;
132     char *name;
133     mode_t mode;
134
135     if (!(name = mem_alloc( len + 1 ))) return NULL;
136     memcpy( name, nameptr, len );
137     name[len] = 0;
138
139     switch(create)
140     {
141     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;
147     default:                set_error( STATUS_INVALID_PARAMETER ); goto error;
148     }
149
150     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     }
157     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;
162
163     if (!(file = alloc_object( &file_ops ))) goto error;
164
165     file->access     = access;
166     file->options    = options;
167     list_init( &file->read_q );
168     list_init( &file->write_q );
169
170     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
171     if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) ||
172         !(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE,
173                               &mode, access, sharing, options )))
174     {
175         free( name );
176         release_object( file );
177         return NULL;
178     }
179     free( name );
180
181     /* 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;
190
191  error:
192     free( name );
193     return NULL;
194 }
195
196 /* check if two file objects point to the same file */
197 int is_same_file( struct file *file1, struct file *file2 )
198 {
199     return is_same_file_fd( file1->fd, file2->fd );
200 }
201
202 /* create a temp file for anonymous mappings */
203 struct file *create_temp_file( int access )
204 {
205     char tmpfn[16];
206     int fd;
207
208     sprintf( tmpfn, "anonmap.XXXXXX" );  /* create it in the server directory */
209     fd = mkstemps( tmpfn, 0 );
210     if (fd == -1)
211     {
212         file_set_error();
213         return NULL;
214     }
215     unlink( tmpfn );
216     return create_file_for_fd( fd, access, 0 );
217 }
218
219 static void file_dump( struct object *obj, int verbose )
220 {
221     struct file *file = (struct file *)obj;
222     assert( obj->ops == &file_ops );
223     fprintf( stderr, "File fd=%p options=%08x\n", file->fd, file->options );
224 }
225
226 static int file_get_poll_events( struct fd *fd )
227 {
228     struct file *file = get_fd_user( fd );
229     int events = 0;
230     assert( file->obj.ops == &file_ops );
231     if (file->access & GENERIC_READ) events |= POLLIN;
232     if (file->access & GENERIC_WRITE) events |= POLLOUT;
233     return events;
234 }
235
236 static void file_poll_event( struct fd *fd, int event )
237 {
238     struct file *file = get_fd_user( fd );
239     assert( file->obj.ops == &file_ops );
240     if (is_overlapped( file ))
241     {
242         if (!list_empty( &file->read_q ) && (POLLIN & event) )
243         {
244             async_terminate_head( &file->read_q, STATUS_ALERTED );
245             return;
246         }
247         if (!list_empty( &file->write_q ) && (POLLOUT & event) )
248         {
249             async_terminate_head( &file->write_q, STATUS_ALERTED );
250             return;
251         }
252     }
253     default_poll_event( fd, event );
254 }
255
256
257 static int file_flush( struct fd *fd, struct event **event )
258 {
259     int ret = (fsync( get_unix_fd(fd) ) != -1);
260     if (!ret) file_set_error();
261     return ret;
262 }
263
264 static int file_get_info( struct fd *fd )
265 {
266     struct file *file = get_fd_user( fd );
267
268     if (is_overlapped( file )) return FD_FLAG_OVERLAPPED;
269     else return 0;
270 }
271
272 static void file_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
273                               int type, int count )
274 {
275     struct file *file = get_fd_user( fd );
276     struct list *queue;
277     int events;
278
279     assert( file->obj.ops == &file_ops );
280
281     if (!is_overlapped( file ))
282     {
283         set_error( STATUS_INVALID_HANDLE );
284         return;
285     }
286
287     switch (type)
288     {
289     case ASYNC_TYPE_READ:
290         queue = &file->read_q;
291         break;
292     case ASYNC_TYPE_WRITE:
293         queue = &file->write_q;
294         break;
295     default:
296         set_error( STATUS_INVALID_PARAMETER );
297         return;
298     }
299
300     if (!create_async( current, NULL, queue, apc, user, iosb ))
301         return;
302
303     /* 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 );
306
307     set_fd_events( fd, file_get_poll_events( fd ));
308 }
309
310 static 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
319 static 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 );
324 }
325
326 static void file_destroy( struct object *obj )
327 {
328     struct file *file = (struct file *)obj;
329     assert( obj->ops == &file_ops );
330
331     if (is_overlapped( file ))
332     {
333         async_terminate_queue( &file->read_q, STATUS_CANCELLED );
334         async_terminate_queue( &file->write_q, STATUS_CANCELLED );
335     }
336     if (file->fd) release_object( file->fd );
337 }
338
339 /* set the last error depending on errno */
340 void file_set_error(void)
341 {
342     switch (errno)
343     {
344     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;
347     case EACCES:
348     case ESRCH:
349     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;
353     case EISDIR:    set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
354     case ENFILE:
355     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;
358     case ESPIPE:    set_win32_error( ERROR_SEEK ); break;
359     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
360     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
361     case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
362     case EFBIG:     set_error( STATUS_SECTION_TOO_BIG ); break;
363     case ENODEV:    set_error( STATUS_NO_SUCH_DEVICE ); break;
364     case ENXIO:     set_error( STATUS_NO_SUCH_DEVICE ); break;
365 #ifdef EOVERFLOW
366     case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
367 #endif
368     default:        perror("file_set_error"); set_error( STATUS_UNSUCCESSFUL ); break;
369     }
370 }
371
372 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
373 {
374     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
375 }
376
377 int get_file_unix_fd( struct file *file )
378 {
379     return get_unix_fd( file->fd );
380 }
381
382 /* extend a file beyond the current end of file */
383 static int extend_file( struct file *file, file_pos_t new_size )
384 {
385     static const char zero;
386     int unix_fd = get_file_unix_fd( file );
387     off_t size = new_size;
388
389     if (sizeof(new_size) > sizeof(size) && size != new_size)
390     {
391         set_error( STATUS_INVALID_PARAMETER );
392         return 0;
393     }
394     /* 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 */
396     if (pwrite( unix_fd, &zero, 1, size ) != -1)
397     {
398         ftruncate( unix_fd, size );
399         return 1;
400     }
401     file_set_error();
402     return 0;
403 }
404
405 /* try to grow the file to the specified size */
406 int grow_file( struct file *file, file_pos_t size )
407 {
408     struct stat st;
409     int unix_fd = get_file_unix_fd( file );
410
411     if (fstat( unix_fd, &st ) == -1)
412     {
413         file_set_error();
414         return 0;
415     }
416     if (st.st_size >= size) return 1;  /* already large enough */
417     return extend_file( file, size );
418 }
419
420 /* create a file */
421 DECL_HANDLER(create_file)
422 {
423     struct object *file;
424
425     reply->handle = 0;
426     if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
427                              req->sharing, req->create, req->options, req->attrs )))
428     {
429         reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
430         release_object( file );
431     }
432 }
433
434 /* allocate a file handle for a Unix fd */
435 DECL_HANDLER(alloc_file_handle)
436 {
437     struct file *file;
438     int fd;
439
440     reply->handle = 0;
441     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
442     {
443         set_error( STATUS_INVALID_HANDLE );
444         return;
445     }
446     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
447     {
448         reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
449         release_object( file );
450     }
451 }
452
453 /* lock a region of a file */
454 DECL_HANDLER(lock_file)
455 {
456     struct file *file;
457     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;
459
460     if ((file = get_file_obj( current->process, req->handle, 0 )))
461     {
462         reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
463         reply->overlapped = is_overlapped( file );
464         release_object( file );
465     }
466 }
467
468 /* unlock a region of a file */
469 DECL_HANDLER(unlock_file)
470 {
471     struct file *file;
472     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;
474
475     if ((file = get_file_obj( current->process, req->handle, 0 )))
476     {
477         unlock_fd( file->fd, offset, count );
478         release_object( file );
479     }
480 }