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>
35 struct object obj
; /* object header */
36 struct file
*next
; /* next file in hashing list */
37 char *name
; /* file name */
38 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
39 unsigned int flags
; /* flags (FILE_FLAG_*) */
40 unsigned int sharing
; /* file sharing mode */
43 #define NAME_HASH_SIZE 37
45 static struct file
*file_hash
[NAME_HASH_SIZE
];
47 static void file_dump( struct object
*obj
, int verbose
);
48 static int file_get_poll_events( struct object
*obj
);
49 static int file_get_fd( struct object
*obj
);
50 static int file_flush( struct object
*obj
);
51 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
);
52 static void file_destroy( struct object
*obj
);
54 static const struct object_ops file_ops
=
56 sizeof(struct file
), /* size */
58 default_poll_add_queue
, /* add_queue */
59 default_poll_remove_queue
, /* remove_queue */
60 default_poll_signaled
, /* signaled */
61 no_satisfied
, /* satisfied */
62 file_get_poll_events
, /* get_poll_events */
63 default_poll_event
, /* poll_event */
64 file_get_fd
, /* get_fd */
65 file_flush
, /* flush */
66 file_get_info
, /* get_file_info */
67 file_destroy
/* destroy */
71 static int get_name_hash( const char *name
)
74 while (*name
) hash
^= (unsigned char)*name
++;
75 return hash
% NAME_HASH_SIZE
;
78 /* check if the desired access is possible without violating */
79 /* the sharing mode of other opens of the same file */
80 static int check_sharing( const char *name
, int hash
, unsigned int access
,
81 unsigned int sharing
)
84 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
85 unsigned int existing_access
= 0;
87 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
89 if (strcmp( file
->name
, name
)) continue;
90 existing_sharing
&= file
->sharing
;
91 existing_access
|= file
->access
;
93 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) goto error
;
94 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) goto error
;
95 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) goto error
;
96 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) goto error
;
99 set_error( STATUS_SHARING_VIOLATION
);
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
, fd
)))
113 file
->access
= access
;
115 file
->sharing
= sharing
;
121 static struct file
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
122 unsigned int sharing
, int create
, unsigned int attrs
)
131 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
132 memcpy( name
, nameptr
, len
);
135 /* check sharing mode */
136 hash
= get_name_hash( name
);
137 if (!check_sharing( name
, hash
, access
, sharing
)) goto error
;
141 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
142 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
143 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
144 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
145 case OPEN_EXISTING
: flags
= 0; 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 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
162 if ((fd
= open( name
, flags
| O_NONBLOCK
| O_LARGEFILE
, mode
)) == -1 )
164 /* refuse to open a directory */
165 if (fstat( fd
, &st
) == -1) goto file_error
;
166 if (S_ISDIR(st
.st_mode
))
168 set_error( STATUS_ACCESS_DENIED
);
172 if (!(file
= create_file_for_fd( fd
, access
, sharing
, attrs
)))
178 file
->next
= file_hash
[hash
];
179 file_hash
[hash
] = file
;
185 if (fd
!= -1) close( fd
);
190 /* check if two file objects point to the same file */
191 int is_same_file( struct file
*file1
, struct file
*file2
)
193 return !strcmp( file1
->name
, file2
->name
);
196 /* Create an anonymous Unix file */
197 int create_anonymous_file(void)
204 if (!(name
= tmpnam(NULL
)))
206 set_error( STATUS_TOO_MANY_OPENED_FILES
);
209 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
210 } while ((fd
== -1) && (errno
== EEXIST
));
220 /* Create a temp file for anonymous mappings */
221 struct file
*create_temp_file( int access
)
225 if ((fd
= create_anonymous_file()) == -1) return NULL
;
226 return create_file_for_fd( fd
, access
, 0, 0 );
229 static void file_dump( struct object
*obj
, int verbose
)
231 struct file
*file
= (struct file
*)obj
;
232 assert( obj
->ops
== &file_ops
);
233 fprintf( stderr
, "File fd=%d flags=%08x name='%s'\n", file
->obj
.fd
, file
->flags
, file
->name
);
236 static int file_get_poll_events( struct object
*obj
)
238 struct file
*file
= (struct file
*)obj
;
240 assert( obj
->ops
== &file_ops
);
241 if (file
->access
& GENERIC_READ
) events
|= POLLIN
;
242 if (file
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
246 static int file_get_fd( struct object
*obj
)
248 struct file
*file
= (struct file
*)obj
;
249 assert( obj
->ops
== &file_ops
);
253 static int file_flush( struct object
*obj
)
256 struct file
*file
= (struct file
*)grab_object(obj
);
257 assert( obj
->ops
== &file_ops
);
259 ret
= (fsync( file
->obj
.fd
) != -1);
260 if (!ret
) file_set_error();
261 release_object( file
);
265 static int file_get_info( struct object
*obj
, struct get_file_info_request
*req
)
268 struct file
*file
= (struct file
*)obj
;
269 assert( obj
->ops
== &file_ops
);
271 if (fstat( file
->obj
.fd
, &st
) == -1)
276 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
277 S_ISSOCK(st
.st_mode
) || isatty(file
->obj
.fd
)) req
->type
= FILE_TYPE_CHAR
;
278 else req
->type
= FILE_TYPE_DISK
;
279 if (S_ISDIR(st
.st_mode
)) req
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
280 else req
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
281 if (!(st
.st_mode
& S_IWUSR
)) req
->attr
|= FILE_ATTRIBUTE_READONLY
;
282 req
->access_time
= st
.st_atime
;
283 req
->write_time
= st
.st_mtime
;
284 if (S_ISDIR(st
.st_mode
))
291 req
->size_high
= st
.st_size
>> 32;
292 req
->size_low
= st
.st_size
& 0xffffffff;
294 req
->links
= st
.st_nlink
;
295 req
->index_high
= st
.st_dev
;
296 req
->index_low
= st
.st_ino
;
297 req
->serial
= 0; /* FIXME */
301 static void file_destroy( struct object
*obj
)
303 struct file
*file
= (struct file
*)obj
;
304 assert( obj
->ops
== &file_ops
);
308 /* remove it from the hashing list */
309 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
310 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
312 *pptr
= (*pptr
)->next
;
313 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
318 /* set the last error depending on errno */
319 void file_set_error(void)
323 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
324 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
325 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
328 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
329 case EROFS
: set_error( STATUS_MEDIA_WRITE_PROTECTED
); break;
330 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
331 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
332 case EISDIR
: set_error( 0xc0010000 | ERROR_CANNOT_MAKE
/* FIXME */ ); break;
334 case EMFILE
: set_error( STATUS_NO_MORE_FILES
); break;
335 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
336 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
337 case ESPIPE
: set_error( 0xc0010000 | ERROR_SEEK
/* FIXME */ ); break;
338 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
339 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
340 default: perror("file_set_error"); set_error( ERROR_UNKNOWN
/* FIXME */ ); break;
344 struct file
*get_file_obj( struct process
*process
, handle_t handle
, unsigned int access
)
346 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
349 static int set_file_pointer( handle_t handle
, unsigned int *low
, int *high
, int whence
)
354 xto
= *low
+((off_t
)*high
<<32);
355 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
357 if ((result
= lseek(file
->obj
.fd
,xto
,whence
))==-1)
359 /* Check for seek before start of file */
361 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
362 if (((errno
== EINVAL
) || (errno
== EPERM
))
363 && (whence
!= SEEK_SET
) && (*high
< 0))
364 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK
/* FIXME */ );
367 release_object( file
);
370 *low
= result
& 0xffffffff;
371 *high
= result
>> 32;
372 release_object( file
);
376 static int truncate_file( handle_t handle
)
381 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
383 if (((result
= lseek( file
->obj
.fd
, 0, SEEK_CUR
)) == -1) ||
384 (ftruncate( file
->obj
.fd
, result
) == -1))
387 release_object( file
);
390 release_object( file
);
394 /* try to grow the file to the specified size */
395 int grow_file( struct file
*file
, int size_high
, int size_low
)
398 off_t size
= size_low
+ (((off_t
)size_high
)<<32);
400 if (fstat( file
->obj
.fd
, &st
) == -1)
405 if (st
.st_size
>= size
) return 1; /* already large enough */
406 if (ftruncate( file
->obj
.fd
, size
) != -1) return 1;
411 static int set_file_time( handle_t handle
, time_t access_time
, time_t write_time
)
414 struct utimbuf utimbuf
;
416 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
420 set_error( STATUS_INVALID_HANDLE
);
421 release_object( file
);
424 if (!access_time
|| !write_time
)
427 if (stat( file
->name
, &st
) == -1) goto error
;
428 if (!access_time
) access_time
= st
.st_atime
;
429 if (!write_time
) write_time
= st
.st_mtime
;
431 utimbuf
.actime
= access_time
;
432 utimbuf
.modtime
= write_time
;
433 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
434 release_object( file
);
438 release_object( file
);
442 static int file_lock( struct file
*file
, int offset_high
, int offset_low
,
443 int count_high
, int count_low
)
445 /* FIXME: implement this */
449 static int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
450 int count_high
, int count_low
)
452 /* FIXME: implement this */
457 DECL_HANDLER(create_file
)
462 if ((file
= create_file( get_req_data(req
), get_req_data_size(req
), req
->access
,
463 req
->sharing
, req
->create
, req
->attrs
)))
465 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
466 release_object( file
);
470 /* allocate a file handle for a Unix fd */
471 DECL_HANDLER(alloc_file_handle
)
477 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
479 set_error( STATUS_INVALID_HANDLE
);
482 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0 )))
484 req
->handle
= alloc_handle( current
->process
, file
, req
->access
, 0 );
485 release_object( file
);
489 /* get a Unix fd to access a file */
490 DECL_HANDLER(get_handle_fd
)
495 if ((obj
= get_handle_obj( current
->process
, req
->handle
, req
->access
, NULL
)))
497 int fd
= get_handle_fd( current
->process
, req
->handle
, req
->access
);
498 if (fd
!= -1) req
->fd
= fd
;
499 else if (!get_error())
501 if ((fd
= obj
->ops
->get_fd( obj
)) != -1)
502 send_client_fd( current
->process
, fd
, req
->handle
);
504 release_object( obj
);
508 /* set a file current position */
509 DECL_HANDLER(set_file_pointer
)
511 int high
= req
->high
;
513 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
515 req
->new_high
= high
;
518 /* truncate (or extend) a file */
519 DECL_HANDLER(truncate_file
)
521 truncate_file( req
->handle
);
524 /* flush a file buffers */
525 DECL_HANDLER(flush_file
)
529 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
531 obj
->ops
->flush( obj
);
532 release_object( obj
);
536 /* set a file access and modification times */
537 DECL_HANDLER(set_file_time
)
539 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
542 /* get a file information */
543 DECL_HANDLER(get_file_info
)
547 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
549 obj
->ops
->get_file_info( obj
, req
);
550 release_object( obj
);
554 /* lock a region of a file */
555 DECL_HANDLER(lock_file
)
559 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
561 file_lock( file
, req
->offset_high
, req
->offset_low
,
562 req
->count_high
, req
->count_low
);
563 release_object( file
);
567 /* unlock a region of a file */
568 DECL_HANDLER(unlock_file
)
572 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
574 file_unlock( file
, req
->offset_high
, req
->offset_low
,
575 req
->count_high
, req
->count_low
);
576 release_object( file
);