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