- Path changes are saved to the struct correctly.
[wine/multimedia.git] / server / file.c
blob96d525040c4c6453d60bc58a944d2967042f811f
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 <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
32 #endif
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <time.h>
37 #include <unistd.h>
38 #ifdef HAVE_UTIME_H
39 #include <utime.h>
40 #endif
42 #include "winerror.h"
43 #include "winbase.h"
45 #include "file.h"
46 #include "handle.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "async.h"
51 struct file
53 struct object obj; /* object header */
54 struct fd *fd; /* file descriptor for this file */
55 struct file *next; /* next file in hashing list */
56 char *name; /* file name */
57 unsigned int access; /* file access (GENERIC_READ/WRITE) */
58 unsigned int flags; /* flags (FILE_FLAG_*) */
59 unsigned int sharing; /* file sharing mode */
60 int drive_type; /* type of drive the file is on */
61 struct async_queue read_q;
62 struct async_queue write_q;
65 #define NAME_HASH_SIZE 37
67 static struct file *file_hash[NAME_HASH_SIZE];
69 static void file_dump( struct object *obj, int verbose );
70 static struct fd *file_get_fd( struct object *obj );
71 static void file_destroy( struct object *obj );
73 static int file_get_poll_events( struct fd *fd );
74 static void file_poll_event( struct fd *fd, int event );
75 static int file_flush( struct fd *fd, struct event **event );
76 static int file_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
77 static void file_queue_async( struct fd *fd, void *ptr, unsigned int status, int type, int count );
79 static const struct object_ops file_ops =
81 sizeof(struct file), /* size */
82 file_dump, /* dump */
83 default_fd_add_queue, /* add_queue */
84 default_fd_remove_queue, /* remove_queue */
85 default_fd_signaled, /* signaled */
86 no_satisfied, /* satisfied */
87 file_get_fd, /* get_fd */
88 file_destroy /* destroy */
91 static const struct fd_ops file_fd_ops =
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 */
100 static int get_name_hash( const char *name )
102 int hash = 0;
103 while (*name) hash ^= (unsigned char)*name++;
104 return hash % NAME_HASH_SIZE;
107 /* check if the desired access is possible without violating */
108 /* the sharing mode of other opens of the same file */
109 static int check_sharing( const char *name, int hash, unsigned int access,
110 unsigned int sharing )
112 struct file *file;
113 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
114 unsigned int existing_access = 0;
116 for (file = file_hash[hash]; file; file = file->next)
118 if (strcmp( file->name, name )) continue;
119 existing_sharing &= file->sharing;
120 existing_access |= file->access;
122 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error;
123 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
124 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
125 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
126 return 1;
127 error:
128 set_error( STATUS_SHARING_VIOLATION );
129 return 0;
132 /* create a file from a file descriptor */
133 /* if the function fails the fd is closed */
134 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
135 unsigned int attrs, int drive_type )
137 struct file *file;
139 if ((file = alloc_object( &file_ops )))
141 file->name = NULL;
142 file->next = NULL;
143 file->access = access;
144 file->flags = attrs;
145 file->sharing = sharing;
146 file->drive_type = drive_type;
147 if (file->flags & FILE_FLAG_OVERLAPPED)
149 init_async_queue (&file->read_q);
150 init_async_queue (&file->write_q);
152 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
154 release_object( file );
155 return NULL;
158 return file;
162 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
163 unsigned int sharing, int create, unsigned int attrs,
164 int drive_type )
166 struct file *file;
167 int hash, flags;
168 char *name;
169 mode_t mode;
171 if (!(name = mem_alloc( len + 1 ))) return NULL;
172 memcpy( name, nameptr, len );
173 name[len] = 0;
175 /* check sharing mode */
176 hash = get_name_hash( name );
177 if (!check_sharing( name, hash, access, sharing )) goto error;
179 switch(create)
181 case CREATE_NEW: flags = O_CREAT | O_EXCL; break;
182 case CREATE_ALWAYS: flags = O_CREAT | O_TRUNC; break;
183 case OPEN_ALWAYS: flags = O_CREAT; break;
184 case TRUNCATE_EXISTING: flags = O_TRUNC; break;
185 case OPEN_EXISTING: flags = 0; break;
186 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
188 switch(access & (GENERIC_READ | GENERIC_WRITE))
190 case 0: break;
191 case GENERIC_READ: flags |= O_RDONLY; break;
192 case GENERIC_WRITE: flags |= O_WRONLY; break;
193 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
195 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
197 if (len >= 4 &&
198 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
199 mode |= 0111;
201 if (!(file = alloc_object( &file_ops ))) goto error;
203 file->access = access;
204 file->flags = attrs;
205 file->sharing = sharing;
206 file->drive_type = drive_type;
207 file->name = name;
208 file->next = file_hash[hash];
209 file_hash[hash] = file;
210 if (file->flags & FILE_FLAG_OVERLAPPED)
212 init_async_queue (&file->read_q);
213 init_async_queue (&file->write_q);
216 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
217 if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) ||
218 !(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE, &mode )))
220 release_object( file );
221 return NULL;
223 /* refuse to open a directory */
224 if (S_ISDIR(mode) && !(file->flags & FILE_FLAG_BACKUP_SEMANTICS))
226 set_error( STATUS_ACCESS_DENIED );
227 release_object( file );
228 return NULL;
230 return file;
232 error:
233 free( name );
234 return NULL;
237 /* check if two file objects point to the same file */
238 int is_same_file( struct file *file1, struct file *file2 )
240 return !strcmp( file1->name, file2->name );
243 /* get the type of drive the file is on */
244 int get_file_drive_type( struct file *file )
246 return file->drive_type;
249 /* create a temp file for anonymous mappings */
250 struct file *create_temp_file( int access )
252 char tmpfn[16];
253 int fd;
255 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
256 fd = mkstemps( tmpfn, 0 );
257 if (fd == -1)
259 file_set_error();
260 return NULL;
262 unlink( tmpfn );
263 return create_file_for_fd( fd, access, 0, 0, DRIVE_FIXED );
266 static void file_dump( struct object *obj, int verbose )
268 struct file *file = (struct file *)obj;
269 assert( obj->ops == &file_ops );
270 fprintf( stderr, "File fd=%p flags=%08x name='%s'\n", file->fd, file->flags, file->name );
273 static int file_get_poll_events( struct fd *fd )
275 struct file *file = get_fd_user( fd );
276 int events = 0;
277 assert( file->obj.ops == &file_ops );
278 if (file->access & GENERIC_READ) events |= POLLIN;
279 if (file->access & GENERIC_WRITE) events |= POLLOUT;
280 return events;
283 static void file_poll_event( struct fd *fd, int event )
285 struct file *file = get_fd_user( fd );
286 assert( file->obj.ops == &file_ops );
287 if ( file->flags & FILE_FLAG_OVERLAPPED )
289 if( IS_READY(file->read_q) && (POLLIN & event) )
291 async_notify(file->read_q.head, STATUS_ALERTED);
292 return;
294 if( IS_READY(file->write_q) && (POLLOUT & event) )
296 async_notify(file->write_q.head, STATUS_ALERTED);
297 return;
300 default_poll_event( fd, event );
304 static int file_flush( struct fd *fd, struct event **event )
306 int ret = (fsync( get_unix_fd(fd) ) != -1);
307 if (!ret) file_set_error();
308 return ret;
311 static int file_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
313 struct stat st;
314 struct file *file = get_fd_user( fd );
315 int unix_fd = get_unix_fd( fd );
317 if (reply)
319 if (fstat( unix_fd, &st ) == -1)
321 file_set_error();
322 return FD_TYPE_INVALID;
324 if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
325 S_ISSOCK(st.st_mode) || isatty(unix_fd)) reply->type = FILE_TYPE_CHAR;
326 else reply->type = FILE_TYPE_DISK;
327 if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
328 else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
329 if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
330 reply->access_time = st.st_atime;
331 reply->write_time = st.st_mtime;
332 reply->change_time = st.st_ctime;
333 if (S_ISDIR(st.st_mode))
335 reply->size_high = 0;
336 reply->size_low = 0;
337 reply->alloc_high = 0;
338 reply->alloc_low = 0;
340 else
342 file_pos_t alloc;
343 reply->size_high = st.st_size >> 32;
344 reply->size_low = st.st_size & 0xffffffff;
345 alloc = (file_pos_t)st.st_blksize * st.st_blocks;
346 reply->alloc_high = alloc >> 32;
347 reply->alloc_low = alloc & 0xffffffff;
349 reply->links = st.st_nlink;
350 reply->index_high = st.st_dev;
351 reply->index_low = st.st_ino;
352 reply->serial = 0; /* FIXME */
354 *flags = 0;
355 if (file->flags & FILE_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
356 return FD_TYPE_DEFAULT;
359 static void file_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
361 struct file *file = get_fd_user( fd );
362 struct async *async;
363 struct async_queue *q;
365 assert( file->obj.ops == &file_ops );
367 if ( !(file->flags & FILE_FLAG_OVERLAPPED) )
369 set_error ( STATUS_INVALID_HANDLE );
370 return;
373 switch(type)
375 case ASYNC_TYPE_READ:
376 q = &file->read_q;
377 break;
378 case ASYNC_TYPE_WRITE:
379 q = &file->write_q;
380 break;
381 default:
382 set_error( STATUS_INVALID_PARAMETER );
383 return;
386 async = find_async ( q, current, ptr );
388 if ( status == STATUS_PENDING )
390 int events;
392 if ( !async )
393 async = create_async ( &file->obj, current, ptr );
394 if ( !async )
395 return;
397 async->status = STATUS_PENDING;
398 if ( !async->q )
399 async_insert( q, async );
401 /* Check if the new pending request can be served immediately */
402 events = check_fd_events( fd, file_get_poll_events( fd ) );
403 if (events) file_poll_event ( fd, events );
405 else if ( async ) destroy_async ( async );
406 else set_error ( STATUS_INVALID_PARAMETER );
408 set_fd_events( fd, file_get_poll_events( fd ));
411 static struct fd *file_get_fd( struct object *obj )
413 struct file *file = (struct file *)obj;
414 assert( obj->ops == &file_ops );
415 return (struct fd *)grab_object( file->fd );
418 static void file_destroy( struct object *obj )
420 struct file *file = (struct file *)obj;
421 assert( obj->ops == &file_ops );
423 if (file->name)
425 /* remove it from the hashing list */
426 struct file **pptr = &file_hash[get_name_hash( file->name )];
427 while (*pptr && *pptr != file) pptr = &(*pptr)->next;
428 assert( *pptr );
429 *pptr = (*pptr)->next;
430 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
431 free( file->name );
433 if (file->flags & FILE_FLAG_OVERLAPPED)
435 destroy_async_queue (&file->read_q);
436 destroy_async_queue (&file->write_q);
438 if (file->fd) release_object( file->fd );
441 /* set the last error depending on errno */
442 void file_set_error(void)
444 switch (errno)
446 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
447 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
448 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
449 case EACCES:
450 case ESRCH:
451 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
452 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
453 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
454 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
455 case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
456 case ENFILE:
457 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
458 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
459 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
460 case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
461 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
462 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
463 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
464 default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
468 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
470 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
473 int get_file_unix_fd( struct file *file )
475 return get_unix_fd( file->fd );
478 static int set_file_pointer( obj_handle_t handle, unsigned int *low, int *high, int whence )
480 struct file *file;
481 off_t result,xto;
483 xto = *low+((off_t)*high<<32);
484 if (!(file = get_file_obj( current->process, handle, 0 )))
485 return 0;
486 if ((result = lseek( get_file_unix_fd(file), xto, whence))==-1)
488 /* Check for seek before start of file */
490 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
491 if (((errno == EINVAL) || (errno == EPERM))
492 && (whence != SEEK_SET) && (*high < 0))
493 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
494 else
495 file_set_error();
496 release_object( file );
497 return 0;
499 *low = result & 0xffffffff;
500 *high = result >> 32;
501 release_object( file );
502 return 1;
505 /* extend a file beyond the current end of file */
506 static int extend_file( struct file *file, off_t size )
508 static const char zero;
509 int unix_fd = get_file_unix_fd( file );
511 /* extend the file one byte beyond the requested size and then truncate it */
512 /* this should work around ftruncate implementations that can't extend files */
513 if ((lseek( unix_fd, size, SEEK_SET ) != -1) &&
514 (write( unix_fd, &zero, 1 ) != -1))
516 ftruncate( unix_fd, size );
517 return 1;
519 file_set_error();
520 return 0;
523 /* truncate file at current position */
524 static int truncate_file( struct file *file )
526 int ret = 0;
527 int unix_fd = get_file_unix_fd( file );
528 off_t pos = lseek( unix_fd, 0, SEEK_CUR );
529 off_t eof = lseek( unix_fd, 0, SEEK_END );
531 if (eof < pos) ret = extend_file( file, pos );
532 else
534 if (ftruncate( unix_fd, pos ) != -1) ret = 1;
535 else file_set_error();
537 lseek( unix_fd, pos, SEEK_SET ); /* restore file pos */
538 return ret;
541 /* try to grow the file to the specified size */
542 int grow_file( struct file *file, int size_high, int size_low )
544 int ret = 0;
545 struct stat st;
546 int unix_fd = get_file_unix_fd( file );
547 off_t old_pos, size = size_low + (((off_t)size_high)<<32);
549 if (fstat( unix_fd, &st ) == -1)
551 file_set_error();
552 return 0;
554 if (st.st_size >= size) return 1; /* already large enough */
555 old_pos = lseek( unix_fd, 0, SEEK_CUR ); /* save old pos */
556 ret = extend_file( file, size );
557 lseek( unix_fd, old_pos, SEEK_SET ); /* restore file pos */
558 return ret;
561 static int set_file_time( obj_handle_t handle, time_t access_time, time_t write_time )
563 struct file *file;
564 struct utimbuf utimbuf;
566 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
567 return 0;
568 if (!file->name)
570 set_error( STATUS_INVALID_HANDLE );
571 release_object( file );
572 return 0;
574 if (!access_time || !write_time)
576 struct stat st;
577 if (stat( file->name, &st ) == -1) goto error;
578 if (!access_time) access_time = st.st_atime;
579 if (!write_time) write_time = st.st_mtime;
581 utimbuf.actime = access_time;
582 utimbuf.modtime = write_time;
583 if (utime( file->name, &utimbuf ) == -1) goto error;
584 release_object( file );
585 return 1;
586 error:
587 file_set_error();
588 release_object( file );
589 return 0;
592 /* create a file */
593 DECL_HANDLER(create_file)
595 struct file *file;
597 reply->handle = 0;
598 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
599 req->sharing, req->create, req->attrs, req->drive_type )))
601 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
602 release_object( file );
606 /* allocate a file handle for a Unix fd */
607 DECL_HANDLER(alloc_file_handle)
609 struct file *file;
610 int fd;
612 reply->handle = 0;
613 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
615 set_error( STATUS_INVALID_HANDLE );
616 return;
618 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE,
619 0, DRIVE_UNKNOWN )))
621 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
622 release_object( file );
626 /* set a file current position */
627 DECL_HANDLER(set_file_pointer)
629 int high = req->high;
630 int low = req->low;
631 set_file_pointer( req->handle, &low, &high, req->whence );
632 reply->new_low = low;
633 reply->new_high = high;
636 /* truncate (or extend) a file */
637 DECL_HANDLER(truncate_file)
639 struct file *file;
641 if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
643 truncate_file( file );
644 release_object( file );
648 /* set a file access and modification times */
649 DECL_HANDLER(set_file_time)
651 set_file_time( req->handle, req->access_time, req->write_time );
654 /* lock a region of a file */
655 DECL_HANDLER(lock_file)
657 struct file *file;
658 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
659 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
661 if ((file = get_file_obj( current->process, req->handle, 0 )))
663 reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
664 reply->overlapped = (file->flags & FILE_FLAG_OVERLAPPED) != 0;
665 release_object( file );
669 /* unlock a region of a file */
670 DECL_HANDLER(unlock_file)
672 struct file *file;
673 file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
674 file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
676 if ((file = get_file_obj( current->process, req->handle, 0 )))
678 unlock_fd( file->fd, offset, count );
679 release_object( file );