2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
16 #ifdef HAVE_SYS_ERRNO_H
17 #include <sys/errno.h>
21 #include <sys/types.h>
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 */
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 )
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 )
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;
99 set_error( STATUS_SHARING_VIOLATION );
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,
109 if ((file = alloc_object( &file_ops, fd )))
113 file->access = access;
115 file->sharing = sharing;
121 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
122 unsigned int sharing, int create, unsigned int attrs )
130 if (!(name = mem_alloc( len + 1 ))) return NULL;
131 memcpy( name, nameptr, len );
134 /* check sharing mode */
135 hash = get_name_hash( name );
136 if (!check_sharing( name, hash, access, sharing )) goto error;
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))
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)
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 );
167 if (!(file = create_file_for_fd( fd, access, sharing, attrs )))
173 file->next = file_hash[hash];
174 file_hash[hash] = file;
180 if (fd != -1) close( fd );
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)
199 if (!(name = tmpnam(NULL)))
201 set_error( STATUS_TOO_MANY_OPENED_FILES );
204 fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
205 } while ((fd == -1) && (errno == EEXIST));
215 /* Create a temp file for anonymous mappings */
216 struct file *create_temp_file( int access )
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;
235 assert( obj->ops == &file_ops );
236 if (file->access & GENERIC_READ) events |= POLLIN;
237 if (file->access & GENERIC_WRITE) events |= POLLOUT;
241 static int file_get_fd( struct object *obj )
243 struct file *file = (struct file *)obj;
244 assert( obj->ops == &file_ops );
248 static int file_flush( struct object *obj )
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 );
260 static int file_get_info( struct object *obj, struct get_file_info_request *req )
263 struct file *file = (struct file *)obj;
264 assert( obj->ops == &file_ops );
266 if (fstat64( file->obj.fd, &st ) == -1)
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))
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 */
296 static void file_destroy( struct object *obj )
298 struct file *file = (struct file *)obj;
299 assert( obj->ops == &file_ops );
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;
307 *pptr = (*pptr)->next;
308 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
313 /* set the last error depending on errno */
314 void file_set_error(void)
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;
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;
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 )
348 xto = *low+((off64_t)*high<<32);
349 if (!(file = get_file_obj( current->process, handle, 0 )))
351 if ((result = lseek64(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) && (*low < 0))
358 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
361 release_object( file );
364 *low = result & 0xffffffff;
365 *high = result >> 32;
366 release_object( file );
370 static int truncate_file( handle_t handle )
375 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
377 if (((result = lseek64( file->obj.fd, 0, SEEK_CUR )) == -1) ||
378 (ftruncate64( file->obj.fd, result ) == -1))
381 release_object( file );
384 release_object( file );
388 /* try to grow the file to the specified size */
389 int grow_file( struct file *file, int size_high, int size_low )
392 off64_t size = size_low + (((off64_t)size_high)<<32);
394 if (fstat64( file->obj.fd, &st ) == -1)
399 if (st.st_size >= size) return 1; /* already large enough */
400 if (ftruncate64( file->obj.fd, size ) != -1) return 1;
405 static int set_file_time( handle_t handle, time_t access_time, time_t write_time )
408 struct utimbuf utimbuf;
410 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
414 set_error( STATUS_INVALID_HANDLE );
415 release_object( file );
418 if (!access_time || !write_time)
421 if (stat( file->name, &st ) == -1) goto error;
422 if (!access_time) access_time = st.st_atime;
423 if (!write_time) write_time = st.st_mtime;
425 utimbuf.actime = access_time;
426 utimbuf.modtime = write_time;
427 if (utime( file->name, &utimbuf ) == -1) goto error;
428 release_object( file );
432 release_object( file );
436 static int file_lock( struct file *file, int offset_high, int offset_low,
437 int count_high, int count_low )
439 /* FIXME: implement this */
443 static int file_unlock( struct file *file, int offset_high, int offset_low,
444 int count_high, int count_low )
446 /* FIXME: implement this */
451 DECL_HANDLER(create_file)
456 if ((file = create_file( get_req_data(req), get_req_data_size(req), req->access,
457 req->sharing, req->create, req->attrs )))
459 req->handle = alloc_handle( current->process, file, req->access, req->inherit );
460 release_object( file );
464 /* allocate a file handle for a Unix fd */
465 DECL_HANDLER(alloc_file_handle)
471 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
473 set_error( STATUS_INVALID_HANDLE );
476 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE, 0 )))
478 req->handle = alloc_handle( current->process, file, req->access, 0 );
479 release_object( file );
483 /* get a Unix fd to access a file */
484 DECL_HANDLER(get_handle_fd)
489 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
491 int fd = get_handle_fd( current->process, req->handle, req->access );
492 if (fd != -1) req->fd = fd;
493 else if (!get_error())
495 if ((fd = obj->ops->get_fd( obj )) != -1)
496 send_client_fd( current->process, fd, req->handle );
498 release_object( obj );
502 /* set a file current position */
503 DECL_HANDLER(set_file_pointer)
505 int high = req->high;
507 set_file_pointer( req->handle, &low, &high, req->whence );
509 req->new_high = high;
512 /* truncate (or extend) a file */
513 DECL_HANDLER(truncate_file)
515 truncate_file( req->handle );
518 /* flush a file buffers */
519 DECL_HANDLER(flush_file)
523 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
525 obj->ops->flush( obj );
526 release_object( obj );
530 /* set a file access and modification times */
531 DECL_HANDLER(set_file_time)
533 set_file_time( req->handle, req->access_time, req->write_time );
536 /* get a file information */
537 DECL_HANDLER(get_file_info)
541 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
543 obj->ops->get_file_info( obj, req );
544 release_object( obj );
548 /* lock a region of a file */
549 DECL_HANDLER(lock_file)
553 if ((file = get_file_obj( current->process, req->handle, 0 )))
555 file_lock( file, req->offset_high, req->offset_low,
556 req->count_high, req->count_low );
557 release_object( file );
561 /* unlock a region of a file */
562 DECL_HANDLER(unlock_file)
566 if ((file = get_file_obj( current->process, req->handle, 0 )))
568 file_unlock( file, req->offset_high, req->offset_low,
569 req->count_high, req->count_low );
570 release_object( file );