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 file
*next
; /* next file in hashing list */
36 char *name
; /* file name */
37 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
38 unsigned int flags
; /* flags (FILE_FLAG_*) */
39 unsigned int sharing
; /* file sharing mode */
42 #define NAME_HASH_SIZE 37
44 static struct file
*file_hash
[NAME_HASH_SIZE
];
46 static void file_dump( struct object
*obj
, int verbose
);
47 static int file_get_poll_events( struct object
*obj
);
48 static int file_get_read_fd( struct object
*obj
);
49 static int file_get_write_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_read_fd
, /* get_read_fd */
65 file_get_write_fd
, /* get_write_fd */
66 file_flush
, /* flush */
67 file_get_info
, /* get_file_info */
68 file_destroy
/* destroy */
72 static int get_name_hash( const char *name
)
75 while (*name
) hash
^= (unsigned char)*name
++;
76 return hash
% NAME_HASH_SIZE
;
79 /* check if the desired access is possible without violating */
80 /* the sharing mode of other opens of the same file */
81 static int check_sharing( const char *name
, int hash
, unsigned int access
,
82 unsigned int sharing
)
85 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
86 unsigned int existing_access
= 0;
88 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
90 if (strcmp( file
->name
, name
)) continue;
91 existing_sharing
&= file
->sharing
;
92 existing_access
|= file
->access
;
94 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) goto error
;
95 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) goto error
;
96 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) goto error
;
97 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) goto error
;
100 set_error( STATUS_SHARING_VIOLATION
);
104 /* create a file from a file descriptor */
105 /* if the function fails the fd is closed */
106 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
,
110 if ((file
= alloc_object( &file_ops
, fd
)))
114 file
->access
= access
;
116 file
->sharing
= sharing
;
122 static struct file
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
123 unsigned int sharing
, int create
, unsigned int attrs
)
131 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
132 memcpy( name
, nameptr
, len
);
135 /* check sharing mode */
136 hash
= get_name_hash( name
);
137 if (!check_sharing( name
, hash
, access
, sharing
)) goto error
;
141 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
142 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
143 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
144 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
145 case OPEN_EXISTING
: flags
= 0; break;
146 default: set_error( STATUS_INVALID_PARAMETER
); goto error
;
148 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
151 case GENERIC_READ
: flags
|= O_RDONLY
; break;
152 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
153 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
156 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
157 if ((fd
= open( name
, flags
| O_NONBLOCK
,
158 (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666 )) == -1)
160 /* refuse to open a directory */
161 if (fstat( fd
, &st
) == -1) goto file_error
;
162 if (S_ISDIR(st
.st_mode
))
164 set_error( STATUS_ACCESS_DENIED
);
168 if (!(file
= create_file_for_fd( fd
, access
, sharing
, attrs
)))
174 file
->next
= file_hash
[hash
];
175 file_hash
[hash
] = file
;
181 if (fd
!= -1) close( fd
);
186 /* Create an anonymous Unix file */
187 int create_anonymous_file(void)
194 if (!(name
= tmpnam(NULL
)))
196 set_error( STATUS_TOO_MANY_OPENED_FILES
);
199 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
200 } while ((fd
== -1) && (errno
== EEXIST
));
210 /* Create a temp file for anonymous mappings */
211 struct file
*create_temp_file( int access
)
215 if ((fd
= create_anonymous_file()) == -1) return NULL
;
216 return create_file_for_fd( fd
, access
, 0, 0 );
219 static void file_dump( struct object
*obj
, int verbose
)
221 struct file
*file
= (struct file
*)obj
;
222 assert( obj
->ops
== &file_ops
);
223 fprintf( stderr
, "File fd=%d flags=%08x name='%s'\n", file
->obj
.fd
, file
->flags
, file
->name
);
226 static int file_get_poll_events( struct object
*obj
)
228 struct file
*file
= (struct file
*)obj
;
230 assert( obj
->ops
== &file_ops
);
231 if (file
->access
& GENERIC_READ
) events
|= POLLIN
;
232 if (file
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
236 static int file_get_read_fd( struct object
*obj
)
238 struct file
*file
= (struct file
*)obj
;
239 assert( obj
->ops
== &file_ops
);
240 return dup( file
->obj
.fd
);
243 static int file_get_write_fd( struct object
*obj
)
245 struct file
*file
= (struct file
*)obj
;
246 assert( obj
->ops
== &file_ops
);
247 return dup( file
->obj
.fd
);
250 static int file_flush( struct object
*obj
)
253 struct file
*file
= (struct file
*)grab_object(obj
);
254 assert( obj
->ops
== &file_ops
);
256 ret
= (fsync( file
->obj
.fd
) != -1);
257 if (!ret
) file_set_error();
258 release_object( file
);
262 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
)
265 struct file
*file
= (struct file
*)obj
;
266 assert( obj
->ops
== &file_ops
);
268 if (fstat( file
->obj
.fd
, &st
) == -1)
273 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
274 S_ISSOCK(st
.st_mode
) || isatty(file
->obj
.fd
)) req
->type
= FILE_TYPE_CHAR
;
275 else req
->type
= FILE_TYPE_DISK
;
276 if (S_ISDIR(st
.st_mode
)) req
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
277 else req
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
278 if (!(st
.st_mode
& S_IWUSR
)) req
->attr
|= FILE_ATTRIBUTE_READONLY
;
279 req
->access_time
= st
.st_atime
;
280 req
->write_time
= st
.st_mtime
;
282 req
->size_low
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
283 req
->links
= st
.st_nlink
;
284 req
->index_high
= st
.st_dev
;
285 req
->index_low
= st
.st_ino
;
286 req
->serial
= 0; /* FIXME */
290 static void file_destroy( struct object
*obj
)
292 struct file
*file
= (struct file
*)obj
;
293 assert( obj
->ops
== &file_ops
);
297 /* remove it from the hashing list */
298 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
299 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
301 *pptr
= (*pptr
)->next
;
302 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
307 /* set the last error depending on errno */
308 void file_set_error(void)
312 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
313 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
314 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
316 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
317 case EROFS
: set_error( STATUS_MEDIA_WRITE_PROTECTED
); break;
318 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
319 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
320 case EISDIR
: set_error( 0xc0010000 | ERROR_CANNOT_MAKE
/* FIXME */ ); break;
322 case EMFILE
: set_error( STATUS_NO_MORE_FILES
); break;
323 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
324 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
325 case ESPIPE
: set_error( 0xc0010000 | ERROR_SEEK
/* FIXME */ ); break;
326 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
327 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
328 default: perror("file_set_error"); set_error( ERROR_UNKNOWN
/* FIXME */ ); break;
332 struct file
*get_file_obj( struct process
*process
, int handle
, unsigned int access
)
334 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
337 int file_get_mmap_fd( struct file
*file
)
339 return dup( file
->obj
.fd
);
342 static int set_file_pointer( int handle
, int *low
, int *high
, int whence
)
347 if ((*low
>= 0 && *high
!= 0) || (*low
< 0 && *high
!= -1))
349 fprintf( stderr
, "set_file_pointer: offset > 2Gb not supported yet\n" );
350 set_error( STATUS_INVALID_PARAMETER
);
354 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
356 if ((result
= lseek( file
->obj
.fd
, *low
, whence
)) == -1)
358 /* Check for seek before start of file */
359 if ((errno
== EINVAL
) && (whence
!= SEEK_SET
) && (*low
< 0))
360 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK
/* FIXME */ );
363 release_object( file
);
367 release_object( file
);
371 static int truncate_file( int handle
)
376 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
378 if (((result
= lseek( file
->obj
.fd
, 0, SEEK_CUR
)) == -1) ||
379 (ftruncate( file
->obj
.fd
, result
) == -1))
382 release_object( file
);
385 release_object( file
);
390 /* try to grow the file to the specified size */
391 int grow_file( struct file
*file
, int size_high
, int size_low
)
397 set_error( STATUS_INVALID_PARAMETER
);
400 if (fstat( file
->obj
.fd
, &st
) == -1)
405 if (st
.st_size
>= size_low
) return 1; /* already large enough */
406 if (ftruncate( file
->obj
.fd
, size_low
) != -1) return 1;
411 static int set_file_time( int handle
, time_t access_time
, time_t write_time
)
414 struct utimbuf utimbuf
;
416 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
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
)
470 if (current
->pass_fd
!= -1)
472 if ((file
= create_file_for_fd( current
->pass_fd
, req
->access
,
473 FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0 )))
475 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, 0 );
476 release_object( file
);
478 current
->pass_fd
= -1;
480 else set_error( STATUS_INVALID_PARAMETER
);
483 /* get a Unix fd to read from a file */
484 DECL_HANDLER(get_read_fd
)
488 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_READ
, NULL
)))
490 set_reply_fd( current
, obj
->ops
->get_read_fd( obj
) );
491 release_object( obj
);
495 /* get a Unix fd to write to a file */
496 DECL_HANDLER(get_write_fd
)
500 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
502 set_reply_fd( current
, obj
->ops
->get_write_fd( obj
) );
503 release_object( obj
);
507 /* set a file current position */
508 DECL_HANDLER(set_file_pointer
)
510 int high
= req
->high
;
512 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
514 req
->new_high
= high
;
517 /* truncate (or extend) a file */
518 DECL_HANDLER(truncate_file
)
520 truncate_file( req
->handle
);
523 /* flush a file buffers */
524 DECL_HANDLER(flush_file
)
528 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
530 obj
->ops
->flush( obj
);
531 release_object( obj
);
535 /* set a file access and modification times */
536 DECL_HANDLER(set_file_time
)
538 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
541 /* get a file information */
542 DECL_HANDLER(get_file_info
)
546 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
548 obj
->ops
->get_file_info( obj
, req
);
549 release_object( obj
);
553 /* lock a region of a file */
554 DECL_HANDLER(lock_file
)
558 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
560 file_lock( file
, req
->offset_high
, req
->offset_low
,
561 req
->count_high
, req
->count_low
);
562 release_object( file
);
566 /* unlock a region of a file */
567 DECL_HANDLER(unlock_file
)
571 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
573 file_unlock( file
, req
->offset_high
, req
->offset_low
,
574 req
->count_high
, req
->count_low
);
575 release_object( file
);