Changed the create_file server request to take NtCreateFile flags
[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 <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
33 #endif
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_UTIME_H
40 #include <utime.h>
41 #endif
42
43 #include "winerror.h"
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winreg.h"
47 #include "winternl.h"
48
49 #include "file.h"
50 #include "handle.h"
51 #include "thread.h"
52 #include "request.h"
53 #include "async.h"
54
55 struct file
56 {
57     struct object       obj;        /* object header */
58     struct fd          *fd;         /* file descriptor for this file */
59     struct file        *next;       /* next file in hashing list */
60     char               *name;       /* file name */
61     unsigned int        access;     /* file access (GENERIC_READ/WRITE) */
62     unsigned int        options;    /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
63     unsigned int        sharing;    /* file sharing mode */
64     int                 removable;  /* is file on removable media? */
65     struct async_queue  read_q;
66     struct async_queue  write_q;
67 };
68
69 #define NAME_HASH_SIZE 37
70
71 static struct file *file_hash[NAME_HASH_SIZE];
72
73 static void file_dump( struct object *obj, int verbose );
74 static struct fd *file_get_fd( struct object *obj );
75 static void file_destroy( struct object *obj );
76
77 static int file_get_poll_events( struct fd *fd );
78 static void file_poll_event( struct fd *fd, int event );
79 static int file_flush( struct fd *fd, struct event **event );
80 static int file_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
81 static void file_queue_async( struct fd *fd, void *ptr, unsigned int status, int type, int count );
82
83 static const struct object_ops file_ops =
84 {
85     sizeof(struct file),          /* size */
86     file_dump,                    /* dump */
87     default_fd_add_queue,         /* add_queue */
88     default_fd_remove_queue,      /* remove_queue */
89     default_fd_signaled,          /* signaled */
90     no_satisfied,                 /* satisfied */
91     file_get_fd,                  /* get_fd */
92     file_destroy                  /* destroy */
93 };
94
95 static const struct fd_ops file_fd_ops =
96 {
97     file_get_poll_events,         /* get_poll_events */
98     file_poll_event,              /* poll_event */
99     file_flush,                   /* flush */
100     file_get_info,                /* get_file_info */
101     file_queue_async              /* queue_async */
102 };
103
104 static inline int is_overlapped( const struct file *file )
105 {
106     return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
107 }
108
109 static int get_name_hash( const char *name )
110 {
111     int hash = 0;
112     while (*name) hash ^= (unsigned char)*name++;
113     return hash % NAME_HASH_SIZE;
114 }
115
116 /* check if the desired access is possible without violating */
117 /* the sharing mode of other opens of the same file */
118 static int check_sharing( const char *name, int hash, unsigned int access,
119                           unsigned int sharing )
120 {
121     struct file *file;
122     unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
123     unsigned int existing_access = 0;
124
125     for (file = file_hash[hash]; file; file = file->next)
126     {
127         if (strcmp( file->name, name )) continue;
128         existing_sharing &= file->sharing;
129         existing_access |= file->access;
130     }
131     if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error;
132     if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
133     if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
134     if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
135     return 1;
136  error:
137     set_error( STATUS_SHARING_VIOLATION );
138     return 0;
139 }
140
141 /* create a file from a file descriptor */
142 /* if the function fails the fd is closed */
143 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
144 {
145     struct file *file;
146
147     if ((file = alloc_object( &file_ops )))
148     {
149         file->name       = NULL;
150         file->next       = NULL;
151         file->access     = access;
152         file->options    = FILE_SYNCHRONOUS_IO_NONALERT;
153         file->sharing    = sharing;
154         file->removable  = 0;
155         if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
156         {
157             release_object( file );
158             return NULL;
159         }
160     }
161     return file;
162 }
163
164
165 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
166                                  unsigned int sharing, int create, unsigned int options,
167                                  unsigned int attrs, int removable )
168 {
169     struct file *file;
170     int hash, flags;
171     char *name;
172     mode_t mode;
173
174     if (!(name = mem_alloc( len + 1 ))) return NULL;
175     memcpy( name, nameptr, len );
176     name[len] = 0;
177
178     /* check sharing mode */
179     hash = get_name_hash( name );
180     if (!check_sharing( name, hash, access, sharing )) goto error;
181
182     switch(create)
183     {
184     case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
185     case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
186     case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
187     case FILE_OPEN:         flags = 0; break;
188     case FILE_OPEN_IF:      flags = O_CREAT; break;
189     case FILE_OVERWRITE:    flags = O_TRUNC; break;
190     default:                set_error( STATUS_INVALID_PARAMETER ); goto error;
191     }
192     switch(access & (GENERIC_READ | GENERIC_WRITE))
193     {
194     case 0: break;
195     case GENERIC_READ:  flags |= O_RDONLY; break;
196     case GENERIC_WRITE: flags |= O_WRONLY; break;
197     case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
198     }
199     mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
200
201     if (len >= 4 &&
202         (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
203         mode |= 0111;
204
205     if (!(file = alloc_object( &file_ops ))) goto error;
206
207     file->access     = access;
208     file->options    = options;
209     file->sharing    = sharing;
210     file->removable  = removable;
211     file->name       = name;
212     file->next       = file_hash[hash];
213     file_hash[hash]  = file;
214     if (is_overlapped( file ))
215     {
216         init_async_queue (&file->read_q);
217         init_async_queue (&file->write_q);
218     }
219
220     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
221     if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) ||
222         !(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE, &mode )))
223     {
224         release_object( file );
225         return NULL;
226     }
227     /* refuse to open a directory */
228     if (S_ISDIR(mode) && !(options & FILE_OPEN_FOR_BACKUP_INTENT))
229     {
230         set_error( STATUS_ACCESS_DENIED );
231         release_object( file );
232         return NULL;
233     }
234     return file;
235
236  error:
237     free( name );
238     return NULL;
239 }
240
241 /* check if two file objects point to the same file */
242 int is_same_file( struct file *file1, struct file *file2 )
243 {
244     return !strcmp( file1->name, file2->name );
245 }
246
247 /* check if the file is on removable media */
248 int is_file_removable( struct file *file )
249 {
250     return file->removable;
251 }
252
253 /* create a temp file for anonymous mappings */
254 struct file *create_temp_file( int access )
255 {
256     char tmpfn[16];
257     int fd;
258
259     sprintf( tmpfn, "anonmap.XXXXXX" );  /* create it in the server directory */
260     fd = mkstemps( tmpfn, 0 );
261     if (fd == -1)
262     {
263         file_set_error();
264         return NULL;
265     }
266     unlink( tmpfn );
267     return create_file_for_fd( fd, access, 0 );
268 }
269
270 static void file_dump( struct object *obj, int verbose )
271 {
272     struct file *file = (struct file *)obj;
273     assert( obj->ops == &file_ops );
274     fprintf( stderr, "File fd=%p options=%08x name='%s'\n", file->fd, file->options, file->name );
275 }
276
277 static int file_get_poll_events( struct fd *fd )
278 {
279     struct file *file = get_fd_user( fd );
280     int events = 0;
281     assert( file->obj.ops == &file_ops );
282     if (file->access & GENERIC_READ) events |= POLLIN;
283     if (file->access & GENERIC_WRITE) events |= POLLOUT;
284     return events;
285 }
286
287 static void file_poll_event( struct fd *fd, int event )
288 {
289     struct file *file = get_fd_user( fd );
290     assert( file->obj.ops == &file_ops );
291     if (is_overlapped( file ))
292     {
293         if( IS_READY(file->read_q) && (POLLIN & event) )
294         {
295             async_notify(file->read_q.head, STATUS_ALERTED);
296             return;
297         }
298         if( IS_READY(file->write_q) && (POLLOUT & event) )
299         {
300             async_notify(file->write_q.head, STATUS_ALERTED);
301             return;
302         }
303     }
304     default_poll_event( fd, event );
305 }
306
307
308 static int file_flush( struct fd *fd, struct event **event )
309 {
310     int ret = (fsync( get_unix_fd(fd) ) != -1);
311     if (!ret) file_set_error();
312     return ret;
313 }
314
315 static int file_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
316 {
317     struct stat st;
318     struct file *file = get_fd_user( fd );
319     int unix_fd = get_unix_fd( fd );
320
321     if (reply)
322     {
323         if (fstat( unix_fd, &st ) == -1)
324         {
325             file_set_error();
326             return FD_TYPE_INVALID;
327         }
328         if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
329             S_ISSOCK(st.st_mode) || isatty(unix_fd)) reply->type = FILE_TYPE_CHAR;
330         else reply->type = FILE_TYPE_DISK;
331         if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
332         else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
333         if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
334         reply->access_time = st.st_atime;
335         reply->write_time  = st.st_mtime;
336         reply->change_time = st.st_ctime;
337         if (S_ISDIR(st.st_mode))
338         {
339             reply->size_high  = 0;
340             reply->size_low   = 0;
341             reply->alloc_high = 0;
342             reply->alloc_low  = 0;
343         }
344         else
345         {
346             file_pos_t  alloc;
347             reply->size_high  = st.st_size >> 32;
348             reply->size_low   = st.st_size & 0xffffffff;
349             alloc = (file_pos_t)st.st_blksize * st.st_blocks;
350             reply->alloc_high = alloc >> 32;
351             reply->alloc_low  = alloc & 0xffffffff;
352         }
353         reply->links       = st.st_nlink;
354         reply->index_high  = st.st_dev;
355         reply->index_low   = st.st_ino;
356         reply->serial      = 0; /* FIXME */
357     }
358     *flags = 0;
359     if (is_overlapped( file )) *flags |= FD_FLAG_OVERLAPPED;
360     return FD_TYPE_DEFAULT;
361 }
362
363 static void file_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
364 {
365     struct file *file = get_fd_user( fd );
366     struct async *async;
367     struct async_queue *q;
368
369     assert( file->obj.ops == &file_ops );
370
371     if (!is_overlapped( file ))
372     {
373         set_error ( STATUS_INVALID_HANDLE );
374         return;
375     }
376
377     switch(type)
378     {
379     case ASYNC_TYPE_READ:
380         q = &file->read_q;
381         break;
382     case ASYNC_TYPE_WRITE:
383         q = &file->write_q;
384         break;
385     default:
386         set_error( STATUS_INVALID_PARAMETER );
387         return;
388     }
389
390     async = find_async ( q, current, ptr );
391
392     if ( status == STATUS_PENDING )
393     {
394         int events;
395
396         if ( !async )
397             async = create_async ( &file->obj, current, ptr );
398         if ( !async )
399             return;
400
401         async->status = STATUS_PENDING;
402         if ( !async->q )
403             async_insert( q, async );
404
405         /* Check if the new pending request can be served immediately */
406         events = check_fd_events( fd, file_get_poll_events( fd ) );
407         if (events) file_poll_event ( fd, events );
408     }
409     else if ( async ) destroy_async ( async );
410     else set_error ( STATUS_INVALID_PARAMETER );
411
412     set_fd_events( fd, file_get_poll_events( fd ));
413 }
414
415 static struct fd *file_get_fd( struct object *obj )
416 {
417     struct file *file = (struct file *)obj;
418     assert( obj->ops == &file_ops );
419     return (struct fd *)grab_object( file->fd );
420 }
421
422 static void file_destroy( struct object *obj )
423 {
424     struct file *file = (struct file *)obj;
425     assert( obj->ops == &file_ops );
426
427     if (file->name)
428     {
429         /* remove it from the hashing list */
430         struct file **pptr = &file_hash[get_name_hash( file->name )];
431         while (*pptr && *pptr != file) pptr = &(*pptr)->next;
432         assert( *pptr );
433         *pptr = (*pptr)->next;
434         if (file->options & FILE_DELETE_ON_CLOSE) unlink( file->name );
435         free( file->name );
436     }
437     if (is_overlapped( file ))
438     {
439         destroy_async_queue (&file->read_q);
440         destroy_async_queue (&file->write_q);
441     }
442     if (file->fd) release_object( file->fd );
443 }
444
445 /* set the last error depending on errno */
446 void file_set_error(void)
447 {
448     switch (errno)
449     {
450     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
451     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
452     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
453     case EACCES:
454     case ESRCH:
455     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
456     case EROFS:     set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
457     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
458     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
459     case EISDIR:    set_win32_error( ERROR_CANNOT_MAKE ); break;
460     case ENFILE:
461     case EMFILE:    set_error( STATUS_NO_MORE_FILES ); break;
462     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
463     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
464     case ESPIPE:    set_win32_error( ERROR_SEEK ); break;
465     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
466     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
467     case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
468 #ifdef EOVERFLOW
469     case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
470 #endif
471     default:        perror("file_set_error"); set_win32_error( ERROR_UNKNOWN ); break;
472     }
473 }
474
475 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
476 {
477     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
478 }
479
480 int get_file_unix_fd( struct file *file )
481 {
482     return get_unix_fd( file->fd );
483 }
484
485 static int set_file_pointer( obj_handle_t handle, unsigned int *low, int *high, int whence )
486 {
487     struct file *file;
488     off_t result,xto;
489
490     xto = *low+((off_t)*high<<32);
491     if (!(file = get_file_obj( current->process, handle, 0 )))
492         return 0;
493     if ((result = lseek( get_file_unix_fd(file), xto, whence))==-1)
494     {
495         /* Check for seek before start of file */
496
497         /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
498         if (((errno == EINVAL) || (errno == EPERM))
499             && (whence != SEEK_SET) && (*high < 0))
500             set_win32_error( ERROR_NEGATIVE_SEEK );
501         else
502             file_set_error();
503         release_object( file );
504         return 0;
505     }
506     *low  = result & 0xffffffff;
507     *high = result >> 32;
508     release_object( file );
509     return 1;
510 }
511
512 /* extend a file beyond the current end of file */
513 static int extend_file( struct file *file, off_t size )
514 {
515     static const char zero;
516     int unix_fd = get_file_unix_fd( file );
517
518     /* extend the file one byte beyond the requested size and then truncate it */
519     /* this should work around ftruncate implementations that can't extend files */
520     if ((lseek( unix_fd, size, SEEK_SET ) != -1) &&
521         (write( unix_fd, &zero, 1 ) != -1))
522     {
523         ftruncate( unix_fd, size );
524         return 1;
525     }
526     file_set_error();
527     return 0;
528 }
529
530 /* truncate file at current position */
531 static int truncate_file( struct file *file )
532 {
533     int ret = 0;
534     int unix_fd = get_file_unix_fd( file );
535     off_t pos = lseek( unix_fd, 0, SEEK_CUR );
536     off_t eof = lseek( unix_fd, 0, SEEK_END );
537
538     if (eof < pos) ret = extend_file( file, pos );
539     else
540     {
541         if (ftruncate( unix_fd, pos ) != -1) ret = 1;
542         else file_set_error();
543     }
544     lseek( unix_fd, pos, SEEK_SET );  /* restore file pos */
545     return ret;
546 }
547
548 /* try to grow the file to the specified size */
549 int grow_file( struct file *file, int size_high, int size_low )
550 {
551     int ret = 0;
552     struct stat st;
553     int unix_fd = get_file_unix_fd( file );
554     off_t old_pos, size = size_low + (((off_t)size_high)<<32);
555
556     if (fstat( unix_fd, &st ) == -1)
557     {
558         file_set_error();
559         return 0;
560     }
561     if (st.st_size >= size) return 1;  /* already large enough */
562     old_pos = lseek( unix_fd, 0, SEEK_CUR );  /* save old pos */
563     ret = extend_file( file, size );
564     lseek( unix_fd, old_pos, SEEK_SET );  /* restore file pos */
565     return ret;
566 }
567
568 static int set_file_time( obj_handle_t handle, time_t access_time, time_t write_time )
569 {
570     struct file *file;
571     struct utimbuf utimbuf;
572
573     if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
574         return 0;
575     if (!file->name)
576     {
577         set_error( STATUS_INVALID_HANDLE );
578         release_object( file );
579         return 0;
580     }
581     if (!access_time || !write_time)
582     {
583         struct stat st;
584         if (stat( file->name, &st ) == -1) goto error;
585         if (!access_time) access_time = st.st_atime;
586         if (!write_time) write_time = st.st_mtime;
587     }
588     utimbuf.actime  = access_time;
589     utimbuf.modtime = write_time;
590     if (utime( file->name, &utimbuf ) == -1) goto error;
591     release_object( file );
592     return 1;
593  error:
594     file_set_error();
595     release_object( file );
596     return 0;
597 }
598
599 /* create a file */
600 DECL_HANDLER(create_file)
601 {
602     struct file *file;
603
604     reply->handle = 0;
605     if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
606                              req->sharing, req->create, req->options, req->attrs, req->removable )))
607     {
608         reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
609         release_object( file );
610     }
611 }
612
613 /* allocate a file handle for a Unix fd */
614 DECL_HANDLER(alloc_file_handle)
615 {
616     struct file *file;
617     int fd;
618
619     reply->handle = 0;
620     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
621     {
622         set_error( STATUS_INVALID_HANDLE );
623         return;
624     }
625     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
626     {
627         reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
628         release_object( file );
629     }
630 }
631
632 /* set a file current position */
633 DECL_HANDLER(set_file_pointer)
634 {
635     int high = req->high;
636     int low  = req->low;
637     set_file_pointer( req->handle, &low, &high, req->whence );
638     reply->new_low  = low;
639     reply->new_high = high;
640 }
641
642 /* truncate (or extend) a file */
643 DECL_HANDLER(truncate_file)
644 {
645     struct file *file;
646
647     if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
648     {
649         truncate_file( file );
650         release_object( file );
651     }
652 }
653
654 /* set a file access and modification times */
655 DECL_HANDLER(set_file_time)
656 {
657     set_file_time( req->handle, req->access_time, req->write_time );
658 }
659
660 /* lock a region of a file */
661 DECL_HANDLER(lock_file)
662 {
663     struct file *file;
664     file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
665     file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
666
667     if ((file = get_file_obj( current->process, req->handle, 0 )))
668     {
669         reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
670         reply->overlapped = is_overlapped( file );
671         release_object( file );
672     }
673 }
674
675 /* unlock a region of a file */
676 DECL_HANDLER(unlock_file)
677 {
678     struct file *file;
679     file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
680     file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
681
682     if ((file = get_file_obj( current->process, req->handle, 0 )))
683     {
684         unlock_fd( file->fd, offset, count );
685         release_object( file );
686     }
687 }