2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
13 #ifdef HAVE_SYS_ERRNO_H
14 #include <sys/errno.h>
18 #include <sys/types.h>
32 struct object obj
; /* object header */
33 struct select_user select
; /* select user */
34 struct file
*next
; /* next file in hashing list */
35 char *name
; /* file name */
36 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
37 unsigned int flags
; /* flags (FILE_FLAG_*) */
38 unsigned int sharing
; /* file sharing mode */
41 #define NAME_HASH_SIZE 37
43 static struct file
*file_hash
[NAME_HASH_SIZE
];
45 static void file_dump( struct object
*obj
, int verbose
);
46 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
47 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
48 static int file_signaled( struct object
*obj
, struct thread
*thread
);
49 static int file_get_read_fd( struct object
*obj
);
50 static int file_get_write_fd( struct object
*obj
);
51 static int file_flush( struct object
*obj
);
52 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
);
53 static void file_destroy( struct object
*obj
);
55 static const struct object_ops file_ops
=
71 static int get_name_hash( const char *name
)
74 while (*name
) hash
^= *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( ERROR_SHARING_VIOLATION
);
103 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
,
107 if ((file
= alloc_object( &file_ops
)))
111 file
->select
.fd
= fd
;
112 file
->select
.func
= default_select_event
;
113 file
->select
.private = file
;
114 file
->access
= access
;
116 file
->sharing
= sharing
;
117 register_select_user( &file
->select
);
123 static struct file
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
124 unsigned int sharing
, int create
, unsigned int attrs
)
132 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
133 memcpy( name
, nameptr
, len
);
136 /* check sharing mode */
137 hash
= get_name_hash( name
);
138 if (!check_sharing( name
, hash
, access
, sharing
)) goto error
;
142 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
143 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
144 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
145 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
146 case OPEN_EXISTING
: flags
= 0; break;
147 default: set_error( ERROR_INVALID_PARAMETER
); goto error
;
149 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
152 case GENERIC_READ
: flags
|= O_RDONLY
; break;
153 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
154 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
157 /* FIXME: should set error to ERROR_ALREADY_EXISTS if file existed before */
158 if ((fd
= open( name
, flags
| O_NONBLOCK
,
159 (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666 )) == -1)
161 /* refuse to open a directory */
162 if (fstat( fd
, &st
) == -1) goto file_error
;
163 if (S_ISDIR(st
.st_mode
))
165 set_error( ERROR_ACCESS_DENIED
);
169 if (!(file
= create_file_for_fd( fd
, access
, sharing
, attrs
))) goto error
;
171 file
->next
= file_hash
[hash
];
172 file_hash
[hash
] = file
;
178 if (fd
!= -1) close( fd
);
183 /* Create an anonymous Unix file */
184 int create_anonymous_file(void)
191 if (!(name
= tmpnam(NULL
)))
193 set_error( ERROR_TOO_MANY_OPEN_FILES
);
196 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
197 } while ((fd
== -1) && (errno
== EEXIST
));
207 /* Create a temp file for anonymous mappings */
208 struct file
*create_temp_file( int access
)
213 if ((fd
= create_anonymous_file()) == -1) return NULL
;
214 if (!(file
= create_file_for_fd( fd
, access
, 0, 0 ))) close( fd
);
218 static void file_dump( struct object
*obj
, int verbose
)
220 struct file
*file
= (struct file
*)obj
;
221 assert( obj
->ops
== &file_ops
);
222 fprintf( stderr
, "File fd=%d flags=%08x name='%s'\n",
223 file
->select
.fd
, file
->flags
, file
->name
);
226 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
228 struct file
*file
= (struct file
*)obj
;
229 assert( obj
->ops
== &file_ops
);
230 if (!obj
->head
) /* first on the queue */
233 if (file
->access
& GENERIC_READ
) events
|= READ_EVENT
;
234 if (file
->access
& GENERIC_WRITE
) events
|= WRITE_EVENT
;
235 set_select_events( &file
->select
, events
);
237 add_queue( obj
, entry
);
241 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
243 struct file
*file
= (struct file
*)grab_object(obj
);
244 assert( obj
->ops
== &file_ops
);
246 remove_queue( obj
, entry
);
247 if (!obj
->head
) /* last on the queue is gone */
248 set_select_events( &file
->select
, 0 );
249 release_object( obj
);
252 static int file_signaled( struct object
*obj
, struct thread
*thread
)
255 struct file
*file
= (struct file
*)obj
;
256 assert( obj
->ops
== &file_ops
);
258 if (file
->access
& GENERIC_READ
) events
|= READ_EVENT
;
259 if (file
->access
& GENERIC_WRITE
) events
|= WRITE_EVENT
;
260 if (check_select_events( &file
->select
, events
))
262 /* stop waiting on select() if we are signaled */
263 set_select_events( &file
->select
, 0 );
268 /* restart waiting on select() if we are no longer signaled */
269 if (obj
->head
) set_select_events( &file
->select
, events
);
274 static int file_get_read_fd( struct object
*obj
)
276 struct file
*file
= (struct file
*)obj
;
277 assert( obj
->ops
== &file_ops
);
278 return dup( file
->select
.fd
);
281 static int file_get_write_fd( struct object
*obj
)
283 struct file
*file
= (struct file
*)obj
;
284 assert( obj
->ops
== &file_ops
);
285 return dup( file
->select
.fd
);
288 static int file_flush( struct object
*obj
)
291 struct file
*file
= (struct file
*)grab_object(obj
);
292 assert( obj
->ops
== &file_ops
);
294 ret
= (fsync( file
->select
.fd
) != -1);
295 if (!ret
) file_set_error();
296 release_object( file
);
300 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
)
303 struct file
*file
= (struct file
*)obj
;
304 assert( obj
->ops
== &file_ops
);
306 if (fstat( file
->select
.fd
, &st
) == -1)
311 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
312 S_ISSOCK(st
.st_mode
) || isatty(file
->select
.fd
)) req
->type
= FILE_TYPE_CHAR
;
313 else req
->type
= FILE_TYPE_DISK
;
314 if (S_ISDIR(st
.st_mode
)) req
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
315 else req
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
316 if (!(st
.st_mode
& S_IWUSR
)) req
->attr
|= FILE_ATTRIBUTE_READONLY
;
317 req
->access_time
= st
.st_atime
;
318 req
->write_time
= st
.st_mtime
;
320 req
->size_low
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
321 req
->links
= st
.st_nlink
;
322 req
->index_high
= st
.st_dev
;
323 req
->index_low
= st
.st_ino
;
324 req
->serial
= 0; /* FIXME */
328 static void file_destroy( struct object
*obj
)
330 struct file
*file
= (struct file
*)obj
;
331 assert( obj
->ops
== &file_ops
);
335 /* remove it from the hashing list */
336 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
337 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
339 *pptr
= (*pptr
)->next
;
340 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
343 unregister_select_user( &file
->select
);
344 close( file
->select
.fd
);
347 /* set the last error depending on errno */
348 void file_set_error(void)
352 case EAGAIN
: set_error( ERROR_SHARING_VIOLATION
); break;
353 case EBADF
: set_error( ERROR_INVALID_HANDLE
); break;
354 case ENOSPC
: set_error( ERROR_HANDLE_DISK_FULL
); break;
356 case EPERM
: set_error( ERROR_ACCESS_DENIED
); break;
357 case EROFS
: set_error( ERROR_WRITE_PROTECT
); break;
358 case EBUSY
: set_error( ERROR_LOCK_VIOLATION
); break;
359 case ENOENT
: set_error( ERROR_FILE_NOT_FOUND
); break;
360 case EISDIR
: set_error( ERROR_CANNOT_MAKE
); break;
362 case EMFILE
: set_error( ERROR_NO_MORE_FILES
); break;
363 case EEXIST
: set_error( ERROR_FILE_EXISTS
); break;
364 case EINVAL
: set_error( ERROR_INVALID_PARAMETER
); break;
365 case ESPIPE
: set_error( ERROR_SEEK
); break;
366 case ENOTEMPTY
: set_error( ERROR_DIR_NOT_EMPTY
); break;
367 default: perror("file_set_error"); set_error( ERROR_UNKNOWN
); break;
371 struct file
*get_file_obj( struct process
*process
, int handle
,
372 unsigned int access
)
374 return (struct file
*)get_handle_obj( current
->process
, handle
,
378 int file_get_mmap_fd( struct file
*file
)
380 return dup( file
->select
.fd
);
383 static int set_file_pointer( int handle
, int *low
, int *high
, int whence
)
390 fprintf( stderr
, "set_file_pointer: offset > 4Gb not supported yet\n" );
391 set_error( ERROR_INVALID_PARAMETER
);
395 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
397 if ((result
= lseek( file
->select
.fd
, *low
, whence
)) == -1)
399 /* Check for seek before start of file */
400 if ((errno
== EINVAL
) && (whence
!= SEEK_SET
) && (*low
< 0))
401 set_error( ERROR_NEGATIVE_SEEK
);
404 release_object( file
);
408 release_object( file
);
412 static int truncate_file( int handle
)
417 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
419 if (((result
= lseek( file
->select
.fd
, 0, SEEK_CUR
)) == -1) ||
420 (ftruncate( file
->select
.fd
, result
) == -1))
423 release_object( file
);
426 release_object( file
);
431 /* try to grow the file to the specified size */
432 int grow_file( struct file
*file
, int size_high
, int size_low
)
438 set_error( ERROR_INVALID_PARAMETER
);
441 if (fstat( file
->select
.fd
, &st
) == -1)
446 if (st
.st_size
>= size_low
) return 1; /* already large enough */
447 if (ftruncate( file
->select
.fd
, size_low
) != -1) return 1;
452 static int set_file_time( int handle
, time_t access_time
, time_t write_time
)
455 struct utimbuf utimbuf
;
457 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
459 if (!access_time
|| !write_time
)
462 if (stat( file
->name
, &st
) == -1) goto error
;
463 if (!access_time
) access_time
= st
.st_atime
;
464 if (!write_time
) write_time
= st
.st_mtime
;
466 utimbuf
.actime
= access_time
;
467 utimbuf
.modtime
= write_time
;
468 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
469 release_object( file
);
473 release_object( file
);
477 static int file_lock( struct file
*file
, int offset_high
, int offset_low
,
478 int count_high
, int count_low
)
480 /* FIXME: implement this */
484 static int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
485 int count_high
, int count_low
)
487 /* FIXME: implement this */
492 DECL_HANDLER(create_file
)
494 size_t len
= get_req_strlen( req
->name
);
498 if ((file
= create_file( req
->name
, len
, req
->access
,
499 req
->sharing
, req
->create
, req
->attrs
)))
501 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
502 release_object( file
);
506 /* allocate a file handle for a Unix fd */
507 DECL_HANDLER(alloc_file_handle
)
512 if ((fd
= dup(fd
)) != -1)
514 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0 )))
516 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, 0 );
517 release_object( file
);
521 else file_set_error();
524 /* get a Unix fd to read from a file */
525 DECL_HANDLER(get_read_fd
)
529 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_READ
, NULL
)))
531 set_reply_fd( current
, obj
->ops
->get_read_fd( obj
) );
532 release_object( obj
);
536 /* get a Unix fd to write to a file */
537 DECL_HANDLER(get_write_fd
)
541 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
543 set_reply_fd( current
, obj
->ops
->get_write_fd( obj
) );
544 release_object( obj
);
548 /* set a file current position */
549 DECL_HANDLER(set_file_pointer
)
551 int high
= req
->high
;
553 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
555 req
->new_high
= high
;
558 /* truncate (or extend) a file */
559 DECL_HANDLER(truncate_file
)
561 truncate_file( req
->handle
);
564 /* flush a file buffers */
565 DECL_HANDLER(flush_file
)
569 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
571 obj
->ops
->flush( obj
);
572 release_object( obj
);
576 /* set a file access and modification times */
577 DECL_HANDLER(set_file_time
)
579 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
582 /* get a file information */
583 DECL_HANDLER(get_file_info
)
587 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
589 obj
->ops
->get_file_info( obj
, req
);
590 release_object( obj
);
594 /* lock a region of a file */
595 DECL_HANDLER(lock_file
)
599 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
601 file_lock( file
, req
->offset_high
, req
->offset_low
,
602 req
->count_high
, req
->count_low
);
603 release_object( file
);
607 /* unlock a region of a file */
608 DECL_HANDLER(unlock_file
)
612 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
614 file_unlock( file
, req
->offset_high
, req
->offset_low
,
615 req
->count_high
, req
->count_low
);
616 release_object( file
);