Add more flexibility to the queue_async server call by moving most
[wine] / server / file.c
1 /*
2  * Server-side file management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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>
39
40 #include "winerror.h"
41 #include "winbase.h"
42
43 #include "handle.h"
44 #include "thread.h"
45 #include "request.h"
46 #include "async.h"
47
48 struct file
49 {
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;
59 };
60
61 #define NAME_HASH_SIZE 37
62
63 static struct file *file_hash[NAME_HASH_SIZE];
64
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);
73
74 static const struct object_ops file_ops =
75 {
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 */
89 };
90
91
92 static int get_name_hash( const char *name )
93 {
94     int hash = 0;
95     while (*name) hash ^= (unsigned char)*name++;
96     return hash % NAME_HASH_SIZE;
97 }
98
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 )
103 {
104     struct file *file;
105     unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
106     unsigned int existing_access = 0;
107
108     for (file = file_hash[hash]; file; file = file->next)
109     {
110         if (strcmp( file->name, name )) continue;
111         existing_sharing &= file->sharing;
112         existing_access |= file->access;
113     }
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;
122 }
123
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 )
128 {
129     struct file *file;
130     if ((file = alloc_object( &file_ops, fd )))
131     {
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)
139         {
140             init_async_queue (&file->read_q);
141             init_async_queue (&file->write_q);
142         }
143     }
144     return file;
145 }
146
147
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 )
151 {
152     struct file *file;
153     int hash, flags;
154     struct stat st;
155     char *name;
156     int fd = -1;
157     mode_t mode;
158
159     if (!(name = mem_alloc( len + 1 ))) return NULL;
160     memcpy( name, nameptr, len );
161     name[len] = 0;
162
163     /* check sharing mode */
164     hash = get_name_hash( name );
165     if (!check_sharing( name, hash, access, sharing )) goto error;
166
167     switch(create)
168     {
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;
175     }
176     switch(access & (GENERIC_READ | GENERIC_WRITE))
177     {
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;
182     }
183     mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
184
185     if (len >= 4 &&
186         (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
187         mode |= 0111;
188
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))
195     {
196         set_error( STATUS_ACCESS_DENIED );
197         goto error;
198     }
199
200     if (!(file = create_file_for_fd( fd, access, sharing, attrs, drive_type )))
201     {
202         free( name );
203         return NULL;
204     }
205     file->name = name;
206     file->next = file_hash[hash];
207     file_hash[hash] = file;
208     return file;
209
210  file_error:
211     file_set_error();
212  error:
213     if (fd != -1) close( fd );
214     free( name );
215     return NULL;
216 }
217
218 /* check if two file objects point to the same file */
219 int is_same_file( struct file *file1, struct file *file2 )
220 {
221     return !strcmp( file1->name, file2->name );
222 }
223
224 /* get the type of drive the file is on */
225 int get_file_drive_type( struct file *file )
226 {
227     return file->drive_type;
228 }
229
230 /* Create an anonymous Unix file */
231 int create_anonymous_file(void)
232 {
233     char *name;
234     int fd;
235
236     do
237     {
238         if (!(name = tmpnam(NULL)))
239         {
240             set_error( STATUS_TOO_MANY_OPENED_FILES );
241             return -1;
242         }
243         fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
244     } while ((fd == -1) && (errno == EEXIST));
245     if (fd == -1)
246     {
247         file_set_error();
248         return -1;
249     }
250     unlink( name );
251     return fd;
252 }
253
254 /* Create a temp file for anonymous mappings */
255 struct file *create_temp_file( int access )
256 {
257     int fd;
258
259     if ((fd = create_anonymous_file()) == -1) return NULL;
260     return create_file_for_fd( fd, access, 0, 0, DRIVE_FIXED );
261 }
262
263 static void file_dump( struct object *obj, int verbose )
264 {
265     struct file *file = (struct file *)obj;
266     assert( obj->ops == &file_ops );
267     fprintf( stderr, "File fd=%d flags=%08x name='%s'\n", file->obj.fd, file->flags, file->name );
268 }
269
270 static int file_get_poll_events( struct object *obj )
271 {
272     struct file *file = (struct file *)obj;
273     int events = 0;
274     assert( obj->ops == &file_ops );
275     if (file->access & GENERIC_READ) events |= POLLIN;
276     if (file->access & GENERIC_WRITE) events |= POLLOUT;
277     return events;
278 }
279
280 static void file_poll_event( struct object *obj, int event )
281 {
282     struct file *file = (struct file *)obj;
283     assert( obj->ops == &file_ops );
284     if ( file->flags & FILE_FLAG_OVERLAPPED )
285     {
286         if( IS_READY(file->read_q) && (POLLIN & event) )
287         {
288             async_notify(file->read_q.head, STATUS_ALERTED);
289             return;
290         }
291         if( IS_READY(file->write_q) && (POLLOUT & event) )
292         {
293             async_notify(file->write_q.head, STATUS_ALERTED);
294             return;
295         }
296     }
297     default_poll_event( obj, event );
298 }
299
300
301 static int file_get_fd( struct object *obj )
302 {
303     struct file *file = (struct file *)obj;
304     assert( obj->ops == &file_ops );
305     return file->obj.fd;
306 }
307
308 static int file_flush( struct object *obj )
309 {
310     int ret;
311     struct file *file = (struct file *)grab_object(obj);
312     assert( obj->ops == &file_ops );
313
314     ret = (fsync( file->obj.fd ) != -1);
315     if (!ret) file_set_error();
316     release_object( file );
317     return ret;
318 }
319
320 static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
321 {
322     struct stat st;
323     struct file *file = (struct file *)obj;
324     assert( obj->ops == &file_ops );
325
326     if (reply)
327     {
328         if (fstat( file->obj.fd, &st ) == -1)
329         {
330             file_set_error();
331             return FD_TYPE_INVALID;
332         }
333         if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
334             S_ISSOCK(st.st_mode) || isatty(file->obj.fd)) reply->type = FILE_TYPE_CHAR;
335         else reply->type = FILE_TYPE_DISK;
336         if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
337         else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
338         if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
339         reply->access_time = st.st_atime;
340         reply->write_time  = st.st_mtime;
341         if (S_ISDIR(st.st_mode))
342         {
343             reply->size_high = 0;
344             reply->size_low  = 0;
345         }
346         else
347         {
348             reply->size_high = st.st_size >> 32;
349             reply->size_low  = st.st_size & 0xffffffff;
350         }
351         reply->links       = st.st_nlink;
352         reply->index_high  = st.st_dev;
353         reply->index_low   = st.st_ino;
354         reply->serial      = 0; /* FIXME */
355     }
356     *flags = 0;
357     if (file->flags & FILE_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
358     return FD_TYPE_DEFAULT;
359 }
360
361 static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
362 {
363     struct file *file = (struct file *)obj;
364     struct async *async;
365     struct async_queue *q;
366
367     assert( obj->ops == &file_ops );
368
369     if ( !(file->flags & FILE_FLAG_OVERLAPPED) )
370     {
371         set_error ( STATUS_INVALID_HANDLE );
372         return;
373     }
374
375     switch(type)
376     {
377     case ASYNC_TYPE_READ:
378         q = &file->read_q;
379         break;
380     case ASYNC_TYPE_WRITE:
381         q = &file->write_q;
382         break;
383     default:
384         set_error( STATUS_INVALID_PARAMETER );
385         return;
386     }
387
388     async = find_async ( q, current, ptr );
389
390     if ( status == STATUS_PENDING )
391     {
392         if ( !async )
393             async = create_async ( obj, current, ptr );
394         if ( !async )
395             return;
396
397         async->status = STATUS_PENDING;
398         if ( !async->q )
399             async_insert( q, async );
400     }
401     else if ( async ) destroy_async ( async );
402     else set_error ( STATUS_INVALID_PARAMETER );
403
404     set_select_events ( obj, file_get_poll_events ( obj ));
405 }
406
407 static void file_destroy( struct object *obj )
408 {
409     struct file *file = (struct file *)obj;
410     assert( obj->ops == &file_ops );
411
412     if (file->name)
413     {
414         /* remove it from the hashing list */
415         struct file **pptr = &file_hash[get_name_hash( file->name )];
416         while (*pptr && *pptr != file) pptr = &(*pptr)->next;
417         assert( *pptr );
418         *pptr = (*pptr)->next;
419         if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
420         free( file->name );
421     }
422     if (file->flags & FILE_FLAG_OVERLAPPED)
423     {
424         destroy_async_queue (&file->read_q);
425         destroy_async_queue (&file->write_q);
426     }
427 }
428
429 /* set the last error depending on errno */
430 void file_set_error(void)
431 {
432     switch (errno)
433     {
434     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
435     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
436     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
437     case EACCES:
438     case ESRCH:
439     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
440     case EROFS:     set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
441     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
442     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
443     case EISDIR:    set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
444     case ENFILE:
445     case EMFILE:    set_error( STATUS_NO_MORE_FILES ); break;
446     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
447     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
448     case ESPIPE:    set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
449     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
450     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
451     default:        perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
452     }
453 }
454
455 struct file *get_file_obj( struct process *process, handle_t handle, unsigned int access )
456 {
457     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
458 }
459
460 static int set_file_pointer( handle_t handle, unsigned int *low, int *high, int whence )
461 {
462     struct file *file;
463     off_t result,xto;
464
465     xto = *low+((off_t)*high<<32);
466     if (!(file = get_file_obj( current->process, handle, 0 )))
467         return 0;
468     if ((result = lseek(file->obj.fd,xto,whence))==-1)
469     {
470         /* Check for seek before start of file */
471
472         /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
473         if (((errno == EINVAL) || (errno == EPERM))
474             && (whence != SEEK_SET) && (*high < 0))
475             set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
476         else
477             file_set_error();
478         release_object( file );
479         return 0;
480     }
481     *low  = result & 0xffffffff;
482     *high = result >> 32;
483     release_object( file );
484     return 1;
485 }
486
487 /* extend a file beyond the current end of file */
488 static int extend_file( struct file *file, off_t size )
489 {
490     static const char zero;
491
492     /* extend the file one byte beyond the requested size and then truncate it */
493     /* this should work around ftruncate implementations that can't extend files */
494     if ((lseek( file->obj.fd, size, SEEK_SET ) != -1) &&
495         (write( file->obj.fd, &zero, 1 ) != -1))
496     {
497         ftruncate( file->obj.fd, size );
498         return 1;
499     }
500     file_set_error();
501     return 0;
502 }
503
504 /* truncate file at current position */
505 static int truncate_file( struct file *file )
506 {
507     int ret = 0;
508     off_t pos = lseek( file->obj.fd, 0, SEEK_CUR );
509     off_t eof = lseek( file->obj.fd, 0, SEEK_END );
510
511     if (eof < pos) ret = extend_file( file, pos );
512     else
513     {
514         if (ftruncate( file->obj.fd, pos ) != -1) ret = 1;
515         else file_set_error();
516     }
517     lseek( file->obj.fd, pos, SEEK_SET );  /* restore file pos */
518     return ret;
519 }
520
521 /* try to grow the file to the specified size */
522 int grow_file( struct file *file, int size_high, int size_low )
523 {
524     int ret = 0;
525     struct stat st;
526     off_t old_pos, size = size_low + (((off_t)size_high)<<32);
527
528     if (fstat( file->obj.fd, &st ) == -1)
529     {
530         file_set_error();
531         return 0;
532     }
533     if (st.st_size >= size) return 1;  /* already large enough */
534     old_pos = lseek( file->obj.fd, 0, SEEK_CUR );  /* save old pos */
535     ret = extend_file( file, size );
536     lseek( file->obj.fd, old_pos, SEEK_SET );  /* restore file pos */
537     return ret;
538 }
539
540 static int set_file_time( handle_t handle, time_t access_time, time_t write_time )
541 {
542     struct file *file;
543     struct utimbuf utimbuf;
544
545     if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
546         return 0;
547     if (!file->name)
548     {
549         set_error( STATUS_INVALID_HANDLE );
550         release_object( file );
551         return 0;
552     }
553     if (!access_time || !write_time)
554     {
555         struct stat st;
556         if (stat( file->name, &st ) == -1) goto error;
557         if (!access_time) access_time = st.st_atime;
558         if (!write_time) write_time = st.st_mtime;
559     }
560     utimbuf.actime  = access_time;
561     utimbuf.modtime = write_time;
562     if (utime( file->name, &utimbuf ) == -1) goto error;
563     release_object( file );
564     return 1;
565  error:
566     file_set_error();
567     release_object( file );
568     return 0;
569 }
570
571 static int file_lock( struct file *file, int offset_high, int offset_low,
572                       int count_high, int count_low )
573 {
574     /* FIXME: implement this */
575     return 1;
576 }
577
578 static int file_unlock( struct file *file, int offset_high, int offset_low,
579                         int count_high, int count_low )
580 {
581     /* FIXME: implement this */
582     return 1;
583 }
584
585 /* create a file */
586 DECL_HANDLER(create_file)
587 {
588     struct file *file;
589
590     reply->handle = 0;
591     if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
592                              req->sharing, req->create, req->attrs, req->drive_type )))
593     {
594         reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
595         release_object( file );
596     }
597 }
598
599 /* allocate a file handle for a Unix fd */
600 DECL_HANDLER(alloc_file_handle)
601 {
602     struct file *file;
603     int fd;
604
605     reply->handle = 0;
606     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
607     {
608         set_error( STATUS_INVALID_HANDLE );
609         return;
610     }
611     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE,
612                                     0, DRIVE_UNKNOWN )))
613     {
614         reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
615         release_object( file );
616     }
617 }
618
619 /* get a Unix fd to access a file */
620 DECL_HANDLER(get_handle_fd)
621 {
622     struct object *obj;
623
624     reply->fd = -1;
625     reply->type = FD_TYPE_INVALID;
626     if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
627     {
628         int fd = get_handle_fd( current->process, req->handle, req->access );
629         if (fd != -1) reply->fd = fd;
630         else if (!get_error())
631         {
632             if ((fd = obj->ops->get_fd( obj )) != -1)
633                 send_client_fd( current->process, fd, req->handle );
634         }
635         reply->type = obj->ops->get_file_info( obj, NULL, &reply->flags );
636         release_object( obj );
637     }
638 }
639
640 /* set a file current position */
641 DECL_HANDLER(set_file_pointer)
642 {
643     int high = req->high;
644     int low  = req->low;
645     set_file_pointer( req->handle, &low, &high, req->whence );
646     reply->new_low  = low;
647     reply->new_high = high;
648 }
649
650 /* truncate (or extend) a file */
651 DECL_HANDLER(truncate_file)
652 {
653     struct file *file;
654
655     if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
656     {
657         truncate_file( file );
658         release_object( file );
659     }
660 }
661
662 /* flush a file buffers */
663 DECL_HANDLER(flush_file)
664 {
665     struct object *obj;
666
667     if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
668     {
669         obj->ops->flush( obj );
670         release_object( obj );
671     }
672 }
673
674 /* set a file access and modification times */
675 DECL_HANDLER(set_file_time)
676 {
677     set_file_time( req->handle, req->access_time, req->write_time );
678 }
679
680 /* get a file information */
681 DECL_HANDLER(get_file_info)
682 {
683     struct object *obj;
684
685     if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
686     {
687         int flags;
688         obj->ops->get_file_info( obj, reply, &flags );
689         release_object( obj );
690     }
691 }
692
693 /* lock a region of a file */
694 DECL_HANDLER(lock_file)
695 {
696     struct file *file;
697
698     if ((file = get_file_obj( current->process, req->handle, 0 )))
699     {
700         file_lock( file, req->offset_high, req->offset_low,
701                    req->count_high, req->count_low );
702         release_object( file );
703     }
704 }
705
706 /* unlock a region of a file */
707 DECL_HANDLER(unlock_file)
708 {
709     struct file *file;
710
711     if ((file = get_file_obj( current->process, req->handle, 0 )))
712     {
713         file_unlock( file, req->offset_high, req->offset_low,
714                      req->count_high, req->count_low );
715         release_object( file );
716     }
717 }