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 */
41 int drive_type
; /* type of drive the file is on */
44 #define NAME_HASH_SIZE 37
46 static struct file
*file_hash
[NAME_HASH_SIZE
];
48 static void file_dump( struct object
*obj
, int verbose
);
49 static int file_get_poll_events( struct object
*obj
);
50 static int file_get_fd( struct object
*obj
);
51 static int file_flush( struct object
*obj
);
52 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
);
53 static void file_destroy( struct object
*obj
);
55 static const struct object_ops file_ops
=
57 sizeof(struct file
), /* size */
59 default_poll_add_queue
, /* add_queue */
60 default_poll_remove_queue
, /* remove_queue */
61 default_poll_signaled
, /* signaled */
62 no_satisfied
, /* satisfied */
63 file_get_poll_events
, /* get_poll_events */
64 default_poll_event
, /* poll_event */
65 file_get_fd
, /* get_fd */
66 file_flush
, /* flush */
67 file_get_info
, /* get_file_info */
68 NULL
, /* queue_async */
69 file_destroy
/* destroy */
73 static int get_name_hash( const char *name
)
76 while (*name
) hash
^= (unsigned char)*name
++;
77 return hash
% NAME_HASH_SIZE
;
80 /* check if the desired access is possible without violating */
81 /* the sharing mode of other opens of the same file */
82 static int check_sharing( const char *name
, int hash
, unsigned int access
,
83 unsigned int sharing
)
86 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
87 unsigned int existing_access
= 0;
89 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
91 if (strcmp( file
->name
, name
)) continue;
92 existing_sharing
&= file
->sharing
;
93 existing_access
|= file
->access
;
95 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) goto error
;
96 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) goto error
;
97 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) goto error
;
98 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) goto error
;
101 set_error( STATUS_SHARING_VIOLATION
);
105 /* create a file from a file descriptor */
106 /* if the function fails the fd is closed */
107 static struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
,
108 unsigned int attrs
, int drive_type
)
111 if ((file
= alloc_object( &file_ops
, fd
)))
115 file
->access
= access
;
117 file
->sharing
= sharing
;
118 file
->drive_type
= drive_type
;
124 static struct file
*create_file( const char *nameptr
, size_t len
, unsigned int access
,
125 unsigned int sharing
, int create
, unsigned int attrs
,
135 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
136 memcpy( name
, nameptr
, len
);
139 /* check sharing mode */
140 hash
= get_name_hash( name
);
141 if (!check_sharing( name
, hash
, access
, sharing
)) goto error
;
145 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
146 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
147 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
148 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
149 case OPEN_EXISTING
: flags
= 0; break;
150 default: set_error( STATUS_INVALID_PARAMETER
); goto error
;
152 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
155 case GENERIC_READ
: flags
|= O_RDONLY
; break;
156 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
157 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
159 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
162 (!strcasecmp( name
+ len
- 4, ".exe" ) || !strcasecmp( name
+ len
- 4, ".com" )))
165 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
166 if ((fd
= open( name
, flags
| O_NONBLOCK
| O_LARGEFILE
, mode
)) == -1 )
168 /* refuse to open a directory */
169 if (fstat( fd
, &st
) == -1) goto file_error
;
170 if (S_ISDIR(st
.st_mode
))
172 set_error( STATUS_ACCESS_DENIED
);
176 if (!(file
= create_file_for_fd( fd
, access
, sharing
, attrs
, drive_type
)))
182 file
->next
= file_hash
[hash
];
183 file_hash
[hash
] = file
;
189 if (fd
!= -1) close( fd
);
194 /* check if two file objects point to the same file */
195 int is_same_file( struct file
*file1
, struct file
*file2
)
197 return !strcmp( file1
->name
, file2
->name
);
200 /* get the type of drive the file is on */
201 int get_file_drive_type( struct file
*file
)
203 return file
->drive_type
;
206 /* Create an anonymous Unix file */
207 int create_anonymous_file(void)
214 if (!(name
= tmpnam(NULL
)))
216 set_error( STATUS_TOO_MANY_OPENED_FILES
);
219 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
220 } while ((fd
== -1) && (errno
== EEXIST
));
230 /* Create a temp file for anonymous mappings */
231 struct file
*create_temp_file( int access
)
235 if ((fd
= create_anonymous_file()) == -1) return NULL
;
236 return create_file_for_fd( fd
, access
, 0, 0, DRIVE_FIXED
);
239 static void file_dump( struct object
*obj
, int verbose
)
241 struct file
*file
= (struct file
*)obj
;
242 assert( obj
->ops
== &file_ops
);
243 fprintf( stderr
, "File fd=%d flags=%08x name='%s'\n", file
->obj
.fd
, file
->flags
, file
->name
);
246 static int file_get_poll_events( struct object
*obj
)
248 struct file
*file
= (struct file
*)obj
;
250 assert( obj
->ops
== &file_ops
);
251 if (file
->access
& GENERIC_READ
) events
|= POLLIN
;
252 if (file
->access
& GENERIC_WRITE
) events
|= POLLOUT
;
256 static int file_get_fd( struct object
*obj
)
258 struct file
*file
= (struct file
*)obj
;
259 assert( obj
->ops
== &file_ops
);
263 static int file_flush( struct object
*obj
)
266 struct file
*file
= (struct file
*)grab_object(obj
);
267 assert( obj
->ops
== &file_ops
);
269 ret
= (fsync( file
->obj
.fd
) != -1);
270 if (!ret
) file_set_error();
271 release_object( file
);
275 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
)
278 struct file
*file
= (struct file
*)obj
;
279 assert( obj
->ops
== &file_ops
);
283 if (fstat( file
->obj
.fd
, &st
) == -1)
286 return FD_TYPE_INVALID
;
288 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
289 S_ISSOCK(st
.st_mode
) || isatty(file
->obj
.fd
)) reply
->type
= FILE_TYPE_CHAR
;
290 else reply
->type
= FILE_TYPE_DISK
;
291 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
292 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
293 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
294 reply
->access_time
= st
.st_atime
;
295 reply
->write_time
= st
.st_mtime
;
296 if (S_ISDIR(st
.st_mode
))
298 reply
->size_high
= 0;
303 reply
->size_high
= st
.st_size
>> 32;
304 reply
->size_low
= st
.st_size
& 0xffffffff;
306 reply
->links
= st
.st_nlink
;
307 reply
->index_high
= st
.st_dev
;
308 reply
->index_low
= st
.st_ino
;
309 reply
->serial
= 0; /* FIXME */
311 return FD_TYPE_DEFAULT
;
314 static void file_destroy( struct object
*obj
)
316 struct file
*file
= (struct file
*)obj
;
317 assert( obj
->ops
== &file_ops
);
321 /* remove it from the hashing list */
322 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
323 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
325 *pptr
= (*pptr
)->next
;
326 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
331 /* set the last error depending on errno */
332 void file_set_error(void)
336 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
337 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
338 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
341 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
342 case EROFS
: set_error( STATUS_MEDIA_WRITE_PROTECTED
); break;
343 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
344 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
345 case EISDIR
: set_error( 0xc0010000 | ERROR_CANNOT_MAKE
/* FIXME */ ); break;
347 case EMFILE
: set_error( STATUS_NO_MORE_FILES
); break;
348 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
349 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
350 case ESPIPE
: set_error( 0xc0010000 | ERROR_SEEK
/* FIXME */ ); break;
351 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
352 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
353 default: perror("file_set_error"); set_error( ERROR_UNKNOWN
/* FIXME */ ); break;
357 struct file
*get_file_obj( struct process
*process
, handle_t handle
, unsigned int access
)
359 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
362 static int set_file_pointer( handle_t handle
, unsigned int *low
, int *high
, int whence
)
367 xto
= *low
+((off_t
)*high
<<32);
368 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
370 if ((result
= lseek(file
->obj
.fd
,xto
,whence
))==-1)
372 /* Check for seek before start of file */
374 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
375 if (((errno
== EINVAL
) || (errno
== EPERM
))
376 && (whence
!= SEEK_SET
) && (*high
< 0))
377 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK
/* FIXME */ );
380 release_object( file
);
383 *low
= result
& 0xffffffff;
384 *high
= result
>> 32;
385 release_object( file
);
389 static int truncate_file( handle_t handle
)
394 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
396 if (((result
= lseek( file
->obj
.fd
, 0, SEEK_CUR
)) == -1) ||
397 (ftruncate( file
->obj
.fd
, result
) == -1))
400 release_object( file
);
403 release_object( file
);
407 /* try to grow the file to the specified size */
408 int grow_file( struct file
*file
, int size_high
, int size_low
)
411 off_t size
= size_low
+ (((off_t
)size_high
)<<32);
413 if (fstat( file
->obj
.fd
, &st
) == -1)
418 if (st
.st_size
>= size
) return 1; /* already large enough */
419 if (ftruncate( file
->obj
.fd
, size
) != -1) return 1;
424 static int set_file_time( handle_t handle
, time_t access_time
, time_t write_time
)
427 struct utimbuf utimbuf
;
429 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
433 set_error( STATUS_INVALID_HANDLE
);
434 release_object( file
);
437 if (!access_time
|| !write_time
)
440 if (stat( file
->name
, &st
) == -1) goto error
;
441 if (!access_time
) access_time
= st
.st_atime
;
442 if (!write_time
) write_time
= st
.st_mtime
;
444 utimbuf
.actime
= access_time
;
445 utimbuf
.modtime
= write_time
;
446 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
447 release_object( file
);
451 release_object( file
);
455 static int file_lock( struct file
*file
, int offset_high
, int offset_low
,
456 int count_high
, int count_low
)
458 /* FIXME: implement this */
462 static int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
463 int count_high
, int count_low
)
465 /* FIXME: implement this */
470 DECL_HANDLER(create_file
)
475 if ((file
= create_file( get_req_data(), get_req_data_size(), req
->access
,
476 req
->sharing
, req
->create
, req
->attrs
, req
->drive_type
)))
478 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
479 release_object( file
);
483 /* allocate a file handle for a Unix fd */
484 DECL_HANDLER(alloc_file_handle
)
490 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
492 set_error( STATUS_INVALID_HANDLE
);
495 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
498 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->inherit
);
499 release_object( file
);
503 /* get a Unix fd to access a file */
504 DECL_HANDLER(get_handle_fd
)
509 reply
->type
= FD_TYPE_INVALID
;
510 if ((obj
= get_handle_obj( current
->process
, req
->handle
, req
->access
, NULL
)))
512 int fd
= get_handle_fd( current
->process
, req
->handle
, req
->access
);
513 if (fd
!= -1) reply
->fd
= fd
;
514 else if (!get_error())
516 if ((fd
= obj
->ops
->get_fd( obj
)) != -1)
517 send_client_fd( current
->process
, fd
, req
->handle
);
519 reply
->type
= obj
->ops
->get_file_info( obj
, NULL
);
520 release_object( obj
);
524 /* set a file current position */
525 DECL_HANDLER(set_file_pointer
)
527 int high
= req
->high
;
529 set_file_pointer( req
->handle
, &low
, &high
, req
->whence
);
530 reply
->new_low
= low
;
531 reply
->new_high
= high
;
534 /* truncate (or extend) a file */
535 DECL_HANDLER(truncate_file
)
537 truncate_file( req
->handle
);
540 /* flush a file buffers */
541 DECL_HANDLER(flush_file
)
545 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
547 obj
->ops
->flush( obj
);
548 release_object( obj
);
552 /* set a file access and modification times */
553 DECL_HANDLER(set_file_time
)
555 set_file_time( req
->handle
, req
->access_time
, req
->write_time
);
558 /* get a file information */
559 DECL_HANDLER(get_file_info
)
563 if ((obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
)))
565 obj
->ops
->get_file_info( obj
, reply
);
566 release_object( obj
);
570 /* lock a region of a file */
571 DECL_HANDLER(lock_file
)
575 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
577 file_lock( file
, req
->offset_high
, req
->offset_low
,
578 req
->count_high
, req
->count_low
);
579 release_object( file
);
583 /* unlock a region of a file */
584 DECL_HANDLER(unlock_file
)
588 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
590 file_unlock( file
, req
->offset_high
, req
->offset_low
,
591 req
->count_high
, req
->count_low
);
592 release_object( file
);