2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
12 #include <sys/errno.h>
15 #include <sys/types.h>
22 #include "server/thread.h"
26 struct object obj
; /* object header */
27 struct file
*next
; /* next file in hashing list */
28 char *name
; /* file name */
29 int fd
; /* Unix file descriptor */
30 unsigned int access
; /* file access (GENERIC_READ/WRITE) */
31 unsigned int flags
; /* flags (FILE_FLAG_*) */
32 unsigned int sharing
; /* file sharing mode */
35 #define NAME_HASH_SIZE 37
37 static struct file
*file_hash
[NAME_HASH_SIZE
];
39 static void file_dump( struct object
*obj
, int verbose
);
40 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
41 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
42 static int file_signaled( struct object
*obj
, struct thread
*thread
);
43 static int file_get_read_fd( struct object
*obj
);
44 static int file_get_write_fd( struct object
*obj
);
45 static int file_flush( struct object
*obj
);
46 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
);
47 static void file_destroy( struct object
*obj
);
49 static const struct object_ops file_ops
=
63 static const struct select_ops select_ops
=
66 NULL
/* we never set a timeout on a file */
70 static int get_name_hash( const char *name
)
73 while (*name
) hash
^= *name
++;
74 return hash
% NAME_HASH_SIZE
;
77 /* check if the desired access is possible without violating */
78 /* the sharing mode of other opens of the same file */
79 static int check_sharing( const char *name
, int hash
, unsigned int access
,
80 unsigned int sharing
)
83 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
84 unsigned int existing_access
= 0;
86 for (file
= file_hash
[hash
]; file
; file
= file
->next
)
88 if (strcmp( file
->name
, name
)) continue;
89 existing_sharing
&= file
->sharing
;
90 existing_access
|= file
->access
;
92 if ((access
& GENERIC_READ
) && !(existing_sharing
& FILE_SHARE_READ
)) return 0;
93 if ((access
& GENERIC_WRITE
) && !(existing_sharing
& FILE_SHARE_WRITE
)) return 0;
94 if ((existing_access
& GENERIC_READ
) && !(sharing
& FILE_SHARE_READ
)) return 0;
95 if ((existing_access
& GENERIC_WRITE
) && !(sharing
& FILE_SHARE_WRITE
)) return 0;
99 struct object
*create_file( int fd
, const char *name
, unsigned int access
,
100 unsigned int sharing
, int create
, unsigned int attrs
)
112 SET_ERROR( ERROR_INVALID_PARAMETER
);
116 /* check sharing mode */
117 hash
= get_name_hash( name
);
118 if (!check_sharing( name
, hash
, access
, sharing
))
120 SET_ERROR( ERROR_SHARING_VIOLATION
);
126 case CREATE_NEW
: flags
= O_CREAT
| O_EXCL
; break;
127 case CREATE_ALWAYS
: flags
= O_CREAT
| O_TRUNC
; break;
128 case OPEN_ALWAYS
: flags
= O_CREAT
; break;
129 case TRUNCATE_EXISTING
: flags
= O_TRUNC
; break;
130 case OPEN_EXISTING
: flags
= 0; break;
131 default: SET_ERROR( ERROR_INVALID_PARAMETER
); return NULL
;
133 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
136 case GENERIC_READ
: flags
|= O_RDONLY
; break;
137 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
138 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
141 if ((fd
= open( name
, flags
| O_NONBLOCK
,
142 (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666 )) == -1)
147 /* Refuse to open a directory */
148 if (fstat( fd
, &st
) == -1)
154 if (S_ISDIR(st
.st_mode
))
156 SET_ERROR( ERROR_ACCESS_DENIED
);
163 if ((fd
= dup(fd
)) == -1)
170 if (!(file
= mem_alloc( sizeof(*file
) )))
177 if (!(file
->name
= mem_alloc( strlen(name
) + 1 )))
183 strcpy( file
->name
, name
);
184 file
->next
= file_hash
[hash
];
185 file_hash
[hash
] = file
;
192 init_object( &file
->obj
, &file_ops
, NULL
);
194 file
->access
= access
;
196 file
->sharing
= sharing
;
201 static void file_dump( struct object
*obj
, int verbose
)
203 struct file
*file
= (struct file
*)obj
;
204 assert( obj
->ops
== &file_ops
);
205 printf( "File fd=%d flags=%08x name='%s'\n",
206 file
->fd
, file
->flags
, file
->name
);
209 static int file_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
211 struct file
*file
= (struct file
*)obj
;
212 assert( obj
->ops
== &file_ops
);
213 if (!obj
->head
) /* first on the queue */
215 if (!add_select_user( file
->fd
, READ_EVENT
| WRITE_EVENT
, &select_ops
, file
))
217 SET_ERROR( ERROR_OUTOFMEMORY
);
221 add_queue( obj
, entry
);
225 static void file_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
227 struct file
*file
= (struct file
*)grab_object(obj
);
228 assert( obj
->ops
== &file_ops
);
230 remove_queue( obj
, entry
);
231 if (!obj
->head
) /* last on the queue is gone */
232 remove_select_user( file
->fd
);
233 release_object( obj
);
236 static int file_signaled( struct object
*obj
, struct thread
*thread
)
238 fd_set read_fds
, write_fds
;
239 struct timeval tv
= { 0, 0 };
241 struct file
*file
= (struct file
*)obj
;
242 assert( obj
->ops
== &file_ops
);
244 FD_ZERO( &read_fds
);
245 FD_ZERO( &write_fds
);
246 if (file
->access
& GENERIC_READ
) FD_SET( file
->fd
, &read_fds
);
247 if (file
->access
& GENERIC_WRITE
) FD_SET( file
->fd
, &write_fds
);
248 return select( file
->fd
+ 1, &read_fds
, &write_fds
, NULL
, &tv
) > 0;
251 static int file_get_read_fd( struct object
*obj
)
253 struct file
*file
= (struct file
*)obj
;
254 assert( obj
->ops
== &file_ops
);
255 return dup( file
->fd
);
258 static int file_get_write_fd( struct object
*obj
)
260 struct file
*file
= (struct file
*)obj
;
261 assert( obj
->ops
== &file_ops
);
262 return dup( file
->fd
);
265 static int file_flush( struct object
*obj
)
268 struct file
*file
= (struct file
*)grab_object(obj
);
269 assert( obj
->ops
== &file_ops
);
271 ret
= (fsync( file
->fd
) != -1);
272 if (!ret
) file_set_error();
273 release_object( file
);
277 static int file_get_info( struct object
*obj
, struct get_file_info_reply
*reply
)
280 struct file
*file
= (struct file
*)obj
;
281 assert( obj
->ops
== &file_ops
);
283 if (fstat( file
->fd
, &st
) == -1)
288 if (S_ISCHR(st
.st_mode
) || S_ISFIFO(st
.st_mode
) ||
289 S_ISSOCK(st
.st_mode
) || isatty(file
->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 reply
->size_high
= 0;
297 reply
->size_low
= S_ISDIR(st
.st_mode
) ? 0 : st
.st_size
;
298 reply
->links
= st
.st_nlink
;
299 reply
->index_high
= st
.st_dev
;
300 reply
->index_low
= st
.st_ino
;
301 reply
->serial
= 0; /* FIXME */
305 static void file_destroy( struct object
*obj
)
308 struct file
*file
= (struct file
*)obj
;
309 assert( obj
->ops
== &file_ops
);
311 /* remove it from the hashing list */
312 pptr
= &file_hash
[get_name_hash( file
->name
)];
313 while (*pptr
&& *pptr
!= file
) pptr
= &(*pptr
)->next
;
315 *pptr
= (*pptr
)->next
;
322 /* set the last error depending on errno */
323 void file_set_error(void)
327 case EAGAIN
: SET_ERROR( ERROR_SHARING_VIOLATION
); break;
328 case EBADF
: SET_ERROR( ERROR_INVALID_HANDLE
); break;
329 case ENOSPC
: SET_ERROR( ERROR_HANDLE_DISK_FULL
); break;
331 case EPERM
: SET_ERROR( ERROR_ACCESS_DENIED
); break;
332 case EROFS
: SET_ERROR( ERROR_WRITE_PROTECT
); break;
333 case EBUSY
: SET_ERROR( ERROR_LOCK_VIOLATION
); break;
334 case ENOENT
: SET_ERROR( ERROR_FILE_NOT_FOUND
); break;
335 case EISDIR
: SET_ERROR( ERROR_CANNOT_MAKE
); break;
337 case EMFILE
: SET_ERROR( ERROR_NO_MORE_FILES
); break;
338 case EEXIST
: SET_ERROR( ERROR_FILE_EXISTS
); break;
339 case EINVAL
: SET_ERROR( ERROR_INVALID_PARAMETER
); break;
340 case ESPIPE
: SET_ERROR( ERROR_SEEK
); break;
341 case ENOTEMPTY
: SET_ERROR( ERROR_DIR_NOT_EMPTY
); break;
342 default: perror("file_set_error"); SET_ERROR( ERROR_UNKNOWN
); break;
346 struct file
*get_file_obj( struct process
*process
, int handle
,
347 unsigned int access
)
349 return (struct file
*)get_handle_obj( current
->process
, handle
,
353 int file_get_mmap_fd( struct file
*file
)
355 return dup( file
->fd
);
358 int set_file_pointer( int handle
, int *low
, int *high
, int whence
)
365 fprintf( stderr
, "set_file_pointer: offset > 4Gb not supported yet\n" );
366 SET_ERROR( ERROR_INVALID_PARAMETER
);
370 if (!(file
= get_file_obj( current
->process
, handle
, 0 )))
372 if ((result
= lseek( file
->fd
, *low
, whence
)) == -1)
374 /* Check for seek before start of file */
375 if ((errno
== EINVAL
) && (whence
!= SEEK_SET
) && (*low
< 0))
376 SET_ERROR( ERROR_NEGATIVE_SEEK
);
379 release_object( file
);
383 release_object( file
);
387 int truncate_file( int handle
)
392 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
394 if (((result
= lseek( file
->fd
, 0, SEEK_CUR
)) == -1) ||
395 (ftruncate( file
->fd
, result
) == -1))
398 release_object( file
);
401 release_object( file
);
406 int set_file_time( int handle
, time_t access_time
, time_t write_time
)
409 struct utimbuf utimbuf
;
411 if (!(file
= get_file_obj( current
->process
, handle
, GENERIC_WRITE
)))
413 utimbuf
.actime
= access_time
;
414 utimbuf
.modtime
= write_time
;
415 if (utime( file
->name
, &utimbuf
) == -1)
418 release_object( file
);
421 release_object( file
);