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>
53 struct object obj; /* object header */
54 struct file *next; /* next file in hashing list */
55 char *name; /* file name */
56 unsigned int access; /* file access (GENERIC_READ/WRITE) */
57 unsigned int flags; /* flags (FILE_FLAG_*) */
58 unsigned int sharing; /* file sharing mode */
59 int drive_type; /* type of drive the file is on */
60 struct async_queue read_q;
61 struct async_queue write_q;
64 #define NAME_HASH_SIZE 37
66 static struct file *file_hash[NAME_HASH_SIZE];
68 static void file_dump( struct object *obj, int verbose );
69 static int file_get_poll_events( struct object *obj );
70 static void file_poll_event( struct object *obj, int event );
71 static int file_flush( struct object *obj );
72 static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
73 static void file_destroy( struct object *obj );
74 static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
76 static const struct object_ops file_ops =
78 sizeof(struct file), /* size */
80 default_fd_add_queue, /* add_queue */
81 default_fd_remove_queue, /* remove_queue */
82 default_fd_signaled, /* signaled */
83 no_satisfied, /* satisfied */
84 default_get_fd, /* get_fd */
85 file_get_info, /* get_file_info */
86 file_destroy /* destroy */
89 static const struct fd_ops file_fd_ops =
91 file_get_poll_events, /* get_poll_events */
92 file_poll_event, /* poll_event */
93 file_flush, /* flush */
94 file_get_info, /* get_file_info */
95 file_queue_async /* queue_async */
98 static int get_name_hash( const char *name )
101 while (*name) hash ^= (unsigned char)*name++;
102 return hash % NAME_HASH_SIZE;
105 /* check if the desired access is possible without violating */
106 /* the sharing mode of other opens of the same file */
107 static int check_sharing( const char *name, int hash, unsigned int access,
108 unsigned int sharing )
111 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
112 unsigned int existing_access = 0;
114 for (file = file_hash[hash]; file; file = file->next)
116 if (strcmp( file->name, name )) continue;
117 existing_sharing &= file->sharing;
118 existing_access |= file->access;
120 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error;
121 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
122 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
123 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
126 set_error( STATUS_SHARING_VIOLATION );
130 /* create a file from a file descriptor */
131 /* if the function fails the fd is closed */
132 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
133 unsigned int attrs, int drive_type )
137 if ((file = alloc_fd_object( &file_ops, &file_fd_ops, fd )))
141 file->access = access;
143 file->sharing = sharing;
144 file->drive_type = drive_type;
145 if (file->flags & FILE_FLAG_OVERLAPPED)
147 init_async_queue (&file->read_q);
148 init_async_queue (&file->write_q);
155 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
156 unsigned int sharing, int create, unsigned int attrs,
166 if (!(name = mem_alloc( len + 1 ))) return NULL;
167 memcpy( name, nameptr, len );
170 /* check sharing mode */
171 hash = get_name_hash( name );
172 if (!check_sharing( name, hash, access, sharing )) goto error;
176 case CREATE_NEW: flags = O_CREAT | O_EXCL; break;
177 case CREATE_ALWAYS: flags = O_CREAT | O_TRUNC; break;
178 case OPEN_ALWAYS: flags = O_CREAT; break;
179 case TRUNCATE_EXISTING: flags = O_TRUNC; break;
180 case OPEN_EXISTING: flags = 0; break;
181 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
183 switch(access & (GENERIC_READ | GENERIC_WRITE))
186 case GENERIC_READ: flags |= O_RDONLY; break;
187 case GENERIC_WRITE: flags |= O_WRONLY; break;
188 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
190 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
193 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
196 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
197 if ((fd = open( name, flags | O_NONBLOCK | O_LARGEFILE, mode )) == -1 )
199 /* refuse to open a directory */
200 if (fstat( fd, &st ) == -1) goto file_error;
201 if (S_ISDIR(st.st_mode))
203 set_error( STATUS_ACCESS_DENIED );
207 if (!(file = create_file_for_fd( fd, access, sharing, attrs, drive_type )))
213 file->next = file_hash[hash];
214 file_hash[hash] = file;
220 if (fd != -1) close( fd );
225 /* check if two file objects point to the same file */
226 int is_same_file( struct file *file1, struct file *file2 )
228 return !strcmp( file1->name, file2->name );
231 /* get the type of drive the file is on */
232 int get_file_drive_type( struct file *file )
234 return file->drive_type;
237 /* Create an anonymous Unix file */
238 int create_anonymous_file(void)
243 sprintf(tmpfn,"/tmp/anonmap.XXXXXX");
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_flush( struct object *obj )
303 int ret = (fsync( get_unix_fd(obj) ) != -1);
304 if (!ret) file_set_error();
308 static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
311 struct file *file = (struct file *)obj;
312 int unix_fd = get_unix_fd( obj );
316 if (fstat( unix_fd, &st ) == -1)
319 return FD_TYPE_INVALID;
321 if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
322 S_ISSOCK(st.st_mode) || isatty(unix_fd)) reply->type = FILE_TYPE_CHAR;
323 else reply->type = FILE_TYPE_DISK;
324 if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
325 else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
326 if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
327 reply->access_time = st.st_atime;
328 reply->write_time = st.st_mtime;
329 if (S_ISDIR(st.st_mode))
331 reply->size_high = 0;
336 reply->size_high = st.st_size >> 32;
337 reply->size_low = st.st_size & 0xffffffff;
339 reply->links = st.st_nlink;
340 reply->index_high = st.st_dev;
341 reply->index_low = st.st_ino;
342 reply->serial = 0; /* FIXME */
345 if (file->flags & FILE_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED;
346 return FD_TYPE_DEFAULT;
349 static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
351 struct file *file = (struct file *)obj;
353 struct async_queue *q;
355 assert( obj->ops == &file_ops );
357 if ( !(file->flags & FILE_FLAG_OVERLAPPED) )
359 set_error ( STATUS_INVALID_HANDLE );
365 case ASYNC_TYPE_READ:
368 case ASYNC_TYPE_WRITE:
372 set_error( STATUS_INVALID_PARAMETER );
376 async = find_async ( q, current, ptr );
378 if ( status == STATUS_PENDING )
383 async = create_async ( obj, current, ptr );
387 async->status = STATUS_PENDING;
389 async_insert( q, async );
391 /* Check if the new pending request can be served immediately */
392 pfd.fd = get_unix_fd( obj );
393 pfd.events = file_get_poll_events ( obj );
398 file_poll_event ( obj, pfd.revents );
400 else if ( async ) destroy_async ( async );
401 else set_error ( STATUS_INVALID_PARAMETER );
403 set_select_events ( obj, file_get_poll_events ( obj ));
406 static void file_destroy( struct object *obj )
408 struct file *file = (struct file *)obj;
409 assert( obj->ops == &file_ops );
413 /* remove it from the hashing list */
414 struct file **pptr = &file_hash[get_name_hash( file->name )];
415 while (*pptr && *pptr != file) pptr = &(*pptr)->next;
417 *pptr = (*pptr)->next;
418 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
421 if (file->flags & FILE_FLAG_OVERLAPPED)
423 destroy_async_queue (&file->read_q);
424 destroy_async_queue (&file->write_q);
428 /* set the last error depending on errno */
429 void file_set_error(void)
433 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
434 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
435 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
438 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
439 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
440 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
441 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
442 case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
444 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
445 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
446 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
447 case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
448 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
449 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
450 default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
454 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
456 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
459 static int set_file_pointer( obj_handle_t handle, unsigned int *low, int *high, int whence )
464 xto = *low+((off_t)*high<<32);
465 if (!(file = get_file_obj( current->process, handle, 0 )))
467 if ((result = lseek( get_unix_fd(&file->obj), xto, whence))==-1)
469 /* Check for seek before start of file */
471 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
472 if (((errno == EINVAL) || (errno == EPERM))
473 && (whence != SEEK_SET) && (*high < 0))
474 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
477 release_object( file );
480 *low = result & 0xffffffff;
481 *high = result >> 32;
482 release_object( file );
486 /* extend a file beyond the current end of file */
487 static int extend_file( struct file *file, off_t size )
489 static const char zero;
490 int unix_fd = get_unix_fd( &file->obj );
492 /* extend the file one byte beyond the requested size and then truncate it */
493 /* this should work around ftruncate implementations that can't extend files */
494 if ((lseek( unix_fd, size, SEEK_SET ) != -1) &&
495 (write( unix_fd, &zero, 1 ) != -1))
497 ftruncate( unix_fd, size );
504 /* truncate file at current position */
505 static int truncate_file( struct file *file )
508 int unix_fd = get_unix_fd( &file->obj );
509 off_t pos = lseek( unix_fd, 0, SEEK_CUR );
510 off_t eof = lseek( unix_fd, 0, SEEK_END );
512 if (eof < pos) ret = extend_file( file, pos );
515 if (ftruncate( unix_fd, pos ) != -1) ret = 1;
516 else file_set_error();
518 lseek( unix_fd, pos, SEEK_SET ); /* restore file pos */
522 /* try to grow the file to the specified size */
523 int grow_file( struct file *file, int size_high, int size_low )
527 int unix_fd = get_unix_fd( &file->obj );
528 off_t old_pos, size = size_low + (((off_t)size_high)<<32);
530 if (fstat( unix_fd, &st ) == -1)
535 if (st.st_size >= size) return 1; /* already large enough */
536 old_pos = lseek( unix_fd, 0, SEEK_CUR ); /* save old pos */
537 ret = extend_file( file, size );
538 lseek( unix_fd, old_pos, SEEK_SET ); /* restore file pos */
542 static int set_file_time( obj_handle_t handle, time_t access_time, time_t write_time )
545 struct utimbuf utimbuf;
547 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
551 set_error( STATUS_INVALID_HANDLE );
552 release_object( file );
555 if (!access_time || !write_time)
558 if (stat( file->name, &st ) == -1) goto error;
559 if (!access_time) access_time = st.st_atime;
560 if (!write_time) write_time = st.st_mtime;
562 utimbuf.actime = access_time;
563 utimbuf.modtime = write_time;
564 if (utime( file->name, &utimbuf ) == -1) goto error;
565 release_object( file );
569 release_object( file );
573 static int file_lock( struct file *file, int offset_high, int offset_low,
574 int count_high, int count_low )
576 /* FIXME: implement this */
580 static int file_unlock( struct file *file, int offset_high, int offset_low,
581 int count_high, int count_low )
583 /* FIXME: implement this */
588 DECL_HANDLER(create_file)
593 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
594 req->sharing, req->create, req->attrs, req->drive_type )))
596 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
597 release_object( file );
601 /* allocate a file handle for a Unix fd */
602 DECL_HANDLER(alloc_file_handle)
608 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
610 set_error( STATUS_INVALID_HANDLE );
613 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE,
616 reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
617 release_object( file );
621 /* get a Unix fd to access a file */
622 DECL_HANDLER(get_handle_fd)
627 reply->type = FD_TYPE_INVALID;
628 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
630 int fd = get_handle_fd( current->process, req->handle, req->access );
631 if (fd != -1) reply->fd = fd;
632 else if (!get_error())
634 int unix_fd = get_unix_fd( obj );
635 if (unix_fd != -1) send_client_fd( current->process, unix_fd, req->handle );
637 reply->type = obj->ops->get_file_info( obj, NULL, &reply->flags );
638 release_object( obj );
642 /* set a file current position */
643 DECL_HANDLER(set_file_pointer)
645 int high = req->high;
647 set_file_pointer( req->handle, &low, &high, req->whence );
648 reply->new_low = low;
649 reply->new_high = high;
652 /* truncate (or extend) a file */
653 DECL_HANDLER(truncate_file)
657 if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
659 truncate_file( file );
660 release_object( file );
664 /* set a file access and modification times */
665 DECL_HANDLER(set_file_time)
667 set_file_time( req->handle, req->access_time, req->write_time );
670 /* get a file information */
671 DECL_HANDLER(get_file_info)
675 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
678 obj->ops->get_file_info( obj, reply, &flags );
679 release_object( obj );
683 /* lock a region of a file */
684 DECL_HANDLER(lock_file)
688 if ((file = get_file_obj( current->process, req->handle, 0 )))
690 file_lock( file, req->offset_high, req->offset_low,
691 req->count_high, req->count_low );
692 release_object( file );
696 /* unlock a region of a file */
697 DECL_HANDLER(unlock_file)
701 if ((file = get_file_obj( current->process, req->handle, 0 )))
703 file_unlock( file, req->offset_high, req->offset_low,
704 req->count_high, req->count_low );
705 release_object( file );