Skip queue cleanups if queue has been destroyed already.
[wine] / server / file.c
1 /*
2  * Server-side file management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8 #include "wine/port.h"
9
10 #include <assert.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #ifdef HAVE_SYS_ERRNO_H
17 #include <sys/errno.h>
18 #endif
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <utime.h>
25
26 #include "winerror.h"
27 #include "winbase.h"
28
29 #include "handle.h"
30 #include "thread.h"
31 #include "request.h"
32
33 struct file
34 {
35     struct object       obj;        /* object header */
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 */
41     int                 drive_type; /* type of drive the file is on */
42 };
43
44 #define NAME_HASH_SIZE 37
45
46 static struct file *file_hash[NAME_HASH_SIZE];
47
48 static void file_dump( struct object *obj, int verbose );
49 static int file_get_poll_events( struct object *obj );
50 static int file_get_fd( struct object *obj );
51 static int file_flush( struct object *obj );
52 static int file_get_info( struct object *obj, struct get_file_info_request *req );
53 static void file_destroy( struct object *obj );
54
55 static const struct object_ops file_ops =
56 {
57     sizeof(struct file),          /* size */
58     file_dump,                    /* dump */
59     default_poll_add_queue,       /* add_queue */
60     default_poll_remove_queue,    /* remove_queue */
61     default_poll_signaled,        /* signaled */
62     no_satisfied,                 /* satisfied */
63     file_get_poll_events,         /* get_poll_events */
64     default_poll_event,           /* poll_event */
65     file_get_fd,                  /* get_fd */
66     file_flush,                   /* flush */
67     file_get_info,                /* get_file_info */
68     file_destroy                  /* destroy */
69 };
70
71
72 static int get_name_hash( const char *name )
73 {
74     int hash = 0;
75     while (*name) hash ^= (unsigned char)*name++;
76     return hash % NAME_HASH_SIZE;
77 }
78
79 /* check if the desired access is possible without violating */
80 /* the sharing mode of other opens of the same file */
81 static int check_sharing( const char *name, int hash, unsigned int access,
82                           unsigned int sharing )
83 {
84     struct file *file;
85     unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
86     unsigned int existing_access = 0;
87
88     for (file = file_hash[hash]; file; file = file->next)
89     {
90         if (strcmp( file->name, name )) continue;
91         existing_sharing &= file->sharing;
92         existing_access |= file->access;
93     }
94     if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error;
95     if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
96     if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
97     if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
98     return 1;
99  error:
100     set_error( STATUS_SHARING_VIOLATION );
101     return 0;
102 }
103
104 /* create a file from a file descriptor */
105 /* if the function fails the fd is closed */
106 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
107                                         unsigned int attrs, int drive_type )
108 {
109     struct file *file;
110     if ((file = alloc_object( &file_ops, fd )))
111     {
112         file->name       = NULL;
113         file->next       = NULL;
114         file->access     = access;
115         file->flags      = attrs;
116         file->sharing    = sharing;
117         file->drive_type = drive_type;
118     }
119     return file;
120 }
121
122
123 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
124                                  unsigned int sharing, int create, unsigned int attrs,
125                                  int drive_type )
126 {
127     struct file *file;
128     int hash, flags;
129     struct stat st;
130     char *name;
131     int fd = -1;
132     mode_t mode;
133
134     if (!(name = mem_alloc( len + 1 ))) return NULL;
135     memcpy( name, nameptr, len );
136     name[len] = 0;
137
138     /* check sharing mode */
139     hash = get_name_hash( name );
140     if (!check_sharing( name, hash, access, sharing )) goto error;
141
142     switch(create)
143     {
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( STATUS_INVALID_PARAMETER ); goto error;
150     }
151     switch(access & (GENERIC_READ | GENERIC_WRITE))
152     {
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;
157     }
158     mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
159
160     if (len >= 4 &&
161         (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
162         mode |= 0111;
163
164     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
165     if ((fd = open( name, flags | O_NONBLOCK | O_LARGEFILE, mode )) == -1 )
166         goto file_error;
167     /* refuse to open a directory */
168     if (fstat( fd, &st ) == -1) goto file_error;
169     if (S_ISDIR(st.st_mode))
170     {
171         set_error( STATUS_ACCESS_DENIED );
172         goto error;
173     }
174
175     if (!(file = create_file_for_fd( fd, access, sharing, attrs, drive_type )))
176     {
177         free( name );
178         return NULL;
179     }
180     file->name = name;
181     file->next = file_hash[hash];
182     file_hash[hash] = file;
183     return file;
184
185  file_error:
186     file_set_error();
187  error:
188     if (fd != -1) close( fd );
189     free( name );
190     return NULL;
191 }
192
193 /* check if two file objects point to the same file */
194 int is_same_file( struct file *file1, struct file *file2 )
195 {
196     return !strcmp( file1->name, file2->name );
197 }
198
199 /* get the type of drive the file is on */
200 int get_file_drive_type( struct file *file )
201 {
202     return file->drive_type;
203 }
204
205 /* Create an anonymous Unix file */
206 int create_anonymous_file(void)
207 {
208     char *name;
209     int fd;
210
211     do
212     {
213         if (!(name = tmpnam(NULL)))
214         {
215             set_error( STATUS_TOO_MANY_OPENED_FILES );
216             return -1;
217         }
218         fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
219     } while ((fd == -1) && (errno == EEXIST));
220     if (fd == -1)
221     {
222         file_set_error();
223         return -1;
224     }
225     unlink( name );
226     return fd;
227 }
228
229 /* Create a temp file for anonymous mappings */
230 struct file *create_temp_file( int access )
231 {
232     int fd;
233
234     if ((fd = create_anonymous_file()) == -1) return NULL;
235     return create_file_for_fd( fd, access, 0, 0, DRIVE_FIXED );
236 }
237
238 static void file_dump( struct object *obj, int verbose )
239 {
240     struct file *file = (struct file *)obj;
241     assert( obj->ops == &file_ops );
242     fprintf( stderr, "File fd=%d flags=%08x name='%s'\n", file->obj.fd, file->flags, file->name );
243 }
244
245 static int file_get_poll_events( struct object *obj )
246 {
247     struct file *file = (struct file *)obj;
248     int events = 0;
249     assert( obj->ops == &file_ops );
250     if (file->access & GENERIC_READ) events |= POLLIN;
251     if (file->access & GENERIC_WRITE) events |= POLLOUT;
252     return events;
253 }
254
255 static int file_get_fd( struct object *obj )
256 {
257     struct file *file = (struct file *)obj;
258     assert( obj->ops == &file_ops );
259     return file->obj.fd;
260 }
261
262 static int file_flush( struct object *obj )
263 {
264     int ret;
265     struct file *file = (struct file *)grab_object(obj);
266     assert( obj->ops == &file_ops );
267
268     ret = (fsync( file->obj.fd ) != -1);
269     if (!ret) file_set_error();
270     release_object( file );
271     return ret;
272 }
273
274 static int file_get_info( struct object *obj, struct get_file_info_request *req )
275 {
276     struct stat st;
277     struct file *file = (struct file *)obj;
278     assert( obj->ops == &file_ops );
279
280     if (req)
281     {
282         if (fstat( file->obj.fd, &st ) == -1)
283         {
284             file_set_error();
285             return FD_TYPE_INVALID;
286         }
287         if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
288             S_ISSOCK(st.st_mode) || isatty(file->obj.fd)) req->type = FILE_TYPE_CHAR;
289         else req->type = FILE_TYPE_DISK;
290         if (S_ISDIR(st.st_mode)) req->attr = FILE_ATTRIBUTE_DIRECTORY;
291         else req->attr = FILE_ATTRIBUTE_ARCHIVE;
292         if (!(st.st_mode & S_IWUSR)) req->attr |= FILE_ATTRIBUTE_READONLY;
293         req->access_time = st.st_atime;
294         req->write_time  = st.st_mtime;
295         if (S_ISDIR(st.st_mode))
296         {
297             req->size_high = 0;
298             req->size_low  = 0;
299         }
300         else
301         {
302             req->size_high = st.st_size >> 32;
303             req->size_low  = st.st_size & 0xffffffff;
304         }
305         req->links       = st.st_nlink;
306         req->index_high  = st.st_dev;
307         req->index_low   = st.st_ino;
308         req->serial      = 0; /* FIXME */
309     }
310     return FD_TYPE_DEFAULT;
311 }
312
313 static void file_destroy( struct object *obj )
314 {
315     struct file *file = (struct file *)obj;
316     assert( obj->ops == &file_ops );
317
318     if (file->name)
319     {
320         /* remove it from the hashing list */
321         struct file **pptr = &file_hash[get_name_hash( file->name )];
322         while (*pptr && *pptr != file) pptr = &(*pptr)->next;
323         assert( *pptr );
324         *pptr = (*pptr)->next;
325         if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
326         free( file->name );
327     }
328 }
329
330 /* set the last error depending on errno */
331 void file_set_error(void)
332 {
333     switch (errno)
334     {
335     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
336     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
337     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
338     case EACCES:
339     case ESRCH:
340     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
341     case EROFS:     set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
342     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
343     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
344     case EISDIR:    set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
345     case ENFILE:
346     case EMFILE:    set_error( STATUS_NO_MORE_FILES ); break;
347     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
348     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
349     case ESPIPE:    set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
350     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
351     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
352     default:        perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
353     }
354 }
355
356 struct file *get_file_obj( struct process *process, handle_t handle, unsigned int access )
357 {
358     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
359 }
360
361 static int set_file_pointer( handle_t handle, unsigned int *low, int *high, int whence )
362 {
363     struct file *file;
364     off_t result,xto;
365
366     xto = *low+((off_t)*high<<32);
367     if (!(file = get_file_obj( current->process, handle, 0 )))
368         return 0;
369     if ((result = lseek(file->obj.fd,xto,whence))==-1)
370     {
371         /* Check for seek before start of file */
372
373         /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
374         if (((errno == EINVAL) || (errno == EPERM))
375             && (whence != SEEK_SET) && (*high < 0))
376             set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
377         else
378             file_set_error();
379         release_object( file );
380         return 0;
381     }
382     *low  = result & 0xffffffff;
383     *high = result >> 32;
384     release_object( file );
385     return 1;
386 }
387
388 static int truncate_file( handle_t handle )
389 {
390     struct file *file;
391     off_t result;
392
393     if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
394         return 0;
395     if (((result = lseek( file->obj.fd, 0, SEEK_CUR )) == -1) ||
396         (ftruncate( file->obj.fd, result ) == -1))
397     {
398         file_set_error();
399         release_object( file );
400         return 0;
401     }
402     release_object( file );
403     return 1;
404 }
405
406 /* try to grow the file to the specified size */
407 int grow_file( struct file *file, int size_high, int size_low )
408 {
409     struct stat st;
410     off_t size = size_low + (((off_t)size_high)<<32);
411
412     if (fstat( file->obj.fd, &st ) == -1)
413     {
414         file_set_error();
415         return 0;
416     }
417     if (st.st_size >= size) return 1;  /* already large enough */
418     if (ftruncate( file->obj.fd, size ) != -1) return 1;
419     file_set_error();
420     return 0;
421 }
422
423 static int set_file_time( handle_t handle, time_t access_time, time_t write_time )
424 {
425     struct file *file;
426     struct utimbuf utimbuf;
427
428     if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
429         return 0;
430     if (!file->name)
431     {
432         set_error( STATUS_INVALID_HANDLE );
433         release_object( file );
434         return 0;
435     }
436     if (!access_time || !write_time)
437     {
438         struct stat st;
439         if (stat( file->name, &st ) == -1) goto error;
440         if (!access_time) access_time = st.st_atime;
441         if (!write_time) write_time = st.st_mtime;
442     }
443     utimbuf.actime  = access_time;
444     utimbuf.modtime = write_time;
445     if (utime( file->name, &utimbuf ) == -1) goto error;
446     release_object( file );
447     return 1;
448  error:
449     file_set_error();
450     release_object( file );
451     return 0;
452 }
453
454 static int file_lock( struct file *file, int offset_high, int offset_low,
455                       int count_high, int count_low )
456 {
457     /* FIXME: implement this */
458     return 1;
459 }
460
461 static int file_unlock( struct file *file, int offset_high, int offset_low,
462                         int count_high, int count_low )
463 {
464     /* FIXME: implement this */
465     return 1;
466 }
467
468 /* create a file */
469 DECL_HANDLER(create_file)
470 {
471     struct file *file;
472
473     req->handle = 0;
474     if ((file = create_file( get_req_data(req), get_req_data_size(req), req->access,
475                              req->sharing, req->create, req->attrs, req->drive_type )))
476     {
477         req->handle = alloc_handle( current->process, file, req->access, req->inherit );
478         release_object( file );
479     }
480 }
481
482 /* allocate a file handle for a Unix fd */
483 DECL_HANDLER(alloc_file_handle)
484 {
485     struct file *file;
486     int fd;
487
488     req->handle = 0;
489     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
490     {
491         set_error( STATUS_INVALID_HANDLE );
492         return;
493     }
494     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE,
495                                     0, DRIVE_UNKNOWN )))
496     {
497         req->handle = alloc_handle( current->process, file, req->access, req->inherit );
498         release_object( file );
499     }
500 }
501
502 /* get a Unix fd to access a file */
503 DECL_HANDLER(get_handle_fd)
504 {
505     struct object *obj;
506
507     req->fd = -1;
508     req->type = FD_TYPE_INVALID;
509     if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
510     {
511         int fd = get_handle_fd( current->process, req->handle, req->access );
512         if (fd != -1) req->fd = fd;
513         else if (!get_error())
514         {
515             if ((fd = obj->ops->get_fd( obj )) != -1)
516                 send_client_fd( current->process, fd, req->handle );
517         }
518         req->type = obj->ops->get_file_info( obj, NULL );
519         release_object( obj );
520     }
521 }
522
523 /* set a file current position */
524 DECL_HANDLER(set_file_pointer)
525 {
526     int high = req->high;
527     int low  = req->low;
528     set_file_pointer( req->handle, &low, &high, req->whence );
529     req->new_low  = low;
530     req->new_high = high;
531 }
532
533 /* truncate (or extend) a file */
534 DECL_HANDLER(truncate_file)
535 {
536     truncate_file( req->handle );
537 }
538
539 /* flush a file buffers */
540 DECL_HANDLER(flush_file)
541 {
542     struct object *obj;
543
544     if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
545     {
546         obj->ops->flush( obj );
547         release_object( obj );
548     }
549 }
550
551 /* set a file access and modification times */
552 DECL_HANDLER(set_file_time)
553 {
554     set_file_time( req->handle, req->access_time, req->write_time );
555 }
556
557 /* get a file information */
558 DECL_HANDLER(get_file_info)
559 {
560     struct object *obj;
561
562     if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
563     {
564         obj->ops->get_file_info( obj, req );
565         release_object( obj );
566     }
567 }
568
569 /* lock a region of a file */
570 DECL_HANDLER(lock_file)
571 {
572     struct file *file;
573
574     if ((file = get_file_obj( current->process, req->handle, 0 )))
575     {
576         file_lock( file, req->offset_high, req->offset_low,
577                    req->count_high, req->count_low );
578         release_object( file );
579     }
580 }
581
582 /* unlock a region of a file */
583 DECL_HANDLER(unlock_file)
584 {
585     struct file *file;
586
587     if ((file = get_file_obj( current->process, req->handle, 0 )))
588     {
589         file_unlock( file, req->offset_high, req->offset_low,
590                      req->count_high, req->count_low );
591         release_object( file );
592     }
593 }