2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
36 #include <sys/types.h>
57 struct object obj
; /* object header */
58 struct fd
*fd
; /* file descriptor for this file */
59 struct file
*next
; /* next file in hashing list */
60 char *name
; /* file name */
61 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
62 unsigned int options
; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
63 unsigned int sharing
; /* file sharing mode */
64 int removable
; /* is file on removable media? */
65 struct async_queue read_q
;
66 struct async_queue write_q
;
69 #define NAME_HASH_SIZE 37
71 static struct file
*file_hash
[NAME_HASH_SIZE
];
73 static void file_dump( struct object
*obj
, int verbose
);
74 static struct fd
*file_get_fd( struct object
*obj
);
75 static void file_destroy( struct object
*obj
);
77 static int file_get_poll_events( struct fd
*fd
);
78 static void file_poll_event( struct fd
*fd
, int event
);
79 static int file_flush( struct fd
*fd
, struct event
**event
);
80 static int file_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
);
81 static void file_queue_async( struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
);
83 static const struct object_ops file_ops
=
85 sizeof(struct file
), /* size */
87 default_fd_add_queue
, /* add_queue */
88 default_fd_remove_queue
, /* remove_queue */
89 default_fd_signaled
, /* signaled */
90 no_satisfied
, /* satisfied */
91 file_get_fd
, /* get_fd */
92 file_destroy
/* destroy */
95 static const struct fd_ops file_fd_ops
=
97 file_get_poll_events
, /* get_poll_events */
98 file_poll_event
, /* poll_event */
99 file_flush
, /* flush */
100 file_get_info
, /* get_file_info */
101 file_queue_async
/* queue_async */
104 static inline int is_overlapped( const struct file
*file
)
106 return !(file
->options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
109 static int get_name_hash( const char *name
)
112 while (*name
) hash
^= (unsigned char)*name
++;
113 return hash
% NAME_HASH_SIZE
;
116 /* check if the desired access is possible without violating */
117 /* the sharing mode of other opens of the same file */
118 static int check_sharing( const char *name
, int hash
, unsigned int access
,
119 unsigned int sharing
)
122 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
123 unsigned int existing_access
= 0;
125 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
127 if (strcmp( file
->name
, name
)) continue;
128 existing_sharing
&= file
->sharing
;
129 existing_access
|= file
->access
;
131 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) goto error
;
132 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) goto error
;
133 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) goto error
;
134 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) goto error
;
137 set_error( STATUS_SHARING_VIOLATION
);
141 /* create a file from a file descriptor */
142 /* if the function fails the fd is closed */
143 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
)
147 if ((file
= alloc_object( &file_ops
)))
151 file
->access
= access
;
152 file
->options
= FILE_SYNCHRONOUS_IO_NONALERT
;
153 file
->sharing
= sharing
;
155 if (!(file
->fd
= create_anonymous_fd( &file_fd_ops
, fd
, &file
->obj
)))
157 release_object( file
);
165 static struct file
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
166 unsigned int sharing
, int create
, unsigned int options
,
167 unsigned int attrs
, int removable
)
174 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
175 memcpy( name
, nameptr
, len
);
178 /* check sharing mode */
179 hash
= get_name_hash( name
);
180 if (!check_sharing( name
, hash
, access
, sharing
)) goto error
;
184 case FILE_CREATE
: flags
= O_CREAT
| O_EXCL
; break;
185 case FILE_OVERWRITE_IF
: /* FIXME: the difference is whether we trash existing attr or not */
186 case FILE_SUPERSEDE
: flags
= O_CREAT
| O_TRUNC
; break;
187 case FILE_OPEN
: flags
= 0; break;
188 case FILE_OPEN_IF
: flags
= O_CREAT
; break;
189 case FILE_OVERWRITE
: flags
= O_TRUNC
; break;
190 default: set_error( STATUS_INVALID_PARAMETER
); goto error
;
192 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
195 case GENERIC_READ
: flags
|= O_RDONLY
; break;
196 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
197 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
199 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
202 (!strcasecmp( name
+ len
- 4, ".exe" ) || !strcasecmp( name
+ len
- 4, ".com" )))
205 if (!(file
= alloc_object( &file_ops
))) goto error
;
207 file
->access
= access
;
208 file
->options
= options
;
209 file
->sharing
= sharing
;
210 file
->removable
= removable
;
212 file
->next
= file_hash
[hash
];
213 file_hash
[hash
] = file
;
214 if (is_overlapped( file
))
216 init_async_queue (&file
->read_q
);
217 init_async_queue (&file
->write_q
);
220 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
221 if (!(file
->fd
= alloc_fd( &file_fd_ops
, &file
->obj
)) ||
222 !(file
->fd
= open_fd( file
->fd
, name
, flags
| O_NONBLOCK
| O_LARGEFILE
, &mode
)))
224 release_object( file
);
227 /* refuse to open a directory */
228 if (S_ISDIR(mode
) && !(options
& FILE_OPEN_FOR_BACKUP_INTENT
))
230 set_error( STATUS_ACCESS_DENIED
);
231 release_object( file
);
241 /* check if two file objects point to the same file */
242 int is_same_file( struct file
*file1
, struct file
*file2
)
244 return !strcmp( file1
->name
, file2
->name
);
247 /* check if the file is on removable media */
248 int is_file_removable( struct file
*file
)
250 return file
->removable
;
253 /* create a temp file for anonymous mappings */
254 struct file
*create_temp_file( int access
)
259 sprintf( tmpfn
, "anonmap.XXXXXX" ); /* create it in the server directory */
260 fd
= mkstemps( tmpfn
, 0 );
267 return create_file_for_fd( fd
, access
, 0 );
270 static void file_dump( struct object
*obj
, int verbose
)
272 struct file
*file
= (struct file
*)obj
;
273 assert( obj
->ops
== &file_ops
);
274 fprintf( stderr
, "File fd=%p options=%08x name='%s'\n", file
->fd
, file
->options
, file
->name
);
277 static int file_get_poll_events( struct fd
*fd
)
279 struct file
*file
= get_fd_user( fd
);
281 assert( file
->obj
.ops
== &file_ops
);
282 if (file
->access
& GENERIC_READ
) events
|= POLLIN
;
283 if (file
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
287 static void file_poll_event( struct fd
*fd
, int event
)
289 struct file
*file
= get_fd_user( fd
);
290 assert( file
->obj
.ops
== &file_ops
);
291 if (is_overlapped( file
))
293 if( IS_READY(file
->read_q
) && (POLLIN
& event
) )
295 async_notify(file
->read_q
.head
, STATUS_ALERTED
);
298 if( IS_READY(file
->write_q
) && (POLLOUT
& event
) )
300 async_notify(file
->write_q
.head
, STATUS_ALERTED
);
304 default_poll_event( fd
, event
);
308 static int file_flush( struct fd
*fd
, struct event
**event
)
310 int ret
= (fsync( get_unix_fd(fd
) ) != -1);
311 if (!ret
) file_set_error();
315 static int file_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
)
318 struct file
*file
= get_fd_user( fd
);
319 int unix_fd
= get_unix_fd( fd
);
323 if (fstat( unix_fd
, &st
) == -1)
326 return FD_TYPE_INVALID
;
328 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
329 S_ISSOCK(st
.st_mode
) || isatty(unix_fd
)) reply
->type
= FILE_TYPE_CHAR
;
330 else reply
->type
= FILE_TYPE_DISK
;
331 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
332 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
333 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
334 reply
->access_time
= st
.st_atime
;
335 reply
->write_time
= st
.st_mtime
;
336 reply
->change_time
= st
.st_ctime
;
337 if (S_ISDIR(st
.st_mode
))
339 reply
->size_high
= 0;
341 reply
->alloc_high
= 0;
342 reply
->alloc_low
= 0;
347 reply
->size_high
= st
.st_size
>> 32;
348 reply
->size_low
= st
.st_size
& 0xffffffff;
349 alloc
= (file_pos_t
)st
.st_blksize
* st
.st_blocks
;
350 reply
->alloc_high
= alloc
>> 32;
351 reply
->alloc_low
= alloc
& 0xffffffff;
353 reply
->links
= st
.st_nlink
;
354 reply
->index_high
= st
.st_dev
;
355 reply
->index_low
= st
.st_ino
;
356 reply
->serial
= 0; /* FIXME */
359 if (is_overlapped( file
)) *flags
|= FD_FLAG_OVERLAPPED
;
360 return FD_TYPE_DEFAULT
;
363 static void file_queue_async(struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
)
365 struct file
*file
= get_fd_user( fd
);
367 struct async_queue
*q
;
369 assert( file
->obj
.ops
== &file_ops
);
371 if (!is_overlapped( file
))
373 set_error ( STATUS_INVALID_HANDLE
);
379 case ASYNC_TYPE_READ
:
382 case ASYNC_TYPE_WRITE
:
386 set_error( STATUS_INVALID_PARAMETER
);
390 async
= find_async ( q
, current
, ptr
);
392 if ( status
== STATUS_PENDING
)
397 async
= create_async ( &file
->obj
, current
, ptr
);
401 async
->status
= STATUS_PENDING
;
403 async_insert( q
, async
);
405 /* Check if the new pending request can be served immediately */
406 events
= check_fd_events( fd
, file_get_poll_events( fd
) );
407 if (events
) file_poll_event ( fd
, events
);
409 else if ( async
) destroy_async ( async
);
410 else set_error ( STATUS_INVALID_PARAMETER
);
412 set_fd_events( fd
, file_get_poll_events( fd
));
415 static struct fd
*file_get_fd( struct object
*obj
)
417 struct file
*file
= (struct file
*)obj
;
418 assert( obj
->ops
== &file_ops
);
419 return (struct fd
*)grab_object( file
->fd
);
422 static void file_destroy( struct object
*obj
)
424 struct file
*file
= (struct file
*)obj
;
425 assert( obj
->ops
== &file_ops
);
429 /* remove it from the hashing list */
430 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
431 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
433 *pptr
= (*pptr
)->next
;
434 if (file
->options
& FILE_DELETE_ON_CLOSE
) unlink( file
->name
);
437 if (is_overlapped( file
))
439 destroy_async_queue (&file
->read_q
);
440 destroy_async_queue (&file
->write_q
);
442 if (file
->fd
) release_object( file
->fd
);
445 /* set the last error depending on errno */
446 void file_set_error(void)
450 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
451 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
452 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
455 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
456 case EROFS
: set_error( STATUS_MEDIA_WRITE_PROTECTED
); break;
457 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
458 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
459 case EISDIR
: set_win32_error( ERROR_CANNOT_MAKE
); break;
461 case EMFILE
: set_error( STATUS_NO_MORE_FILES
); break;
462 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
463 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
464 case ESPIPE
: set_win32_error( ERROR_SEEK
); break;
465 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
466 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
467 case ENOTDIR
: set_error( STATUS_NOT_A_DIRECTORY
); break;
469 case EOVERFLOW
: set_error( STATUS_INVALID_PARAMETER
); break;
471 default: perror("file_set_error"); set_win32_error( ERROR_UNKNOWN
); break;
475 struct file
*get_file_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
477 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
480 int get_file_unix_fd( struct file
*file
)
482 return get_unix_fd( file
->fd
);
485 static int set_file_pointer( obj_handle_t handle
, unsigned int *low
, int *high
, int whence
)
490 xto
= *low
+((off_t
)*high
<<32);
491 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
493 if ((result
= lseek( get_file_unix_fd(file
), xto
, whence
))==-1)
495 /* Check for seek before start of file */
497 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
498 if (((errno
== EINVAL
) || (errno
== EPERM
))
499 && (whence
!= SEEK_SET
) && (*high
< 0))
500 set_win32_error( ERROR_NEGATIVE_SEEK
);
503 release_object( file
);
506 *low
= result
& 0xffffffff;
507 *high
= result
>> 32;
508 release_object( file
);
512 /* extend a file beyond the current end of file */
513 static int extend_file( struct file
*file
, off_t size
)
515 static const char zero
;
516 int unix_fd
= get_file_unix_fd( file
);
518 /* extend the file one byte beyond the requested size and then truncate it */
519 /* this should work around ftruncate implementations that can't extend files */
520 if ((lseek( unix_fd
, size
, SEEK_SET
) != -1) &&
521 (write( unix_fd
, &zero
, 1 ) != -1))
523 ftruncate( unix_fd
, size
);
530 /* truncate file at current position */
531 static int truncate_file( struct file
*file
)
534 int unix_fd
= get_file_unix_fd( file
);
535 off_t pos
= lseek( unix_fd
, 0, SEEK_CUR
);
536 off_t eof
= lseek( unix_fd
, 0, SEEK_END
);
538 if (eof
< pos
) ret
= extend_file( file
, pos
);
541 if (ftruncate( unix_fd
, pos
) != -1) ret
= 1;
542 else file_set_error();
544 lseek( unix_fd
, pos
, SEEK_SET
); /* restore file pos */
548 /* try to grow the file to the specified size */
549 int grow_file( struct file
*file
, int size_high
, int size_low
)
553 int unix_fd
= get_file_unix_fd( file
);
554 off_t old_pos
, size
= size_low
+ (((off_t
)size_high
)<<32);
556 if (fstat( unix_fd
, &st
) == -1)
561 if (st
.st_size
>= size
) return 1; /* already large enough */
562 old_pos
= lseek( unix_fd
, 0, SEEK_CUR
); /* save old pos */
563 ret
= extend_file( file
, size
);
564 lseek( unix_fd
, old_pos
, SEEK_SET
); /* restore file pos */
568 static int set_file_time( obj_handle_t handle
, time_t access_time
, time_t write_time
)
571 struct utimbuf utimbuf
;
573 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
577 set_error( STATUS_INVALID_HANDLE
);
578 release_object( file
);
581 if (!access_time
|| !write_time
)
584 if (stat( file
->name
, &st
) == -1) goto error
;
585 if (!access_time
) access_time
= st
.st_atime
;
586 if (!write_time
) write_time
= st
.st_mtime
;
588 utimbuf
.actime
= access_time
;
589 utimbuf
.modtime
= write_time
;
590 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
591 release_object( file
);
595 release_object( file
);
600 DECL_HANDLER(create_file
)
605 if ((file
= create_file( get_req_data(), get_req_data_size(), req
->access
,
606 req
->sharing
, req
->create
, req
->options
, req
->attrs
, req
->removable
)))
608 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
609 release_object( file
);
613 /* allocate a file handle for a Unix fd */
614 DECL_HANDLER(alloc_file_handle
)
620 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
622 set_error( STATUS_INVALID_HANDLE
);
625 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
)))
627 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
628 release_object( file
);
632 /* set a file current position */
633 DECL_HANDLER(set_file_pointer
)
635 int high
= req
->high
;
637 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
638 reply
->new_low
= low
;
639 reply
->new_high
= high
;
642 /* truncate (or extend) a file */
643 DECL_HANDLER(truncate_file
)
647 if ((file
= get_file_obj( current
->process
, req
->handle
, GENERIC_WRITE
)))
649 truncate_file( file
);
650 release_object( file
);
654 /* set a file access and modification times */
655 DECL_HANDLER(set_file_time
)
657 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
660 /* lock a region of a file */
661 DECL_HANDLER(lock_file
)
664 file_pos_t offset
= ((file_pos_t
)req
->offset_high
<< 32) | req
->offset_low
;
665 file_pos_t count
= ((file_pos_t
)req
->count_high
<< 32) | req
->count_low
;
667 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
669 reply
->handle
= lock_fd( file
->fd
, offset
, count
, req
->shared
, req
->wait
);
670 reply
->overlapped
= is_overlapped( file
);
671 release_object( file
);
675 /* unlock a region of a file */
676 DECL_HANDLER(unlock_file
)
679 file_pos_t offset
= ((file_pos_t
)req
->offset_high
<< 32) | req
->offset_low
;
680 file_pos_t count
= ((file_pos_t
)req
->count_high
<< 32) | req
->count_low
;
682 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
684 unlock_fd( file
->fd
, offset
, count
);
685 release_object( file
);