Move the name directive from the .spec file to the Makefile.
[wine/gsoc_dplay.git] / server / file.c
blobb302fe67f1b85bf5e539918353098113224793ae
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 #include <utime.h>
40 #include "winerror.h"
41 #include "winbase.h"
43 #include "handle.h"
44 #include "thread.h"
45 #include "request.h"
46 #include "async.h"
48 struct file
50 struct object obj; /* object header */
51 struct file *next; /* next file in hashing list */
52 char *name; /* file name */
53 unsigned int access; /* file access (GENERIC_READ/WRITE) */
54 unsigned int flags; /* flags (FILE_FLAG_*) */
55 unsigned int sharing; /* file sharing mode */
56 int drive_type; /* type of drive the file is on */
57 struct async_queue read_q;
58 struct async_queue write_q;
61 #define NAME_HASH_SIZE 37
63 static struct file *file_hash[NAME_HASH_SIZE];
65 static void file_dump( struct object *obj, int verbose );
66 static int file_get_poll_events( struct object *obj );
67 static void file_poll_event( struct object *obj, int event );
68 static int file_get_fd( struct object *obj );
69 static int file_flush( struct object *obj );
70 static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
71 static void file_destroy( struct object *obj );
72 static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
74 static const struct object_ops file_ops =
76 sizeof(struct file), /* size */
77 file_dump, /* dump */
78 default_poll_add_queue, /* add_queue */
79 default_poll_remove_queue, /* remove_queue */
80 default_poll_signaled, /* signaled */
81 no_satisfied, /* satisfied */
82 file_get_poll_events, /* get_poll_events */
83 file_poll_event, /* poll_event */
84 file_get_fd, /* get_fd */
85 file_flush, /* flush */
86 file_get_info, /* get_file_info */
87 file_queue_async, /* queue_async */
88 file_destroy /* destroy */
92 static int get_name_hash( const char *name )
94 int hash = 0;
95 while (*name) hash ^= (unsigned char)*name++;
96 return hash % NAME_HASH_SIZE;
99 /* check if the desired access is possible without violating */
100 /* the sharing mode of other opens of the same file */
101 static int check_sharing( const char *name, int hash, unsigned int access,
102 unsigned int sharing )
104 struct file *file;
105 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
106 unsigned int existing_access = 0;
108 for (file = file_hash[hash]; file; file = file->next)
110 if (strcmp( file->name, name )) continue;
111 existing_sharing &= file->sharing;
112 existing_access |= file->access;
114 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error;
115 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
116 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
117 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
118 return 1;
119 error:
120 set_error( STATUS_SHARING_VIOLATION );
121 return 0;
124 /* create a file from a file descriptor */
125 /* if the function fails the fd is closed */
126 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
127 unsigned int attrs, int drive_type )
129 struct file *file;
130 if ((file = alloc_object( &file_ops, fd )))
132 file->name = NULL;
133 file->next = NULL;
134 file->access = access;
135 file->flags = attrs;
136 file->sharing = sharing;
137 file->drive_type = drive_type;
138 if (file->flags & FILE_FLAG_OVERLAPPED)
140 init_async_queue (&file->read_q);
141 init_async_queue (&file->write_q);
144 return file;
148 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
149 unsigned int sharing, int create, unsigned int attrs,
150 int drive_type )
152 struct file *file;
153 int hash, flags;
154 struct stat st;
155 char *name;
156 int fd = -1;
157 mode_t mode;
159 if (!(name = mem_alloc( len + 1 ))) return NULL;
160 memcpy( name, nameptr, len );
161 name[len] = 0;
163 /* check sharing mode */
164 hash = get_name_hash( name );
165 if (!check_sharing( name, hash, access, sharing )) goto error;
167 switch(create)
169 case CREATE_NEW: flags = O_CREAT | O_EXCL; break;
170 case CREATE_ALWAYS: flags = O_CREAT | O_TRUNC; break;
171 case OPEN_ALWAYS: flags = O_CREAT; break;
172 case TRUNCATE_EXISTING: flags = O_TRUNC; break;
173 case OPEN_EXISTING: flags = 0; break;
174 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
176 switch(access & (GENERIC_READ | GENERIC_WRITE))
178 case 0: break;
179 case GENERIC_READ: flags |= O_RDONLY; break;
180 case GENERIC_WRITE: flags |= O_WRONLY; break;
181 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
183 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
185 if (len >= 4 &&
186 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
187 mode |= 0111;
189 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
190 if ((fd = open( name, flags | O_NONBLOCK | O_LARGEFILE, mode )) == -1 )
191 goto file_error;
192 /* refuse to open a directory */
193 if (fstat( fd, &st ) == -1) goto file_error;
194 if (S_ISDIR(st.st_mode))
196 set_error( STATUS_ACCESS_DENIED );
197 goto error;
200 if (!(file = create_file_for_fd( fd, access, sharing, attrs, drive_type )))
202 free( name );
203 return NULL;
205 file->name = name;
206 file->next = file_hash[hash];
207 file_hash[hash] = file;
208 return file;
210 file_error:
211 file_set_error();
212 error:
213 if (fd != -1) close( fd );
214 free( name );
215 return NULL;
218 /* check if two file objects point to the same file */
219 int is_same_file( struct file *file1, struct file *file2 )
221 return !strcmp( file1->name, file2->name );
224 /* get the type of drive the file is on */
225 int get_file_drive_type( struct file *file )
227 return file->drive_type;
230 /* Create an anonymous Unix file */
231 int create_anonymous_file(void)
233 char tmpfn[21];
234 int fd;
236 sprintf(tmpfn,"/tmp/anonmap.XXXXXX");
237 fd = mkstemp(tmpfn);
238 if (fd == -1)
240 file_set_error();
241 return -1;
243 unlink( tmpfn );
244 return fd;
247 /* Create a temp file for anonymous mappings */
248 struct file *create_temp_file( int access )
250 int fd;
252 if ((fd = create_anonymous_file()) == -1) return NULL;
253 return create_file_for_fd( fd, access, 0, 0, DRIVE_FIXED );
256 static void file_dump( struct object *obj, int verbose )
258 struct file *file = (struct file *)obj;
259 assert( obj->ops == &file_ops );
260 fprintf( stderr, "File fd=%d flags=%08x name='%s'\n", file->obj.fd, file->flags, file->name );
263 static int file_get_poll_events( struct object *obj )
265 struct file *file = (struct file *)obj;
266 int events = 0;
267 assert( obj->ops == &file_ops );
268 if (file->access & GENERIC_READ) events |= POLLIN;
269 if (file->access & GENERIC_WRITE) events |= POLLOUT;
270 return events;
273 static void file_poll_event( struct object *obj, int event )
275 struct file *file = (struct file *)obj;
276 assert( obj->ops == &file_ops );
277 if ( file->flags & FILE_FLAG_OVERLAPPED )
279 if( IS_READY(file->read_q) && (POLLIN & event) )
281 async_notify(file->read_q.head, STATUS_ALERTED);
282 return;
284 if( IS_READY(file->write_q) && (POLLOUT & event) )
286 async_notify(file->write_q.head, STATUS_ALERTED);
287 return;
290 default_poll_event( obj, event );
294 static int file_get_fd( struct object *obj )
296 struct file *file = (struct file *)obj;
297 assert( obj->ops == &file_ops );
298 return file->obj.fd;
301 static int file_flush( struct object *obj )
303 int ret;
304 struct file *file = (struct file *)grab_object(obj);
305 assert( obj->ops == &file_ops );
307 ret = (fsync( file->obj.fd ) != -1);
308 if (!ret) file_set_error();
309 release_object( file );
310 return ret;
313 static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
315 struct stat st;
316 struct file *file = (struct file *)obj;
317 assert( obj->ops == &file_ops );
319 if (reply)
321 if (fstat( file->obj.fd, &st ) == -1)
323 file_set_error();
324 return FD_TYPE_INVALID;
326 if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
327 S_ISSOCK(st.st_mode) || isatty(file->obj.fd)) reply->type = FILE_TYPE_CHAR;
328 else reply->type = FILE_TYPE_DISK;
329 if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
330 else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
331 if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
332 reply->access_time = st.st_atime;
333 reply->write_time = st.st_mtime;
334 if (S_ISDIR(st.st_mode))
336 reply->size_high = 0;
337 reply->size_low = 0;
339 else
341 reply->size_high = st.st_size >> 32;
342 reply->size_low = st.st_size & 0xffffffff;
344 reply->links = st.st_nlink;
345 reply->index_high = st.st_dev;
346 reply->index_low = st.st_ino;
347 reply->serial = 0; /* FIXME */
349 *flags = 0;
350 if (file->flags & FILE_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
351 return FD_TYPE_DEFAULT;
354 static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
356 struct file *file = (struct file *)obj;
357 struct async *async;
358 struct async_queue *q;
360 assert( obj->ops == &file_ops );
362 if ( !(file->flags & FILE_FLAG_OVERLAPPED) )
364 set_error ( STATUS_INVALID_HANDLE );
365 return;
368 switch(type)
370 case ASYNC_TYPE_READ:
371 q = &file->read_q;
372 break;
373 case ASYNC_TYPE_WRITE:
374 q = &file->write_q;
375 break;
376 default:
377 set_error( STATUS_INVALID_PARAMETER );
378 return;
381 async = find_async ( q, current, ptr );
383 if ( status == STATUS_PENDING )
385 struct pollfd pfd;
387 if ( !async )
388 async = create_async ( obj, current, ptr );
389 if ( !async )
390 return;
392 async->status = STATUS_PENDING;
393 if ( !async->q )
394 async_insert( q, async );
396 /* Check if the new pending request can be served immediately */
397 pfd.fd = obj->fd;
398 pfd.events = file_get_poll_events ( obj );
399 pfd.revents = 0;
400 poll ( &pfd, 1, 0 );
402 if ( pfd.revents )
403 file_poll_event ( obj, pfd.revents );
405 else if ( async ) destroy_async ( async );
406 else set_error ( STATUS_INVALID_PARAMETER );
408 set_select_events ( obj, file_get_poll_events ( obj ));
411 static void file_destroy( struct object *obj )
413 struct file *file = (struct file *)obj;
414 assert( obj->ops == &file_ops );
416 if (file->name)
418 /* remove it from the hashing list */
419 struct file **pptr = &file_hash[get_name_hash( file->name )];
420 while (*pptr && *pptr != file) pptr = &(*pptr)->next;
421 assert( *pptr );
422 *pptr = (*pptr)->next;
423 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
424 free( file->name );
426 if (file->flags & FILE_FLAG_OVERLAPPED)
428 destroy_async_queue (&file->read_q);
429 destroy_async_queue (&file->write_q);
433 /* set the last error depending on errno */
434 void file_set_error(void)
436 switch (errno)
438 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
439 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
440 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
441 case EACCES:
442 case ESRCH:
443 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
444 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
445 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
446 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
447 case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
448 case ENFILE:
449 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
450 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
451 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
452 case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
453 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
454 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
455 default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
459 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
461 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
464 static int set_file_pointer( obj_handle_t handle, unsigned int *low, int *high, int whence )
466 struct file *file;
467 off_t result,xto;
469 xto = *low+((off_t)*high<<32);
470 if (!(file = get_file_obj( current->process, handle, 0 )))
471 return 0;
472 if ((result = lseek(file->obj.fd,xto,whence))==-1)
474 /* Check for seek before start of file */
476 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
477 if (((errno == EINVAL) || (errno == EPERM))
478 && (whence != SEEK_SET) && (*high < 0))
479 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
480 else
481 file_set_error();
482 release_object( file );
483 return 0;
485 *low = result & 0xffffffff;
486 *high = result >> 32;
487 release_object( file );
488 return 1;
491 /* extend a file beyond the current end of file */
492 static int extend_file( struct file *file, off_t size )
494 static const char zero;
496 /* extend the file one byte beyond the requested size and then truncate it */
497 /* this should work around ftruncate implementations that can't extend files */
498 if ((lseek( file->obj.fd, size, SEEK_SET ) != -1) &&
499 (write( file->obj.fd, &zero, 1 ) != -1))
501 ftruncate( file->obj.fd, size );
502 return 1;
504 file_set_error();
505 return 0;
508 /* truncate file at current position */
509 static int truncate_file( struct file *file )
511 int ret = 0;
512 off_t pos = lseek( file->obj.fd, 0, SEEK_CUR );
513 off_t eof = lseek( file->obj.fd, 0, SEEK_END );
515 if (eof < pos) ret = extend_file( file, pos );
516 else
518 if (ftruncate( file->obj.fd, pos ) != -1) ret = 1;
519 else file_set_error();
521 lseek( file->obj.fd, pos, SEEK_SET ); /* restore file pos */
522 return ret;
525 /* try to grow the file to the specified size */
526 int grow_file( struct file *file, int size_high, int size_low )
528 int ret = 0;
529 struct stat st;
530 off_t old_pos, size = size_low + (((off_t)size_high)<<32);
532 if (fstat( file->obj.fd, &st ) == -1)
534 file_set_error();
535 return 0;
537 if (st.st_size >= size) return 1; /* already large enough */
538 old_pos = lseek( file->obj.fd, 0, SEEK_CUR ); /* save old pos */
539 ret = extend_file( file, size );
540 lseek( file->obj.fd, old_pos, SEEK_SET ); /* restore file pos */
541 return ret;
544 static int set_file_time( obj_handle_t handle, time_t access_time, time_t write_time )
546 struct file *file;
547 struct utimbuf utimbuf;
549 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
550 return 0;
551 if (!file->name)
553 set_error( STATUS_INVALID_HANDLE );
554 release_object( file );
555 return 0;
557 if (!access_time || !write_time)
559 struct stat st;
560 if (stat( file->name, &st ) == -1) goto error;
561 if (!access_time) access_time = st.st_atime;
562 if (!write_time) write_time = st.st_mtime;
564 utimbuf.actime = access_time;
565 utimbuf.modtime = write_time;
566 if (utime( file->name, &utimbuf ) == -1) goto error;
567 release_object( file );
568 return 1;
569 error:
570 file_set_error();
571 release_object( file );
572 return 0;
575 static int file_lock( struct file *file, int offset_high, int offset_low,
576 int count_high, int count_low )
578 /* FIXME: implement this */
579 return 1;
582 static int file_unlock( struct file *file, int offset_high, int offset_low,
583 int count_high, int count_low )
585 /* FIXME: implement this */
586 return 1;
589 /* create a file */
590 DECL_HANDLER(create_file)
592 struct file *file;
594 reply->handle = 0;
595 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
596 req->sharing, req->create, req->attrs, req->drive_type )))
598 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
599 release_object( file );
603 /* allocate a file handle for a Unix fd */
604 DECL_HANDLER(alloc_file_handle)
606 struct file *file;
607 int fd;
609 reply->handle = 0;
610 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
612 set_error( STATUS_INVALID_HANDLE );
613 return;
615 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE,
616 0, DRIVE_UNKNOWN )))
618 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
619 release_object( file );
623 /* get a Unix fd to access a file */
624 DECL_HANDLER(get_handle_fd)
626 struct object *obj;
628 reply->fd = -1;
629 reply->type = FD_TYPE_INVALID;
630 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
632 int fd = get_handle_fd( current->process, req->handle, req->access );
633 if (fd != -1) reply->fd = fd;
634 else if (!get_error())
636 if ((fd = obj->ops->get_fd( obj )) != -1)
637 send_client_fd( current->process, fd, req->handle );
639 reply->type = obj->ops->get_file_info( obj, NULL, &reply->flags );
640 release_object( obj );
644 /* set a file current position */
645 DECL_HANDLER(set_file_pointer)
647 int high = req->high;
648 int low = req->low;
649 set_file_pointer( req->handle, &low, &high, req->whence );
650 reply->new_low = low;
651 reply->new_high = high;
654 /* truncate (or extend) a file */
655 DECL_HANDLER(truncate_file)
657 struct file *file;
659 if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
661 truncate_file( file );
662 release_object( file );
666 /* flush a file buffers */
667 DECL_HANDLER(flush_file)
669 struct object *obj;
671 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
673 obj->ops->flush( obj );
674 release_object( obj );
678 /* set a file access and modification times */
679 DECL_HANDLER(set_file_time)
681 set_file_time( req->handle, req->access_time, req->write_time );
684 /* get a file information */
685 DECL_HANDLER(get_file_info)
687 struct object *obj;
689 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
691 int flags;
692 obj->ops->get_file_info( obj, reply, &flags );
693 release_object( obj );
697 /* lock a region of a file */
698 DECL_HANDLER(lock_file)
700 struct file *file;
702 if ((file = get_file_obj( current->process, req->handle, 0 )))
704 file_lock( file, req->offset_high, req->offset_low,
705 req->count_high, req->count_low );
706 release_object( file );
710 /* unlock a region of a file */
711 DECL_HANDLER(unlock_file)
713 struct file *file;
715 if ((file = get_file_obj( current->process, req->handle, 0 )))
717 file_unlock( file, req->offset_high, req->offset_low,
718 req->count_high, req->count_low );
719 release_object( file );