ntdll: Simplify the platform-specific dispatcher interface.
[wine.git] / server / file.c
blobaff4d9e09e18c5f128c4fb7aedc1c4cb19b8f840
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 static const WCHAR file_name[] = {'F','i','l','e'};
57 struct type_descr file_type =
59 { file_name, sizeof(file_name) }, /* name */
60 FILE_ALL_ACCESS, /* valid_access */
61 { /* mapping */
62 FILE_GENERIC_READ,
63 FILE_GENERIC_WRITE,
64 FILE_GENERIC_EXECUTE,
65 FILE_ALL_ACCESS
69 struct file
71 struct object obj; /* object header */
72 struct fd *fd; /* file descriptor for this file */
73 unsigned int access; /* file access (FILE_READ_DATA etc.) */
74 mode_t mode; /* file stat.st_mode */
75 uid_t uid; /* file stat.st_uid */
76 struct list kernel_object; /* list of kernel object pointers */
79 static void file_dump( struct object *obj, int verbose );
80 static struct fd *file_get_fd( struct object *obj );
81 static struct security_descriptor *file_get_sd( struct object *obj );
82 static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
83 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
84 unsigned int attr, struct object *root );
85 static struct object *file_open_file( struct object *obj, unsigned int access,
86 unsigned int sharing, unsigned int options );
87 static struct list *file_get_kernel_obj_list( struct object *obj );
88 static void file_destroy( struct object *obj );
90 static enum server_fd_type file_get_fd_type( struct fd *fd );
92 static const struct object_ops file_ops =
94 sizeof(struct file), /* size */
95 &file_type, /* type */
96 file_dump, /* dump */
97 add_queue, /* add_queue */
98 remove_queue, /* remove_queue */
99 default_fd_signaled, /* signaled */
100 no_satisfied, /* satisfied */
101 no_signal, /* signal */
102 file_get_fd, /* get_fd */
103 default_map_access, /* map_access */
104 file_get_sd, /* get_sd */
105 file_set_sd, /* set_sd */
106 no_get_full_name, /* get_full_name */
107 file_lookup_name, /* lookup_name */
108 no_link_name, /* link_name */
109 NULL, /* unlink_name */
110 file_open_file, /* open_file */
111 file_get_kernel_obj_list, /* get_kernel_obj_list */
112 no_close_handle, /* close_handle */
113 file_destroy /* destroy */
116 static const struct fd_ops file_fd_ops =
118 default_fd_get_poll_events, /* get_poll_events */
119 default_poll_event, /* poll_event */
120 file_get_fd_type, /* get_fd_type */
121 no_fd_read, /* read */
122 no_fd_write, /* write */
123 no_fd_flush, /* flush */
124 default_fd_get_file_info, /* get_file_info */
125 no_fd_get_volume_info, /* get_volume_info */
126 default_fd_ioctl, /* ioctl */
127 default_fd_queue_async, /* queue_async */
128 default_fd_reselect_async /* reselect_async */
131 /* create a file from a file descriptor */
132 /* if the function fails the fd is closed */
133 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
135 struct file *file;
136 struct stat st;
138 if (fstat( fd, &st ) == -1)
140 file_set_error();
141 close( fd );
142 return NULL;
145 if (!(file = alloc_object( &file_ops )))
147 close( fd );
148 return NULL;
151 file->mode = st.st_mode;
152 file->access = default_map_access( &file->obj, access );
153 list_init( &file->kernel_object );
154 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
155 FILE_SYNCHRONOUS_IO_NONALERT )))
157 release_object( file );
158 return NULL;
160 allow_fd_caching( file->fd );
161 return file;
164 /* create a file by duplicating an fd object */
165 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
167 struct file *file;
168 struct stat st;
170 if (fstat( get_unix_fd(fd), &st ) == -1)
172 file_set_error();
173 return NULL;
176 if ((file = alloc_object( &file_ops )))
178 file->mode = st.st_mode;
179 file->access = default_map_access( &file->obj, access );
180 list_init( &file->kernel_object );
181 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
183 release_object( file );
184 return NULL;
186 set_fd_user( file->fd, &file_fd_ops, &file->obj );
188 return file;
191 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
193 struct file *file = alloc_object( &file_ops );
195 if (!file) return NULL;
196 file->access = access;
197 file->mode = mode;
198 file->uid = ~(uid_t)0;
199 file->fd = fd;
200 list_init( &file->kernel_object );
201 grab_object( fd );
202 set_fd_user( fd, &file_fd_ops, &file->obj );
203 return &file->obj;
206 int is_file_executable( const char *name )
208 int len = strlen( name );
209 return len >= 4 && (!strcasecmp( name + len - 4, ".exe") || !strcasecmp( name + len - 4, ".com" ));
212 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
213 struct unicode_str nt_name,
214 unsigned int access, unsigned int sharing, int create,
215 unsigned int options, unsigned int attrs,
216 const struct security_descriptor *sd )
218 struct object *obj = NULL;
219 struct fd *fd;
220 int flags;
221 char *name;
222 mode_t mode;
224 if (!len || ((nameptr[0] == '/') ^ !root) || (nt_name.len && ((nt_name.str[0] == '\\') ^ !root)))
226 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
227 return NULL;
229 if (!(name = mem_alloc( len + 1 ))) return NULL;
230 memcpy( name, nameptr, len );
231 name[len] = 0;
233 switch(create)
235 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
236 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
237 access |= FILE_WRITE_ATTRIBUTES;
238 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
239 case FILE_OPEN: flags = 0; break;
240 case FILE_OPEN_IF: flags = O_CREAT; break;
241 case FILE_OVERWRITE: flags = O_TRUNC;
242 access |= FILE_WRITE_ATTRIBUTES; break;
243 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
246 if (sd)
248 const SID *owner = sd_get_owner( sd );
249 if (!owner)
250 owner = token_get_user( current->process->token );
251 mode = sd_to_mode( sd, owner );
253 else if (options & FILE_DIRECTORY_FILE)
254 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
255 else
256 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
258 if (is_file_executable( name ))
260 if (mode & S_IRUSR)
261 mode |= S_IXUSR;
262 if (mode & S_IRGRP)
263 mode |= S_IXGRP;
264 if (mode & S_IROTH)
265 mode |= S_IXOTH;
268 access = map_access( access, &file_type.mapping );
270 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
271 fd = open_fd( root, name, nt_name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
272 if (!fd) goto done;
274 if (S_ISDIR(mode))
275 obj = create_dir_obj( fd, access, mode );
276 else if (S_ISCHR(mode) && is_serial_fd( fd ))
277 obj = create_serial( fd );
278 else
279 obj = create_file_obj( fd, access, mode );
281 release_object( fd );
283 done:
284 free( name );
285 return obj;
288 static void file_dump( struct object *obj, int verbose )
290 struct file *file = (struct file *)obj;
291 assert( obj->ops == &file_ops );
292 fprintf( stderr, "File fd=%p\n", file->fd );
295 static enum server_fd_type file_get_fd_type( struct fd *fd )
297 struct file *file = get_fd_user( fd );
299 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
300 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
301 return FD_TYPE_CHAR;
304 static struct fd *file_get_fd( struct object *obj )
306 struct file *file = (struct file *)obj;
307 assert( obj->ops == &file_ops );
308 return (struct fd *)grab_object( file->fd );
311 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
313 struct security_descriptor *sd;
314 size_t dacl_size;
315 ACE_HEADER *current_ace;
316 ACCESS_ALLOWED_ACE *aaa;
317 ACL *dacl;
318 SID *sid;
319 char *ptr;
320 const SID *world_sid = security_world_sid;
321 const SID *local_system_sid = security_local_system_sid;
323 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
324 security_sid_len( local_system_sid );
325 if (mode & S_IRWXU)
326 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
327 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
328 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
329 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
330 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
331 if (mode & S_IRWXO)
332 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
334 sd = mem_alloc( sizeof(struct security_descriptor) +
335 security_sid_len( user ) + security_sid_len( group ) +
336 dacl_size );
337 if (!sd) return sd;
339 sd->control = SE_DACL_PRESENT;
340 sd->owner_len = security_sid_len( user );
341 sd->group_len = security_sid_len( group );
342 sd->sacl_len = 0;
343 sd->dacl_len = dacl_size;
345 ptr = (char *)(sd + 1);
346 memcpy( ptr, user, sd->owner_len );
347 ptr += sd->owner_len;
348 memcpy( ptr, group, sd->group_len );
349 ptr += sd->group_len;
351 dacl = (ACL *)ptr;
352 dacl->AclRevision = ACL_REVISION;
353 dacl->Sbz1 = 0;
354 dacl->AclSize = dacl_size;
355 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
356 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
357 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
358 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
359 dacl->AceCount++;
360 dacl->Sbz2 = 0;
362 /* always give FILE_ALL_ACCESS for Local System */
363 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
364 current_ace = &aaa->Header;
365 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
366 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
367 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
368 aaa->Mask = FILE_ALL_ACCESS;
369 sid = (SID *)&aaa->SidStart;
370 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
372 if (mode & S_IRWXU)
374 /* appropriate access rights for the user */
375 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
376 current_ace = &aaa->Header;
377 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
378 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
379 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
380 aaa->Mask = WRITE_DAC | WRITE_OWNER;
381 if (mode & S_IRUSR)
382 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
383 if (mode & S_IWUSR)
384 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
385 sid = (SID *)&aaa->SidStart;
386 memcpy( sid, user, security_sid_len( user ));
388 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
389 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
390 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
392 /* deny just in case the user is a member of the group */
393 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
394 current_ace = &ada->Header;
395 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
396 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
397 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
398 ada->Mask = 0;
399 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
400 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
401 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
402 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
403 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
404 sid = (SID *)&ada->SidStart;
405 memcpy( sid, user, security_sid_len( user ));
407 if (mode & S_IRWXO)
409 /* appropriate access rights for Everyone */
410 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
411 current_ace = &aaa->Header;
412 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
413 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
414 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
415 aaa->Mask = 0;
416 if (mode & S_IROTH)
417 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
418 if (mode & S_IWOTH)
419 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
420 sid = (SID *)&aaa->SidStart;
421 memcpy( sid, world_sid, security_sid_len( world_sid ));
424 return sd;
427 static struct security_descriptor *file_get_sd( struct object *obj )
429 struct file *file = (struct file *)obj;
430 struct stat st;
431 int unix_fd;
432 struct security_descriptor *sd;
434 assert( obj->ops == &file_ops );
436 unix_fd = get_file_unix_fd( file );
438 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
439 return obj->sd;
441 /* mode and uid the same? if so, no need to re-generate security descriptor */
442 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
443 (st.st_uid == file->uid))
444 return obj->sd;
446 sd = mode_to_sd( st.st_mode,
447 security_unix_uid_to_sid( st.st_uid ),
448 token_get_primary_group( current->process->token ));
449 if (!sd) return obj->sd;
451 file->mode = st.st_mode;
452 file->uid = st.st_uid;
453 free( obj->sd );
454 obj->sd = sd;
455 return sd;
458 static mode_t file_access_to_mode( unsigned int access )
460 mode_t mode = 0;
462 access = map_access( access, &file_type.mapping );
463 if (access & FILE_READ_DATA) mode |= 4;
464 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
465 if (access & FILE_EXECUTE) mode |= 1;
466 return mode;
469 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
471 mode_t new_mode = 0;
472 mode_t bits_to_set = ~0;
473 mode_t mode;
474 int present;
475 const ACL *dacl = sd_get_dacl( sd, &present );
476 if (present && dacl)
478 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
479 ULONG i;
480 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
482 const ACCESS_ALLOWED_ACE *aa_ace;
483 const ACCESS_DENIED_ACE *ad_ace;
484 const SID *sid;
486 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
488 switch (ace->AceType)
490 case ACCESS_DENIED_ACE_TYPE:
491 ad_ace = (const ACCESS_DENIED_ACE *)ace;
492 sid = (const SID *)&ad_ace->SidStart;
493 mode = file_access_to_mode( ad_ace->Mask );
494 if (security_equal_sid( sid, security_world_sid ))
496 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
498 else if (token_sid_present( current->process->token, owner, TRUE ) &&
499 token_sid_present( current->process->token, sid, TRUE ))
501 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
503 else if (security_equal_sid( sid, owner ))
505 bits_to_set &= ~(mode << 6); /* user only */
507 break;
508 case ACCESS_ALLOWED_ACE_TYPE:
509 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
510 sid = (const SID *)&aa_ace->SidStart;
511 mode = file_access_to_mode( aa_ace->Mask );
512 if (security_equal_sid( sid, security_world_sid ))
514 mode = (mode << 6) | (mode << 3) | mode; /* all */
515 new_mode |= mode & bits_to_set;
516 bits_to_set &= ~mode;
518 else if (token_sid_present( current->process->token, owner, FALSE ) &&
519 token_sid_present( current->process->token, sid, FALSE ))
521 mode = (mode << 6) | (mode << 3); /* user + group */
522 new_mode |= mode & bits_to_set;
523 bits_to_set &= ~mode;
525 else if (security_equal_sid( sid, owner ))
527 mode = (mode << 6); /* user only */
528 new_mode |= mode & bits_to_set;
529 bits_to_set &= ~mode;
531 break;
535 else
536 /* no ACL means full access rights to anyone */
537 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
539 return new_mode;
542 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
543 unsigned int set_info )
545 struct file *file = (struct file *)obj;
546 const SID *owner;
547 struct stat st;
548 mode_t mode;
549 int unix_fd;
551 assert( obj->ops == &file_ops );
553 unix_fd = get_file_unix_fd( file );
555 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
557 if (set_info & OWNER_SECURITY_INFORMATION)
559 owner = sd_get_owner( sd );
560 if (!owner)
562 set_error( STATUS_INVALID_SECURITY_DESCR );
563 return 0;
565 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
567 /* FIXME: get Unix uid and call fchown */
570 else if (obj->sd)
571 owner = sd_get_owner( obj->sd );
572 else
573 owner = token_get_user( current->process->token );
575 /* group and sacl not supported */
577 if (set_info & DACL_SECURITY_INFORMATION)
579 /* keep the bits that we don't map to access rights in the ACL */
580 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
581 mode |= sd_to_mode( sd, owner );
583 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
585 file_set_error();
586 return 0;
589 return 1;
592 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
593 unsigned int attr, struct object *root )
595 if (!name || !name->len) return NULL; /* open the file itself */
597 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
598 return NULL;
601 static struct object *file_open_file( struct object *obj, unsigned int access,
602 unsigned int sharing, unsigned int options )
604 struct file *file = (struct file *)obj;
605 struct object *new_file = NULL;
606 struct unicode_str nt_name;
607 char *unix_name;
609 assert( obj->ops == &file_ops );
611 if ((unix_name = dup_fd_name( file->fd, "" )))
613 get_nt_name( file->fd, &nt_name );
614 new_file = create_file( NULL, unix_name, strlen(unix_name), nt_name, access,
615 sharing, FILE_OPEN, options, 0, NULL );
616 free( unix_name );
618 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
619 return new_file;
622 static struct list *file_get_kernel_obj_list( struct object *obj )
624 struct file *file = (struct file *)obj;
625 return &file->kernel_object;
628 static void file_destroy( struct object *obj )
630 struct file *file = (struct file *)obj;
631 assert( obj->ops == &file_ops );
633 if (file->fd) release_object( file->fd );
636 /* set the last error depending on errno */
637 void file_set_error(void)
639 switch (errno)
641 case ETXTBSY:
642 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
643 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
644 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
645 case EACCES:
646 case ESRCH:
647 case EROFS:
648 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
649 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
650 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
651 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
652 case ENFILE:
653 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
654 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
655 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
656 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
657 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
658 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
659 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
660 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
661 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
662 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
663 case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
664 case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
665 #ifdef EOVERFLOW
666 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
667 #endif
668 default:
669 perror("wineserver: file_set_error() can't map error");
670 set_error( STATUS_UNSUCCESSFUL );
671 break;
675 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
677 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
680 int get_file_unix_fd( struct file *file )
682 return get_unix_fd( file->fd );
685 /* create a file */
686 DECL_HANDLER(create_file)
688 struct object *file;
689 struct fd *root_fd = NULL;
690 struct unicode_str nt_name;
691 const struct security_descriptor *sd;
692 const struct object_attributes *objattr = get_req_object_attributes( &sd, &nt_name, NULL );
693 const char *name;
694 data_size_t name_len;
696 if (!objattr) return;
698 if (objattr->rootdir)
700 struct dir *root;
702 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
703 root_fd = get_obj_fd( (struct object *)root );
704 release_object( root );
705 if (!root_fd) return;
708 name = get_req_data_after_objattr( objattr, &name_len );
710 reply->handle = 0;
711 if ((file = create_file( root_fd, name, name_len, nt_name, req->access, req->sharing,
712 req->create, req->options, req->attrs, sd )))
714 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
715 release_object( file );
717 if (root_fd) release_object( root_fd );
720 /* allocate a file handle for a Unix fd */
721 DECL_HANDLER(alloc_file_handle)
723 struct file *file;
724 int fd;
726 reply->handle = 0;
727 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
729 set_error( STATUS_INVALID_HANDLE );
730 return;
732 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
734 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
735 release_object( file );
739 /* lock a region of a file */
740 DECL_HANDLER(lock_file)
742 struct file *file;
744 if ((file = get_file_obj( current->process, req->handle, 0 )))
746 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
747 reply->overlapped = is_fd_overlapped( file->fd );
748 release_object( file );
752 /* unlock a region of a file */
753 DECL_HANDLER(unlock_file)
755 struct file *file;
757 if ((file = get_file_obj( current->process, req->handle, 0 )))
759 unlock_fd( file->fd, req->offset, req->count );
760 release_object( file );