wined3d: Pass a view to blit_shader.color_fill().
[wine.git] / server / file.c
blobbf883f3cf76db2020d2347cb2f0f03d9fca05242
1 /*
2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include <unistd.h>
36 #ifdef HAVE_UTIME_H
37 #include <utime.h>
38 #endif
39 #ifdef HAVE_POLL_H
40 #include <poll.h>
41 #endif
43 #include "ntstatus.h"
44 #define WIN32_NO_STATUS
45 #include "windef.h"
46 #include "winternl.h"
48 #include "file.h"
49 #include "handle.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "process.h"
53 #include "security.h"
55 struct file
57 struct object obj; /* object header */
58 struct fd *fd; /* file descriptor for this file */
59 unsigned int access; /* file access (FILE_READ_DATA etc.) */
60 mode_t mode; /* file stat.st_mode */
61 uid_t uid; /* file stat.st_uid */
64 static unsigned int generic_file_map_access( unsigned int access );
66 static void file_dump( struct object *obj, int verbose );
67 static struct object_type *file_get_type( struct object *obj );
68 static struct fd *file_get_fd( struct object *obj );
69 static struct security_descriptor *file_get_sd( struct object *obj );
70 static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
71 static struct object *file_open_file( struct object *obj, unsigned int access,
72 unsigned int sharing, unsigned int options );
73 static void file_destroy( struct object *obj );
75 static int file_get_poll_events( struct fd *fd );
76 static obj_handle_t file_flush( struct fd *fd, const async_data_t *async, int blocking );
77 static enum server_fd_type file_get_fd_type( struct fd *fd );
79 static const struct object_ops file_ops =
81 sizeof(struct file), /* size */
82 file_dump, /* dump */
83 file_get_type, /* get_type */
84 add_queue, /* add_queue */
85 remove_queue, /* remove_queue */
86 default_fd_signaled, /* signaled */
87 no_satisfied, /* satisfied */
88 no_signal, /* signal */
89 file_get_fd, /* get_fd */
90 default_fd_map_access, /* map_access */
91 file_get_sd, /* get_sd */
92 file_set_sd, /* set_sd */
93 no_lookup_name, /* lookup_name */
94 file_open_file, /* open_file */
95 fd_close_handle, /* close_handle */
96 file_destroy /* destroy */
99 static const struct fd_ops file_fd_ops =
101 file_get_poll_events, /* get_poll_events */
102 default_poll_event, /* poll_event */
103 file_get_fd_type, /* get_fd_type */
104 no_fd_read, /* read */
105 no_fd_write, /* write */
106 file_flush, /* flush */
107 default_fd_ioctl, /* ioctl */
108 default_fd_queue_async, /* queue_async */
109 default_fd_reselect_async, /* reselect_async */
110 default_fd_cancel_async /* cancel_async */
113 static inline int is_overlapped( const struct file *file )
115 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
118 /* create a file from a file descriptor */
119 /* if the function fails the fd is closed */
120 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
122 struct file *file;
123 struct stat st;
125 if (fstat( fd, &st ) == -1)
127 file_set_error();
128 close( fd );
129 return NULL;
132 if (!(file = alloc_object( &file_ops )))
134 close( fd );
135 return NULL;
138 file->mode = st.st_mode;
139 file->access = default_fd_map_access( &file->obj, access );
140 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
141 FILE_SYNCHRONOUS_IO_NONALERT )))
143 release_object( file );
144 return NULL;
146 allow_fd_caching( file->fd );
147 return file;
150 /* create a file by duplicating an fd object */
151 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
153 struct file *file;
154 struct stat st;
156 if (fstat( get_unix_fd(fd), &st ) == -1)
158 file_set_error();
159 return NULL;
162 if ((file = alloc_object( &file_ops )))
164 file->mode = st.st_mode;
165 file->access = default_fd_map_access( &file->obj, access );
166 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
168 release_object( file );
169 return NULL;
171 set_fd_user( file->fd, &file_fd_ops, &file->obj );
173 return file;
176 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
178 struct file *file = alloc_object( &file_ops );
180 if (!file) return NULL;
181 file->access = access;
182 file->mode = mode;
183 file->uid = ~(uid_t)0;
184 file->fd = fd;
185 grab_object( fd );
186 set_fd_user( fd, &file_fd_ops, &file->obj );
187 return &file->obj;
190 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
191 unsigned int access, unsigned int sharing, int create,
192 unsigned int options, unsigned int attrs,
193 const struct security_descriptor *sd )
195 struct object *obj = NULL;
196 struct fd *fd;
197 int flags;
198 char *name;
199 mode_t mode;
201 if (!len || ((nameptr[0] == '/') ^ !root))
203 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
204 return NULL;
206 if (!(name = mem_alloc( len + 1 ))) return NULL;
207 memcpy( name, nameptr, len );
208 name[len] = 0;
210 switch(create)
212 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
213 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
214 access |= FILE_WRITE_ATTRIBUTES;
215 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
216 case FILE_OPEN: flags = 0; break;
217 case FILE_OPEN_IF: flags = O_CREAT; break;
218 case FILE_OVERWRITE: flags = O_TRUNC;
219 access |= FILE_WRITE_ATTRIBUTES; break;
220 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
223 if (sd)
225 const SID *owner = sd_get_owner( sd );
226 if (!owner)
227 owner = token_get_user( current->process->token );
228 mode = sd_to_mode( sd, owner );
230 else if (options & FILE_DIRECTORY_FILE)
231 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
232 else
233 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
235 if (len >= 4 &&
236 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
238 if (mode & S_IRUSR)
239 mode |= S_IXUSR;
240 if (mode & S_IRGRP)
241 mode |= S_IXGRP;
242 if (mode & S_IROTH)
243 mode |= S_IXOTH;
246 access = generic_file_map_access( access );
248 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
249 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
250 if (!fd) goto done;
252 if (S_ISDIR(mode))
253 obj = create_dir_obj( fd, access, mode );
254 else if (S_ISCHR(mode) && is_serial_fd( fd ))
255 obj = create_serial( fd );
256 else
257 obj = create_file_obj( fd, access, mode );
259 release_object( fd );
261 done:
262 free( name );
263 return obj;
266 /* check if two file objects point to the same file */
267 int is_same_file( struct file *file1, struct file *file2 )
269 return is_same_file_fd( file1->fd, file2->fd );
272 static void file_dump( struct object *obj, int verbose )
274 struct file *file = (struct file *)obj;
275 assert( obj->ops == &file_ops );
276 fprintf( stderr, "File fd=%p\n", file->fd );
279 static struct object_type *file_get_type( struct object *obj )
281 static const WCHAR name[] = {'F','i','l','e'};
282 static const struct unicode_str str = { name, sizeof(name) };
283 return get_object_type( &str );
286 static int file_get_poll_events( struct fd *fd )
288 struct file *file = get_fd_user( fd );
289 int events = 0;
290 assert( file->obj.ops == &file_ops );
291 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
292 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
293 return events;
296 static obj_handle_t file_flush( struct fd *fd, const async_data_t *async, int blocking )
298 int unix_fd = get_unix_fd( fd );
299 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
300 return 0;
303 static enum server_fd_type file_get_fd_type( struct fd *fd )
305 struct file *file = get_fd_user( fd );
307 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
308 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
309 return FD_TYPE_CHAR;
312 static struct fd *file_get_fd( struct object *obj )
314 struct file *file = (struct file *)obj;
315 assert( obj->ops == &file_ops );
316 return (struct fd *)grab_object( file->fd );
319 static unsigned int generic_file_map_access( unsigned int access )
321 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
322 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
323 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
324 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
325 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
328 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
330 struct security_descriptor *sd;
331 size_t dacl_size;
332 ACE_HEADER *current_ace;
333 ACCESS_ALLOWED_ACE *aaa;
334 ACL *dacl;
335 SID *sid;
336 char *ptr;
337 const SID *world_sid = security_world_sid;
338 const SID *local_system_sid = security_local_system_sid;
340 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
341 security_sid_len( local_system_sid );
342 if (mode & S_IRWXU)
343 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
344 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
345 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
346 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
347 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
348 if (mode & S_IRWXO)
349 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
351 sd = mem_alloc( sizeof(struct security_descriptor) +
352 security_sid_len( user ) + security_sid_len( group ) +
353 dacl_size );
354 if (!sd) return sd;
356 sd->control = SE_DACL_PRESENT;
357 sd->owner_len = security_sid_len( user );
358 sd->group_len = security_sid_len( group );
359 sd->sacl_len = 0;
360 sd->dacl_len = dacl_size;
362 ptr = (char *)(sd + 1);
363 memcpy( ptr, user, sd->owner_len );
364 ptr += sd->owner_len;
365 memcpy( ptr, group, sd->group_len );
366 ptr += sd->group_len;
368 dacl = (ACL *)ptr;
369 dacl->AclRevision = ACL_REVISION;
370 dacl->Sbz1 = 0;
371 dacl->AclSize = dacl_size;
372 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
373 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
374 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
375 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
376 dacl->AceCount++;
377 dacl->Sbz2 = 0;
379 /* always give FILE_ALL_ACCESS for Local System */
380 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
381 current_ace = &aaa->Header;
382 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
383 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
384 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
385 aaa->Mask = FILE_ALL_ACCESS;
386 sid = (SID *)&aaa->SidStart;
387 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
389 if (mode & S_IRWXU)
391 /* appropriate access rights for the user */
392 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
393 current_ace = &aaa->Header;
394 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
395 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
396 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
397 aaa->Mask = WRITE_DAC | WRITE_OWNER;
398 if (mode & S_IRUSR)
399 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
400 if (mode & S_IWUSR)
401 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
402 sid = (SID *)&aaa->SidStart;
403 memcpy( sid, user, security_sid_len( user ));
405 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
406 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
407 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
409 /* deny just in case the user is a member of the group */
410 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
411 current_ace = &ada->Header;
412 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
413 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
414 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
415 ada->Mask = 0;
416 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
417 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
418 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
419 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
420 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
421 sid = (SID *)&ada->SidStart;
422 memcpy( sid, user, security_sid_len( user ));
424 if (mode & S_IRWXO)
426 /* appropriate access rights for Everyone */
427 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
428 current_ace = &aaa->Header;
429 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
430 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
431 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
432 aaa->Mask = 0;
433 if (mode & S_IROTH)
434 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
435 if (mode & S_IWOTH)
436 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
437 sid = (SID *)&aaa->SidStart;
438 memcpy( sid, world_sid, security_sid_len( world_sid ));
441 return sd;
444 static struct security_descriptor *file_get_sd( struct object *obj )
446 struct file *file = (struct file *)obj;
447 struct stat st;
448 int unix_fd;
449 struct security_descriptor *sd;
451 assert( obj->ops == &file_ops );
453 unix_fd = get_file_unix_fd( file );
455 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
456 return obj->sd;
458 /* mode and uid the same? if so, no need to re-generate security descriptor */
459 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
460 (st.st_uid == file->uid))
461 return obj->sd;
463 sd = mode_to_sd( st.st_mode,
464 security_unix_uid_to_sid( st.st_uid ),
465 token_get_primary_group( current->process->token ));
466 if (!sd) return obj->sd;
468 file->mode = st.st_mode;
469 file->uid = st.st_uid;
470 free( obj->sd );
471 obj->sd = sd;
472 return sd;
475 static mode_t file_access_to_mode( unsigned int access )
477 mode_t mode = 0;
479 access = generic_file_map_access( access );
480 if (access & FILE_READ_DATA) mode |= 4;
481 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
482 if (access & FILE_EXECUTE) mode |= 1;
483 return mode;
486 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
488 mode_t new_mode = 0;
489 mode_t bits_to_set = ~0;
490 mode_t mode;
491 int present;
492 const ACL *dacl = sd_get_dacl( sd, &present );
493 const SID *user = token_get_user( current->process->token );
494 if (present && dacl)
496 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
497 ULONG i;
498 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
500 const ACCESS_ALLOWED_ACE *aa_ace;
501 const ACCESS_DENIED_ACE *ad_ace;
502 const SID *sid;
504 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
506 switch (ace->AceType)
508 case ACCESS_DENIED_ACE_TYPE:
509 ad_ace = (const ACCESS_DENIED_ACE *)ace;
510 sid = (const SID *)&ad_ace->SidStart;
511 mode = file_access_to_mode( ad_ace->Mask );
512 if (security_equal_sid( sid, security_world_sid ))
514 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
516 else if ((security_equal_sid( user, owner ) &&
517 token_sid_present( current->process->token, sid, TRUE )))
519 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
521 else if (security_equal_sid( sid, owner ))
523 bits_to_set &= ~(mode << 6); /* user only */
525 break;
526 case ACCESS_ALLOWED_ACE_TYPE:
527 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
528 sid = (const SID *)&aa_ace->SidStart;
529 mode = file_access_to_mode( aa_ace->Mask );
530 if (security_equal_sid( sid, security_world_sid ))
532 mode = (mode << 6) | (mode << 3) | mode; /* all */
533 new_mode |= mode & bits_to_set;
534 bits_to_set &= ~mode;
536 else if ((security_equal_sid( user, owner ) &&
537 token_sid_present( current->process->token, sid, FALSE )))
539 mode = (mode << 6) | (mode << 3); /* user + group */
540 new_mode |= mode & bits_to_set;
541 bits_to_set &= ~mode;
543 else if (security_equal_sid( sid, owner ))
545 mode = (mode << 6); /* user only */
546 new_mode |= mode & bits_to_set;
547 bits_to_set &= ~mode;
549 break;
553 else
554 /* no ACL means full access rights to anyone */
555 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
557 return new_mode;
560 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
561 unsigned int set_info )
563 struct file *file = (struct file *)obj;
564 const SID *owner;
565 struct stat st;
566 mode_t mode;
567 int unix_fd;
569 assert( obj->ops == &file_ops );
571 unix_fd = get_file_unix_fd( file );
573 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
575 if (set_info & OWNER_SECURITY_INFORMATION)
577 owner = sd_get_owner( sd );
578 if (!owner)
580 set_error( STATUS_INVALID_SECURITY_DESCR );
581 return 0;
583 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
585 /* FIXME: get Unix uid and call fchown */
588 else if (obj->sd)
589 owner = sd_get_owner( obj->sd );
590 else
591 owner = token_get_user( current->process->token );
593 /* group and sacl not supported */
595 if (set_info & DACL_SECURITY_INFORMATION)
597 /* keep the bits that we don't map to access rights in the ACL */
598 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
599 mode |= sd_to_mode( sd, owner );
601 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
603 file_set_error();
604 return 0;
607 return 1;
610 static struct object *file_open_file( struct object *obj, unsigned int access,
611 unsigned int sharing, unsigned int options )
613 struct file *file = (struct file *)obj;
614 struct object *new_file = NULL;
615 char *unix_name;
617 assert( obj->ops == &file_ops );
619 if ((unix_name = dup_fd_name( file->fd, "" )))
621 new_file = create_file( NULL, unix_name, strlen(unix_name), access,
622 sharing, FILE_OPEN, options, 0, NULL );
623 free( unix_name );
625 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
626 return new_file;
629 static void file_destroy( struct object *obj )
631 struct file *file = (struct file *)obj;
632 assert( obj->ops == &file_ops );
634 if (file->fd) release_object( file->fd );
637 /* set the last error depending on errno */
638 void file_set_error(void)
640 switch (errno)
642 case ETXTBSY:
643 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
644 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
645 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
646 case EACCES:
647 case ESRCH:
648 case EROFS:
649 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
650 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
651 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
652 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
653 case ENFILE:
654 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
655 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
656 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
657 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
658 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
659 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
660 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
661 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
662 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
663 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
664 #ifdef EOVERFLOW
665 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
666 #endif
667 default:
668 perror("wineserver: file_set_error() can't map error");
669 set_error( STATUS_UNSUCCESSFUL );
670 break;
674 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
676 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
679 int get_file_unix_fd( struct file *file )
681 return get_unix_fd( file->fd );
684 /* create a file */
685 DECL_HANDLER(create_file)
687 struct object *file;
688 struct fd *root_fd = NULL;
689 struct unicode_str unicode_name;
690 const struct security_descriptor *sd;
691 const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
692 const char *name;
693 data_size_t name_len;
695 if (!objattr) return;
697 /* name is transferred in the unix codepage outside of the objattr structure */
698 if (unicode_name.len)
700 set_error( STATUS_INVALID_PARAMETER );
701 return;
704 if (objattr->rootdir)
706 struct dir *root;
708 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
709 root_fd = get_obj_fd( (struct object *)root );
710 release_object( root );
711 if (!root_fd) return;
714 name = get_req_data_after_objattr( objattr, &name_len );
716 reply->handle = 0;
717 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
718 req->create, req->options, req->attrs, sd )))
720 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
721 release_object( file );
723 if (root_fd) release_object( root_fd );
726 /* allocate a file handle for a Unix fd */
727 DECL_HANDLER(alloc_file_handle)
729 struct file *file;
730 int fd;
732 reply->handle = 0;
733 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
735 set_error( STATUS_INVALID_HANDLE );
736 return;
738 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
740 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
741 release_object( file );
745 /* lock a region of a file */
746 DECL_HANDLER(lock_file)
748 struct file *file;
750 if ((file = get_file_obj( current->process, req->handle, 0 )))
752 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
753 reply->overlapped = is_overlapped( file );
754 release_object( file );
758 /* unlock a region of a file */
759 DECL_HANDLER(unlock_file)
761 struct file *file;
763 if ((file = get_file_obj( current->process, req->handle, 0 )))
765 unlock_fd( file->fd, req->offset, req->count );
766 release_object( file );