IDirectDrawSurface::Blt::SRCCOPY is the default ROP operation, do not
[wine.git] / server / file.c
blob5c0f59a285e3ea56ce69a66463c70a297109bc26
1 /*
2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #ifdef HAVE_SYS_ERRNO_H
16 #include <sys/errno.h>
17 #endif
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <utime.h>
25 #include "winerror.h"
26 #include "winbase.h"
28 #include "handle.h"
29 #include "thread.h"
30 #include "request.h"
32 struct file
34 struct object obj; /* object header */
35 struct file *next; /* next file in hashing list */
36 char *name; /* file name */
37 unsigned int access; /* file access (GENERIC_READ/WRITE) */
38 unsigned int flags; /* flags (FILE_FLAG_*) */
39 unsigned int sharing; /* file sharing mode */
42 #define NAME_HASH_SIZE 37
44 static struct file *file_hash[NAME_HASH_SIZE];
46 static void file_dump( struct object *obj, int verbose );
47 static int file_get_poll_events( struct object *obj );
48 static int file_get_fd( struct object *obj );
49 static int file_flush( struct object *obj );
50 static int file_get_info( struct object *obj, struct get_file_info_request *req );
51 static void file_destroy( struct object *obj );
53 static const struct object_ops file_ops =
55 sizeof(struct file), /* size */
56 file_dump, /* dump */
57 default_poll_add_queue, /* add_queue */
58 default_poll_remove_queue, /* remove_queue */
59 default_poll_signaled, /* signaled */
60 no_satisfied, /* satisfied */
61 file_get_poll_events, /* get_poll_events */
62 default_poll_event, /* poll_event */
63 file_get_fd, /* get_fd */
64 file_flush, /* flush */
65 file_get_info, /* get_file_info */
66 file_destroy /* destroy */
70 static int get_name_hash( const char *name )
72 int hash = 0;
73 while (*name) hash ^= (unsigned char)*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 )
82 struct file *file;
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)) goto error;
93 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
94 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
95 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
96 return 1;
97 error:
98 set_error( STATUS_SHARING_VIOLATION );
99 return 0;
102 /* create a file from a file descriptor */
103 /* if the function fails the fd is closed */
104 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
105 unsigned int attrs )
107 struct file *file;
108 if ((file = alloc_object( &file_ops, fd )))
110 file->name = NULL;
111 file->next = NULL;
112 file->access = access;
113 file->flags = attrs;
114 file->sharing = sharing;
116 return file;
120 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
121 unsigned int sharing, int create, unsigned int attrs )
123 struct file *file;
124 int hash, flags;
125 struct stat st;
126 char *name;
127 int fd = -1;
129 if (!(name = mem_alloc( len + 1 ))) return NULL;
130 memcpy( name, nameptr, len );
131 name[len] = 0;
133 /* check sharing mode */
134 hash = get_name_hash( name );
135 if (!check_sharing( name, hash, access, sharing )) goto error;
137 switch(create)
139 case CREATE_NEW: flags = O_CREAT | O_EXCL; break;
140 case CREATE_ALWAYS: flags = O_CREAT | O_TRUNC; break;
141 case OPEN_ALWAYS: flags = O_CREAT; break;
142 case TRUNCATE_EXISTING: flags = O_TRUNC; break;
143 case OPEN_EXISTING: flags = 0; break;
144 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
146 switch(access & (GENERIC_READ | GENERIC_WRITE))
148 case 0: break;
149 case GENERIC_READ: flags |= O_RDONLY; break;
150 case GENERIC_WRITE: flags |= O_WRONLY; break;
151 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
154 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
155 if ((fd = open( name, flags | O_NONBLOCK,
156 (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666 )) == -1)
157 goto file_error;
158 /* refuse to open a directory */
159 if (fstat( fd, &st ) == -1) goto file_error;
160 if (S_ISDIR(st.st_mode))
162 set_error( STATUS_ACCESS_DENIED );
163 goto error;
166 if (!(file = create_file_for_fd( fd, access, sharing, attrs )))
168 free( name );
169 return NULL;
171 file->name = name;
172 file->next = file_hash[hash];
173 file_hash[hash] = file;
174 return file;
176 file_error:
177 file_set_error();
178 error:
179 if (fd != -1) close( fd );
180 free( name );
181 return NULL;
184 /* Create an anonymous Unix file */
185 int create_anonymous_file(void)
187 char *name;
188 int fd;
192 if (!(name = tmpnam(NULL)))
194 set_error( STATUS_TOO_MANY_OPENED_FILES );
195 return -1;
197 fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
198 } while ((fd == -1) && (errno == EEXIST));
199 if (fd == -1)
201 file_set_error();
202 return -1;
204 unlink( name );
205 return fd;
208 /* Create a temp file for anonymous mappings */
209 struct file *create_temp_file( int access )
211 int fd;
213 if ((fd = create_anonymous_file()) == -1) return NULL;
214 return create_file_for_fd( fd, access, 0, 0 );
217 static void file_dump( struct object *obj, int verbose )
219 struct file *file = (struct file *)obj;
220 assert( obj->ops == &file_ops );
221 fprintf( stderr, "File fd=%d flags=%08x name='%s'\n", file->obj.fd, file->flags, file->name );
224 static int file_get_poll_events( struct object *obj )
226 struct file *file = (struct file *)obj;
227 int events = 0;
228 assert( obj->ops == &file_ops );
229 if (file->access & GENERIC_READ) events |= POLLIN;
230 if (file->access & GENERIC_WRITE) events |= POLLOUT;
231 return events;
234 static int file_get_fd( struct object *obj )
236 struct file *file = (struct file *)obj;
237 assert( obj->ops == &file_ops );
238 return dup( file->obj.fd );
241 static int file_flush( struct object *obj )
243 int ret;
244 struct file *file = (struct file *)grab_object(obj);
245 assert( obj->ops == &file_ops );
247 ret = (fsync( file->obj.fd ) != -1);
248 if (!ret) file_set_error();
249 release_object( file );
250 return ret;
253 static int file_get_info( struct object *obj, struct get_file_info_request *req )
255 struct stat st;
256 struct file *file = (struct file *)obj;
257 assert( obj->ops == &file_ops );
259 if (fstat( file->obj.fd, &st ) == -1)
261 file_set_error();
262 return 0;
264 if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
265 S_ISSOCK(st.st_mode) || isatty(file->obj.fd)) req->type = FILE_TYPE_CHAR;
266 else req->type = FILE_TYPE_DISK;
267 if (S_ISDIR(st.st_mode)) req->attr = FILE_ATTRIBUTE_DIRECTORY;
268 else req->attr = FILE_ATTRIBUTE_ARCHIVE;
269 if (!(st.st_mode & S_IWUSR)) req->attr |= FILE_ATTRIBUTE_READONLY;
270 req->access_time = st.st_atime;
271 req->write_time = st.st_mtime;
272 req->size_high = 0;
273 req->size_low = S_ISDIR(st.st_mode) ? 0 : st.st_size;
274 req->links = st.st_nlink;
275 req->index_high = st.st_dev;
276 req->index_low = st.st_ino;
277 req->serial = 0; /* FIXME */
278 return 1;
281 static void file_destroy( struct object *obj )
283 struct file *file = (struct file *)obj;
284 assert( obj->ops == &file_ops );
286 if (file->name)
288 /* remove it from the hashing list */
289 struct file **pptr = &file_hash[get_name_hash( file->name )];
290 while (*pptr && *pptr != file) pptr = &(*pptr)->next;
291 assert( *pptr );
292 *pptr = (*pptr)->next;
293 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
294 free( file->name );
298 /* set the last error depending on errno */
299 void file_set_error(void)
301 switch (errno)
303 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
304 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
305 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
306 case EACCES:
307 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
308 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
309 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
310 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
311 case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
312 case ENFILE:
313 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
314 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
315 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
316 case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
317 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
318 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
319 default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
323 struct file *get_file_obj( struct process *process, int handle, unsigned int access )
325 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
328 int file_get_mmap_fd( struct file *file )
330 return dup( file->obj.fd );
333 static int set_file_pointer( int handle, int *low, int *high, int whence )
335 struct file *file;
336 int result;
338 if ((*low >= 0 && *high != 0) || (*low < 0 && *high != -1))
340 fprintf( stderr, "set_file_pointer: offset > 2Gb not supported yet\n" );
341 set_error( STATUS_INVALID_PARAMETER );
342 return 0;
345 if (!(file = get_file_obj( current->process, handle, 0 )))
346 return 0;
347 if ((result = lseek( file->obj.fd, *low, whence )) == -1)
349 /* Check for seek before start of file */
350 if ((errno == EINVAL) && (whence != SEEK_SET) && (*low < 0))
351 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
352 else
353 file_set_error();
354 release_object( file );
355 return 0;
357 *low = result;
358 release_object( file );
359 return 1;
362 static int truncate_file( int handle )
364 struct file *file;
365 int result;
367 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
368 return 0;
369 if (((result = lseek( file->obj.fd, 0, SEEK_CUR )) == -1) ||
370 (ftruncate( file->obj.fd, result ) == -1))
372 file_set_error();
373 release_object( file );
374 return 0;
376 release_object( file );
377 return 1;
381 /* try to grow the file to the specified size */
382 int grow_file( struct file *file, int size_high, int size_low )
384 struct stat st;
386 if (size_high)
388 set_error( STATUS_INVALID_PARAMETER );
389 return 0;
391 if (fstat( file->obj.fd, &st ) == -1)
393 file_set_error();
394 return 0;
396 if (st.st_size >= size_low) return 1; /* already large enough */
397 if (ftruncate( file->obj.fd, size_low ) != -1) return 1;
398 file_set_error();
399 return 0;
402 static int set_file_time( int handle, time_t access_time, time_t write_time )
404 struct file *file;
405 struct utimbuf utimbuf;
407 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
408 return 0;
409 if (!access_time || !write_time)
411 struct stat st;
412 if (stat( file->name, &st ) == -1) goto error;
413 if (!access_time) access_time = st.st_atime;
414 if (!write_time) write_time = st.st_mtime;
416 utimbuf.actime = access_time;
417 utimbuf.modtime = write_time;
418 if (utime( file->name, &utimbuf ) == -1) goto error;
419 release_object( file );
420 return 1;
421 error:
422 file_set_error();
423 release_object( file );
424 return 0;
427 static int file_lock( struct file *file, int offset_high, int offset_low,
428 int count_high, int count_low )
430 /* FIXME: implement this */
431 return 1;
434 static int file_unlock( struct file *file, int offset_high, int offset_low,
435 int count_high, int count_low )
437 /* FIXME: implement this */
438 return 1;
441 /* create a file */
442 DECL_HANDLER(create_file)
444 struct file *file;
446 req->handle = -1;
447 if ((file = create_file( get_req_data(req), get_req_data_size(req), req->access,
448 req->sharing, req->create, req->attrs )))
450 req->handle = alloc_handle( current->process, file, req->access, req->inherit );
451 release_object( file );
455 /* allocate a file handle for a Unix fd */
456 DECL_HANDLER(alloc_file_handle)
458 struct file *file;
460 req->handle = -1;
461 if (current->pass_fd != -1)
463 if ((file = create_file_for_fd( current->pass_fd, req->access,
464 FILE_SHARE_READ | FILE_SHARE_WRITE, 0 )))
466 req->handle = alloc_handle( current->process, file, req->access, 0 );
467 release_object( file );
469 current->pass_fd = -1;
471 else set_error( STATUS_INVALID_PARAMETER );
474 /* get a Unix fd to access a file */
475 DECL_HANDLER(get_handle_fd)
477 struct object *obj;
479 req->fd = -1;
480 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
482 set_reply_fd( current, obj->ops->get_fd( obj ) );
483 release_object( obj );
487 /* set a file current position */
488 DECL_HANDLER(set_file_pointer)
490 int high = req->high;
491 int low = req->low;
492 set_file_pointer( req->handle, &low, &high, req->whence );
493 req->new_low = low;
494 req->new_high = high;
497 /* truncate (or extend) a file */
498 DECL_HANDLER(truncate_file)
500 truncate_file( req->handle );
503 /* flush a file buffers */
504 DECL_HANDLER(flush_file)
506 struct object *obj;
508 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
510 obj->ops->flush( obj );
511 release_object( obj );
515 /* set a file access and modification times */
516 DECL_HANDLER(set_file_time)
518 set_file_time( req->handle, req->access_time, req->write_time );
521 /* get a file information */
522 DECL_HANDLER(get_file_info)
524 struct object *obj;
526 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
528 obj->ops->get_file_info( obj, req );
529 release_object( obj );
533 /* lock a region of a file */
534 DECL_HANDLER(lock_file)
536 struct file *file;
538 if ((file = get_file_obj( current->process, req->handle, 0 )))
540 file_lock( file, req->offset_high, req->offset_low,
541 req->count_high, req->count_low );
542 release_object( file );
546 /* unlock a region of a file */
547 DECL_HANDLER(unlock_file)
549 struct file *file;
551 if ((file = get_file_obj( current->process, req->handle, 0 )))
553 file_unlock( file, req->offset_high, req->offset_low,
554 req->count_high, req->count_low );
555 release_object( file );