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 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
60 unsigned int options
; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
61 int removable
; /* is file on removable media? */
62 struct async_queue read_q
;
63 struct async_queue write_q
;
66 static void file_dump( struct object
*obj
, int verbose
);
67 static struct fd
*file_get_fd( struct object
*obj
);
68 static void file_destroy( struct object
*obj
);
70 static int file_get_poll_events( struct fd
*fd
);
71 static void file_poll_event( struct fd
*fd
, int event
);
72 static int file_flush( struct fd
*fd
, struct event
**event
);
73 static int file_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
);
74 static void file_queue_async( struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
);
76 static const struct object_ops file_ops
=
78 sizeof(struct file
), /* size */
80 default_fd_add_queue
, /* add_queue */
81 default_fd_remove_queue
, /* remove_queue */
82 default_fd_signaled
, /* signaled */
83 no_satisfied
, /* satisfied */
84 file_get_fd
, /* get_fd */
85 file_destroy
/* destroy */
88 static const struct fd_ops file_fd_ops
=
90 file_get_poll_events
, /* get_poll_events */
91 file_poll_event
, /* poll_event */
92 file_flush
, /* flush */
93 file_get_info
, /* get_file_info */
94 file_queue_async
/* queue_async */
97 static inline int is_overlapped( const struct file
*file
)
99 return !(file
->options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
102 /* create a file from a file descriptor */
103 /* if the function fails the fd is closed */
104 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
)
108 if ((file
= alloc_object( &file_ops
)))
110 file
->access
= access
;
111 file
->options
= FILE_SYNCHRONOUS_IO_NONALERT
;
113 if (!(file
->fd
= create_anonymous_fd( &file_fd_ops
, fd
, &file
->obj
)))
115 release_object( file
);
123 static struct object
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
124 unsigned int sharing
, int create
, unsigned int options
,
125 unsigned int attrs
, int removable
)
132 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
133 memcpy( name
, nameptr
, len
);
138 case FILE_CREATE
: flags
= O_CREAT
| O_EXCL
; break;
139 case FILE_OVERWRITE_IF
: /* FIXME: the difference is whether we trash existing attr or not */
140 case FILE_SUPERSEDE
: flags
= O_CREAT
| O_TRUNC
; break;
141 case FILE_OPEN
: flags
= 0; break;
142 case FILE_OPEN_IF
: flags
= O_CREAT
; break;
143 case FILE_OVERWRITE
: flags
= O_TRUNC
; break;
144 default: set_error( STATUS_INVALID_PARAMETER
); goto error
;
146 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
149 case GENERIC_READ
: flags
|= O_RDONLY
; break;
150 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
151 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
153 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
156 (!strcasecmp( name
+ len
- 4, ".exe" ) || !strcasecmp( name
+ len
- 4, ".com" )))
159 if (!(file
= alloc_object( &file_ops
))) goto error
;
161 file
->access
= access
;
162 file
->options
= options
;
163 file
->removable
= removable
;
164 if (is_overlapped( file
))
166 init_async_queue (&file
->read_q
);
167 init_async_queue (&file
->write_q
);
170 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
171 if (!(file
->fd
= alloc_fd( &file_fd_ops
, &file
->obj
)) ||
172 !(file
->fd
= open_fd( file
->fd
, name
, flags
| O_NONBLOCK
| O_LARGEFILE
,
173 &mode
, access
, sharing
,
174 (options
& FILE_DELETE_ON_CLOSE
) ? name
: "" )))
177 release_object( file
);
182 /* refuse to open a directory */
183 if (S_ISDIR(mode
) && !(options
& FILE_OPEN_FOR_BACKUP_INTENT
))
185 set_error( STATUS_ACCESS_DENIED
);
186 release_object( file
);
189 /* check for serial port */
190 if (S_ISCHR(mode
) && is_serial_fd( file
->fd
))
192 struct object
*obj
= create_serial( file
->fd
, file
->options
);
193 release_object( file
);
204 /* check if two file objects point to the same file */
205 int is_same_file( struct file
*file1
, struct file
*file2
)
207 return is_same_file_fd( file1
->fd
, file2
->fd
);
210 /* check if the file is on removable media */
211 int is_file_removable( struct file
*file
)
213 return file
->removable
;
216 /* create a temp file for anonymous mappings */
217 struct file
*create_temp_file( int access
)
222 sprintf( tmpfn
, "anonmap.XXXXXX" ); /* create it in the server directory */
223 fd
= mkstemps( tmpfn
, 0 );
230 return create_file_for_fd( fd
, access
, 0 );
233 static void file_dump( struct object
*obj
, int verbose
)
235 struct file
*file
= (struct file
*)obj
;
236 assert( obj
->ops
== &file_ops
);
237 fprintf( stderr
, "File fd=%p options=%08x\n", file
->fd
, file
->options
);
240 static int file_get_poll_events( struct fd
*fd
)
242 struct file
*file
= get_fd_user( fd
);
244 assert( file
->obj
.ops
== &file_ops
);
245 if (file
->access
& GENERIC_READ
) events
|= POLLIN
;
246 if (file
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
250 static void file_poll_event( struct fd
*fd
, int event
)
252 struct file
*file
= get_fd_user( fd
);
253 assert( file
->obj
.ops
== &file_ops
);
254 if (is_overlapped( file
))
256 if( IS_READY(file
->read_q
) && (POLLIN
& event
) )
258 async_notify(file
->read_q
.head
, STATUS_ALERTED
);
261 if( IS_READY(file
->write_q
) && (POLLOUT
& event
) )
263 async_notify(file
->write_q
.head
, STATUS_ALERTED
);
267 default_poll_event( fd
, event
);
271 static int file_flush( struct fd
*fd
, struct event
**event
)
273 int ret
= (fsync( get_unix_fd(fd
) ) != -1);
274 if (!ret
) file_set_error();
278 static int file_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
)
281 struct file
*file
= get_fd_user( fd
);
282 int unix_fd
= get_unix_fd( fd
);
286 if (fstat( unix_fd
, &st
) == -1)
289 return FD_TYPE_INVALID
;
291 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
292 S_ISSOCK(st
.st_mode
) || isatty(unix_fd
)) reply
->type
= FILE_TYPE_CHAR
;
293 else reply
->type
= FILE_TYPE_DISK
;
294 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
295 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
296 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
297 reply
->access_time
= st
.st_atime
;
298 reply
->write_time
= st
.st_mtime
;
299 reply
->change_time
= st
.st_ctime
;
300 if (S_ISDIR(st
.st_mode
))
302 reply
->size_high
= 0;
304 reply
->alloc_high
= 0;
305 reply
->alloc_low
= 0;
310 reply
->size_high
= st
.st_size
>> 32;
311 reply
->size_low
= st
.st_size
& 0xffffffff;
312 alloc
= (file_pos_t
)st
.st_blksize
* st
.st_blocks
;
313 reply
->alloc_high
= alloc
>> 32;
314 reply
->alloc_low
= alloc
& 0xffffffff;
316 reply
->links
= st
.st_nlink
;
317 reply
->index_high
= st
.st_dev
;
318 reply
->index_low
= st
.st_ino
;
319 reply
->serial
= 0; /* FIXME */
322 if (is_overlapped( file
)) *flags
|= FD_FLAG_OVERLAPPED
;
323 return FD_TYPE_DEFAULT
;
326 static void file_queue_async(struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
)
328 struct file
*file
= get_fd_user( fd
);
330 struct async_queue
*q
;
332 assert( file
->obj
.ops
== &file_ops
);
334 if (!is_overlapped( file
))
336 set_error ( STATUS_INVALID_HANDLE
);
342 case ASYNC_TYPE_READ
:
345 case ASYNC_TYPE_WRITE
:
349 set_error( STATUS_INVALID_PARAMETER
);
353 async
= find_async ( q
, current
, ptr
);
355 if ( status
== STATUS_PENDING
)
360 async
= create_async ( &file
->obj
, current
, ptr
);
364 async
->status
= STATUS_PENDING
;
366 async_insert( q
, async
);
368 /* Check if the new pending request can be served immediately */
369 events
= check_fd_events( fd
, file_get_poll_events( fd
) );
370 if (events
) file_poll_event ( fd
, events
);
372 else if ( async
) destroy_async ( async
);
373 else set_error ( STATUS_INVALID_PARAMETER
);
375 set_fd_events( fd
, file_get_poll_events( fd
));
378 static struct fd
*file_get_fd( struct object
*obj
)
380 struct file
*file
= (struct file
*)obj
;
381 assert( obj
->ops
== &file_ops
);
382 return (struct fd
*)grab_object( file
->fd
);
385 static void file_destroy( struct object
*obj
)
387 struct file
*file
= (struct file
*)obj
;
388 assert( obj
->ops
== &file_ops
);
390 if (is_overlapped( file
))
392 destroy_async_queue (&file
->read_q
);
393 destroy_async_queue (&file
->write_q
);
395 if (file
->fd
) release_object( file
->fd
);
398 /* set the last error depending on errno */
399 void file_set_error(void)
403 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
404 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
405 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
408 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
409 case EROFS
: set_error( STATUS_MEDIA_WRITE_PROTECTED
); break;
410 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
411 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
412 case EISDIR
: set_win32_error( ERROR_CANNOT_MAKE
); break;
414 case EMFILE
: set_error( STATUS_NO_MORE_FILES
); break;
415 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
416 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
417 case ESPIPE
: set_win32_error( ERROR_SEEK
); break;
418 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
419 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
420 case ENOTDIR
: set_error( STATUS_NOT_A_DIRECTORY
); break;
422 case EOVERFLOW
: set_error( STATUS_INVALID_PARAMETER
); break;
424 default: perror("file_set_error"); set_win32_error( ERROR_UNKNOWN
); break;
428 struct file
*get_file_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
430 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
433 int get_file_unix_fd( struct file
*file
)
435 return get_unix_fd( file
->fd
);
438 static int set_file_pointer( obj_handle_t handle
, unsigned int *low
, int *high
, int whence
)
443 xto
= *low
+((off_t
)*high
<<32);
444 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
446 if ((result
= lseek( get_file_unix_fd(file
), xto
, whence
))==-1)
448 /* Check for seek before start of file */
450 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
451 if (((errno
== EINVAL
) || (errno
== EPERM
))
452 && (whence
!= SEEK_SET
) && (*high
< 0))
453 set_win32_error( ERROR_NEGATIVE_SEEK
);
456 release_object( file
);
459 *low
= result
& 0xffffffff;
460 *high
= result
>> 32;
461 release_object( file
);
465 /* extend a file beyond the current end of file */
466 static int extend_file( struct file
*file
, off_t size
)
468 static const char zero
;
469 int unix_fd
= get_file_unix_fd( file
);
471 /* extend the file one byte beyond the requested size and then truncate it */
472 /* this should work around ftruncate implementations that can't extend files */
473 if ((lseek( unix_fd
, size
, SEEK_SET
) != -1) &&
474 (write( unix_fd
, &zero
, 1 ) != -1))
476 ftruncate( unix_fd
, size
);
483 /* truncate file at current position */
484 static int truncate_file( struct file
*file
)
487 int unix_fd
= get_file_unix_fd( file
);
488 off_t pos
= lseek( unix_fd
, 0, SEEK_CUR
);
489 off_t eof
= lseek( unix_fd
, 0, SEEK_END
);
491 if (eof
< pos
) ret
= extend_file( file
, pos
);
494 if (ftruncate( unix_fd
, pos
) != -1) ret
= 1;
495 else file_set_error();
497 lseek( unix_fd
, pos
, SEEK_SET
); /* restore file pos */
501 /* try to grow the file to the specified size */
502 int grow_file( struct file
*file
, int size_high
, int size_low
)
506 int unix_fd
= get_file_unix_fd( file
);
507 off_t old_pos
, size
= size_low
+ (((off_t
)size_high
)<<32);
509 if (fstat( unix_fd
, &st
) == -1)
514 if (st
.st_size
>= size
) return 1; /* already large enough */
515 old_pos
= lseek( unix_fd
, 0, SEEK_CUR
); /* save old pos */
516 ret
= extend_file( file
, size
);
517 lseek( unix_fd
, old_pos
, SEEK_SET
); /* restore file pos */
522 DECL_HANDLER(create_file
)
527 if ((file
= create_file( get_req_data(), get_req_data_size(), req
->access
,
528 req
->sharing
, req
->create
, req
->options
, req
->attrs
, req
->removable
)))
530 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
531 release_object( file
);
535 /* allocate a file handle for a Unix fd */
536 DECL_HANDLER(alloc_file_handle
)
542 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
544 set_error( STATUS_INVALID_HANDLE
);
547 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
)))
549 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
550 release_object( file
);
554 /* set a file current position */
555 DECL_HANDLER(set_file_pointer
)
557 int high
= req
->high
;
559 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
560 reply
->new_low
= low
;
561 reply
->new_high
= high
;
564 /* truncate (or extend) a file */
565 DECL_HANDLER(truncate_file
)
569 if ((file
= get_file_obj( current
->process
, req
->handle
, GENERIC_WRITE
)))
571 truncate_file( file
);
572 release_object( file
);
576 /* lock a region of a file */
577 DECL_HANDLER(lock_file
)
580 file_pos_t offset
= ((file_pos_t
)req
->offset_high
<< 32) | req
->offset_low
;
581 file_pos_t count
= ((file_pos_t
)req
->count_high
<< 32) | req
->count_low
;
583 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
585 reply
->handle
= lock_fd( file
->fd
, offset
, count
, req
->shared
, req
->wait
);
586 reply
->overlapped
= is_overlapped( file
);
587 release_object( file
);
591 /* unlock a region of a file */
592 DECL_HANDLER(unlock_file
)
595 file_pos_t offset
= ((file_pos_t
)req
->offset_high
<< 32) | req
->offset_low
;
596 file_pos_t count
= ((file_pos_t
)req
->count_high
<< 32) | req
->count_low
;
598 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
600 unlock_fd( file
->fd
, offset
, count
);
601 release_object( file
);