2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include <sys/errno.h>
16 #include <sys/types.h>
30 struct object obj
; /* object header */
31 struct select_user select
; /* select user */
32 struct file
*next
; /* next file in hashing list */
33 char *name
; /* file name */
34 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
35 unsigned int flags
; /* flags (FILE_FLAG_*) */
36 unsigned int sharing
; /* file sharing mode */
39 #define NAME_HASH_SIZE 37
41 static struct file
*file_hash
[NAME_HASH_SIZE
];
43 static void file_dump( struct object
*obj
, int verbose
);
44 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
45 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
46 static int file_signaled( struct object
*obj
, struct thread
*thread
);
47 static int file_get_read_fd( struct object
*obj
);
48 static int file_get_write_fd( struct object
*obj
);
49 static int file_flush( struct object
*obj
);
50 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
);
51 static void file_destroy( struct object
*obj
);
53 static const struct object_ops file_ops
=
69 static int get_name_hash( const char *name
)
72 while (*name
) hash
^= *name
++;
73 return hash
% NAME_HASH_SIZE
;
76 /* check if the desired access is possible without violating */
77 /* the sharing mode of other opens of the same file */
78 static int check_sharing( const char *name
, int hash
, unsigned int access
,
79 unsigned int sharing
)
82 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
83 unsigned int existing_access
= 0;
85 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
87 if (strcmp( file
->name
, name
)) continue;
88 existing_sharing
&= file
->sharing
;
89 existing_access
|= file
->access
;
91 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) goto error
;
92 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) goto error
;
93 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) goto error
;
94 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) goto error
;
97 set_error( ERROR_SHARING_VIOLATION
);
101 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
,
105 if ((file
= alloc_object( &file_ops
)))
109 file
->select
.fd
= fd
;
110 file
->select
.func
= default_select_event
;
111 file
->select
.private = file
;
112 file
->access
= access
;
114 file
->sharing
= sharing
;
115 register_select_user( &file
->select
);
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( ERROR_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 ERROR_ALREADY_EXISTS if file existed before */
156 if ((fd
= open( name
, flags
| O_NONBLOCK
,
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( ERROR_ACCESS_DENIED
);
167 if (!(file
= create_file_for_fd( fd
, access
, sharing
, attrs
))) goto error
;
169 file
->next
= file_hash
[hash
];
170 file_hash
[hash
] = file
;
176 if (fd
!= -1) close( fd
);
181 /* Create an anonymous Unix file */
182 int create_anonymous_file(void)
189 if (!(name
= tmpnam(NULL
)))
191 set_error( ERROR_TOO_MANY_OPEN_FILES
);
194 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
195 } while ((fd
== -1) && (errno
== EEXIST
));
205 /* Create a temp file for anonymous mappings */
206 struct file
*create_temp_file( int access
)
211 if ((fd
= create_anonymous_file()) == -1) return NULL
;
212 if (!(file
= create_file_for_fd( fd
, access
, 0, 0 ))) close( fd
);
216 static void file_dump( struct object
*obj
, int verbose
)
218 struct file
*file
= (struct file
*)obj
;
219 assert( obj
->ops
== &file_ops
);
220 fprintf( stderr
, "File fd=%d flags=%08x name='%s'\n",
221 file
->select
.fd
, file
->flags
, file
->name
);
224 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
226 struct file
*file
= (struct file
*)obj
;
227 assert( obj
->ops
== &file_ops
);
228 if (!obj
->head
) /* first on the queue */
231 if (file
->access
& GENERIC_READ
) events
|= READ_EVENT
;
232 if (file
->access
& GENERIC_WRITE
) events
|= WRITE_EVENT
;
233 set_select_events( &file
->select
, events
);
235 add_queue( obj
, entry
);
239 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
241 struct file
*file
= (struct file
*)grab_object(obj
);
242 assert( obj
->ops
== &file_ops
);
244 remove_queue( obj
, entry
);
245 if (!obj
->head
) /* last on the queue is gone */
246 set_select_events( &file
->select
, 0 );
247 release_object( obj
);
250 static int file_signaled( struct object
*obj
, struct thread
*thread
)
253 struct file
*file
= (struct file
*)obj
;
254 assert( obj
->ops
== &file_ops
);
256 if (file
->access
& GENERIC_READ
) events
|= READ_EVENT
;
257 if (file
->access
& GENERIC_WRITE
) events
|= WRITE_EVENT
;
258 if (check_select_events( &file
->select
, events
))
260 /* stop waiting on select() if we are signaled */
261 set_select_events( &file
->select
, 0 );
266 /* restart waiting on select() if we are no longer signaled */
267 if (obj
->head
) set_select_events( &file
->select
, events
);
272 static int file_get_read_fd( struct object
*obj
)
274 struct file
*file
= (struct file
*)obj
;
275 assert( obj
->ops
== &file_ops
);
276 return dup( file
->select
.fd
);
279 static int file_get_write_fd( struct object
*obj
)
281 struct file
*file
= (struct file
*)obj
;
282 assert( obj
->ops
== &file_ops
);
283 return dup( file
->select
.fd
);
286 static int file_flush( struct object
*obj
)
289 struct file
*file
= (struct file
*)grab_object(obj
);
290 assert( obj
->ops
== &file_ops
);
292 ret
= (fsync( file
->select
.fd
) != -1);
293 if (!ret
) file_set_error();
294 release_object( file
);
298 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
)
301 struct file
*file
= (struct file
*)obj
;
302 assert( obj
->ops
== &file_ops
);
304 if (fstat( file
->select
.fd
, &st
) == -1)
309 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
310 S_ISSOCK(st
.st_mode
) || isatty(file
->select
.fd
)) req
->type
= FILE_TYPE_CHAR
;
311 else req
->type
= FILE_TYPE_DISK
;
312 if (S_ISDIR(st
.st_mode
)) req
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
313 else req
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
314 if (!(st
.st_mode
& S_IWUSR
)) req
->attr
|= FILE_ATTRIBUTE_READONLY
;
315 req
->access_time
= st
.st_atime
;
316 req
->write_time
= st
.st_mtime
;
318 req
->size_low
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
319 req
->links
= st
.st_nlink
;
320 req
->index_high
= st
.st_dev
;
321 req
->index_low
= st
.st_ino
;
322 req
->serial
= 0; /* FIXME */
326 static void file_destroy( struct object
*obj
)
328 struct file
*file
= (struct file
*)obj
;
329 assert( obj
->ops
== &file_ops
);
333 /* remove it from the hashing list */
334 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
335 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
337 *pptr
= (*pptr
)->next
;
338 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
341 unregister_select_user( &file
->select
);
342 close( file
->select
.fd
);
345 /* set the last error depending on errno */
346 void file_set_error(void)
350 case EAGAIN
: set_error( ERROR_SHARING_VIOLATION
); break;
351 case EBADF
: set_error( ERROR_INVALID_HANDLE
); break;
352 case ENOSPC
: set_error( ERROR_HANDLE_DISK_FULL
); break;
354 case EPERM
: set_error( ERROR_ACCESS_DENIED
); break;
355 case EROFS
: set_error( ERROR_WRITE_PROTECT
); break;
356 case EBUSY
: set_error( ERROR_LOCK_VIOLATION
); break;
357 case ENOENT
: set_error( ERROR_FILE_NOT_FOUND
); break;
358 case EISDIR
: set_error( ERROR_CANNOT_MAKE
); break;
360 case EMFILE
: set_error( ERROR_NO_MORE_FILES
); break;
361 case EEXIST
: set_error( ERROR_FILE_EXISTS
); break;
362 case EINVAL
: set_error( ERROR_INVALID_PARAMETER
); break;
363 case ESPIPE
: set_error( ERROR_SEEK
); break;
364 case ENOTEMPTY
: set_error( ERROR_DIR_NOT_EMPTY
); break;
365 default: perror("file_set_error"); set_error( ERROR_UNKNOWN
); break;
369 struct file
*get_file_obj( struct process
*process
, int handle
,
370 unsigned int access
)
372 return (struct file
*)get_handle_obj( current
->process
, handle
,
376 int file_get_mmap_fd( struct file
*file
)
378 return dup( file
->select
.fd
);
381 static int set_file_pointer( int handle
, int *low
, int *high
, int whence
)
388 fprintf( stderr
, "set_file_pointer: offset > 4Gb not supported yet\n" );
389 set_error( ERROR_INVALID_PARAMETER
);
393 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
395 if ((result
= lseek( file
->select
.fd
, *low
, whence
)) == -1)
397 /* Check for seek before start of file */
398 if ((errno
== EINVAL
) && (whence
!= SEEK_SET
) && (*low
< 0))
399 set_error( ERROR_NEGATIVE_SEEK
);
402 release_object( file
);
406 release_object( file
);
410 static int truncate_file( int handle
)
415 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
417 if (((result
= lseek( file
->select
.fd
, 0, SEEK_CUR
)) == -1) ||
418 (ftruncate( file
->select
.fd
, result
) == -1))
421 release_object( file
);
424 release_object( file
);
429 /* try to grow the file to the specified size */
430 int grow_file( struct file
*file
, int size_high
, int size_low
)
436 set_error( ERROR_INVALID_PARAMETER
);
439 if (fstat( file
->select
.fd
, &st
) == -1)
444 if (st
.st_size
>= size_low
) return 1; /* already large enough */
445 if (ftruncate( file
->select
.fd
, size_low
) != -1) return 1;
450 static int set_file_time( int handle
, time_t access_time
, time_t write_time
)
453 struct utimbuf utimbuf
;
455 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
457 if (!access_time
|| !write_time
)
460 if (stat( file
->name
, &st
) == -1) goto error
;
461 if (!access_time
) access_time
= st
.st_atime
;
462 if (!write_time
) write_time
= st
.st_mtime
;
464 utimbuf
.actime
= access_time
;
465 utimbuf
.modtime
= write_time
;
466 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
467 release_object( file
);
471 release_object( file
);
475 static int file_lock( struct file
*file
, int offset_high
, int offset_low
,
476 int count_high
, int count_low
)
478 /* FIXME: implement this */
482 static int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
483 int count_high
, int count_low
)
485 /* FIXME: implement this */
490 DECL_HANDLER(create_file
)
492 size_t len
= get_req_strlen( req
->name
);
496 if ((file
= create_file( req
->name
, len
, req
->access
,
497 req
->sharing
, req
->create
, req
->attrs
)))
499 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
500 release_object( file
);
504 /* allocate a file handle for a Unix fd */
505 DECL_HANDLER(alloc_file_handle
)
510 if ((fd
= dup(fd
)) != -1)
512 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0 )))
514 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, 0 );
515 release_object( file
);
519 else file_set_error();
522 /* get a Unix fd to read from a file */
523 DECL_HANDLER(get_read_fd
)
527 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_READ
, NULL
)))
529 set_reply_fd( current
, obj
->ops
->get_read_fd( obj
) );
530 release_object( obj
);
534 /* get a Unix fd to write to a file */
535 DECL_HANDLER(get_write_fd
)
539 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
541 set_reply_fd( current
, obj
->ops
->get_write_fd( obj
) );
542 release_object( obj
);
546 /* set a file current position */
547 DECL_HANDLER(set_file_pointer
)
549 int high
= req
->high
;
551 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
553 req
->new_high
= high
;
556 /* truncate (or extend) a file */
557 DECL_HANDLER(truncate_file
)
559 truncate_file( req
->handle
);
562 /* flush a file buffers */
563 DECL_HANDLER(flush_file
)
567 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
569 obj
->ops
->flush( obj
);
570 release_object( obj
);
574 /* set a file access and modification times */
575 DECL_HANDLER(set_file_time
)
577 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
580 /* get a file information */
581 DECL_HANDLER(get_file_info
)
585 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
587 obj
->ops
->get_file_info( obj
, req
);
588 release_object( obj
);
592 /* lock a region of a file */
593 DECL_HANDLER(lock_file
)
597 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
599 file_lock( file
, req
->offset_high
, req
->offset_low
,
600 req
->count_high
, req
->count_low
);
601 release_object( file
);
605 /* unlock a region of a file */
606 DECL_HANDLER(unlock_file
)
610 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
612 file_unlock( file
, req
->offset_high
, req
->offset_low
,
613 req
->count_high
, req
->count_low
);
614 release_object( file
);