Fixed deadlock in capture callback.
[wine/gsoc_dplay.git] / server / file.c
blob8f599ec5c95386b1e7df1e487a0505d3dc0b1845
1 /*
2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
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
43 #include "winerror.h"
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winreg.h"
47 #include "winternl.h"
49 #include "file.h"
50 #include "handle.h"
51 #include "thread.h"
52 #include "request.h"
53 #include "async.h"
55 struct file
57 struct object obj; /* object header */
58 struct fd *fd; /* file descriptor for this file */
59 unsigned int access; /* file access (GENERIC_READ/WRITE) */
60 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
61 int removable; /* is file on removable media? */
62 struct async_queue read_q;
63 struct async_queue write_q;
66 static void file_dump( struct object *obj, int verbose );
67 static struct fd *file_get_fd( struct object *obj );
68 static void file_destroy( struct object *obj );
70 static int file_get_poll_events( struct fd *fd );
71 static void file_poll_event( struct fd *fd, int event );
72 static int file_flush( struct fd *fd, struct event **event );
73 static int file_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
74 static void file_queue_async( struct fd *fd, void *ptr, unsigned int status, int type, int count );
76 static const struct object_ops file_ops =
78 sizeof(struct file), /* size */
79 file_dump, /* dump */
80 default_fd_add_queue, /* add_queue */
81 default_fd_remove_queue, /* remove_queue */
82 default_fd_signaled, /* signaled */
83 no_satisfied, /* satisfied */
84 file_get_fd, /* get_fd */
85 file_destroy /* destroy */
88 static const struct fd_ops file_fd_ops =
90 file_get_poll_events, /* get_poll_events */
91 file_poll_event, /* poll_event */
92 file_flush, /* flush */
93 file_get_info, /* get_file_info */
94 file_queue_async /* queue_async */
97 static inline int is_overlapped( const struct file *file )
99 return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
102 /* create a file from a file descriptor */
103 /* if the function fails the fd is closed */
104 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
106 struct file *file;
108 if ((file = alloc_object( &file_ops )))
110 file->access = access;
111 file->options = FILE_SYNCHRONOUS_IO_NONALERT;
112 file->removable = 0;
113 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
115 release_object( file );
116 return NULL;
119 return file;
123 static struct object *create_file( const char *nameptr, size_t len, unsigned int access,
124 unsigned int sharing, int create, unsigned int options,
125 unsigned int attrs, int removable )
127 struct file *file;
128 int flags;
129 char *name;
130 mode_t mode;
132 if (!(name = mem_alloc( len + 1 ))) return NULL;
133 memcpy( name, nameptr, len );
134 name[len] = 0;
136 switch(create)
138 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
139 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
140 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
141 case FILE_OPEN: flags = 0; break;
142 case FILE_OPEN_IF: flags = O_CREAT; break;
143 case FILE_OVERWRITE: flags = O_TRUNC; break;
144 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
146 switch(access & (GENERIC_READ | GENERIC_WRITE))
148 case 0: break;
149 case GENERIC_READ: flags |= O_RDONLY; break;
150 case GENERIC_WRITE: flags |= O_WRONLY; break;
151 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
153 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
155 if (len >= 4 &&
156 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
157 mode |= 0111;
159 if (!(file = alloc_object( &file_ops ))) goto error;
161 file->access = access;
162 file->options = options;
163 file->removable = removable;
164 if (is_overlapped( file ))
166 init_async_queue (&file->read_q);
167 init_async_queue (&file->write_q);
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,
174 (options & FILE_DELETE_ON_CLOSE) ? name : "" )))
176 free( name );
177 release_object( file );
178 return NULL;
180 free( name );
182 /* refuse to open a directory */
183 if (S_ISDIR(mode) && !(options & FILE_OPEN_FOR_BACKUP_INTENT))
185 set_error( STATUS_ACCESS_DENIED );
186 release_object( file );
187 return NULL;
189 /* check for serial port */
190 if (S_ISCHR(mode) && is_serial_fd( file->fd ))
192 struct object *obj = create_serial( file->fd, file->options );
193 release_object( file );
194 return obj;
197 return &file->obj;
199 error:
200 free( name );
201 return NULL;
204 /* check if two file objects point to the same file */
205 int is_same_file( struct file *file1, struct file *file2 )
207 return is_same_file_fd( file1->fd, file2->fd );
210 /* check if the file is on removable media */
211 int is_file_removable( struct file *file )
213 return file->removable;
216 /* create a temp file for anonymous mappings */
217 struct file *create_temp_file( int access )
219 char tmpfn[16];
220 int fd;
222 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
223 fd = mkstemps( tmpfn, 0 );
224 if (fd == -1)
226 file_set_error();
227 return NULL;
229 unlink( tmpfn );
230 return create_file_for_fd( fd, access, 0 );
233 static void file_dump( struct object *obj, int verbose )
235 struct file *file = (struct file *)obj;
236 assert( obj->ops == &file_ops );
237 fprintf( stderr, "File fd=%p options=%08x\n", file->fd, file->options );
240 static int file_get_poll_events( struct fd *fd )
242 struct file *file = get_fd_user( fd );
243 int events = 0;
244 assert( file->obj.ops == &file_ops );
245 if (file->access & GENERIC_READ) events |= POLLIN;
246 if (file->access & GENERIC_WRITE) events |= POLLOUT;
247 return events;
250 static void file_poll_event( struct fd *fd, int event )
252 struct file *file = get_fd_user( fd );
253 assert( file->obj.ops == &file_ops );
254 if (is_overlapped( file ))
256 if( IS_READY(file->read_q) && (POLLIN & event) )
258 async_notify(file->read_q.head, STATUS_ALERTED);
259 return;
261 if( IS_READY(file->write_q) && (POLLOUT & event) )
263 async_notify(file->write_q.head, STATUS_ALERTED);
264 return;
267 default_poll_event( fd, event );
271 static int file_flush( struct fd *fd, struct event **event )
273 int ret = (fsync( get_unix_fd(fd) ) != -1);
274 if (!ret) file_set_error();
275 return ret;
278 static int file_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
280 struct stat st;
281 struct file *file = get_fd_user( fd );
282 int unix_fd = get_unix_fd( fd );
284 if (reply)
286 if (fstat( unix_fd, &st ) == -1)
288 file_set_error();
289 return FD_TYPE_INVALID;
291 if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
292 S_ISSOCK(st.st_mode) || isatty(unix_fd)) reply->type = FILE_TYPE_CHAR;
293 else reply->type = FILE_TYPE_DISK;
294 if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
295 else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
296 if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
297 reply->access_time = st.st_atime;
298 reply->write_time = st.st_mtime;
299 reply->change_time = st.st_ctime;
300 if (S_ISDIR(st.st_mode))
302 reply->size_high = 0;
303 reply->size_low = 0;
304 reply->alloc_high = 0;
305 reply->alloc_low = 0;
307 else
309 file_pos_t alloc;
310 reply->size_high = st.st_size >> 32;
311 reply->size_low = st.st_size & 0xffffffff;
312 alloc = (file_pos_t)st.st_blksize * st.st_blocks;
313 reply->alloc_high = alloc >> 32;
314 reply->alloc_low = alloc & 0xffffffff;
316 reply->links = st.st_nlink;
317 reply->index_high = st.st_dev;
318 reply->index_low = st.st_ino;
319 reply->serial = 0; /* FIXME */
321 *flags = 0;
322 if (is_overlapped( file )) *flags |= FD_FLAG_OVERLAPPED;
323 return FD_TYPE_DEFAULT;
326 static void file_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
328 struct file *file = get_fd_user( fd );
329 struct async *async;
330 struct async_queue *q;
332 assert( file->obj.ops == &file_ops );
334 if (!is_overlapped( file ))
336 set_error ( STATUS_INVALID_HANDLE );
337 return;
340 switch(type)
342 case ASYNC_TYPE_READ:
343 q = &file->read_q;
344 break;
345 case ASYNC_TYPE_WRITE:
346 q = &file->write_q;
347 break;
348 default:
349 set_error( STATUS_INVALID_PARAMETER );
350 return;
353 async = find_async ( q, current, ptr );
355 if ( status == STATUS_PENDING )
357 int events;
359 if ( !async )
360 async = create_async ( &file->obj, current, ptr );
361 if ( !async )
362 return;
364 async->status = STATUS_PENDING;
365 if ( !async->q )
366 async_insert( q, async );
368 /* Check if the new pending request can be served immediately */
369 events = check_fd_events( fd, file_get_poll_events( fd ) );
370 if (events) file_poll_event ( fd, events );
372 else if ( async ) destroy_async ( async );
373 else set_error ( STATUS_INVALID_PARAMETER );
375 set_fd_events( fd, file_get_poll_events( fd ));
378 static struct fd *file_get_fd( struct object *obj )
380 struct file *file = (struct file *)obj;
381 assert( obj->ops == &file_ops );
382 return (struct fd *)grab_object( file->fd );
385 static void file_destroy( struct object *obj )
387 struct file *file = (struct file *)obj;
388 assert( obj->ops == &file_ops );
390 if (is_overlapped( file ))
392 destroy_async_queue (&file->read_q);
393 destroy_async_queue (&file->write_q);
395 if (file->fd) release_object( file->fd );
398 /* set the last error depending on errno */
399 void file_set_error(void)
401 switch (errno)
403 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
404 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
405 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
406 case EACCES:
407 case ESRCH:
408 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
409 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
410 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
411 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
412 case EISDIR: set_win32_error( ERROR_CANNOT_MAKE ); break;
413 case ENFILE:
414 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
415 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
416 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
417 case ESPIPE: set_win32_error( ERROR_SEEK ); break;
418 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
419 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
420 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
421 #ifdef EOVERFLOW
422 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
423 #endif
424 default: perror("file_set_error"); set_win32_error( ERROR_UNKNOWN ); break;
428 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
430 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
433 int get_file_unix_fd( struct file *file )
435 return get_unix_fd( file->fd );
438 static int set_file_pointer( obj_handle_t handle, unsigned int *low, int *high, int whence )
440 struct file *file;
441 off_t result,xto;
443 xto = *low+((off_t)*high<<32);
444 if (!(file = get_file_obj( current->process, handle, 0 )))
445 return 0;
446 if ((result = lseek( get_file_unix_fd(file), xto, whence))==-1)
448 /* Check for seek before start of file */
450 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
451 if (((errno == EINVAL) || (errno == EPERM))
452 && (whence != SEEK_SET) && (*high < 0))
453 set_win32_error( ERROR_NEGATIVE_SEEK );
454 else
455 file_set_error();
456 release_object( file );
457 return 0;
459 *low = result & 0xffffffff;
460 *high = result >> 32;
461 release_object( file );
462 return 1;
465 /* extend a file beyond the current end of file */
466 static int extend_file( struct file *file, off_t size )
468 static const char zero;
469 int unix_fd = get_file_unix_fd( file );
471 /* extend the file one byte beyond the requested size and then truncate it */
472 /* this should work around ftruncate implementations that can't extend files */
473 if ((lseek( unix_fd, size, SEEK_SET ) != -1) &&
474 (write( unix_fd, &zero, 1 ) != -1))
476 ftruncate( unix_fd, size );
477 return 1;
479 file_set_error();
480 return 0;
483 /* truncate file at current position */
484 static int truncate_file( struct file *file )
486 int ret = 0;
487 int unix_fd = get_file_unix_fd( file );
488 off_t pos = lseek( unix_fd, 0, SEEK_CUR );
489 off_t eof = lseek( unix_fd, 0, SEEK_END );
491 if (eof < pos) ret = extend_file( file, pos );
492 else
494 if (ftruncate( unix_fd, pos ) != -1) ret = 1;
495 else file_set_error();
497 lseek( unix_fd, pos, SEEK_SET ); /* restore file pos */
498 return ret;
501 /* try to grow the file to the specified size */
502 int grow_file( struct file *file, int size_high, int size_low )
504 int ret = 0;
505 struct stat st;
506 int unix_fd = get_file_unix_fd( file );
507 off_t old_pos, size = size_low + (((off_t)size_high)<<32);
509 if (fstat( unix_fd, &st ) == -1)
511 file_set_error();
512 return 0;
514 if (st.st_size >= size) return 1; /* already large enough */
515 old_pos = lseek( unix_fd, 0, SEEK_CUR ); /* save old pos */
516 ret = extend_file( file, size );
517 lseek( unix_fd, old_pos, SEEK_SET ); /* restore file pos */
518 return ret;
521 /* create a file */
522 DECL_HANDLER(create_file)
524 struct object *file;
526 reply->handle = 0;
527 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
528 req->sharing, req->create, req->options, req->attrs, req->removable )))
530 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
531 release_object( file );
535 /* allocate a file handle for a Unix fd */
536 DECL_HANDLER(alloc_file_handle)
538 struct file *file;
539 int fd;
541 reply->handle = 0;
542 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
544 set_error( STATUS_INVALID_HANDLE );
545 return;
547 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
549 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
550 release_object( file );
554 /* set a file current position */
555 DECL_HANDLER(set_file_pointer)
557 int high = req->high;
558 int low = req->low;
559 set_file_pointer( req->handle, &low, &high, req->whence );
560 reply->new_low = low;
561 reply->new_high = high;
564 /* truncate (or extend) a file */
565 DECL_HANDLER(truncate_file)
567 struct file *file;
569 if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
571 truncate_file( file );
572 release_object( file );
576 /* lock a region of a file */
577 DECL_HANDLER(lock_file)
579 struct file *file;
580 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
581 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
583 if ((file = get_file_obj( current->process, req->handle, 0 )))
585 reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
586 reply->overlapped = is_overlapped( file );
587 release_object( file );
591 /* unlock a region of a file */
592 DECL_HANDLER(unlock_file)
594 struct file *file;
595 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
596 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
598 if ((file = get_file_obj( current->process, req->handle, 0 )))
600 unlock_fd( file->fd, offset, count );
601 release_object( file );