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 char *name
; /* file name */
60 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
61 unsigned int options
; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
62 int removable
; /* is file on removable media? */
63 struct async_queue read_q
;
64 struct async_queue write_q
;
67 static void file_dump( struct object
*obj
, int verbose
);
68 static struct fd
*file_get_fd( struct object
*obj
);
69 static void file_destroy( struct object
*obj
);
71 static int file_get_poll_events( struct fd
*fd
);
72 static void file_poll_event( struct fd
*fd
, int event
);
73 static int file_flush( struct fd
*fd
, struct event
**event
);
74 static int file_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
);
75 static void file_queue_async( struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
);
77 static const struct object_ops file_ops
=
79 sizeof(struct file
), /* size */
81 default_fd_add_queue
, /* add_queue */
82 default_fd_remove_queue
, /* remove_queue */
83 default_fd_signaled
, /* signaled */
84 no_satisfied
, /* satisfied */
85 file_get_fd
, /* get_fd */
86 file_destroy
/* destroy */
89 static const struct fd_ops file_fd_ops
=
91 file_get_poll_events
, /* get_poll_events */
92 file_poll_event
, /* poll_event */
93 file_flush
, /* flush */
94 file_get_info
, /* get_file_info */
95 file_queue_async
/* queue_async */
98 static inline int is_overlapped( const struct file
*file
)
100 return !(file
->options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
103 /* create a file from a file descriptor */
104 /* if the function fails the fd is closed */
105 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
)
109 if ((file
= alloc_object( &file_ops
)))
112 file
->access
= access
;
113 file
->options
= FILE_SYNCHRONOUS_IO_NONALERT
;
115 if (!(file
->fd
= create_anonymous_fd( &file_fd_ops
, fd
, &file
->obj
)))
117 release_object( file
);
125 static struct object
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
126 unsigned int sharing
, int create
, unsigned int options
,
127 unsigned int attrs
, int removable
)
134 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
135 memcpy( name
, nameptr
, len
);
140 case FILE_CREATE
: flags
= O_CREAT
| O_EXCL
; break;
141 case FILE_OVERWRITE_IF
: /* FIXME: the difference is whether we trash existing attr or not */
142 case FILE_SUPERSEDE
: flags
= O_CREAT
| O_TRUNC
; break;
143 case FILE_OPEN
: flags
= 0; break;
144 case FILE_OPEN_IF
: flags
= O_CREAT
; break;
145 case FILE_OVERWRITE
: flags
= O_TRUNC
; break;
146 default: set_error( STATUS_INVALID_PARAMETER
); goto error
;
148 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
151 case GENERIC_READ
: flags
|= O_RDONLY
; break;
152 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
153 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
155 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
158 (!strcasecmp( name
+ len
- 4, ".exe" ) || !strcasecmp( name
+ len
- 4, ".com" )))
161 if (!(file
= alloc_object( &file_ops
))) goto error
;
163 file
->access
= access
;
164 file
->options
= options
;
165 file
->removable
= removable
;
167 if (is_overlapped( file
))
169 init_async_queue (&file
->read_q
);
170 init_async_queue (&file
->write_q
);
173 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
174 if (!(file
->fd
= alloc_fd( &file_fd_ops
, &file
->obj
)) ||
175 !(file
->fd
= open_fd( file
->fd
, name
, flags
| O_NONBLOCK
| O_LARGEFILE
,
176 &mode
, access
, sharing
)))
178 release_object( file
);
181 /* refuse to open a directory */
182 if (S_ISDIR(mode
) && !(options
& FILE_OPEN_FOR_BACKUP_INTENT
))
184 set_error( STATUS_ACCESS_DENIED
);
185 release_object( file
);
188 /* check for serial port */
189 if (S_ISCHR(mode
) && is_serial_fd( file
->fd
))
191 struct object
*obj
= create_serial( file
->fd
, file
->options
);
192 release_object( file
);
203 /* check if two file objects point to the same file */
204 int is_same_file( struct file
*file1
, struct file
*file2
)
206 return is_same_file_fd( file1
->fd
, file2
->fd
);
209 /* check if the file is on removable media */
210 int is_file_removable( struct file
*file
)
212 return file
->removable
;
215 /* create a temp file for anonymous mappings */
216 struct file
*create_temp_file( int access
)
221 sprintf( tmpfn
, "anonmap.XXXXXX" ); /* create it in the server directory */
222 fd
= mkstemps( tmpfn
, 0 );
229 return create_file_for_fd( fd
, access
, 0 );
232 static void file_dump( struct object
*obj
, int verbose
)
234 struct file
*file
= (struct file
*)obj
;
235 assert( obj
->ops
== &file_ops
);
236 fprintf( stderr
, "File fd=%p options=%08x name='%s'\n", file
->fd
, file
->options
, file
->name
);
239 static int file_get_poll_events( struct fd
*fd
)
241 struct file
*file
= get_fd_user( fd
);
243 assert( file
->obj
.ops
== &file_ops
);
244 if (file
->access
& GENERIC_READ
) events
|= POLLIN
;
245 if (file
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
249 static void file_poll_event( struct fd
*fd
, int event
)
251 struct file
*file
= get_fd_user( fd
);
252 assert( file
->obj
.ops
== &file_ops
);
253 if (is_overlapped( file
))
255 if( IS_READY(file
->read_q
) && (POLLIN
& event
) )
257 async_notify(file
->read_q
.head
, STATUS_ALERTED
);
260 if( IS_READY(file
->write_q
) && (POLLOUT
& event
) )
262 async_notify(file
->write_q
.head
, STATUS_ALERTED
);
266 default_poll_event( fd
, event
);
270 static int file_flush( struct fd
*fd
, struct event
**event
)
272 int ret
= (fsync( get_unix_fd(fd
) ) != -1);
273 if (!ret
) file_set_error();
277 static int file_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
)
280 struct file
*file
= get_fd_user( fd
);
281 int unix_fd
= get_unix_fd( fd
);
285 if (fstat( unix_fd
, &st
) == -1)
288 return FD_TYPE_INVALID
;
290 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
291 S_ISSOCK(st
.st_mode
) || isatty(unix_fd
)) reply
->type
= FILE_TYPE_CHAR
;
292 else reply
->type
= FILE_TYPE_DISK
;
293 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
294 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
295 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
296 reply
->access_time
= st
.st_atime
;
297 reply
->write_time
= st
.st_mtime
;
298 reply
->change_time
= st
.st_ctime
;
299 if (S_ISDIR(st
.st_mode
))
301 reply
->size_high
= 0;
303 reply
->alloc_high
= 0;
304 reply
->alloc_low
= 0;
309 reply
->size_high
= st
.st_size
>> 32;
310 reply
->size_low
= st
.st_size
& 0xffffffff;
311 alloc
= (file_pos_t
)st
.st_blksize
* st
.st_blocks
;
312 reply
->alloc_high
= alloc
>> 32;
313 reply
->alloc_low
= alloc
& 0xffffffff;
315 reply
->links
= st
.st_nlink
;
316 reply
->index_high
= st
.st_dev
;
317 reply
->index_low
= st
.st_ino
;
318 reply
->serial
= 0; /* FIXME */
321 if (is_overlapped( file
)) *flags
|= FD_FLAG_OVERLAPPED
;
322 return FD_TYPE_DEFAULT
;
325 static void file_queue_async(struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
)
327 struct file
*file
= get_fd_user( fd
);
329 struct async_queue
*q
;
331 assert( file
->obj
.ops
== &file_ops
);
333 if (!is_overlapped( file
))
335 set_error ( STATUS_INVALID_HANDLE
);
341 case ASYNC_TYPE_READ
:
344 case ASYNC_TYPE_WRITE
:
348 set_error( STATUS_INVALID_PARAMETER
);
352 async
= find_async ( q
, current
, ptr
);
354 if ( status
== STATUS_PENDING
)
359 async
= create_async ( &file
->obj
, current
, ptr
);
363 async
->status
= STATUS_PENDING
;
365 async_insert( q
, async
);
367 /* Check if the new pending request can be served immediately */
368 events
= check_fd_events( fd
, file_get_poll_events( fd
) );
369 if (events
) file_poll_event ( fd
, events
);
371 else if ( async
) destroy_async ( async
);
372 else set_error ( STATUS_INVALID_PARAMETER
);
374 set_fd_events( fd
, file_get_poll_events( fd
));
377 static struct fd
*file_get_fd( struct object
*obj
)
379 struct file
*file
= (struct file
*)obj
;
380 assert( obj
->ops
== &file_ops
);
381 return (struct fd
*)grab_object( file
->fd
);
384 static void file_destroy( struct object
*obj
)
386 struct file
*file
= (struct file
*)obj
;
387 assert( obj
->ops
== &file_ops
);
391 if (file
->options
& FILE_DELETE_ON_CLOSE
) unlink( file
->name
);
394 if (is_overlapped( file
))
396 destroy_async_queue (&file
->read_q
);
397 destroy_async_queue (&file
->write_q
);
399 if (file
->fd
) release_object( file
->fd
);
402 /* set the last error depending on errno */
403 void file_set_error(void)
407 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
408 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
409 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
412 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
413 case EROFS
: set_error( STATUS_MEDIA_WRITE_PROTECTED
); break;
414 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
415 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
416 case EISDIR
: set_win32_error( ERROR_CANNOT_MAKE
); break;
418 case EMFILE
: set_error( STATUS_NO_MORE_FILES
); break;
419 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
420 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
421 case ESPIPE
: set_win32_error( ERROR_SEEK
); break;
422 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
423 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
424 case ENOTDIR
: set_error( STATUS_NOT_A_DIRECTORY
); break;
426 case EOVERFLOW
: set_error( STATUS_INVALID_PARAMETER
); break;
428 default: perror("file_set_error"); set_win32_error( ERROR_UNKNOWN
); break;
432 struct file
*get_file_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
434 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
437 int get_file_unix_fd( struct file
*file
)
439 return get_unix_fd( file
->fd
);
442 static int set_file_pointer( obj_handle_t handle
, unsigned int *low
, int *high
, int whence
)
447 xto
= *low
+((off_t
)*high
<<32);
448 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
450 if ((result
= lseek( get_file_unix_fd(file
), xto
, whence
))==-1)
452 /* Check for seek before start of file */
454 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
455 if (((errno
== EINVAL
) || (errno
== EPERM
))
456 && (whence
!= SEEK_SET
) && (*high
< 0))
457 set_win32_error( ERROR_NEGATIVE_SEEK
);
460 release_object( file
);
463 *low
= result
& 0xffffffff;
464 *high
= result
>> 32;
465 release_object( file
);
469 /* extend a file beyond the current end of file */
470 static int extend_file( struct file
*file
, off_t size
)
472 static const char zero
;
473 int unix_fd
= get_file_unix_fd( file
);
475 /* extend the file one byte beyond the requested size and then truncate it */
476 /* this should work around ftruncate implementations that can't extend files */
477 if ((lseek( unix_fd
, size
, SEEK_SET
) != -1) &&
478 (write( unix_fd
, &zero
, 1 ) != -1))
480 ftruncate( unix_fd
, size
);
487 /* truncate file at current position */
488 static int truncate_file( struct file
*file
)
491 int unix_fd
= get_file_unix_fd( file
);
492 off_t pos
= lseek( unix_fd
, 0, SEEK_CUR
);
493 off_t eof
= lseek( unix_fd
, 0, SEEK_END
);
495 if (eof
< pos
) ret
= extend_file( file
, pos
);
498 if (ftruncate( unix_fd
, pos
) != -1) ret
= 1;
499 else file_set_error();
501 lseek( unix_fd
, pos
, SEEK_SET
); /* restore file pos */
505 /* try to grow the file to the specified size */
506 int grow_file( struct file
*file
, int size_high
, int size_low
)
510 int unix_fd
= get_file_unix_fd( file
);
511 off_t old_pos
, size
= size_low
+ (((off_t
)size_high
)<<32);
513 if (fstat( unix_fd
, &st
) == -1)
518 if (st
.st_size
>= size
) return 1; /* already large enough */
519 old_pos
= lseek( unix_fd
, 0, SEEK_CUR
); /* save old pos */
520 ret
= extend_file( file
, size
);
521 lseek( unix_fd
, old_pos
, SEEK_SET
); /* restore file pos */
526 DECL_HANDLER(create_file
)
531 if ((file
= create_file( get_req_data(), get_req_data_size(), req
->access
,
532 req
->sharing
, req
->create
, req
->options
, req
->attrs
, req
->removable
)))
534 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
535 release_object( file
);
539 /* allocate a file handle for a Unix fd */
540 DECL_HANDLER(alloc_file_handle
)
546 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
548 set_error( STATUS_INVALID_HANDLE
);
551 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
)))
553 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
554 release_object( file
);
558 /* set a file current position */
559 DECL_HANDLER(set_file_pointer
)
561 int high
= req
->high
;
563 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
564 reply
->new_low
= low
;
565 reply
->new_high
= high
;
568 /* truncate (or extend) a file */
569 DECL_HANDLER(truncate_file
)
573 if ((file
= get_file_obj( current
->process
, req
->handle
, GENERIC_WRITE
)))
575 truncate_file( file
);
576 release_object( file
);
580 /* lock a region of a file */
581 DECL_HANDLER(lock_file
)
584 file_pos_t offset
= ((file_pos_t
)req
->offset_high
<< 32) | req
->offset_low
;
585 file_pos_t count
= ((file_pos_t
)req
->count_high
<< 32) | req
->count_low
;
587 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
589 reply
->handle
= lock_fd( file
->fd
, offset
, count
, req
->shared
, req
->wait
);
590 reply
->overlapped
= is_overlapped( file
);
591 release_object( file
);
595 /* unlock a region of a file */
596 DECL_HANDLER(unlock_file
)
599 file_pos_t offset
= ((file_pos_t
)req
->offset_high
<< 32) | req
->offset_low
;
600 file_pos_t count
= ((file_pos_t
)req
->count_high
<< 32) | req
->count_low
;
602 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
604 unlock_fd( file
->fd
, offset
, count
);
605 release_object( file
);