2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
30 #ifdef HAVE_SYS_ERRNO_H
31 #include <sys/errno.h>
35 #include <sys/types.h>
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;
61 #define NAME_HASH_SIZE 37
63 static struct file *file_hash[NAME_HASH_SIZE];
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 struct async_queue * file_queue_async(struct object *obj, struct async* async, int type, int count);
74 static const struct object_ops file_ops =
76 sizeof(struct file), /* size */
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 */
92 static int get_name_hash( const char *name )
95 while (*name) hash ^= (unsigned char)*name++;
96 return hash % NAME_HASH_SIZE;
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 )
105 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
106 unsigned int existing_access = 0;
108 for (file = file_hash[hash]; file; file = file->next)
110 if (strcmp( file->name, name )) continue;
111 existing_sharing &= file->sharing;
112 existing_access |= file->access;
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;
120 set_error( STATUS_SHARING_VIOLATION );
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 )
130 if ((file = alloc_object( &file_ops, fd )))
134 file->access = access;
136 file->sharing = sharing;
137 file->drive_type = drive_type;
138 if (file->flags & FILE_FLAG_OVERLAPPED)
140 init_async_queue (&file->read_q);
141 init_async_queue (&file->write_q);
148 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
149 unsigned int sharing, int create, unsigned int attrs,
159 if (!(name = mem_alloc( len + 1 ))) return NULL;
160 memcpy( name, nameptr, len );
163 /* check sharing mode */
164 hash = get_name_hash( name );
165 if (!check_sharing( name, hash, access, sharing )) goto error;
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;
176 switch(access & (GENERIC_READ | GENERIC_WRITE))
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;
183 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
186 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
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 )
192 /* refuse to open a directory */
193 if (fstat( fd, &st ) == -1) goto file_error;
194 if (S_ISDIR(st.st_mode))
196 set_error( STATUS_ACCESS_DENIED );
200 if (!(file = create_file_for_fd( fd, access, sharing, attrs, drive_type )))
206 file->next = file_hash[hash];
207 file_hash[hash] = file;
213 if (fd != -1) close( fd );
218 /* check if two file objects point to the same file */
219 int is_same_file( struct file *file1, struct file *file2 )
221 return !strcmp( file1->name, file2->name );
224 /* get the type of drive the file is on */
225 int get_file_drive_type( struct file *file )
227 return file->drive_type;
230 /* Create an anonymous Unix file */
231 int create_anonymous_file(void)
238 if (!(name = tmpnam(NULL)))
240 set_error( STATUS_TOO_MANY_OPENED_FILES );
243 fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
244 } while ((fd == -1) && (errno == EEXIST));
254 /* Create a temp file for anonymous mappings */
255 struct file *create_temp_file( int access )
259 if ((fd = create_anonymous_file()) == -1) return NULL;
260 return create_file_for_fd( fd, access, 0, 0, DRIVE_FIXED );
263 static void file_dump( struct object *obj, int verbose )
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 );
270 static int file_get_poll_events( struct object *obj )
272 struct file *file = (struct file *)obj;
274 assert( obj->ops == &file_ops );
275 if (file->access & GENERIC_READ) events |= POLLIN;
276 if (file->access & GENERIC_WRITE) events |= POLLOUT;
280 static void file_poll_event( struct object *obj, int event )
282 struct file *file = (struct file *)obj;
283 assert( obj->ops == &file_ops );
284 if ( file->flags & FILE_FLAG_OVERLAPPED )
286 if( IS_READY(file->read_q) && (POLLIN & event) )
288 async_notify(file->read_q.head, STATUS_ALERTED);
291 if( IS_READY(file->write_q) && (POLLOUT & event) )
293 async_notify(file->write_q.head, STATUS_ALERTED);
297 default_poll_event( obj, event );
301 static int file_get_fd( struct object *obj )
303 struct file *file = (struct file *)obj;
304 assert( obj->ops == &file_ops );
308 static int file_flush( struct object *obj )
311 struct file *file = (struct file *)grab_object(obj);
312 assert( obj->ops == &file_ops );
314 ret = (fsync( file->obj.fd ) != -1);
315 if (!ret) file_set_error();
316 release_object( file );
320 static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
323 struct file *file = (struct file *)obj;
324 assert( obj->ops == &file_ops );
328 if (fstat( file->obj.fd, &st ) == -1)
331 return FD_TYPE_INVALID;
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))
343 reply->size_high = 0;
348 reply->size_high = st.st_size >> 32;
349 reply->size_low = st.st_size & 0xffffffff;
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 */
357 if (file->flags & FILE_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
358 return FD_TYPE_DEFAULT;
361 static struct async_queue *file_queue_async(struct object *obj, struct async *async, int type, int count)
363 struct file *file = (struct file *)obj;
364 struct async_queue *q;
366 assert( obj->ops == &file_ops );
368 if ( !(file->flags & FILE_FLAG_OVERLAPPED) )
370 set_error ( STATUS_INVALID_HANDLE );
376 case ASYNC_TYPE_READ:
379 case ASYNC_TYPE_WRITE:
383 set_error( STATUS_INVALID_PARAMETER );
387 if(async && !async->q)
388 async_insert(q, async);
393 static void file_destroy( struct object *obj )
395 struct file *file = (struct file *)obj;
396 assert( obj->ops == &file_ops );
400 /* remove it from the hashing list */
401 struct file **pptr = &file_hash[get_name_hash( file->name )];
402 while (*pptr && *pptr != file) pptr = &(*pptr)->next;
404 *pptr = (*pptr)->next;
405 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
408 if (file->flags & FILE_FLAG_OVERLAPPED)
410 destroy_async_queue (&file->read_q);
411 destroy_async_queue (&file->write_q);
415 /* set the last error depending on errno */
416 void file_set_error(void)
420 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
421 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
422 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
425 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
426 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
427 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
428 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
429 case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
431 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
432 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
433 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
434 case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
435 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
436 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
437 default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
441 struct file *get_file_obj( struct process *process, handle_t handle, unsigned int access )
443 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
446 static int set_file_pointer( handle_t handle, unsigned int *low, int *high, int whence )
451 xto = *low+((off_t)*high<<32);
452 if (!(file = get_file_obj( current->process, handle, 0 )))
454 if ((result = lseek(file->obj.fd,xto,whence))==-1)
456 /* Check for seek before start of file */
458 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
459 if (((errno == EINVAL) || (errno == EPERM))
460 && (whence != SEEK_SET) && (*high < 0))
461 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
464 release_object( file );
467 *low = result & 0xffffffff;
468 *high = result >> 32;
469 release_object( file );
473 /* extend a file beyond the current end of file */
474 static int extend_file( struct file *file, off_t size )
476 static const char zero;
478 /* extend the file one byte beyond the requested size and then truncate it */
479 /* this should work around ftruncate implementations that can't extend files */
480 if ((lseek( file->obj.fd, size, SEEK_SET ) != -1) &&
481 (write( file->obj.fd, &zero, 1 ) != -1))
483 ftruncate( file->obj.fd, size );
490 /* truncate file at current position */
491 static int truncate_file( struct file *file )
494 off_t pos = lseek( file->obj.fd, 0, SEEK_CUR );
495 off_t eof = lseek( file->obj.fd, 0, SEEK_END );
497 if (eof < pos) ret = extend_file( file, pos );
500 if (ftruncate( file->obj.fd, pos ) != -1) ret = 1;
501 else file_set_error();
503 lseek( file->obj.fd, pos, SEEK_SET ); /* restore file pos */
507 /* try to grow the file to the specified size */
508 int grow_file( struct file *file, int size_high, int size_low )
512 off_t old_pos, size = size_low + (((off_t)size_high)<<32);
514 if (fstat( file->obj.fd, &st ) == -1)
519 if (st.st_size >= size) return 1; /* already large enough */
520 old_pos = lseek( file->obj.fd, 0, SEEK_CUR ); /* save old pos */
521 ret = extend_file( file, size );
522 lseek( file->obj.fd, old_pos, SEEK_SET ); /* restore file pos */
526 static int set_file_time( handle_t handle, time_t access_time, time_t write_time )
529 struct utimbuf utimbuf;
531 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
535 set_error( STATUS_INVALID_HANDLE );
536 release_object( file );
539 if (!access_time || !write_time)
542 if (stat( file->name, &st ) == -1) goto error;
543 if (!access_time) access_time = st.st_atime;
544 if (!write_time) write_time = st.st_mtime;
546 utimbuf.actime = access_time;
547 utimbuf.modtime = write_time;
548 if (utime( file->name, &utimbuf ) == -1) goto error;
549 release_object( file );
553 release_object( file );
557 static int file_lock( struct file *file, int offset_high, int offset_low,
558 int count_high, int count_low )
560 /* FIXME: implement this */
564 static int file_unlock( struct file *file, int offset_high, int offset_low,
565 int count_high, int count_low )
567 /* FIXME: implement this */
572 DECL_HANDLER(create_file)
577 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
578 req->sharing, req->create, req->attrs, req->drive_type )))
580 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
581 release_object( file );
585 /* allocate a file handle for a Unix fd */
586 DECL_HANDLER(alloc_file_handle)
592 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
594 set_error( STATUS_INVALID_HANDLE );
597 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE,
600 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
601 release_object( file );
605 /* get a Unix fd to access a file */
606 DECL_HANDLER(get_handle_fd)
611 reply->type = FD_TYPE_INVALID;
612 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
614 int fd = get_handle_fd( current->process, req->handle, req->access );
615 if (fd != -1) reply->fd = fd;
616 else if (!get_error())
618 if ((fd = obj->ops->get_fd( obj )) != -1)
619 send_client_fd( current->process, fd, req->handle );
621 reply->type = obj->ops->get_file_info( obj, NULL, &reply->flags );
622 release_object( obj );
626 /* set a file current position */
627 DECL_HANDLER(set_file_pointer)
629 int high = req->high;
631 set_file_pointer( req->handle, &low, &high, req->whence );
632 reply->new_low = low;
633 reply->new_high = high;
636 /* truncate (or extend) a file */
637 DECL_HANDLER(truncate_file)
641 if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
643 truncate_file( file );
644 release_object( file );
648 /* flush a file buffers */
649 DECL_HANDLER(flush_file)
653 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
655 obj->ops->flush( obj );
656 release_object( obj );
660 /* set a file access and modification times */
661 DECL_HANDLER(set_file_time)
663 set_file_time( req->handle, req->access_time, req->write_time );
666 /* get a file information */
667 DECL_HANDLER(get_file_info)
671 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
674 obj->ops->get_file_info( obj, reply, &flags );
675 release_object( obj );
679 /* lock a region of a file */
680 DECL_HANDLER(lock_file)
684 if ((file = get_file_obj( current->process, req->handle, 0 )))
686 file_lock( file, req->offset_high, req->offset_low,
687 req->count_high, req->count_low );
688 release_object( file );
692 /* unlock a region of a file */
693 DECL_HANDLER(unlock_file)
697 if ((file = get_file_obj( current->process, req->handle, 0 )))
699 file_unlock( file, req->offset_high, req->offset_low,
700 req->count_high, req->count_low );
701 release_object( file );