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