2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
15 #ifdef HAVE_SYS_ERRNO_H
16 #include <sys/errno.h>
20 #include <sys/types.h>
34 struct object obj
; /* object header */
35 struct select_user select
; /* select user */
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_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
49 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
50 static int file_signaled( struct object
*obj
, struct thread
*thread
);
51 static int file_get_read_fd( struct object
*obj
);
52 static int file_get_write_fd( struct object
*obj
);
53 static int file_flush( struct object
*obj
);
54 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
);
55 static void file_destroy( struct object
*obj
);
57 static const struct object_ops file_ops
=
73 static int get_name_hash( const char *name
)
76 while (*name
) hash
^= (unsigned char)*name
++;
77 return hash
% NAME_HASH_SIZE
;
80 /* check if the desired access is possible without violating */
81 /* the sharing mode of other opens of the same file */
82 static int check_sharing( const char *name
, int hash
, unsigned int access
,
83 unsigned int sharing
)
86 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
87 unsigned int existing_access
= 0;
89 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
91 if (strcmp( file
->name
, name
)) continue;
92 existing_sharing
&= file
->sharing
;
93 existing_access
|= file
->access
;
95 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) goto error
;
96 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) goto error
;
97 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) goto error
;
98 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) goto error
;
101 set_error( ERROR_SHARING_VIOLATION
);
105 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
,
109 if ((file
= alloc_object( &file_ops
)))
113 file
->select
.fd
= fd
;
114 file
->select
.func
= default_select_event
;
115 file
->select
.private = file
;
116 file
->access
= access
;
118 file
->sharing
= sharing
;
119 register_select_user( &file
->select
);
125 static struct file
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
126 unsigned int sharing
, int create
, unsigned int attrs
)
134 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
135 memcpy( name
, nameptr
, len
);
138 /* check sharing mode */
139 hash
= get_name_hash( name
);
140 if (!check_sharing( name
, hash
, access
, sharing
)) goto error
;
144 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
145 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
146 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
147 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
148 case OPEN_EXISTING
: flags
= 0; break;
149 default: set_error( ERROR_INVALID_PARAMETER
); goto error
;
151 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
154 case GENERIC_READ
: flags
|= O_RDONLY
; break;
155 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
156 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
159 /* FIXME: should set error to ERROR_ALREADY_EXISTS if file existed before */
160 if ((fd
= open( name
, flags
| O_NONBLOCK
,
161 (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666 )) == -1)
163 /* refuse to open a directory */
164 if (fstat( fd
, &st
) == -1) goto file_error
;
165 if (S_ISDIR(st
.st_mode
))
167 set_error( ERROR_ACCESS_DENIED
);
171 if (!(file
= create_file_for_fd( fd
, access
, sharing
, attrs
))) goto error
;
173 file
->next
= file_hash
[hash
];
174 file_hash
[hash
] = file
;
180 if (fd
!= -1) close( fd
);
185 /* Create an anonymous Unix file */
186 int create_anonymous_file(void)
193 if (!(name
= tmpnam(NULL
)))
195 set_error( ERROR_TOO_MANY_OPEN_FILES
);
198 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
199 } while ((fd
== -1) && (errno
== EEXIST
));
209 /* Create a temp file for anonymous mappings */
210 struct file
*create_temp_file( int access
)
215 if ((fd
= create_anonymous_file()) == -1) return NULL
;
216 if (!(file
= create_file_for_fd( fd
, access
, 0, 0 ))) close( fd
);
220 static void file_dump( struct object
*obj
, int verbose
)
222 struct file
*file
= (struct file
*)obj
;
223 assert( obj
->ops
== &file_ops
);
224 fprintf( stderr
, "File fd=%d flags=%08x name='%s'\n",
225 file
->select
.fd
, file
->flags
, file
->name
);
228 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
230 struct file
*file
= (struct file
*)obj
;
231 assert( obj
->ops
== &file_ops
);
232 if (!obj
->head
) /* first on the queue */
235 if (file
->access
& GENERIC_READ
) events
|= READ_EVENT
;
236 if (file
->access
& GENERIC_WRITE
) events
|= WRITE_EVENT
;
237 set_select_events( &file
->select
, events
);
239 add_queue( obj
, entry
);
243 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
245 struct file
*file
= (struct file
*)grab_object(obj
);
246 assert( obj
->ops
== &file_ops
);
248 remove_queue( obj
, entry
);
249 if (!obj
->head
) /* last on the queue is gone */
250 set_select_events( &file
->select
, 0 );
251 release_object( obj
);
254 static int file_signaled( struct object
*obj
, struct thread
*thread
)
257 struct file
*file
= (struct file
*)obj
;
258 assert( obj
->ops
== &file_ops
);
260 if (file
->access
& GENERIC_READ
) events
|= READ_EVENT
;
261 if (file
->access
& GENERIC_WRITE
) events
|= WRITE_EVENT
;
262 if (check_select_events( &file
->select
, events
))
264 /* stop waiting on select() if we are signaled */
265 set_select_events( &file
->select
, 0 );
270 /* restart waiting on select() if we are no longer signaled */
271 if (obj
->head
) set_select_events( &file
->select
, events
);
276 static int file_get_read_fd( struct object
*obj
)
278 struct file
*file
= (struct file
*)obj
;
279 assert( obj
->ops
== &file_ops
);
280 return dup( file
->select
.fd
);
283 static int file_get_write_fd( struct object
*obj
)
285 struct file
*file
= (struct file
*)obj
;
286 assert( obj
->ops
== &file_ops
);
287 return dup( file
->select
.fd
);
290 static int file_flush( struct object
*obj
)
293 struct file
*file
= (struct file
*)grab_object(obj
);
294 assert( obj
->ops
== &file_ops
);
296 ret
= (fsync( file
->select
.fd
) != -1);
297 if (!ret
) file_set_error();
298 release_object( file
);
302 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
)
305 struct file
*file
= (struct file
*)obj
;
306 assert( obj
->ops
== &file_ops
);
308 if (fstat( file
->select
.fd
, &st
) == -1)
313 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
314 S_ISSOCK(st
.st_mode
) || isatty(file
->select
.fd
)) req
->type
= FILE_TYPE_CHAR
;
315 else req
->type
= FILE_TYPE_DISK
;
316 if (S_ISDIR(st
.st_mode
)) req
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
317 else req
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
318 if (!(st
.st_mode
& S_IWUSR
)) req
->attr
|= FILE_ATTRIBUTE_READONLY
;
319 req
->access_time
= st
.st_atime
;
320 req
->write_time
= st
.st_mtime
;
322 req
->size_low
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
323 req
->links
= st
.st_nlink
;
324 req
->index_high
= st
.st_dev
;
325 req
->index_low
= st
.st_ino
;
326 req
->serial
= 0; /* FIXME */
330 static void file_destroy( struct object
*obj
)
332 struct file
*file
= (struct file
*)obj
;
333 assert( obj
->ops
== &file_ops
);
337 /* remove it from the hashing list */
338 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
339 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
341 *pptr
= (*pptr
)->next
;
342 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
345 unregister_select_user( &file
->select
);
346 close( file
->select
.fd
);
349 /* set the last error depending on errno */
350 void file_set_error(void)
354 case EAGAIN
: set_error( ERROR_SHARING_VIOLATION
); break;
355 case EBADF
: set_error( ERROR_INVALID_HANDLE
); break;
356 case ENOSPC
: set_error( ERROR_HANDLE_DISK_FULL
); break;
358 case EPERM
: set_error( ERROR_ACCESS_DENIED
); break;
359 case EROFS
: set_error( ERROR_WRITE_PROTECT
); break;
360 case EBUSY
: set_error( ERROR_LOCK_VIOLATION
); break;
361 case ENOENT
: set_error( ERROR_FILE_NOT_FOUND
); break;
362 case EISDIR
: set_error( ERROR_CANNOT_MAKE
); break;
364 case EMFILE
: set_error( ERROR_NO_MORE_FILES
); break;
365 case EEXIST
: set_error( ERROR_FILE_EXISTS
); break;
366 case EINVAL
: set_error( ERROR_INVALID_PARAMETER
); break;
367 case ESPIPE
: set_error( ERROR_SEEK
); break;
368 case ENOTEMPTY
: set_error( ERROR_DIR_NOT_EMPTY
); break;
369 case EIO
: set_error( ERROR_NOACCESS
); break;
370 default: perror("file_set_error"); set_error( ERROR_UNKNOWN
); break;
374 struct file
*get_file_obj( struct process
*process
, int handle
,
375 unsigned int access
)
377 return (struct file
*)get_handle_obj( current
->process
, handle
,
381 int file_get_mmap_fd( struct file
*file
)
383 return dup( file
->select
.fd
);
386 static int set_file_pointer( int handle
, int *low
, int *high
, int whence
)
393 fprintf( stderr
, "set_file_pointer: offset > 4Gb not supported yet\n" );
394 set_error( ERROR_INVALID_PARAMETER
);
398 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
400 if ((result
= lseek( file
->select
.fd
, *low
, whence
)) == -1)
402 /* Check for seek before start of file */
403 if ((errno
== EINVAL
) && (whence
!= SEEK_SET
) && (*low
< 0))
404 set_error( ERROR_NEGATIVE_SEEK
);
407 release_object( file
);
411 release_object( file
);
415 static int truncate_file( int handle
)
420 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
422 if (((result
= lseek( file
->select
.fd
, 0, SEEK_CUR
)) == -1) ||
423 (ftruncate( file
->select
.fd
, result
) == -1))
426 release_object( file
);
429 release_object( file
);
434 /* try to grow the file to the specified size */
435 int grow_file( struct file
*file
, int size_high
, int size_low
)
441 set_error( ERROR_INVALID_PARAMETER
);
444 if (fstat( file
->select
.fd
, &st
) == -1)
449 if (st
.st_size
>= size_low
) return 1; /* already large enough */
450 if (ftruncate( file
->select
.fd
, size_low
) != -1) return 1;
455 static int set_file_time( int handle
, time_t access_time
, time_t write_time
)
458 struct utimbuf utimbuf
;
460 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
462 if (!access_time
|| !write_time
)
465 if (stat( file
->name
, &st
) == -1) goto error
;
466 if (!access_time
) access_time
= st
.st_atime
;
467 if (!write_time
) write_time
= st
.st_mtime
;
469 utimbuf
.actime
= access_time
;
470 utimbuf
.modtime
= write_time
;
471 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
472 release_object( file
);
476 release_object( file
);
480 static int file_lock( struct file
*file
, int offset_high
, int offset_low
,
481 int count_high
, int count_low
)
483 /* FIXME: implement this */
487 static int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
488 int count_high
, int count_low
)
490 /* FIXME: implement this */
495 DECL_HANDLER(create_file
)
497 size_t len
= get_req_strlen( req
->name
);
501 if ((file
= create_file( req
->name
, len
, req
->access
,
502 req
->sharing
, req
->create
, req
->attrs
)))
504 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
505 release_object( file
);
509 /* allocate a file handle for a Unix fd */
510 DECL_HANDLER(alloc_file_handle
)
515 if ((fd
= dup(fd
)) != -1)
517 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0 )))
519 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, 0 );
520 release_object( file
);
524 else file_set_error();
527 /* get a Unix fd to read from a file */
528 DECL_HANDLER(get_read_fd
)
532 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_READ
, NULL
)))
534 set_reply_fd( current
, obj
->ops
->get_read_fd( obj
) );
535 release_object( obj
);
539 /* get a Unix fd to write to a file */
540 DECL_HANDLER(get_write_fd
)
544 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
546 set_reply_fd( current
, obj
->ops
->get_write_fd( obj
) );
547 release_object( obj
);
551 /* set a file current position */
552 DECL_HANDLER(set_file_pointer
)
554 int high
= req
->high
;
556 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
558 req
->new_high
= high
;
561 /* truncate (or extend) a file */
562 DECL_HANDLER(truncate_file
)
564 truncate_file( req
->handle
);
567 /* flush a file buffers */
568 DECL_HANDLER(flush_file
)
572 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
574 obj
->ops
->flush( obj
);
575 release_object( obj
);
579 /* set a file access and modification times */
580 DECL_HANDLER(set_file_time
)
582 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
585 /* get a file information */
586 DECL_HANDLER(get_file_info
)
590 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
592 obj
->ops
->get_file_info( obj
, req
);
593 release_object( obj
);
597 /* lock a region of a file */
598 DECL_HANDLER(lock_file
)
602 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
604 file_lock( file
, req
->offset_high
, req
->offset_low
,
605 req
->count_high
, req
->count_low
);
606 release_object( file
);
610 /* unlock a region of a file */
611 DECL_HANDLER(unlock_file
)
615 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
617 file_unlock( file
, req
->offset_high
, req
->offset_low
,
618 req
->count_high
, req
->count_low
);
619 release_object( file
);