Fixed get_event_obj/get_file_obj to use the process parameter (thanks
[wine/multimedia.git] / server / file.c
blob5369c24c145f578926a02749e1eab76619c64150
1 /*
2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #ifdef HAVE_SYS_ERRNO_H
16 #include <sys/errno.h>
17 #endif
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <utime.h>
25 #include "winerror.h"
26 #include "winbase.h"
28 #include "handle.h"
29 #include "thread.h"
30 #include "request.h"
32 struct file
34 struct object obj; /* object header */
35 struct select_user select; /* select user */
36 struct file *next; /* next file in hashing list */
37 char *name; /* file name */
38 unsigned int access; /* file access (GENERIC_READ/WRITE) */
39 unsigned int flags; /* flags (FILE_FLAG_*) */
40 unsigned int sharing; /* file sharing mode */
43 #define NAME_HASH_SIZE 37
45 static struct file *file_hash[NAME_HASH_SIZE];
47 static void file_dump( struct object *obj, int verbose );
48 static int file_add_queue( struct object *obj, struct wait_queue_entry *entry );
49 static void file_remove_queue( struct object *obj, struct wait_queue_entry *entry );
50 static int file_signaled( struct object *obj, struct thread *thread );
51 static int file_get_read_fd( struct object *obj );
52 static int file_get_write_fd( struct object *obj );
53 static int file_flush( struct object *obj );
54 static int file_get_info( struct object *obj, struct get_file_info_request *req );
55 static void file_destroy( struct object *obj );
57 static const struct object_ops file_ops =
59 sizeof(struct file),
60 file_dump,
61 file_add_queue,
62 file_remove_queue,
63 file_signaled,
64 no_satisfied,
65 file_get_read_fd,
66 file_get_write_fd,
67 file_flush,
68 file_get_info,
69 file_destroy
73 static int get_name_hash( const char *name )
75 int hash = 0;
76 while (*name) hash ^= (unsigned char)*name++;
77 return hash % NAME_HASH_SIZE;
80 /* check if the desired access is possible without violating */
81 /* the sharing mode of other opens of the same file */
82 static int check_sharing( const char *name, int hash, unsigned int access,
83 unsigned int sharing )
85 struct file *file;
86 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
87 unsigned int existing_access = 0;
89 for (file = file_hash[hash]; file; file = file->next)
91 if (strcmp( file->name, name )) continue;
92 existing_sharing &= file->sharing;
93 existing_access |= file->access;
95 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error;
96 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
97 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
98 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
99 return 1;
100 error:
101 set_error( ERROR_SHARING_VIOLATION );
102 return 0;
105 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
106 unsigned int attrs )
108 struct file *file;
109 if ((file = alloc_object( &file_ops )))
111 file->name = NULL;
112 file->next = NULL;
113 file->select.fd = fd;
114 file->select.func = default_select_event;
115 file->select.private = file;
116 file->access = access;
117 file->flags = attrs;
118 file->sharing = sharing;
119 register_select_user( &file->select );
121 return file;
125 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
126 unsigned int sharing, int create, unsigned int attrs )
128 struct file *file;
129 int hash, flags;
130 struct stat st;
131 char *name;
132 int fd = -1;
134 if (!(name = mem_alloc( len + 1 ))) return NULL;
135 memcpy( name, nameptr, len );
136 name[len] = 0;
138 /* check sharing mode */
139 hash = get_name_hash( name );
140 if (!check_sharing( name, hash, access, sharing )) goto error;
142 switch(create)
144 case CREATE_NEW: flags = O_CREAT | O_EXCL; break;
145 case CREATE_ALWAYS: flags = O_CREAT | O_TRUNC; break;
146 case OPEN_ALWAYS: flags = O_CREAT; break;
147 case TRUNCATE_EXISTING: flags = O_TRUNC; break;
148 case OPEN_EXISTING: flags = 0; break;
149 default: set_error( ERROR_INVALID_PARAMETER ); goto error;
151 switch(access & (GENERIC_READ | GENERIC_WRITE))
153 case 0: break;
154 case GENERIC_READ: flags |= O_RDONLY; break;
155 case GENERIC_WRITE: flags |= O_WRONLY; break;
156 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
159 /* FIXME: should set error to ERROR_ALREADY_EXISTS if file existed before */
160 if ((fd = open( name, flags | O_NONBLOCK,
161 (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666 )) == -1)
162 goto file_error;
163 /* refuse to open a directory */
164 if (fstat( fd, &st ) == -1) goto file_error;
165 if (S_ISDIR(st.st_mode))
167 set_error( ERROR_ACCESS_DENIED );
168 goto error;
171 if (!(file = create_file_for_fd( fd, access, sharing, attrs ))) goto error;
172 file->name = name;
173 file->next = file_hash[hash];
174 file_hash[hash] = file;
175 return file;
177 file_error:
178 file_set_error();
179 error:
180 if (fd != -1) close( fd );
181 free( name );
182 return NULL;
185 /* Create an anonymous Unix file */
186 int create_anonymous_file(void)
188 char *name;
189 int fd;
193 if (!(name = tmpnam(NULL)))
195 set_error( ERROR_TOO_MANY_OPEN_FILES );
196 return -1;
198 fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
199 } while ((fd == -1) && (errno == EEXIST));
200 if (fd == -1)
202 file_set_error();
203 return -1;
205 unlink( name );
206 return fd;
209 /* Create a temp file for anonymous mappings */
210 struct file *create_temp_file( int access )
212 struct file *file;
213 int fd;
215 if ((fd = create_anonymous_file()) == -1) return NULL;
216 if (!(file = create_file_for_fd( fd, access, 0, 0 ))) close( fd );
217 return file;
220 static void file_dump( struct object *obj, int verbose )
222 struct file *file = (struct file *)obj;
223 assert( obj->ops == &file_ops );
224 fprintf( stderr, "File fd=%d flags=%08x name='%s'\n",
225 file->select.fd, file->flags, file->name );
228 static int file_add_queue( struct object *obj, struct wait_queue_entry *entry )
230 struct file *file = (struct file *)obj;
231 assert( obj->ops == &file_ops );
232 if (!obj->head) /* first on the queue */
234 int events = 0;
235 if (file->access & GENERIC_READ) events |= READ_EVENT;
236 if (file->access & GENERIC_WRITE) events |= WRITE_EVENT;
237 set_select_events( &file->select, events );
239 add_queue( obj, entry );
240 return 1;
243 static void file_remove_queue( struct object *obj, struct wait_queue_entry *entry )
245 struct file *file = (struct file *)grab_object(obj);
246 assert( obj->ops == &file_ops );
248 remove_queue( obj, entry );
249 if (!obj->head) /* last on the queue is gone */
250 set_select_events( &file->select, 0 );
251 release_object( obj );
254 static int file_signaled( struct object *obj, struct thread *thread )
256 int events = 0;
257 struct file *file = (struct file *)obj;
258 assert( obj->ops == &file_ops );
260 if (file->access & GENERIC_READ) events |= READ_EVENT;
261 if (file->access & GENERIC_WRITE) events |= WRITE_EVENT;
262 if (check_select_events( &file->select, events ))
264 /* stop waiting on select() if we are signaled */
265 set_select_events( &file->select, 0 );
266 return 1;
268 else
270 /* restart waiting on select() if we are no longer signaled */
271 if (obj->head) set_select_events( &file->select, events );
272 return 0;
276 static int file_get_read_fd( struct object *obj )
278 struct file *file = (struct file *)obj;
279 assert( obj->ops == &file_ops );
280 return dup( file->select.fd );
283 static int file_get_write_fd( struct object *obj )
285 struct file *file = (struct file *)obj;
286 assert( obj->ops == &file_ops );
287 return dup( file->select.fd );
290 static int file_flush( struct object *obj )
292 int ret;
293 struct file *file = (struct file *)grab_object(obj);
294 assert( obj->ops == &file_ops );
296 ret = (fsync( file->select.fd ) != -1);
297 if (!ret) file_set_error();
298 release_object( file );
299 return ret;
302 static int file_get_info( struct object *obj, struct get_file_info_request *req )
304 struct stat st;
305 struct file *file = (struct file *)obj;
306 assert( obj->ops == &file_ops );
308 if (fstat( file->select.fd, &st ) == -1)
310 file_set_error();
311 return 0;
313 if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
314 S_ISSOCK(st.st_mode) || isatty(file->select.fd)) req->type = FILE_TYPE_CHAR;
315 else req->type = FILE_TYPE_DISK;
316 if (S_ISDIR(st.st_mode)) req->attr = FILE_ATTRIBUTE_DIRECTORY;
317 else req->attr = FILE_ATTRIBUTE_ARCHIVE;
318 if (!(st.st_mode & S_IWUSR)) req->attr |= FILE_ATTRIBUTE_READONLY;
319 req->access_time = st.st_atime;
320 req->write_time = st.st_mtime;
321 req->size_high = 0;
322 req->size_low = S_ISDIR(st.st_mode) ? 0 : st.st_size;
323 req->links = st.st_nlink;
324 req->index_high = st.st_dev;
325 req->index_low = st.st_ino;
326 req->serial = 0; /* FIXME */
327 return 1;
330 static void file_destroy( struct object *obj )
332 struct file *file = (struct file *)obj;
333 assert( obj->ops == &file_ops );
335 if (file->name)
337 /* remove it from the hashing list */
338 struct file **pptr = &file_hash[get_name_hash( file->name )];
339 while (*pptr && *pptr != file) pptr = &(*pptr)->next;
340 assert( *pptr );
341 *pptr = (*pptr)->next;
342 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
343 free( file->name );
345 unregister_select_user( &file->select );
346 close( file->select.fd );
349 /* set the last error depending on errno */
350 void file_set_error(void)
352 switch (errno)
354 case EAGAIN: set_error( ERROR_SHARING_VIOLATION ); break;
355 case EBADF: set_error( ERROR_INVALID_HANDLE ); break;
356 case ENOSPC: set_error( ERROR_HANDLE_DISK_FULL ); break;
357 case EACCES:
358 case EPERM: set_error( ERROR_ACCESS_DENIED ); break;
359 case EROFS: set_error( ERROR_WRITE_PROTECT ); break;
360 case EBUSY: set_error( ERROR_LOCK_VIOLATION ); break;
361 case ENOENT: set_error( ERROR_FILE_NOT_FOUND ); break;
362 case EISDIR: set_error( ERROR_CANNOT_MAKE ); break;
363 case ENFILE:
364 case EMFILE: set_error( ERROR_NO_MORE_FILES ); break;
365 case EEXIST: set_error( ERROR_FILE_EXISTS ); break;
366 case EINVAL: set_error( ERROR_INVALID_PARAMETER ); break;
367 case ESPIPE: set_error( ERROR_SEEK ); break;
368 case ENOTEMPTY: set_error( ERROR_DIR_NOT_EMPTY ); break;
369 case EIO: set_error( ERROR_NOACCESS ); break;
370 default: perror("file_set_error"); set_error( ERROR_UNKNOWN ); break;
374 struct file *get_file_obj( struct process *process, int handle, unsigned int access )
376 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
379 int file_get_mmap_fd( struct file *file )
381 return dup( file->select.fd );
384 static int set_file_pointer( int handle, int *low, int *high, int whence )
386 struct file *file;
387 int result;
389 if (*high)
391 fprintf( stderr, "set_file_pointer: offset > 4Gb not supported yet\n" );
392 set_error( ERROR_INVALID_PARAMETER );
393 return 0;
396 if (!(file = get_file_obj( current->process, handle, 0 )))
397 return 0;
398 if ((result = lseek( file->select.fd, *low, whence )) == -1)
400 /* Check for seek before start of file */
401 if ((errno == EINVAL) && (whence != SEEK_SET) && (*low < 0))
402 set_error( ERROR_NEGATIVE_SEEK );
403 else
404 file_set_error();
405 release_object( file );
406 return 0;
408 *low = result;
409 release_object( file );
410 return 1;
413 static int truncate_file( int handle )
415 struct file *file;
416 int result;
418 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
419 return 0;
420 if (((result = lseek( file->select.fd, 0, SEEK_CUR )) == -1) ||
421 (ftruncate( file->select.fd, result ) == -1))
423 file_set_error();
424 release_object( file );
425 return 0;
427 release_object( file );
428 return 1;
432 /* try to grow the file to the specified size */
433 int grow_file( struct file *file, int size_high, int size_low )
435 struct stat st;
437 if (size_high)
439 set_error( ERROR_INVALID_PARAMETER );
440 return 0;
442 if (fstat( file->select.fd, &st ) == -1)
444 file_set_error();
445 return 0;
447 if (st.st_size >= size_low) return 1; /* already large enough */
448 if (ftruncate( file->select.fd, size_low ) != -1) return 1;
449 file_set_error();
450 return 0;
453 static int set_file_time( int handle, time_t access_time, time_t write_time )
455 struct file *file;
456 struct utimbuf utimbuf;
458 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
459 return 0;
460 if (!access_time || !write_time)
462 struct stat st;
463 if (stat( file->name, &st ) == -1) goto error;
464 if (!access_time) access_time = st.st_atime;
465 if (!write_time) write_time = st.st_mtime;
467 utimbuf.actime = access_time;
468 utimbuf.modtime = write_time;
469 if (utime( file->name, &utimbuf ) == -1) goto error;
470 release_object( file );
471 return 1;
472 error:
473 file_set_error();
474 release_object( file );
475 return 0;
478 static int file_lock( struct file *file, int offset_high, int offset_low,
479 int count_high, int count_low )
481 /* FIXME: implement this */
482 return 1;
485 static int file_unlock( struct file *file, int offset_high, int offset_low,
486 int count_high, int count_low )
488 /* FIXME: implement this */
489 return 1;
492 /* create a file */
493 DECL_HANDLER(create_file)
495 size_t len = get_req_strlen( req->name );
496 struct file *file;
498 req->handle = -1;
499 if ((file = create_file( req->name, len, req->access,
500 req->sharing, req->create, req->attrs )))
502 req->handle = alloc_handle( current->process, file, req->access, req->inherit );
503 release_object( file );
507 /* allocate a file handle for a Unix fd */
508 DECL_HANDLER(alloc_file_handle)
510 struct file *file;
512 req->handle = -1;
513 if ((fd = dup(fd)) != -1)
515 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE, 0 )))
517 req->handle = alloc_handle( current->process, file, req->access, 0 );
518 release_object( file );
520 else close( fd );
522 else file_set_error();
525 /* get a Unix fd to read from a file */
526 DECL_HANDLER(get_read_fd)
528 struct object *obj;
530 if ((obj = get_handle_obj( current->process, req->handle, GENERIC_READ, NULL )))
532 set_reply_fd( current, obj->ops->get_read_fd( obj ) );
533 release_object( obj );
537 /* get a Unix fd to write to a file */
538 DECL_HANDLER(get_write_fd)
540 struct object *obj;
542 if ((obj = get_handle_obj( current->process, req->handle, GENERIC_WRITE, NULL )))
544 set_reply_fd( current, obj->ops->get_write_fd( obj ) );
545 release_object( obj );
549 /* set a file current position */
550 DECL_HANDLER(set_file_pointer)
552 int high = req->high;
553 int low = req->low;
554 set_file_pointer( req->handle, &low, &high, req->whence );
555 req->new_low = low;
556 req->new_high = high;
559 /* truncate (or extend) a file */
560 DECL_HANDLER(truncate_file)
562 truncate_file( req->handle );
565 /* flush a file buffers */
566 DECL_HANDLER(flush_file)
568 struct object *obj;
570 if ((obj = get_handle_obj( current->process, req->handle, GENERIC_WRITE, NULL )))
572 obj->ops->flush( obj );
573 release_object( obj );
577 /* set a file access and modification times */
578 DECL_HANDLER(set_file_time)
580 set_file_time( req->handle, req->access_time, req->write_time );
583 /* get a file information */
584 DECL_HANDLER(get_file_info)
586 struct object *obj;
588 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
590 obj->ops->get_file_info( obj, req );
591 release_object( obj );
595 /* lock a region of a file */
596 DECL_HANDLER(lock_file)
598 struct file *file;
600 if ((file = get_file_obj( current->process, req->handle, 0 )))
602 file_lock( file, req->offset_high, req->offset_low,
603 req->count_high, req->count_low );
604 release_object( file );
608 /* unlock a region of a file */
609 DECL_HANDLER(unlock_file)
611 struct file *file;
613 if ((file = get_file_obj( current->process, req->handle, 0 )))
615 file_unlock( file, req->offset_high, req->offset_low,
616 req->count_high, req->count_low );
617 release_object( file );