Use the passed hdc in CreateDIBitmap instead of creating a new one.
[wine/multimedia.git] / server / file.c
blob8a966303dc87515f28a50d873d3630ed2e91cffa
1 /*
2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
8 #include "wine/port.h"
10 #include <assert.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #ifdef HAVE_SYS_ERRNO_H
17 #include <sys/errno.h>
18 #endif
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <utime.h>
26 #include "winerror.h"
27 #include "winbase.h"
29 #include "handle.h"
30 #include "thread.h"
31 #include "request.h"
33 struct file
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 */
43 #define NAME_HASH_SIZE 37
45 static struct file *file_hash[NAME_HASH_SIZE];
47 static void file_dump( struct object *obj, int verbose );
48 static int file_get_poll_events( struct object *obj );
49 static int file_get_fd( struct object *obj );
50 static int file_flush( struct object *obj );
51 static int file_get_info( struct object *obj, struct get_file_info_request *req );
52 static void file_destroy( struct object *obj );
54 static const struct object_ops file_ops =
56 sizeof(struct file), /* size */
57 file_dump, /* dump */
58 default_poll_add_queue, /* add_queue */
59 default_poll_remove_queue, /* remove_queue */
60 default_poll_signaled, /* signaled */
61 no_satisfied, /* satisfied */
62 file_get_poll_events, /* get_poll_events */
63 default_poll_event, /* poll_event */
64 file_get_fd, /* get_fd */
65 file_flush, /* flush */
66 file_get_info, /* get_file_info */
67 file_destroy /* destroy */
71 static int get_name_hash( const char *name )
73 int hash = 0;
74 while (*name) hash ^= (unsigned char)*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 )
83 struct file *file;
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)) goto error;
94 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
95 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
96 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
97 return 1;
98 error:
99 set_error( STATUS_SHARING_VIOLATION );
100 return 0;
103 /* create a file from a file descriptor */
104 /* if the function fails the fd is closed */
105 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
106 unsigned int attrs )
108 struct file *file;
109 if ((file = alloc_object( &file_ops, fd )))
111 file->name = NULL;
112 file->next = NULL;
113 file->access = access;
114 file->flags = attrs;
115 file->sharing = sharing;
117 return file;
121 static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
122 unsigned int sharing, int create, unsigned int attrs )
124 struct file *file;
125 int hash, flags;
126 struct stat st;
127 char *name;
128 int fd = -1;
129 mode_t mode;
131 if (!(name = mem_alloc( len + 1 ))) return NULL;
132 memcpy( name, nameptr, len );
133 name[len] = 0;
135 /* check sharing mode */
136 hash = get_name_hash( name );
137 if (!check_sharing( name, hash, access, sharing )) goto error;
139 switch(create)
141 case CREATE_NEW: flags = O_CREAT | O_EXCL; break;
142 case CREATE_ALWAYS: flags = O_CREAT | O_TRUNC; break;
143 case OPEN_ALWAYS: flags = O_CREAT; break;
144 case TRUNCATE_EXISTING: flags = O_TRUNC; break;
145 case OPEN_EXISTING: flags = 0; break;
146 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
148 switch(access & (GENERIC_READ | GENERIC_WRITE))
150 case 0: break;
151 case GENERIC_READ: flags |= O_RDONLY; break;
152 case GENERIC_WRITE: flags |= O_WRONLY; break;
153 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
155 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
157 if (len >= 4 &&
158 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
159 mode |= 0111;
161 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
162 if ((fd = open( name, flags | O_NONBLOCK | O_LARGEFILE, mode )) == -1 )
163 goto file_error;
164 /* refuse to open a directory */
165 if (fstat( fd, &st ) == -1) goto file_error;
166 if (S_ISDIR(st.st_mode))
168 set_error( STATUS_ACCESS_DENIED );
169 goto error;
172 if (!(file = create_file_for_fd( fd, access, sharing, attrs )))
174 free( name );
175 return NULL;
177 file->name = name;
178 file->next = file_hash[hash];
179 file_hash[hash] = file;
180 return file;
182 file_error:
183 file_set_error();
184 error:
185 if (fd != -1) close( fd );
186 free( name );
187 return NULL;
190 /* check if two file objects point to the same file */
191 int is_same_file( struct file *file1, struct file *file2 )
193 return !strcmp( file1->name, file2->name );
196 /* Create an anonymous Unix file */
197 int create_anonymous_file(void)
199 char *name;
200 int fd;
204 if (!(name = tmpnam(NULL)))
206 set_error( STATUS_TOO_MANY_OPENED_FILES );
207 return -1;
209 fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
210 } while ((fd == -1) && (errno == EEXIST));
211 if (fd == -1)
213 file_set_error();
214 return -1;
216 unlink( name );
217 return fd;
220 /* Create a temp file for anonymous mappings */
221 struct file *create_temp_file( int access )
223 int fd;
225 if ((fd = create_anonymous_file()) == -1) return NULL;
226 return create_file_for_fd( fd, access, 0, 0 );
229 static void file_dump( struct object *obj, int verbose )
231 struct file *file = (struct file *)obj;
232 assert( obj->ops == &file_ops );
233 fprintf( stderr, "File fd=%d flags=%08x name='%s'\n", file->obj.fd, file->flags, file->name );
236 static int file_get_poll_events( struct object *obj )
238 struct file *file = (struct file *)obj;
239 int events = 0;
240 assert( obj->ops == &file_ops );
241 if (file->access & GENERIC_READ) events |= POLLIN;
242 if (file->access & GENERIC_WRITE) events |= POLLOUT;
243 return events;
246 static int file_get_fd( struct object *obj )
248 struct file *file = (struct file *)obj;
249 assert( obj->ops == &file_ops );
250 return file->obj.fd;
253 static int file_flush( struct object *obj )
255 int ret;
256 struct file *file = (struct file *)grab_object(obj);
257 assert( obj->ops == &file_ops );
259 ret = (fsync( file->obj.fd ) != -1);
260 if (!ret) file_set_error();
261 release_object( file );
262 return ret;
265 static int file_get_info( struct object *obj, struct get_file_info_request *req )
267 struct stat st;
268 struct file *file = (struct file *)obj;
269 assert( obj->ops == &file_ops );
271 if (fstat( file->obj.fd, &st ) == -1)
273 file_set_error();
274 return 0;
276 if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
277 S_ISSOCK(st.st_mode) || isatty(file->obj.fd)) req->type = FILE_TYPE_CHAR;
278 else req->type = FILE_TYPE_DISK;
279 if (S_ISDIR(st.st_mode)) req->attr = FILE_ATTRIBUTE_DIRECTORY;
280 else req->attr = FILE_ATTRIBUTE_ARCHIVE;
281 if (!(st.st_mode & S_IWUSR)) req->attr |= FILE_ATTRIBUTE_READONLY;
282 req->access_time = st.st_atime;
283 req->write_time = st.st_mtime;
284 if (S_ISDIR(st.st_mode))
286 req->size_high = 0;
287 req->size_low = 0;
289 else
291 req->size_high = st.st_size >> 32;
292 req->size_low = st.st_size & 0xffffffff;
294 req->links = st.st_nlink;
295 req->index_high = st.st_dev;
296 req->index_low = st.st_ino;
297 req->serial = 0; /* FIXME */
298 return 1;
301 static void file_destroy( struct object *obj )
303 struct file *file = (struct file *)obj;
304 assert( obj->ops == &file_ops );
306 if (file->name)
308 /* remove it from the hashing list */
309 struct file **pptr = &file_hash[get_name_hash( file->name )];
310 while (*pptr && *pptr != file) pptr = &(*pptr)->next;
311 assert( *pptr );
312 *pptr = (*pptr)->next;
313 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
314 free( file->name );
318 /* set the last error depending on errno */
319 void file_set_error(void)
321 switch (errno)
323 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
324 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
325 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
326 case EACCES:
327 case ESRCH:
328 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
329 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
330 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
331 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
332 case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
333 case ENFILE:
334 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
335 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
336 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
337 case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
338 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
339 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
340 default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
344 struct file *get_file_obj( struct process *process, handle_t handle, unsigned int access )
346 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
349 static int set_file_pointer( handle_t handle, unsigned int *low, int *high, int whence )
351 struct file *file;
352 off_t result,xto;
354 xto = *low+((off_t)*high<<32);
355 if (!(file = get_file_obj( current->process, handle, 0 )))
356 return 0;
357 if ((result = lseek(file->obj.fd,xto,whence))==-1)
359 /* Check for seek before start of file */
361 /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
362 if (((errno == EINVAL) || (errno == EPERM))
363 && (whence != SEEK_SET) && (*high < 0))
364 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
365 else
366 file_set_error();
367 release_object( file );
368 return 0;
370 *low = result & 0xffffffff;
371 *high = result >> 32;
372 release_object( file );
373 return 1;
376 static int truncate_file( handle_t handle )
378 struct file *file;
379 off_t result;
381 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
382 return 0;
383 if (((result = lseek( file->obj.fd, 0, SEEK_CUR )) == -1) ||
384 (ftruncate( file->obj.fd, result ) == -1))
386 file_set_error();
387 release_object( file );
388 return 0;
390 release_object( file );
391 return 1;
394 /* try to grow the file to the specified size */
395 int grow_file( struct file *file, int size_high, int size_low )
397 struct stat st;
398 off_t size = size_low + (((off_t)size_high)<<32);
400 if (fstat( file->obj.fd, &st ) == -1)
402 file_set_error();
403 return 0;
405 if (st.st_size >= size) return 1; /* already large enough */
406 if (ftruncate( file->obj.fd, size ) != -1) return 1;
407 file_set_error();
408 return 0;
411 static int set_file_time( handle_t handle, time_t access_time, time_t write_time )
413 struct file *file;
414 struct utimbuf utimbuf;
416 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
417 return 0;
418 if (!file->name)
420 set_error( STATUS_INVALID_HANDLE );
421 release_object( file );
422 return 0;
424 if (!access_time || !write_time)
426 struct stat st;
427 if (stat( file->name, &st ) == -1) goto error;
428 if (!access_time) access_time = st.st_atime;
429 if (!write_time) write_time = st.st_mtime;
431 utimbuf.actime = access_time;
432 utimbuf.modtime = write_time;
433 if (utime( file->name, &utimbuf ) == -1) goto error;
434 release_object( file );
435 return 1;
436 error:
437 file_set_error();
438 release_object( file );
439 return 0;
442 static int file_lock( struct file *file, int offset_high, int offset_low,
443 int count_high, int count_low )
445 /* FIXME: implement this */
446 return 1;
449 static int file_unlock( struct file *file, int offset_high, int offset_low,
450 int count_high, int count_low )
452 /* FIXME: implement this */
453 return 1;
456 /* create a file */
457 DECL_HANDLER(create_file)
459 struct file *file;
461 req->handle = 0;
462 if ((file = create_file( get_req_data(req), get_req_data_size(req), req->access,
463 req->sharing, req->create, req->attrs )))
465 req->handle = alloc_handle( current->process, file, req->access, req->inherit );
466 release_object( file );
470 /* allocate a file handle for a Unix fd */
471 DECL_HANDLER(alloc_file_handle)
473 struct file *file;
474 int fd;
476 req->handle = 0;
477 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
479 set_error( STATUS_INVALID_HANDLE );
480 return;
482 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE, 0 )))
484 req->handle = alloc_handle( current->process, file, req->access, 0 );
485 release_object( file );
489 /* get a Unix fd to access a file */
490 DECL_HANDLER(get_handle_fd)
492 struct object *obj;
494 req->fd = -1;
495 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
497 int fd = get_handle_fd( current->process, req->handle, req->access );
498 if (fd != -1) req->fd = fd;
499 else if (!get_error())
501 if ((fd = obj->ops->get_fd( obj )) != -1)
502 send_client_fd( current->process, fd, req->handle );
504 release_object( obj );
508 /* set a file current position */
509 DECL_HANDLER(set_file_pointer)
511 int high = req->high;
512 int low = req->low;
513 set_file_pointer( req->handle, &low, &high, req->whence );
514 req->new_low = low;
515 req->new_high = high;
518 /* truncate (or extend) a file */
519 DECL_HANDLER(truncate_file)
521 truncate_file( req->handle );
524 /* flush a file buffers */
525 DECL_HANDLER(flush_file)
527 struct object *obj;
529 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
531 obj->ops->flush( obj );
532 release_object( obj );
536 /* set a file access and modification times */
537 DECL_HANDLER(set_file_time)
539 set_file_time( req->handle, req->access_time, req->write_time );
542 /* get a file information */
543 DECL_HANDLER(get_file_info)
545 struct object *obj;
547 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
549 obj->ops->get_file_info( obj, req );
550 release_object( obj );
554 /* lock a region of a file */
555 DECL_HANDLER(lock_file)
557 struct file *file;
559 if ((file = get_file_obj( current->process, req->handle, 0 )))
561 file_lock( file, req->offset_high, req->offset_low,
562 req->count_high, req->count_low );
563 release_object( file );
567 /* unlock a region of a file */
568 DECL_HANDLER(unlock_file)
570 struct file *file;
572 if ((file = get_file_obj( current->process, req->handle, 0 )))
574 file_unlock( file, req->offset_high, req->offset_low,
575 req->count_high, req->count_low );
576 release_object( file );