2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include <sys/errno.h>
16 #include <sys/types.h>
23 #include "server/process.h"
24 #include "server/thread.h"
28 struct object obj
; /* object header */
29 struct file
*next
; /* next file in hashing list */
30 char *name
; /* file name */
31 int fd
; /* Unix file descriptor */
32 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
33 unsigned int flags
; /* flags (FILE_FLAG_*) */
34 unsigned int sharing
; /* file sharing mode */
37 #define NAME_HASH_SIZE 37
39 static struct file
*file_hash
[NAME_HASH_SIZE
];
41 static void file_dump( struct object
*obj
, int verbose
);
42 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
43 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
44 static int file_signaled( struct object
*obj
, struct thread
*thread
);
45 static int file_get_read_fd( struct object
*obj
);
46 static int file_get_write_fd( struct object
*obj
);
47 static int file_flush( struct object
*obj
);
48 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
);
49 static void file_destroy( struct object
*obj
);
51 static const struct object_ops file_ops
=
65 static const struct select_ops select_ops
=
68 NULL
/* we never set a timeout on a file */
72 static int get_name_hash( const char *name
)
75 while (*name
) hash
^= *name
++;
76 return hash
% NAME_HASH_SIZE
;
79 /* check if the desired access is possible without violating */
80 /* the sharing mode of other opens of the same file */
81 static int check_sharing( const char *name
, int hash
, unsigned int access
,
82 unsigned int sharing
)
85 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
86 unsigned int existing_access
= 0;
88 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
90 if (strcmp( file
->name
, name
)) continue;
91 existing_sharing
&= file
->sharing
;
92 existing_access
|= file
->access
;
94 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) return 0;
95 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) return 0;
96 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) return 0;
97 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) return 0;
101 struct object
*create_file( int fd
, const char *name
, unsigned int access
,
102 unsigned int sharing
, int create
, unsigned int attrs
)
114 SET_ERROR( ERROR_INVALID_PARAMETER
);
118 /* check sharing mode */
119 hash
= get_name_hash( name
);
120 if (!check_sharing( name
, hash
, access
, sharing
))
122 SET_ERROR( ERROR_SHARING_VIOLATION
);
128 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
129 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
130 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
131 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
132 case OPEN_EXISTING
: flags
= 0; break;
133 default: SET_ERROR( ERROR_INVALID_PARAMETER
); return NULL
;
135 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
138 case GENERIC_READ
: flags
|= O_RDONLY
; break;
139 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
140 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
143 /* FIXME: should set error to ERROR_ALREADY_EXISTS if file existed before */
144 if ((fd
= open( name
, flags
| O_NONBLOCK
,
145 (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666 )) == -1)
150 /* Refuse to open a directory */
151 if (fstat( fd
, &st
) == -1)
157 if (S_ISDIR(st
.st_mode
))
159 SET_ERROR( ERROR_ACCESS_DENIED
);
166 if ((fd
= dup(fd
)) == -1)
173 if (!(file
= mem_alloc( sizeof(*file
) )))
180 if (!(file
->name
= mem_alloc( strlen(name
) + 1 )))
186 strcpy( file
->name
, name
);
187 file
->next
= file_hash
[hash
];
188 file_hash
[hash
] = file
;
195 init_object( &file
->obj
, &file_ops
, NULL
);
197 file
->access
= access
;
199 file
->sharing
= sharing
;
204 /* Create a temp file for anonymous mappings */
205 struct file
*create_temp_file( int access
)
213 if (!(name
= tmpnam(NULL
)))
215 SET_ERROR( ERROR_TOO_MANY_OPEN_FILES
);
218 fd
= open( name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600 );
219 } while ((fd
== -1) && (errno
== EEXIST
));
227 if (!(file
= mem_alloc( sizeof(*file
) )))
232 init_object( &file
->obj
, &file_ops
, NULL
);
236 file
->access
= access
;
243 static void file_dump( struct object
*obj
, int verbose
)
245 struct file
*file
= (struct file
*)obj
;
246 assert( obj
->ops
== &file_ops
);
247 printf( "File fd=%d flags=%08x name='%s'\n",
248 file
->fd
, file
->flags
, file
->name
);
251 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
253 struct file
*file
= (struct file
*)obj
;
254 assert( obj
->ops
== &file_ops
);
255 if (!obj
->head
) /* first on the queue */
257 if (!add_select_user( file
->fd
, READ_EVENT
| WRITE_EVENT
, &select_ops
, file
))
259 SET_ERROR( ERROR_OUTOFMEMORY
);
263 add_queue( obj
, entry
);
267 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
269 struct file
*file
= (struct file
*)grab_object(obj
);
270 assert( obj
->ops
== &file_ops
);
272 remove_queue( obj
, entry
);
273 if (!obj
->head
) /* last on the queue is gone */
274 remove_select_user( file
->fd
);
275 release_object( obj
);
278 static int file_signaled( struct object
*obj
, struct thread
*thread
)
280 fd_set read_fds
, write_fds
;
281 struct timeval tv
= { 0, 0 };
283 struct file
*file
= (struct file
*)obj
;
284 assert( obj
->ops
== &file_ops
);
286 FD_ZERO( &read_fds
);
287 FD_ZERO( &write_fds
);
288 if (file
->access
& GENERIC_READ
) FD_SET( file
->fd
, &read_fds
);
289 if (file
->access
& GENERIC_WRITE
) FD_SET( file
->fd
, &write_fds
);
290 return select( file
->fd
+ 1, &read_fds
, &write_fds
, NULL
, &tv
) > 0;
293 static int file_get_read_fd( struct object
*obj
)
295 struct file
*file
= (struct file
*)obj
;
296 assert( obj
->ops
== &file_ops
);
297 return dup( file
->fd
);
300 static int file_get_write_fd( struct object
*obj
)
302 struct file
*file
= (struct file
*)obj
;
303 assert( obj
->ops
== &file_ops
);
304 return dup( file
->fd
);
307 static int file_flush( struct object
*obj
)
310 struct file
*file
= (struct file
*)grab_object(obj
);
311 assert( obj
->ops
== &file_ops
);
313 ret
= (fsync( file
->fd
) != -1);
314 if (!ret
) file_set_error();
315 release_object( file
);
319 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
)
322 struct file
*file
= (struct file
*)obj
;
323 assert( obj
->ops
== &file_ops
);
325 if (fstat( file
->fd
, &st
) == -1)
330 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
331 S_ISSOCK(st
.st_mode
) || isatty(file
->fd
)) reply
->type
= FILE_TYPE_CHAR
;
332 else reply
->type
= FILE_TYPE_DISK
;
333 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
334 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
335 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
336 reply
->access_time
= st
.st_atime
;
337 reply
->write_time
= st
.st_mtime
;
338 reply
->size_high
= 0;
339 reply
->size_low
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
340 reply
->links
= st
.st_nlink
;
341 reply
->index_high
= st
.st_dev
;
342 reply
->index_low
= st
.st_ino
;
343 reply
->serial
= 0; /* FIXME */
347 static void file_destroy( struct object
*obj
)
349 struct file
*file
= (struct file
*)obj
;
350 assert( obj
->ops
== &file_ops
);
354 /* remove it from the hashing list */
355 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
356 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
358 *pptr
= (*pptr
)->next
;
359 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
366 /* set the last error depending on errno */
367 void file_set_error(void)
371 case EAGAIN
: SET_ERROR( ERROR_SHARING_VIOLATION
); break;
372 case EBADF
: SET_ERROR( ERROR_INVALID_HANDLE
); break;
373 case ENOSPC
: SET_ERROR( ERROR_HANDLE_DISK_FULL
); break;
375 case EPERM
: SET_ERROR( ERROR_ACCESS_DENIED
); break;
376 case EROFS
: SET_ERROR( ERROR_WRITE_PROTECT
); break;
377 case EBUSY
: SET_ERROR( ERROR_LOCK_VIOLATION
); break;
378 case ENOENT
: SET_ERROR( ERROR_FILE_NOT_FOUND
); break;
379 case EISDIR
: SET_ERROR( ERROR_CANNOT_MAKE
); break;
381 case EMFILE
: SET_ERROR( ERROR_NO_MORE_FILES
); break;
382 case EEXIST
: SET_ERROR( ERROR_FILE_EXISTS
); break;
383 case EINVAL
: SET_ERROR( ERROR_INVALID_PARAMETER
); break;
384 case ESPIPE
: SET_ERROR( ERROR_SEEK
); break;
385 case ENOTEMPTY
: SET_ERROR( ERROR_DIR_NOT_EMPTY
); break;
386 default: perror("file_set_error"); SET_ERROR( ERROR_UNKNOWN
); break;
390 struct file
*get_file_obj( struct process
*process
, int handle
,
391 unsigned int access
)
393 return (struct file
*)get_handle_obj( current
->process
, handle
,
397 int file_get_mmap_fd( struct file
*file
)
399 return dup( file
->fd
);
402 int set_file_pointer( int handle
, int *low
, int *high
, int whence
)
409 fprintf( stderr
, "set_file_pointer: offset > 4Gb not supported yet\n" );
410 SET_ERROR( ERROR_INVALID_PARAMETER
);
414 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
416 if ((result
= lseek( file
->fd
, *low
, whence
)) == -1)
418 /* Check for seek before start of file */
419 if ((errno
== EINVAL
) && (whence
!= SEEK_SET
) && (*low
< 0))
420 SET_ERROR( ERROR_NEGATIVE_SEEK
);
423 release_object( file
);
427 release_object( file
);
431 int truncate_file( int handle
)
436 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
438 if (((result
= lseek( file
->fd
, 0, SEEK_CUR
)) == -1) ||
439 (ftruncate( file
->fd
, result
) == -1))
442 release_object( file
);
445 release_object( file
);
450 /* try to grow the file to the specified size */
451 int grow_file( struct file
*file
, int size_high
, int size_low
)
457 SET_ERROR( ERROR_INVALID_PARAMETER
);
460 if (fstat( file
->fd
, &st
) == -1)
465 if (st
.st_size
>= size_low
) return 1; /* already large enough */
466 if (ftruncate( file
->fd
, size_low
) != -1) return 1;
471 int set_file_time( int handle
, time_t access_time
, time_t write_time
)
474 struct utimbuf utimbuf
;
476 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
478 if (!access_time
|| !write_time
)
481 if (stat( file
->name
, &st
) == -1) goto error
;
482 if (!access_time
) access_time
= st
.st_atime
;
483 if (!write_time
) write_time
= st
.st_mtime
;
485 utimbuf
.actime
= access_time
;
486 utimbuf
.modtime
= write_time
;
487 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
488 release_object( file
);
492 release_object( file
);
496 int file_lock( struct file
*file
, int offset_high
, int offset_low
,
497 int count_high
, int count_low
)
499 /* FIXME: implement this */
503 int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
504 int count_high
, int count_low
)
506 /* FIXME: implement this */