Fixed some issues found by winapi_check.
[wine/wine-kai.git] / server / file.c
blobfc6329ab78eeb2ac52f5bdcfc8dd14b72751c0ed
1 /*
2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
8 #include "wine/port.h"
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>
26 #include "winerror.h"
27 #include "winbase.h"
29 #include "handle.h"
30 #include "thread.h"
31 #include "request.h"
33 struct file
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 */
43 #define NAME_HASH_SIZE 37
45 static struct file *file_hash[NAME_HASH_SIZE];
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 );
54 static const struct object_ops file_ops =
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 */
71 static int get_name_hash( const char *name )
73 int hash = 0;
74 while (*name) hash ^= (unsigned char)*name++;
75 return hash % NAME_HASH_SIZE;
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 )
83 struct file *file;
84 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
85 unsigned int existing_access = 0;
87 for (file = file_hash[hash]; file; file = file->next)
89 if (strcmp( file->name, name )) continue;
90 existing_sharing &= file->sharing;
91 existing_access |= file->access;
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;
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 )
108 struct file *file;
109 if ((file = alloc_object( &file_ops, fd )))
111 file->name = NULL;
112 file->next = NULL;
113 file->access = access;
114 file->flags = attrs;
115 file->sharing = sharing;
117 return file;
121 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
122 unsigned int sharing, int create, unsigned int attrs )
124 struct file *file;
125 int hash, flags;
126 struct stat st;
127 char *name;
128 int fd = -1;
130 if (!(name = mem_alloc( len + 1 ))) return NULL;
131 memcpy( name, nameptr, len );
132 name[len] = 0;
134 /* check sharing mode */
135 hash = get_name_hash( name );
136 if (!check_sharing( name, hash, access, sharing )) goto error;
138 switch(create)
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;
147 switch(access & (GENERIC_READ | GENERIC_WRITE))
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;
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))
163 set_error( STATUS_ACCESS_DENIED );
164 goto error;
167 if (!(file = create_file_for_fd( fd, access, sharing, attrs )))
169 free( name );
170 return NULL;
172 file->name = name;
173 file->next = file_hash[hash];
174 file_hash[hash] = file;
175 return file;
177 file_error:
178 file_set_error();
179 error:
180 if (fd != -1) close( fd );
181 free( name );
182 return NULL;
185 /* check if two file objects point to the same file */
186 int is_same_file( struct file *file1, struct file *file2 )
188 return !strcmp( file1->name, file2->name );
191 /* Create an anonymous Unix file */
192 int create_anonymous_file(void)
194 char *name;
195 int fd;
199 if (!(name = tmpnam(NULL)))
201 set_error( STATUS_TOO_MANY_OPENED_FILES );
202 return -1;
204 fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
205 } while ((fd == -1) && (errno == EEXIST));
206 if (fd == -1)
208 file_set_error();
209 return -1;
211 unlink( name );
212 return fd;
215 /* Create a temp file for anonymous mappings */
216 struct file *create_temp_file( int access )
218 int fd;
220 if ((fd = create_anonymous_file()) == -1) return NULL;
221 return create_file_for_fd( fd, access, 0, 0 );
224 static void file_dump( struct object *obj, int verbose )
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 );
231 static int file_get_poll_events( struct object *obj )
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;
241 static int file_get_fd( struct object *obj )
243 struct file *file = (struct file *)obj;
244 assert( obj->ops == &file_ops );
245 return file->obj.fd;
248 static int file_flush( struct object *obj )
250 int ret;
251 struct file *file = (struct file *)grab_object(obj);
252 assert( obj->ops == &file_ops );
254 ret = (fsync( file->obj.fd ) != -1);
255 if (!ret) file_set_error();
256 release_object( file );
257 return ret;
260 static int file_get_info( struct object *obj, struct get_file_info_request *req )
262 struct stat st;
263 struct file *file = (struct file *)obj;
264 assert( obj->ops == &file_ops );
266 if (fstat( file->obj.fd, &st ) == -1)
268 file_set_error();
269 return 0;
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))
281 req->size_high = 0;
282 req->size_low = 0;
284 else
286 req->size_high = st.st_size >> 32;
287 req->size_low = st.st_size & 0xffffffff;
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;
296 static void file_destroy( struct object *obj )
298 struct file *file = (struct file *)obj;
299 assert( obj->ops == &file_ops );
301 if (file->name)
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 );
313 /* set the last error depending on errno */
314 void file_set_error(void)
316 switch (errno)
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;
338 struct file *get_file_obj( struct process *process, handle_t handle, unsigned int access )
340 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
343 static int set_file_pointer( handle_t handle, unsigned int *low, int *high, int whence )
345 struct file *file;
346 off_t result,xto;
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)
353 /* Check for seek before start of file */
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;
364 *low = result & 0xffffffff;
365 *high = result >> 32;
366 release_object( file );
367 return 1;
370 static int truncate_file( handle_t handle )
372 struct file *file;
373 off_t result;
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))
380 file_set_error();
381 release_object( file );
382 return 0;
384 fprintf(stderr,"server:truncated to %Ld\n",result);
385 release_object( file );
386 return 1;
389 /* try to grow the file to the specified size */
390 int grow_file( struct file *file, int size_high, int size_low )
392 struct stat st;
393 off_t size = size_low + (((off_t)size_high)<<32);
395 if (fstat( file->obj.fd, &st ) == -1)
397 file_set_error();
398 return 0;
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;
406 static int set_file_time( handle_t handle, time_t access_time, time_t write_time )
408 struct file *file;
409 struct utimbuf utimbuf;
411 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
412 return 0;
413 if (!file->name)
415 set_error( STATUS_INVALID_HANDLE );
416 release_object( file );
417 return 0;
419 if (!access_time || !write_time)
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;
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;
437 static int file_lock( struct file *file, int offset_high, int offset_low,
438 int count_high, int count_low )
440 /* FIXME: implement this */
441 return 1;
444 static int file_unlock( struct file *file, int offset_high, int offset_low,
445 int count_high, int count_low )
447 /* FIXME: implement this */
448 return 1;
451 /* create a file */
452 DECL_HANDLER(create_file)
454 struct file *file;
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 )))
460 req->handle = alloc_handle( current->process, file, req->access, req->inherit );
461 release_object( file );
465 /* allocate a file handle for a Unix fd */
466 DECL_HANDLER(alloc_file_handle)
468 struct file *file;
469 int fd;
471 req->handle = 0;
472 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
474 set_error( STATUS_INVALID_HANDLE );
475 return;
477 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE, 0 )))
479 req->handle = alloc_handle( current->process, file, req->access, 0 );
480 release_object( file );
484 /* get a Unix fd to access a file */
485 DECL_HANDLER(get_handle_fd)
487 struct object *obj;
489 req->fd = -1;
490 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
492 int fd = get_handle_fd( current->process, req->handle, req->access );
493 if (fd != -1) req->fd = fd;
494 else if (!get_error())
496 if ((fd = obj->ops->get_fd( obj )) != -1)
497 send_client_fd( current->process, fd, req->handle );
499 release_object( obj );
503 /* set a file current position */
504 DECL_HANDLER(set_file_pointer)
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;
513 /* truncate (or extend) a file */
514 DECL_HANDLER(truncate_file)
516 truncate_file( req->handle );
519 /* flush a file buffers */
520 DECL_HANDLER(flush_file)
522 struct object *obj;
524 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
526 obj->ops->flush( obj );
527 release_object( obj );
531 /* set a file access and modification times */
532 DECL_HANDLER(set_file_time)
534 set_file_time( req->handle, req->access_time, req->write_time );
537 /* get a file information */
538 DECL_HANDLER(get_file_info)
540 struct object *obj;
542 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
544 obj->ops->get_file_info( obj, req );
545 release_object( obj );
549 /* lock a region of a file */
550 DECL_HANDLER(lock_file)
552 struct file *file;
554 if ((file = get_file_obj( current->process, req->handle, 0 )))
556 file_lock( file, req->offset_high, req->offset_low,
557 req->count_high, req->count_low );
558 release_object( file );
562 /* unlock a region of a file */
563 DECL_HANDLER(unlock_file)
565 struct file *file;
567 if ((file = get_file_obj( current->process, req->handle, 0 )))
569 file_unlock( file, req->offset_high, req->offset_low,
570 req->count_high, req->count_low );
571 release_object( file );