2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
12 #include <sys/errno.h>
15 #include <sys/types.h>
22 #include "server/process.h"
23 #include "server/thread.h"
27 struct object obj
; /* object header */
28 struct file
*next
; /* next file in hashing list */
29 char *name
; /* file name */
30 int fd
; /* Unix file descriptor */
31 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
32 unsigned int flags
; /* flags (FILE_FLAG_*) */
33 unsigned int sharing
; /* file sharing mode */
36 #define NAME_HASH_SIZE 37
38 static struct file
*file_hash
[NAME_HASH_SIZE
];
40 static void file_dump( struct object
*obj
, int verbose
);
41 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
42 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
43 static int file_signaled( struct object
*obj
, struct thread
*thread
);
44 static int file_get_read_fd( struct object
*obj
);
45 static int file_get_write_fd( struct object
*obj
);
46 static int file_flush( struct object
*obj
);
47 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
);
48 static void file_destroy( struct object
*obj
);
50 static const struct object_ops file_ops
=
64 static const struct select_ops select_ops
=
67 NULL
/* we never set a timeout on a file */
71 static int get_name_hash( const char *name
)
74 while (*name
) hash
^= *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
)) return 0;
94 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) return 0;
95 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) return 0;
96 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) return 0;
100 struct object
*create_file( int fd
, const char *name
, unsigned int access
,
101 unsigned int sharing
, int create
, unsigned int attrs
)
113 SET_ERROR( ERROR_INVALID_PARAMETER
);
117 /* check sharing mode */
118 hash
= get_name_hash( name
);
119 if (!check_sharing( name
, hash
, access
, sharing
))
121 SET_ERROR( ERROR_SHARING_VIOLATION
);
127 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
128 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
129 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
130 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
131 case OPEN_EXISTING
: flags
= 0; break;
132 default: SET_ERROR( ERROR_INVALID_PARAMETER
); return NULL
;
134 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
137 case GENERIC_READ
: flags
|= O_RDONLY
; break;
138 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
139 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
142 if ((fd
= open( name
, flags
| O_NONBLOCK
,
143 (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666 )) == -1)
148 /* Refuse to open a directory */
149 if (fstat( fd
, &st
) == -1)
155 if (S_ISDIR(st
.st_mode
))
157 SET_ERROR( ERROR_ACCESS_DENIED
);
164 if ((fd
= dup(fd
)) == -1)
171 if (!(file
= mem_alloc( sizeof(*file
) )))
178 if (!(file
->name
= mem_alloc( strlen(name
) + 1 )))
184 strcpy( file
->name
, name
);
185 file
->next
= file_hash
[hash
];
186 file_hash
[hash
] = file
;
193 init_object( &file
->obj
, &file_ops
, NULL
);
195 file
->access
= access
;
197 file
->sharing
= sharing
;
202 static void file_dump( struct object
*obj
, int verbose
)
204 struct file
*file
= (struct file
*)obj
;
205 assert( obj
->ops
== &file_ops
);
206 printf( "File fd=%d flags=%08x name='%s'\n",
207 file
->fd
, file
->flags
, file
->name
);
210 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
212 struct file
*file
= (struct file
*)obj
;
213 assert( obj
->ops
== &file_ops
);
214 if (!obj
->head
) /* first on the queue */
216 if (!add_select_user( file
->fd
, READ_EVENT
| WRITE_EVENT
, &select_ops
, file
))
218 SET_ERROR( ERROR_OUTOFMEMORY
);
222 add_queue( obj
, entry
);
226 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
228 struct file
*file
= (struct file
*)grab_object(obj
);
229 assert( obj
->ops
== &file_ops
);
231 remove_queue( obj
, entry
);
232 if (!obj
->head
) /* last on the queue is gone */
233 remove_select_user( file
->fd
);
234 release_object( obj
);
237 static int file_signaled( struct object
*obj
, struct thread
*thread
)
239 fd_set read_fds
, write_fds
;
240 struct timeval tv
= { 0, 0 };
242 struct file
*file
= (struct file
*)obj
;
243 assert( obj
->ops
== &file_ops
);
245 FD_ZERO( &read_fds
);
246 FD_ZERO( &write_fds
);
247 if (file
->access
& GENERIC_READ
) FD_SET( file
->fd
, &read_fds
);
248 if (file
->access
& GENERIC_WRITE
) FD_SET( file
->fd
, &write_fds
);
249 return select( file
->fd
+ 1, &read_fds
, &write_fds
, NULL
, &tv
) > 0;
252 static int file_get_read_fd( struct object
*obj
)
254 struct file
*file
= (struct file
*)obj
;
255 assert( obj
->ops
== &file_ops
);
256 return dup( file
->fd
);
259 static int file_get_write_fd( struct object
*obj
)
261 struct file
*file
= (struct file
*)obj
;
262 assert( obj
->ops
== &file_ops
);
263 return dup( file
->fd
);
266 static int file_flush( struct object
*obj
)
269 struct file
*file
= (struct file
*)grab_object(obj
);
270 assert( obj
->ops
== &file_ops
);
272 ret
= (fsync( file
->fd
) != -1);
273 if (!ret
) file_set_error();
274 release_object( file
);
278 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
)
281 struct file
*file
= (struct file
*)obj
;
282 assert( obj
->ops
== &file_ops
);
284 if (fstat( file
->fd
, &st
) == -1)
289 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
290 S_ISSOCK(st
.st_mode
) || isatty(file
->fd
)) reply
->type
= FILE_TYPE_CHAR
;
291 else reply
->type
= FILE_TYPE_DISK
;
292 if (S_ISDIR(st
.st_mode
)) reply
->attr
= FILE_ATTRIBUTE_DIRECTORY
;
293 else reply
->attr
= FILE_ATTRIBUTE_ARCHIVE
;
294 if (!(st
.st_mode
& S_IWUSR
)) reply
->attr
|= FILE_ATTRIBUTE_READONLY
;
295 reply
->access_time
= st
.st_atime
;
296 reply
->write_time
= st
.st_mtime
;
297 reply
->size_high
= 0;
298 reply
->size_low
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
299 reply
->links
= st
.st_nlink
;
300 reply
->index_high
= st
.st_dev
;
301 reply
->index_low
= st
.st_ino
;
302 reply
->serial
= 0; /* FIXME */
306 static void file_destroy( struct object
*obj
)
308 struct file
*file
= (struct file
*)obj
;
309 assert( obj
->ops
== &file_ops
);
313 /* remove it from the hashing list */
314 struct file
**pptr
= &file_hash
[get_name_hash( file
->name
)];
315 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
317 *pptr
= (*pptr
)->next
;
318 if (file
->flags
& FILE_FLAG_DELETE_ON_CLOSE
) unlink( file
->name
);
325 /* set the last error depending on errno */
326 void file_set_error(void)
330 case EAGAIN
: SET_ERROR( ERROR_SHARING_VIOLATION
); break;
331 case EBADF
: SET_ERROR( ERROR_INVALID_HANDLE
); break;
332 case ENOSPC
: SET_ERROR( ERROR_HANDLE_DISK_FULL
); break;
334 case EPERM
: SET_ERROR( ERROR_ACCESS_DENIED
); break;
335 case EROFS
: SET_ERROR( ERROR_WRITE_PROTECT
); break;
336 case EBUSY
: SET_ERROR( ERROR_LOCK_VIOLATION
); break;
337 case ENOENT
: SET_ERROR( ERROR_FILE_NOT_FOUND
); break;
338 case EISDIR
: SET_ERROR( ERROR_CANNOT_MAKE
); break;
340 case EMFILE
: SET_ERROR( ERROR_NO_MORE_FILES
); break;
341 case EEXIST
: SET_ERROR( ERROR_FILE_EXISTS
); break;
342 case EINVAL
: SET_ERROR( ERROR_INVALID_PARAMETER
); break;
343 case ESPIPE
: SET_ERROR( ERROR_SEEK
); break;
344 case ENOTEMPTY
: SET_ERROR( ERROR_DIR_NOT_EMPTY
); break;
345 default: perror("file_set_error"); SET_ERROR( ERROR_UNKNOWN
); break;
349 struct file
*get_file_obj( struct process
*process
, int handle
,
350 unsigned int access
)
352 return (struct file
*)get_handle_obj( current
->process
, handle
,
356 int file_get_mmap_fd( struct file
*file
)
358 return dup( file
->fd
);
361 int set_file_pointer( int handle
, int *low
, int *high
, int whence
)
368 fprintf( stderr
, "set_file_pointer: offset > 4Gb not supported yet\n" );
369 SET_ERROR( ERROR_INVALID_PARAMETER
);
373 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
375 if ((result
= lseek( file
->fd
, *low
, whence
)) == -1)
377 /* Check for seek before start of file */
378 if ((errno
== EINVAL
) && (whence
!= SEEK_SET
) && (*low
< 0))
379 SET_ERROR( ERROR_NEGATIVE_SEEK
);
382 release_object( file
);
386 release_object( file
);
390 int truncate_file( int handle
)
395 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
397 if (((result
= lseek( file
->fd
, 0, SEEK_CUR
)) == -1) ||
398 (ftruncate( file
->fd
, result
) == -1))
401 release_object( file
);
404 release_object( file
);
409 int set_file_time( int handle
, time_t access_time
, time_t write_time
)
412 struct utimbuf utimbuf
;
414 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
416 if (!access_time
|| !write_time
)
419 if (stat( file
->name
, &st
) == -1) goto error
;
420 if (!access_time
) access_time
= st
.st_atime
;
421 if (!write_time
) write_time
= st
.st_mtime
;
423 utimbuf
.actime
= access_time
;
424 utimbuf
.modtime
= write_time
;
425 if (utime( file
->name
, &utimbuf
) == -1) goto error
;
426 release_object( file
);
430 release_object( file
);
434 int file_lock( struct file
*file
, int offset_high
, int offset_low
,
435 int count_high
, int count_low
)
437 /* FIXME: implement this */
441 int file_unlock( struct file
*file
, int offset_high
, int offset_low
,
442 int count_high
, int count_low
)
444 /* FIXME: implement this */