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 object
*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
);
234 /* check for serial port */
235 if (S_ISCHR(mode
) && is_serial_fd( file
->fd
))
237 struct object
*obj
= create_serial( file
->fd
, file
->options
);
238 release_object( file
);
249 /* check if two file objects point to the same file */
250 int is_same_file( struct file
*file1
, struct file
*file2
)
252 return !strcmp( file1
->name
, file2
->name
);
255 /* check if the file is on removable media */
256 int is_file_removable( struct file
*file
)
258 return file
->removable
;
261 /* create a temp file for anonymous mappings */
262 struct file
*create_temp_file( int access
)
267 sprintf( tmpfn
, "anonmap.XXXXXX" ); /* create it in the server directory */
268 fd
= mkstemps( tmpfn
, 0 );
275 return create_file_for_fd( fd
, access
, 0 );
278 static void file_dump( struct object
*obj
, int verbose
)
280 struct file
*file
= (struct file
*)obj
;
281 assert( obj
->ops
== &file_ops
);
282 fprintf( stderr
, "File fd=%p options=%08x name='%s'\n", file
->fd
, file
->options
, file
->name
);
285 static int file_get_poll_events( struct fd
*fd
)
287 struct file
*file
= get_fd_user( fd
);
289 assert( file
->obj
.ops
== &file_ops
);
290 if (file
->access
& GENERIC_READ
) events
|= POLLIN
;
291 if (file
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
295 static void file_poll_event( struct fd
*fd
, int event
)
297 struct file
*file
= get_fd_user( fd
);
298 assert( file
->obj
.ops
== &file_ops
);
299 if (is_overlapped( file
))
301 if( IS_READY(file
->read_q
) && (POLLIN
& event
) )
303 async_notify(file
->read_q
.head
, STATUS_ALERTED
);
306 if( IS_READY(file
->write_q
) && (POLLOUT
& event
) )
308 async_notify(file
->write_q
.head
, STATUS_ALERTED
);
312 default_poll_event( fd
, event
);
316 static int file_flush( struct fd
*fd
, struct event
**event
)
318 int ret
= (fsync( get_unix_fd(fd
) ) != -1);
319 if (!ret
) file_set_error();
323 static int file_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
)
326 struct file
*file
= get_fd_user( fd
);
327 int unix_fd
= get_unix_fd( fd
);
331 if (fstat( unix_fd
, &st
) == -1)
334 return FD_TYPE_INVALID
;
336 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
337 S_ISSOCK(st
.st_mode
) || isatty(unix_fd
)) reply
->type
= FILE_TYPE_CHAR
;
338 else reply
->type
= FILE_TYPE_DISK
;
339 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
340 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
341 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
342 reply
->access_time
= st
.st_atime
;
343 reply
->write_time
= st
.st_mtime
;
344 reply
->change_time
= st
.st_ctime
;
345 if (S_ISDIR(st
.st_mode
))
347 reply
->size_high
= 0;
349 reply
->alloc_high
= 0;
350 reply
->alloc_low
= 0;
355 reply
->size_high
= st
.st_size
>> 32;
356 reply
->size_low
= st
.st_size
& 0xffffffff;
357 alloc
= (file_pos_t
)st
.st_blksize
* st
.st_blocks
;
358 reply
->alloc_high
= alloc
>> 32;
359 reply
->alloc_low
= alloc
& 0xffffffff;
361 reply
->links
= st
.st_nlink
;
362 reply
->index_high
= st
.st_dev
;
363 reply
->index_low
= st
.st_ino
;
364 reply
->serial
= 0; /* FIXME */
367 if (is_overlapped( file
)) *flags
|= FD_FLAG_OVERLAPPED
;
368 return FD_TYPE_DEFAULT
;
371 static void file_queue_async(struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
)
373 struct file
*file
= get_fd_user( fd
);
375 struct async_queue
*q
;
377 assert( file
->obj
.ops
== &file_ops
);
379 if (!is_overlapped( file
))
381 set_error ( STATUS_INVALID_HANDLE
);
387 case ASYNC_TYPE_READ
:
390 case ASYNC_TYPE_WRITE
:
394 set_error( STATUS_INVALID_PARAMETER
);
398 async
= find_async ( q
, current
, ptr
);
400 if ( status
== STATUS_PENDING
)
405 async
= create_async ( &file
->obj
, current
, ptr
);
409 async
->status
= STATUS_PENDING
;
411 async_insert( q
, async
);
413 /* Check if the new pending request can be served immediately */
414 events
= check_fd_events( fd
, file_get_poll_events( fd
) );
415 if (events
) file_poll_event ( fd
, events
);
417 else if ( async
) destroy_async ( async
);
418 else set_error ( STATUS_INVALID_PARAMETER
);
420 set_fd_events( fd
, file_get_poll_events( fd
));
423 static struct fd
*file_get_fd( struct object
*obj
)
425 struct file
*file
= (struct file
*)obj
;
426 assert( obj
->ops
== &file_ops
);
427 return (struct fd
*)grab_object( file
->fd
);
430 static void file_destroy( struct object
*obj
)
432 struct file
*file
= (struct file
*)obj
;
433 assert( obj
->ops
== &file_ops
);
437 /* remove it from the hashing list */
438 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
439 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
441 *pptr
= (*pptr
)->next
;
442 if (file
->options
& FILE_DELETE_ON_CLOSE
) unlink( file
->name
);
445 if (is_overlapped( file
))
447 destroy_async_queue (&file
->read_q
);
448 destroy_async_queue (&file
->write_q
);
450 if (file
->fd
) release_object( file
->fd
);
453 /* set the last error depending on errno */
454 void file_set_error(void)
458 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
459 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
460 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
463 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
464 case EROFS
: set_error( STATUS_MEDIA_WRITE_PROTECTED
); break;
465 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
466 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
467 case EISDIR
: set_win32_error( ERROR_CANNOT_MAKE
); break;
469 case EMFILE
: set_error( STATUS_NO_MORE_FILES
); break;
470 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
471 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
472 case ESPIPE
: set_win32_error( ERROR_SEEK
); break;
473 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
474 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
475 case ENOTDIR
: set_error( STATUS_NOT_A_DIRECTORY
); break;
477 case EOVERFLOW
: set_error( STATUS_INVALID_PARAMETER
); break;
479 default: perror("file_set_error"); set_win32_error( ERROR_UNKNOWN
); break;
483 struct file
*get_file_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
485 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
488 int get_file_unix_fd( struct file
*file
)
490 return get_unix_fd( file
->fd
);
493 static int set_file_pointer( obj_handle_t handle
, unsigned int *low
, int *high
, int whence
)
498 xto
= *low
+((off_t
)*high
<<32);
499 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
501 if ((result
= lseek( get_file_unix_fd(file
), xto
, whence
))==-1)
503 /* Check for seek before start of file */
505 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
506 if (((errno
== EINVAL
) || (errno
== EPERM
))
507 && (whence
!= SEEK_SET
) && (*high
< 0))
508 set_win32_error( ERROR_NEGATIVE_SEEK
);
511 release_object( file
);
514 *low
= result
& 0xffffffff;
515 *high
= result
>> 32;
516 release_object( file
);
520 /* extend a file beyond the current end of file */
521 static int extend_file( struct file
*file
, off_t size
)
523 static const char zero
;
524 int unix_fd
= get_file_unix_fd( file
);
526 /* extend the file one byte beyond the requested size and then truncate it */
527 /* this should work around ftruncate implementations that can't extend files */
528 if ((lseek( unix_fd
, size
, SEEK_SET
) != -1) &&
529 (write( unix_fd
, &zero
, 1 ) != -1))
531 ftruncate( unix_fd
, size
);
538 /* truncate file at current position */
539 static int truncate_file( struct file
*file
)
542 int unix_fd
= get_file_unix_fd( file
);
543 off_t pos
= lseek( unix_fd
, 0, SEEK_CUR
);
544 off_t eof
= lseek( unix_fd
, 0, SEEK_END
);
546 if (eof
< pos
) ret
= extend_file( file
, pos
);
549 if (ftruncate( unix_fd
, pos
) != -1) ret
= 1;
550 else file_set_error();
552 lseek( unix_fd
, pos
, SEEK_SET
); /* restore file pos */
556 /* try to grow the file to the specified size */
557 int grow_file( struct file
*file
, int size_high
, int size_low
)
561 int unix_fd
= get_file_unix_fd( file
);
562 off_t old_pos
, size
= size_low
+ (((off_t
)size_high
)<<32);
564 if (fstat( unix_fd
, &st
) == -1)
569 if (st
.st_size
>= size
) return 1; /* already large enough */
570 old_pos
= lseek( unix_fd
, 0, SEEK_CUR
); /* save old pos */
571 ret
= extend_file( file
, size
);
572 lseek( unix_fd
, old_pos
, SEEK_SET
); /* restore file pos */
576 static int set_file_time( obj_handle_t handle
, time_t access_time
, time_t write_time
)
579 struct utimbuf utimbuf
;
581 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
585 set_error( STATUS_INVALID_HANDLE
);
586 release_object( file
);
589 if (!access_time
|| !write_time
)
592 if (stat( file
->name
, &st
) == -1) goto error
;
593 if (!access_time
) access_time
= st
.st_atime
;
594 if (!write_time
) write_time
= st
.st_mtime
;
596 utimbuf
.actime
= access_time
;
597 utimbuf
.modtime
= write_time
;
598 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
599 release_object( file
);
603 release_object( file
);
608 DECL_HANDLER(create_file
)
613 if ((file
= create_file( get_req_data(), get_req_data_size(), req
->access
,
614 req
->sharing
, req
->create
, req
->options
, req
->attrs
, req
->removable
)))
616 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
617 release_object( file
);
621 /* allocate a file handle for a Unix fd */
622 DECL_HANDLER(alloc_file_handle
)
628 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
630 set_error( STATUS_INVALID_HANDLE
);
633 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
)))
635 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
636 release_object( file
);
640 /* set a file current position */
641 DECL_HANDLER(set_file_pointer
)
643 int high
= req
->high
;
645 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
646 reply
->new_low
= low
;
647 reply
->new_high
= high
;
650 /* truncate (or extend) a file */
651 DECL_HANDLER(truncate_file
)
655 if ((file
= get_file_obj( current
->process
, req
->handle
, GENERIC_WRITE
)))
657 truncate_file( file
);
658 release_object( file
);
662 /* set a file access and modification times */
663 DECL_HANDLER(set_file_time
)
665 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
668 /* lock a region of a file */
669 DECL_HANDLER(lock_file
)
672 file_pos_t offset
= ((file_pos_t
)req
->offset_high
<< 32) | req
->offset_low
;
673 file_pos_t count
= ((file_pos_t
)req
->count_high
<< 32) | req
->count_low
;
675 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
677 reply
->handle
= lock_fd( file
->fd
, offset
, count
, req
->shared
, req
->wait
);
678 reply
->overlapped
= is_overlapped( file
);
679 release_object( file
);
683 /* unlock a region of a file */
684 DECL_HANDLER(unlock_file
)
687 file_pos_t offset
= ((file_pos_t
)req
->offset_high
<< 32) | req
->offset_low
;
688 file_pos_t count
= ((file_pos_t
)req
->count_high
<< 32) | req
->count_low
;
690 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
692 unlock_fd( file
->fd
, offset
, count
);
693 release_object( file
);