2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
16 #ifdef HAVE_SYS_ERRNO_H
17 #include <sys/errno.h>
21 #include <sys/types.h>
36 struct object obj
; /* object header */
37 struct file
*next
; /* next file in hashing list */
38 char *name
; /* file name */
39 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
40 unsigned int flags
; /* flags (FILE_FLAG_*) */
41 unsigned int sharing
; /* file sharing mode */
42 int drive_type
; /* type of drive the file is on */
43 struct async_queue read_q
;
44 struct async_queue write_q
;
47 #define NAME_HASH_SIZE 37
49 static struct file
*file_hash
[NAME_HASH_SIZE
];
51 static void file_dump( struct object
*obj
, int verbose
);
52 static int file_get_poll_events( struct object
*obj
);
53 static void file_poll_event( struct object
*obj
, int event
);
54 static int file_get_fd( struct object
*obj
);
55 static int file_flush( struct object
*obj
);
56 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
, int *flags
);
57 static void file_destroy( struct object
*obj
);
58 static struct async_queue
* file_queue_async(struct object
*obj
, struct async
* async
, int type
, int count
);
60 static const struct object_ops file_ops
=
62 sizeof(struct file
), /* size */
64 default_poll_add_queue
, /* add_queue */
65 default_poll_remove_queue
, /* remove_queue */
66 default_poll_signaled
, /* signaled */
67 no_satisfied
, /* satisfied */
68 file_get_poll_events
, /* get_poll_events */
69 file_poll_event
, /* poll_event */
70 file_get_fd
, /* get_fd */
71 file_flush
, /* flush */
72 file_get_info
, /* get_file_info */
73 file_queue_async
, /* queue_async */
74 file_destroy
/* destroy */
78 static int get_name_hash( const char *name
)
81 while (*name
) hash
^= (unsigned char)*name
++;
82 return hash
% NAME_HASH_SIZE
;
85 /* check if the desired access is possible without violating */
86 /* the sharing mode of other opens of the same file */
87 static int check_sharing( const char *name
, int hash
, unsigned int access
,
88 unsigned int sharing
)
91 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
92 unsigned int existing_access
= 0;
94 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
96 if (strcmp( file
->name
, name
)) continue;
97 existing_sharing
&= file
->sharing
;
98 existing_access
|= file
->access
;
100 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) goto error
;
101 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) goto error
;
102 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) goto error
;
103 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) goto error
;
106 set_error( STATUS_SHARING_VIOLATION
);
110 /* create a file from a file descriptor */
111 /* if the function fails the fd is closed */
112 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
,
113 unsigned int attrs
, int drive_type
)
116 if ((file
= alloc_object( &file_ops
, fd
)))
120 file
->access
= access
;
122 file
->sharing
= sharing
;
123 file
->drive_type
= drive_type
;
124 if (file
->flags
& FILE_FLAG_OVERLAPPED
)
126 init_async_queue (&file
->read_q
);
127 init_async_queue (&file
->write_q
);
134 static struct file
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
135 unsigned int sharing
, int create
, unsigned int attrs
,
145 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
146 memcpy( name
, nameptr
, len
);
149 /* check sharing mode */
150 hash
= get_name_hash( name
);
151 if (!check_sharing( name
, hash
, access
, sharing
)) goto error
;
155 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
156 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
157 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
158 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
159 case OPEN_EXISTING
: flags
= 0; break;
160 default: set_error( STATUS_INVALID_PARAMETER
); goto error
;
162 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
165 case GENERIC_READ
: flags
|= O_RDONLY
; break;
166 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
167 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
169 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
172 (!strcasecmp( name
+ len
- 4, ".exe" ) || !strcasecmp( name
+ len
- 4, ".com" )))
175 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
176 if ((fd
= open( name
, flags
| O_NONBLOCK
| O_LARGEFILE
, mode
)) == -1 )
178 /* refuse to open a directory */
179 if (fstat( fd
, &st
) == -1) goto file_error
;
180 if (S_ISDIR(st
.st_mode
))
182 set_error( STATUS_ACCESS_DENIED
);
186 if (!(file
= create_file_for_fd( fd
, access
, sharing
, attrs
, drive_type
)))
192 file
->next
= file_hash
[hash
];
193 file_hash
[hash
] = file
;
199 if (fd
!= -1) close( fd
);
204 /* check if two file objects point to the same file */
205 int is_same_file( struct file
*file1
, struct file
*file2
)
207 return !strcmp( file1
->name
, file2
->name
);
210 /* get the type of drive the file is on */
211 int get_file_drive_type( struct file
*file
)
213 return file
->drive_type
;
216 /* Create an anonymous Unix file */
217 int create_anonymous_file(void)
224 if (!(name
= tmpnam(NULL
)))
226 set_error( STATUS_TOO_MANY_OPENED_FILES
);
229 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
230 } while ((fd
== -1) && (errno
== EEXIST
));
240 /* Create a temp file for anonymous mappings */
241 struct file
*create_temp_file( int access
)
245 if ((fd
= create_anonymous_file()) == -1) return NULL
;
246 return create_file_for_fd( fd
, access
, 0, 0, DRIVE_FIXED
);
249 static void file_dump( struct object
*obj
, int verbose
)
251 struct file
*file
= (struct file
*)obj
;
252 assert( obj
->ops
== &file_ops
);
253 fprintf( stderr
, "File fd=%d flags=%08x name='%s'\n", file
->obj
.fd
, file
->flags
, file
->name
);
256 static int file_get_poll_events( struct object
*obj
)
258 struct file
*file
= (struct file
*)obj
;
260 assert( obj
->ops
== &file_ops
);
261 if (file
->access
& GENERIC_READ
) events
|= POLLIN
;
262 if (file
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
266 static void file_poll_event( struct object
*obj
, int event
)
268 struct file
*file
= (struct file
*)obj
;
269 assert( obj
->ops
== &file_ops
);
270 if ( file
->flags
& FILE_FLAG_OVERLAPPED
)
272 if( IS_READY(file
->read_q
) && (POLLIN
& event
) )
274 async_notify(file
->read_q
.head
, STATUS_ALERTED
);
277 if( IS_READY(file
->write_q
) && (POLLOUT
& event
) )
279 async_notify(file
->write_q
.head
, STATUS_ALERTED
);
283 default_poll_event( obj
, event
);
287 static int file_get_fd( struct object
*obj
)
289 struct file
*file
= (struct file
*)obj
;
290 assert( obj
->ops
== &file_ops
);
294 static int file_flush( struct object
*obj
)
297 struct file
*file
= (struct file
*)grab_object(obj
);
298 assert( obj
->ops
== &file_ops
);
300 ret
= (fsync( file
->obj
.fd
) != -1);
301 if (!ret
) file_set_error();
302 release_object( file
);
306 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
, int *flags
)
309 struct file
*file
= (struct file
*)obj
;
310 assert( obj
->ops
== &file_ops
);
314 if (fstat( file
->obj
.fd
, &st
) == -1)
317 return FD_TYPE_INVALID
;
319 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
320 S_ISSOCK(st
.st_mode
) || isatty(file
->obj
.fd
)) reply
->type
= FILE_TYPE_CHAR
;
321 else reply
->type
= FILE_TYPE_DISK
;
322 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
323 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
324 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
325 reply
->access_time
= st
.st_atime
;
326 reply
->write_time
= st
.st_mtime
;
327 if (S_ISDIR(st
.st_mode
))
329 reply
->size_high
= 0;
334 reply
->size_high
= st
.st_size
>> 32;
335 reply
->size_low
= st
.st_size
& 0xffffffff;
337 reply
->links
= st
.st_nlink
;
338 reply
->index_high
= st
.st_dev
;
339 reply
->index_low
= st
.st_ino
;
340 reply
->serial
= 0; /* FIXME */
343 if (file
->flags
& FILE_FLAG_OVERLAPPED
) *flags
|= FD_FLAG_OVERLAPPED
;
344 return FD_TYPE_DEFAULT
;
347 static struct async_queue
*file_queue_async(struct object
*obj
, struct async
*async
, int type
, int count
)
349 struct file
*file
= (struct file
*)obj
;
350 struct async_queue
*q
;
352 assert( obj
->ops
== &file_ops
);
354 if ( !(file
->flags
& FILE_FLAG_OVERLAPPED
) )
356 set_error ( STATUS_INVALID_HANDLE
);
362 case ASYNC_TYPE_READ
:
365 case ASYNC_TYPE_WRITE
:
369 set_error( STATUS_INVALID_PARAMETER
);
373 if(async
&& !async
->q
)
374 async_insert(q
, async
);
379 static void file_destroy( struct object
*obj
)
381 struct file
*file
= (struct file
*)obj
;
382 assert( obj
->ops
== &file_ops
);
386 /* remove it from the hashing list */
387 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
388 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
390 *pptr
= (*pptr
)->next
;
391 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
394 if (file
->flags
& FILE_FLAG_OVERLAPPED
)
396 destroy_async_queue (&file
->read_q
);
397 destroy_async_queue (&file
->write_q
);
401 /* set the last error depending on errno */
402 void file_set_error(void)
406 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
407 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
408 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
411 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
412 case EROFS
: set_error( STATUS_MEDIA_WRITE_PROTECTED
); break;
413 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
414 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
415 case EISDIR
: set_error( 0xc0010000 | ERROR_CANNOT_MAKE
/* FIXME */ ); break;
417 case EMFILE
: set_error( STATUS_NO_MORE_FILES
); break;
418 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
419 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
420 case ESPIPE
: set_error( 0xc0010000 | ERROR_SEEK
/* FIXME */ ); break;
421 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
422 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
423 default: perror("file_set_error"); set_error( ERROR_UNKNOWN
/* FIXME */ ); break;
427 struct file
*get_file_obj( struct process
*process
, handle_t handle
, unsigned int access
)
429 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
432 static int set_file_pointer( handle_t handle
, unsigned int *low
, int *high
, int whence
)
437 xto
= *low
+((off_t
)*high
<<32);
438 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
440 if ((result
= lseek(file
->obj
.fd
,xto
,whence
))==-1)
442 /* Check for seek before start of file */
444 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
445 if (((errno
== EINVAL
) || (errno
== EPERM
))
446 && (whence
!= SEEK_SET
) && (*high
< 0))
447 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK
/* FIXME */ );
450 release_object( file
);
453 *low
= result
& 0xffffffff;
454 *high
= result
>> 32;
455 release_object( file
);
459 /* extend a file beyond the current end of file */
460 static int extend_file( struct file
*file
, off_t size
)
462 static const char zero
;
464 /* extend the file one byte beyond the requested size and then truncate it */
465 /* this should work around ftruncate implementations that can't extend files */
466 if ((lseek( file
->obj
.fd
, size
, SEEK_SET
) != -1) &&
467 (write( file
->obj
.fd
, &zero
, 1 ) != -1))
469 ftruncate( file
->obj
.fd
, size
);
476 /* truncate file at current position */
477 static int truncate_file( struct file
*file
)
480 off_t pos
= lseek( file
->obj
.fd
, 0, SEEK_CUR
);
481 off_t eof
= lseek( file
->obj
.fd
, 0, SEEK_END
);
483 if (eof
< pos
) ret
= extend_file( file
, pos
);
486 if (ftruncate( file
->obj
.fd
, pos
) != -1) ret
= 1;
487 else file_set_error();
489 lseek( file
->obj
.fd
, pos
, SEEK_SET
); /* restore file pos */
493 /* try to grow the file to the specified size */
494 int grow_file( struct file
*file
, int size_high
, int size_low
)
498 off_t old_pos
, size
= size_low
+ (((off_t
)size_high
)<<32);
500 if (fstat( file
->obj
.fd
, &st
) == -1)
505 if (st
.st_size
>= size
) return 1; /* already large enough */
506 old_pos
= lseek( file
->obj
.fd
, 0, SEEK_CUR
); /* save old pos */
507 ret
= extend_file( file
, size
);
508 lseek( file
->obj
.fd
, old_pos
, SEEK_SET
); /* restore file pos */
512 static int set_file_time( handle_t handle
, time_t access_time
, time_t write_time
)
515 struct utimbuf utimbuf
;
517 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
521 set_error( STATUS_INVALID_HANDLE
);
522 release_object( file
);
525 if (!access_time
|| !write_time
)
528 if (stat( file
->name
, &st
) == -1) goto error
;
529 if (!access_time
) access_time
= st
.st_atime
;
530 if (!write_time
) write_time
= st
.st_mtime
;
532 utimbuf
.actime
= access_time
;
533 utimbuf
.modtime
= write_time
;
534 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
535 release_object( file
);
539 release_object( file
);
543 static int file_lock( struct file
*file
, int offset_high
, int offset_low
,
544 int count_high
, int count_low
)
546 /* FIXME: implement this */
550 static int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
551 int count_high
, int count_low
)
553 /* FIXME: implement this */
558 DECL_HANDLER(create_file
)
563 if ((file
= create_file( get_req_data(), get_req_data_size(), req
->access
,
564 req
->sharing
, req
->create
, req
->attrs
, req
->drive_type
)))
566 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
567 release_object( file
);
571 /* allocate a file handle for a Unix fd */
572 DECL_HANDLER(alloc_file_handle
)
578 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
580 set_error( STATUS_INVALID_HANDLE
);
583 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
586 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
587 release_object( file
);
591 /* get a Unix fd to access a file */
592 DECL_HANDLER(get_handle_fd
)
597 reply
->type
= FD_TYPE_INVALID
;
598 if ((obj
= get_handle_obj( current
->process
, req
->handle
, req
->access
, NULL
)))
600 int fd
= get_handle_fd( current
->process
, req
->handle
, req
->access
);
601 if (fd
!= -1) reply
->fd
= fd
;
602 else if (!get_error())
604 if ((fd
= obj
->ops
->get_fd( obj
)) != -1)
605 send_client_fd( current
->process
, fd
, req
->handle
);
607 reply
->type
= obj
->ops
->get_file_info( obj
, NULL
, &reply
->flags
);
608 release_object( obj
);
612 /* set a file current position */
613 DECL_HANDLER(set_file_pointer
)
615 int high
= req
->high
;
617 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
618 reply
->new_low
= low
;
619 reply
->new_high
= high
;
622 /* truncate (or extend) a file */
623 DECL_HANDLER(truncate_file
)
627 if ((file
= get_file_obj( current
->process
, req
->handle
, GENERIC_WRITE
)))
629 truncate_file( file
);
630 release_object( file
);
634 /* flush a file buffers */
635 DECL_HANDLER(flush_file
)
639 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
641 obj
->ops
->flush( obj
);
642 release_object( obj
);
646 /* set a file access and modification times */
647 DECL_HANDLER(set_file_time
)
649 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
652 /* get a file information */
653 DECL_HANDLER(get_file_info
)
657 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
660 obj
->ops
->get_file_info( obj
, reply
, &flags
);
661 release_object( obj
);
665 /* lock a region of a file */
666 DECL_HANDLER(lock_file
)
670 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
672 file_lock( file
, req
->offset_high
, req
->offset_low
,
673 req
->count_high
, req
->count_low
);
674 release_object( file
);
678 /* unlock a region of a file */
679 DECL_HANDLER(unlock_file
)
683 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
685 file_unlock( file
, req
->offset_high
, req
->offset_low
,
686 req
->count_high
, req
->count_low
);
687 release_object( file
);