2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include <sys/errno.h>
16 #include <sys/types.h>
29 struct object obj
; /* object header */
30 struct file
*next
; /* next file in hashing list */
31 char *name
; /* file name */
32 int fd
; /* Unix file descriptor */
33 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
34 unsigned int flags
; /* flags (FILE_FLAG_*) */
35 unsigned int sharing
; /* file sharing mode */
38 #define NAME_HASH_SIZE 37
40 static struct file
*file_hash
[NAME_HASH_SIZE
];
42 static void file_dump( struct object
*obj
, int verbose
);
43 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
44 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
45 static int file_signaled( struct object
*obj
, struct thread
*thread
);
46 static int file_get_read_fd( struct object
*obj
);
47 static int file_get_write_fd( struct object
*obj
);
48 static int file_flush( struct object
*obj
);
49 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
);
50 static void file_destroy( struct object
*obj
);
52 static const struct object_ops file_ops
=
66 static const struct select_ops select_ops
=
69 NULL
/* we never set a timeout on a file */
73 static int get_name_hash( const char *name
)
76 while (*name
) hash
^= *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
)) return 0;
96 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) return 0;
97 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) return 0;
98 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) return 0;
102 static struct object
*create_file( int fd
, const char *name
, unsigned int access
,
103 unsigned int sharing
, int create
, unsigned int attrs
)
115 SET_ERROR( ERROR_INVALID_PARAMETER
);
119 /* check sharing mode */
120 hash
= get_name_hash( name
);
121 if (!check_sharing( name
, hash
, access
, sharing
))
123 SET_ERROR( ERROR_SHARING_VIOLATION
);
129 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
130 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
131 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
132 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
133 case OPEN_EXISTING
: flags
= 0; break;
134 default: SET_ERROR( ERROR_INVALID_PARAMETER
); return NULL
;
136 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
139 case GENERIC_READ
: flags
|= O_RDONLY
; break;
140 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
141 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
144 /* FIXME: should set error to ERROR_ALREADY_EXISTS if file existed before */
145 if ((fd
= open( name
, flags
| O_NONBLOCK
,
146 (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666 )) == -1)
151 /* Refuse to open a directory */
152 if (fstat( fd
, &st
) == -1)
158 if (S_ISDIR(st
.st_mode
))
160 SET_ERROR( ERROR_ACCESS_DENIED
);
167 if ((fd
= dup(fd
)) == -1)
174 if (!(file
= mem_alloc( sizeof(*file
) )))
181 if (!(file
->name
= mem_alloc( strlen(name
) + 1 )))
187 strcpy( file
->name
, name
);
188 file
->next
= file_hash
[hash
];
189 file_hash
[hash
] = file
;
196 init_object( &file
->obj
, &file_ops
, NULL
);
198 file
->access
= access
;
200 file
->sharing
= sharing
;
205 /* Create a temp file for anonymous mappings */
206 struct file
*create_temp_file( int access
)
214 if (!(name
= tmpnam(NULL
)))
216 SET_ERROR( ERROR_TOO_MANY_OPEN_FILES
);
219 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
220 } while ((fd
== -1) && (errno
== EEXIST
));
228 if (!(file
= mem_alloc( sizeof(*file
) )))
233 init_object( &file
->obj
, &file_ops
, NULL
);
237 file
->access
= access
;
244 static void file_dump( struct object
*obj
, int verbose
)
246 struct file
*file
= (struct file
*)obj
;
247 assert( obj
->ops
== &file_ops
);
248 printf( "File fd=%d flags=%08x name='%s'\n",
249 file
->fd
, file
->flags
, file
->name
);
252 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
254 struct file
*file
= (struct file
*)obj
;
255 assert( obj
->ops
== &file_ops
);
256 if (!obj
->head
) /* first on the queue */
258 if (!add_select_user( file
->fd
, READ_EVENT
| WRITE_EVENT
, &select_ops
, file
))
260 SET_ERROR( ERROR_OUTOFMEMORY
);
264 add_queue( obj
, entry
);
268 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
270 struct file
*file
= (struct file
*)grab_object(obj
);
271 assert( obj
->ops
== &file_ops
);
273 remove_queue( obj
, entry
);
274 if (!obj
->head
) /* last on the queue is gone */
275 remove_select_user( file
->fd
);
276 release_object( obj
);
279 static int file_signaled( struct object
*obj
, struct thread
*thread
)
281 fd_set read_fds
, write_fds
;
282 struct timeval tv
= { 0, 0 };
284 struct file
*file
= (struct file
*)obj
;
285 assert( obj
->ops
== &file_ops
);
287 FD_ZERO( &read_fds
);
288 FD_ZERO( &write_fds
);
289 if (file
->access
& GENERIC_READ
) FD_SET( file
->fd
, &read_fds
);
290 if (file
->access
& GENERIC_WRITE
) FD_SET( file
->fd
, &write_fds
);
291 return select( file
->fd
+ 1, &read_fds
, &write_fds
, NULL
, &tv
) > 0;
294 static int file_get_read_fd( struct object
*obj
)
296 struct file
*file
= (struct file
*)obj
;
297 assert( obj
->ops
== &file_ops
);
298 return dup( file
->fd
);
301 static int file_get_write_fd( struct object
*obj
)
303 struct file
*file
= (struct file
*)obj
;
304 assert( obj
->ops
== &file_ops
);
305 return dup( file
->fd
);
308 static int file_flush( struct object
*obj
)
311 struct file
*file
= (struct file
*)grab_object(obj
);
312 assert( obj
->ops
== &file_ops
);
314 ret
= (fsync( file
->fd
) != -1);
315 if (!ret
) file_set_error();
316 release_object( file
);
320 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
)
323 struct file
*file
= (struct file
*)obj
;
324 assert( obj
->ops
== &file_ops
);
326 if (fstat( file
->fd
, &st
) == -1)
331 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
332 S_ISSOCK(st
.st_mode
) || isatty(file
->fd
)) reply
->type
= FILE_TYPE_CHAR
;
333 else reply
->type
= FILE_TYPE_DISK
;
334 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
335 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
336 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
337 reply
->access_time
= st
.st_atime
;
338 reply
->write_time
= st
.st_mtime
;
339 reply
->size_high
= 0;
340 reply
->size_low
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
341 reply
->links
= st
.st_nlink
;
342 reply
->index_high
= st
.st_dev
;
343 reply
->index_low
= st
.st_ino
;
344 reply
->serial
= 0; /* FIXME */
348 static void file_destroy( struct object
*obj
)
350 struct file
*file
= (struct file
*)obj
;
351 assert( obj
->ops
== &file_ops
);
355 /* remove it from the hashing list */
356 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
357 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
359 *pptr
= (*pptr
)->next
;
360 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
367 /* set the last error depending on errno */
368 void file_set_error(void)
372 case EAGAIN
: SET_ERROR( ERROR_SHARING_VIOLATION
); break;
373 case EBADF
: SET_ERROR( ERROR_INVALID_HANDLE
); break;
374 case ENOSPC
: SET_ERROR( ERROR_HANDLE_DISK_FULL
); break;
376 case EPERM
: SET_ERROR( ERROR_ACCESS_DENIED
); break;
377 case EROFS
: SET_ERROR( ERROR_WRITE_PROTECT
); break;
378 case EBUSY
: SET_ERROR( ERROR_LOCK_VIOLATION
); break;
379 case ENOENT
: SET_ERROR( ERROR_FILE_NOT_FOUND
); break;
380 case EISDIR
: SET_ERROR( ERROR_CANNOT_MAKE
); break;
382 case EMFILE
: SET_ERROR( ERROR_NO_MORE_FILES
); break;
383 case EEXIST
: SET_ERROR( ERROR_FILE_EXISTS
); break;
384 case EINVAL
: SET_ERROR( ERROR_INVALID_PARAMETER
); break;
385 case ESPIPE
: SET_ERROR( ERROR_SEEK
); break;
386 case ENOTEMPTY
: SET_ERROR( ERROR_DIR_NOT_EMPTY
); break;
387 default: perror("file_set_error"); SET_ERROR( ERROR_UNKNOWN
); break;
391 struct file
*get_file_obj( struct process
*process
, int handle
,
392 unsigned int access
)
394 return (struct file
*)get_handle_obj( current
->process
, handle
,
398 int file_get_mmap_fd( struct file
*file
)
400 return dup( file
->fd
);
403 static int set_file_pointer( int handle
, int *low
, int *high
, int whence
)
410 fprintf( stderr
, "set_file_pointer: offset > 4Gb not supported yet\n" );
411 SET_ERROR( ERROR_INVALID_PARAMETER
);
415 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
417 if ((result
= lseek( file
->fd
, *low
, whence
)) == -1)
419 /* Check for seek before start of file */
420 if ((errno
== EINVAL
) && (whence
!= SEEK_SET
) && (*low
< 0))
421 SET_ERROR( ERROR_NEGATIVE_SEEK
);
424 release_object( file
);
428 release_object( file
);
432 static int truncate_file( int handle
)
437 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
439 if (((result
= lseek( file
->fd
, 0, SEEK_CUR
)) == -1) ||
440 (ftruncate( file
->fd
, result
) == -1))
443 release_object( file
);
446 release_object( file
);
451 /* try to grow the file to the specified size */
452 int grow_file( struct file
*file
, int size_high
, int size_low
)
458 SET_ERROR( ERROR_INVALID_PARAMETER
);
461 if (fstat( file
->fd
, &st
) == -1)
466 if (st
.st_size
>= size_low
) return 1; /* already large enough */
467 if (ftruncate( file
->fd
, size_low
) != -1) return 1;
472 static int set_file_time( int handle
, time_t access_time
, time_t write_time
)
475 struct utimbuf utimbuf
;
477 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
479 if (!access_time
|| !write_time
)
482 if (stat( file
->name
, &st
) == -1) goto error
;
483 if (!access_time
) access_time
= st
.st_atime
;
484 if (!write_time
) write_time
= st
.st_mtime
;
486 utimbuf
.actime
= access_time
;
487 utimbuf
.modtime
= write_time
;
488 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
489 release_object( file
);
493 release_object( file
);
497 static int file_lock( struct file
*file
, int offset_high
, int offset_low
,
498 int count_high
, int count_low
)
500 /* FIXME: implement this */
504 static int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
505 int count_high
, int count_low
)
507 /* FIXME: implement this */
511 DECL_HANDLER(create_file
)
513 struct create_file_reply reply
= { -1 };
515 char *name
= (char *)data
;
516 if (!len
) name
= NULL
;
517 else CHECK_STRING( "create_file", name
, len
);
519 if ((obj
= create_file( fd
, name
, req
->access
,
520 req
->sharing
, req
->create
, req
->attrs
)) != NULL
)
522 reply
.handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->inherit
);
523 release_object( obj
);
525 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
528 /* get a Unix fd to read from a file */
529 DECL_HANDLER(get_read_fd
)
534 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_READ
, NULL
)))
536 read_fd
= obj
->ops
->get_read_fd( obj
);
537 release_object( obj
);
540 send_reply( current
, read_fd
, 0 );
543 /* get a Unix fd to write to a file */
544 DECL_HANDLER(get_write_fd
)
549 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
551 write_fd
= obj
->ops
->get_write_fd( obj
);
552 release_object( obj
);
555 send_reply( current
, write_fd
, 0 );
558 /* set a file current position */
559 DECL_HANDLER(set_file_pointer
)
561 struct set_file_pointer_reply reply
;
562 reply
.low
= req
->low
;
563 reply
.high
= req
->high
;
564 set_file_pointer( req
->handle
, &reply
.low
, &reply
.high
, req
->whence
);
565 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
568 /* truncate (or extend) a file */
569 DECL_HANDLER(truncate_file
)
571 truncate_file( req
->handle
);
572 send_reply( current
, -1, 0 );
575 /* flush a file buffers */
576 DECL_HANDLER(flush_file
)
580 if ((obj
= get_handle_obj( current
->process
, req
->handle
, GENERIC_WRITE
, NULL
)))
582 obj
->ops
->flush( obj
);
583 release_object( obj
);
585 send_reply( current
, -1, 0 );
588 /* set a file access and modification times */
589 DECL_HANDLER(set_file_time
)
591 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
592 send_reply( current
, -1, 0 );
595 /* get a file information */
596 DECL_HANDLER(get_file_info
)
599 struct get_file_info_reply reply
;
601 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
603 obj
->ops
->get_file_info( obj
, &reply
);
604 release_object( obj
);
606 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
609 /* lock a region of a file */
610 DECL_HANDLER(lock_file
)
614 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
616 file_lock( file
, req
->offset_high
, req
->offset_low
,
617 req
->count_high
, req
->count_low
);
618 release_object( file
);
620 send_reply( current
, -1, 0 );
623 /* unlock a region of a file */
624 DECL_HANDLER(unlock_file
)
628 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
630 file_unlock( file
, req
->offset_high
, req
->offset_low
,
631 req
->count_high
, req
->count_low
);
632 release_object( file
);
634 send_reply( current
, -1, 0 );