mf/evr: Create sample allocators on demand.
[wine/zf.git] / server / file.c
blob4690af2424e967a32b50d1454de2a5fbfd39cdbe
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 */
62 struct list kernel_object; /* list of kernel object pointers */
65 static unsigned int generic_file_map_access( unsigned int access );
67 static void file_dump( struct object *obj, int verbose );
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_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
72 static struct object *file_open_file( struct object *obj, unsigned int access,
73 unsigned int sharing, unsigned int options );
74 static struct list *file_get_kernel_obj_list( struct object *obj );
75 static void file_destroy( struct object *obj );
77 static int file_get_poll_events( struct fd *fd );
78 static enum server_fd_type file_get_fd_type( struct fd *fd );
80 static const struct object_ops file_ops =
82 sizeof(struct file), /* size */
83 file_dump, /* dump */
84 file_get_type, /* get_type */
85 add_queue, /* add_queue */
86 remove_queue, /* remove_queue */
87 default_fd_signaled, /* signaled */
88 no_satisfied, /* satisfied */
89 no_signal, /* signal */
90 file_get_fd, /* get_fd */
91 default_fd_map_access, /* map_access */
92 file_get_sd, /* get_sd */
93 file_set_sd, /* set_sd */
94 no_get_full_name, /* get_full_name */
95 file_lookup_name, /* lookup_name */
96 no_link_name, /* link_name */
97 NULL, /* unlink_name */
98 file_open_file, /* open_file */
99 file_get_kernel_obj_list, /* get_kernel_obj_list */
100 fd_close_handle, /* close_handle */
101 file_destroy /* destroy */
104 static const struct fd_ops file_fd_ops =
106 file_get_poll_events, /* get_poll_events */
107 default_poll_event, /* poll_event */
108 file_get_fd_type, /* get_fd_type */
109 no_fd_read, /* read */
110 no_fd_write, /* write */
111 no_fd_flush, /* flush */
112 default_fd_get_file_info, /* get_file_info */
113 no_fd_get_volume_info, /* get_volume_info */
114 default_fd_ioctl, /* ioctl */
115 default_fd_queue_async, /* queue_async */
116 default_fd_reselect_async /* reselect_async */
119 /* create a file from a file descriptor */
120 /* if the function fails the fd is closed */
121 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
123 struct file *file;
124 struct stat st;
126 if (fstat( fd, &st ) == -1)
128 file_set_error();
129 close( fd );
130 return NULL;
133 if (!(file = alloc_object( &file_ops )))
135 close( fd );
136 return NULL;
139 file->mode = st.st_mode;
140 file->access = default_fd_map_access( &file->obj, access );
141 list_init( &file->kernel_object );
142 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
143 FILE_SYNCHRONOUS_IO_NONALERT )))
145 release_object( file );
146 return NULL;
148 allow_fd_caching( file->fd );
149 return file;
152 /* create a file by duplicating an fd object */
153 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
155 struct file *file;
156 struct stat st;
158 if (fstat( get_unix_fd(fd), &st ) == -1)
160 file_set_error();
161 return NULL;
164 if ((file = alloc_object( &file_ops )))
166 file->mode = st.st_mode;
167 file->access = default_fd_map_access( &file->obj, access );
168 list_init( &file->kernel_object );
169 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
171 release_object( file );
172 return NULL;
174 set_fd_user( file->fd, &file_fd_ops, &file->obj );
176 return file;
179 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
181 struct file *file = alloc_object( &file_ops );
183 if (!file) return NULL;
184 file->access = access;
185 file->mode = mode;
186 file->uid = ~(uid_t)0;
187 file->fd = fd;
188 list_init( &file->kernel_object );
189 grab_object( fd );
190 set_fd_user( fd, &file_fd_ops, &file->obj );
191 return &file->obj;
194 int is_file_executable( const char *name )
196 int len = strlen( name );
197 return len >= 4 && (!strcasecmp( name + len - 4, ".exe") || !strcasecmp( name + len - 4, ".com" ));
200 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
201 unsigned int access, unsigned int sharing, int create,
202 unsigned int options, unsigned int attrs,
203 const struct security_descriptor *sd )
205 struct object *obj = NULL;
206 struct fd *fd;
207 int flags;
208 char *name;
209 mode_t mode;
211 if (!len || ((nameptr[0] == '/') ^ !root))
213 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
214 return NULL;
216 if (!(name = mem_alloc( len + 1 ))) return NULL;
217 memcpy( name, nameptr, len );
218 name[len] = 0;
220 switch(create)
222 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
223 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
224 access |= FILE_WRITE_ATTRIBUTES;
225 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
226 case FILE_OPEN: flags = 0; break;
227 case FILE_OPEN_IF: flags = O_CREAT; break;
228 case FILE_OVERWRITE: flags = O_TRUNC;
229 access |= FILE_WRITE_ATTRIBUTES; break;
230 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
233 if (sd)
235 const SID *owner = sd_get_owner( sd );
236 if (!owner)
237 owner = token_get_user( current->process->token );
238 mode = sd_to_mode( sd, owner );
240 else if (options & FILE_DIRECTORY_FILE)
241 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
242 else
243 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
245 if (is_file_executable( name ))
247 if (mode & S_IRUSR)
248 mode |= S_IXUSR;
249 if (mode & S_IRGRP)
250 mode |= S_IXGRP;
251 if (mode & S_IROTH)
252 mode |= S_IXOTH;
255 access = generic_file_map_access( access );
257 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
258 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
259 if (!fd) goto done;
261 if (S_ISDIR(mode))
262 obj = create_dir_obj( fd, access, mode );
263 else if (S_ISCHR(mode) && is_serial_fd( fd ))
264 obj = create_serial( fd );
265 else
266 obj = create_file_obj( fd, access, mode );
268 release_object( fd );
270 done:
271 free( name );
272 return obj;
275 static void file_dump( struct object *obj, int verbose )
277 struct file *file = (struct file *)obj;
278 assert( obj->ops == &file_ops );
279 fprintf( stderr, "File fd=%p\n", file->fd );
282 struct object_type *file_get_type( struct object *obj )
284 static const WCHAR name[] = {'F','i','l','e'};
285 static const struct unicode_str str = { name, sizeof(name) };
286 return get_object_type( &str );
289 static int file_get_poll_events( struct fd *fd )
291 struct file *file = get_fd_user( fd );
292 int events = 0;
293 assert( file->obj.ops == &file_ops );
294 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
295 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
296 return events;
299 static enum server_fd_type file_get_fd_type( struct fd *fd )
301 struct file *file = get_fd_user( fd );
303 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
304 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
305 return FD_TYPE_CHAR;
308 static struct fd *file_get_fd( struct object *obj )
310 struct file *file = (struct file *)obj;
311 assert( obj->ops == &file_ops );
312 return (struct fd *)grab_object( file->fd );
315 static unsigned int generic_file_map_access( unsigned int access )
317 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
318 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
319 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
320 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
321 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
324 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
326 struct security_descriptor *sd;
327 size_t dacl_size;
328 ACE_HEADER *current_ace;
329 ACCESS_ALLOWED_ACE *aaa;
330 ACL *dacl;
331 SID *sid;
332 char *ptr;
333 const SID *world_sid = security_world_sid;
334 const SID *local_system_sid = security_local_system_sid;
336 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
337 security_sid_len( local_system_sid );
338 if (mode & S_IRWXU)
339 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
340 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
341 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
342 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
343 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
344 if (mode & S_IRWXO)
345 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
347 sd = mem_alloc( sizeof(struct security_descriptor) +
348 security_sid_len( user ) + security_sid_len( group ) +
349 dacl_size );
350 if (!sd) return sd;
352 sd->control = SE_DACL_PRESENT;
353 sd->owner_len = security_sid_len( user );
354 sd->group_len = security_sid_len( group );
355 sd->sacl_len = 0;
356 sd->dacl_len = dacl_size;
358 ptr = (char *)(sd + 1);
359 memcpy( ptr, user, sd->owner_len );
360 ptr += sd->owner_len;
361 memcpy( ptr, group, sd->group_len );
362 ptr += sd->group_len;
364 dacl = (ACL *)ptr;
365 dacl->AclRevision = ACL_REVISION;
366 dacl->Sbz1 = 0;
367 dacl->AclSize = dacl_size;
368 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
369 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
370 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
371 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
372 dacl->AceCount++;
373 dacl->Sbz2 = 0;
375 /* always give FILE_ALL_ACCESS for Local System */
376 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
377 current_ace = &aaa->Header;
378 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
379 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
380 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
381 aaa->Mask = FILE_ALL_ACCESS;
382 sid = (SID *)&aaa->SidStart;
383 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
385 if (mode & S_IRWXU)
387 /* appropriate access rights for the user */
388 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
389 current_ace = &aaa->Header;
390 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
391 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
392 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
393 aaa->Mask = WRITE_DAC | WRITE_OWNER;
394 if (mode & S_IRUSR)
395 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
396 if (mode & S_IWUSR)
397 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
398 sid = (SID *)&aaa->SidStart;
399 memcpy( sid, user, security_sid_len( user ));
401 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
402 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
403 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
405 /* deny just in case the user is a member of the group */
406 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
407 current_ace = &ada->Header;
408 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
409 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
410 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
411 ada->Mask = 0;
412 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
413 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
414 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
415 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
416 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
417 sid = (SID *)&ada->SidStart;
418 memcpy( sid, user, security_sid_len( user ));
420 if (mode & S_IRWXO)
422 /* appropriate access rights for Everyone */
423 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
424 current_ace = &aaa->Header;
425 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
426 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
427 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
428 aaa->Mask = 0;
429 if (mode & S_IROTH)
430 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
431 if (mode & S_IWOTH)
432 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
433 sid = (SID *)&aaa->SidStart;
434 memcpy( sid, world_sid, security_sid_len( world_sid ));
437 return sd;
440 static struct security_descriptor *file_get_sd( struct object *obj )
442 struct file *file = (struct file *)obj;
443 struct stat st;
444 int unix_fd;
445 struct security_descriptor *sd;
447 assert( obj->ops == &file_ops );
449 unix_fd = get_file_unix_fd( file );
451 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
452 return obj->sd;
454 /* mode and uid the same? if so, no need to re-generate security descriptor */
455 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
456 (st.st_uid == file->uid))
457 return obj->sd;
459 sd = mode_to_sd( st.st_mode,
460 security_unix_uid_to_sid( st.st_uid ),
461 token_get_primary_group( current->process->token ));
462 if (!sd) return obj->sd;
464 file->mode = st.st_mode;
465 file->uid = st.st_uid;
466 free( obj->sd );
467 obj->sd = sd;
468 return sd;
471 static mode_t file_access_to_mode( unsigned int access )
473 mode_t mode = 0;
475 access = generic_file_map_access( access );
476 if (access & FILE_READ_DATA) mode |= 4;
477 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
478 if (access & FILE_EXECUTE) mode |= 1;
479 return mode;
482 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
484 mode_t new_mode = 0;
485 mode_t bits_to_set = ~0;
486 mode_t mode;
487 int present;
488 const ACL *dacl = sd_get_dacl( sd, &present );
489 const SID *user = token_get_user( current->process->token );
490 if (present && dacl)
492 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
493 ULONG i;
494 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
496 const ACCESS_ALLOWED_ACE *aa_ace;
497 const ACCESS_DENIED_ACE *ad_ace;
498 const SID *sid;
500 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
502 switch (ace->AceType)
504 case ACCESS_DENIED_ACE_TYPE:
505 ad_ace = (const ACCESS_DENIED_ACE *)ace;
506 sid = (const SID *)&ad_ace->SidStart;
507 mode = file_access_to_mode( ad_ace->Mask );
508 if (security_equal_sid( sid, security_world_sid ))
510 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
512 else if ((security_equal_sid( user, owner ) &&
513 token_sid_present( current->process->token, sid, TRUE )))
515 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
517 else if (security_equal_sid( sid, owner ))
519 bits_to_set &= ~(mode << 6); /* user only */
521 break;
522 case ACCESS_ALLOWED_ACE_TYPE:
523 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
524 sid = (const SID *)&aa_ace->SidStart;
525 mode = file_access_to_mode( aa_ace->Mask );
526 if (security_equal_sid( sid, security_world_sid ))
528 mode = (mode << 6) | (mode << 3) | mode; /* all */
529 new_mode |= mode & bits_to_set;
530 bits_to_set &= ~mode;
532 else if ((security_equal_sid( user, owner ) &&
533 token_sid_present( current->process->token, sid, FALSE )))
535 mode = (mode << 6) | (mode << 3); /* user + group */
536 new_mode |= mode & bits_to_set;
537 bits_to_set &= ~mode;
539 else if (security_equal_sid( sid, owner ))
541 mode = (mode << 6); /* user only */
542 new_mode |= mode & bits_to_set;
543 bits_to_set &= ~mode;
545 break;
549 else
550 /* no ACL means full access rights to anyone */
551 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
553 return new_mode;
556 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
557 unsigned int set_info )
559 struct file *file = (struct file *)obj;
560 const SID *owner;
561 struct stat st;
562 mode_t mode;
563 int unix_fd;
565 assert( obj->ops == &file_ops );
567 unix_fd = get_file_unix_fd( file );
569 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
571 if (set_info & OWNER_SECURITY_INFORMATION)
573 owner = sd_get_owner( sd );
574 if (!owner)
576 set_error( STATUS_INVALID_SECURITY_DESCR );
577 return 0;
579 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
581 /* FIXME: get Unix uid and call fchown */
584 else if (obj->sd)
585 owner = sd_get_owner( obj->sd );
586 else
587 owner = token_get_user( current->process->token );
589 /* group and sacl not supported */
591 if (set_info & DACL_SECURITY_INFORMATION)
593 /* keep the bits that we don't map to access rights in the ACL */
594 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
595 mode |= sd_to_mode( sd, owner );
597 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
599 file_set_error();
600 return 0;
603 return 1;
606 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
608 if (!name || !name->len) return NULL; /* open the file itself */
610 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
611 return NULL;
614 static struct object *file_open_file( struct object *obj, unsigned int access,
615 unsigned int sharing, unsigned int options )
617 struct file *file = (struct file *)obj;
618 struct object *new_file = NULL;
619 char *unix_name;
621 assert( obj->ops == &file_ops );
623 if ((unix_name = dup_fd_name( file->fd, "" )))
625 new_file = create_file( NULL, unix_name, strlen(unix_name), access,
626 sharing, FILE_OPEN, options, 0, NULL );
627 free( unix_name );
629 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
630 return new_file;
633 static struct list *file_get_kernel_obj_list( struct object *obj )
635 struct file *file = (struct file *)obj;
636 return &file->kernel_object;
639 static void file_destroy( struct object *obj )
641 struct file *file = (struct file *)obj;
642 assert( obj->ops == &file_ops );
644 if (file->fd) release_object( file->fd );
647 /* set the last error depending on errno */
648 void file_set_error(void)
650 switch (errno)
652 case ETXTBSY:
653 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
654 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
655 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
656 case EACCES:
657 case ESRCH:
658 case EROFS:
659 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
660 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
661 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
662 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
663 case ENFILE:
664 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
665 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
666 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
667 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
668 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
669 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
670 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
671 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
672 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
673 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
674 case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
675 case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
676 #ifdef EOVERFLOW
677 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
678 #endif
679 default:
680 perror("wineserver: file_set_error() can't map error");
681 set_error( STATUS_UNSUCCESSFUL );
682 break;
686 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
688 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
691 int get_file_unix_fd( struct file *file )
693 return get_unix_fd( file->fd );
696 /* create a file */
697 DECL_HANDLER(create_file)
699 struct object *file;
700 struct fd *root_fd = NULL;
701 struct unicode_str unicode_name;
702 const struct security_descriptor *sd;
703 const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
704 const char *name;
705 data_size_t name_len;
707 if (!objattr) return;
709 /* name is transferred in the unix codepage outside of the objattr structure */
710 if (unicode_name.len)
712 set_error( STATUS_INVALID_PARAMETER );
713 return;
716 if (objattr->rootdir)
718 struct dir *root;
720 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
721 root_fd = get_obj_fd( (struct object *)root );
722 release_object( root );
723 if (!root_fd) return;
726 name = get_req_data_after_objattr( objattr, &name_len );
728 reply->handle = 0;
729 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
730 req->create, req->options, req->attrs, sd )))
732 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
733 release_object( file );
735 if (root_fd) release_object( root_fd );
738 /* allocate a file handle for a Unix fd */
739 DECL_HANDLER(alloc_file_handle)
741 struct file *file;
742 int fd;
744 reply->handle = 0;
745 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
747 set_error( STATUS_INVALID_HANDLE );
748 return;
750 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
752 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
753 release_object( file );
757 /* lock a region of a file */
758 DECL_HANDLER(lock_file)
760 struct file *file;
762 if ((file = get_file_obj( current->process, req->handle, 0 )))
764 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
765 reply->overlapped = is_fd_overlapped( file->fd );
766 release_object( file );
770 /* unlock a region of a file */
771 DECL_HANDLER(unlock_file)
773 struct file *file;
775 if ((file = get_file_obj( current->process, req->handle, 0 )))
777 unlock_fd( file->fd, req->offset, req->count );
778 release_object( file );