ws2_32/tests: Allow ESP and AH protocols to be missing on Windows.
[wine.git] / server / file.c
blob38260cfd2b3391199b5d89d746a1d1a8e16b834b
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 file_lookup_name, /* lookup_name */
95 no_link_name, /* link_name */
96 NULL, /* unlink_name */
97 file_open_file, /* open_file */
98 file_get_kernel_obj_list, /* get_kernel_obj_list */
99 fd_close_handle, /* close_handle */
100 file_destroy /* destroy */
103 static const struct fd_ops file_fd_ops =
105 file_get_poll_events, /* get_poll_events */
106 default_poll_event, /* poll_event */
107 file_get_fd_type, /* get_fd_type */
108 no_fd_read, /* read */
109 no_fd_write, /* write */
110 no_fd_flush, /* flush */
111 default_fd_get_file_info, /* get_file_info */
112 no_fd_get_volume_info, /* get_volume_info */
113 default_fd_ioctl, /* ioctl */
114 default_fd_queue_async, /* queue_async */
115 default_fd_reselect_async /* reselect_async */
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 list_init( &file->kernel_object );
141 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
142 FILE_SYNCHRONOUS_IO_NONALERT )))
144 release_object( file );
145 return NULL;
147 allow_fd_caching( file->fd );
148 return file;
151 /* create a file by duplicating an fd object */
152 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
154 struct file *file;
155 struct stat st;
157 if (fstat( get_unix_fd(fd), &st ) == -1)
159 file_set_error();
160 return NULL;
163 if ((file = alloc_object( &file_ops )))
165 file->mode = st.st_mode;
166 file->access = default_fd_map_access( &file->obj, access );
167 list_init( &file->kernel_object );
168 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
170 release_object( file );
171 return NULL;
173 set_fd_user( file->fd, &file_fd_ops, &file->obj );
175 return file;
178 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
180 struct file *file = alloc_object( &file_ops );
182 if (!file) return NULL;
183 file->access = access;
184 file->mode = mode;
185 file->uid = ~(uid_t)0;
186 file->fd = fd;
187 list_init( &file->kernel_object );
188 grab_object( fd );
189 set_fd_user( fd, &file_fd_ops, &file->obj );
190 return &file->obj;
193 int is_file_executable( const char *name )
195 int len = strlen( name );
196 return len >= 4 && (!strcasecmp( name + len - 4, ".exe") || !strcasecmp( name + len - 4, ".com" ));
199 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
200 unsigned int access, unsigned int sharing, int create,
201 unsigned int options, unsigned int attrs,
202 const struct security_descriptor *sd )
204 struct object *obj = NULL;
205 struct fd *fd;
206 int flags;
207 char *name;
208 mode_t mode;
210 if (!len || ((nameptr[0] == '/') ^ !root))
212 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
213 return NULL;
215 if (!(name = mem_alloc( len + 1 ))) return NULL;
216 memcpy( name, nameptr, len );
217 name[len] = 0;
219 switch(create)
221 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
222 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
223 access |= FILE_WRITE_ATTRIBUTES;
224 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
225 case FILE_OPEN: flags = 0; break;
226 case FILE_OPEN_IF: flags = O_CREAT; break;
227 case FILE_OVERWRITE: flags = O_TRUNC;
228 access |= FILE_WRITE_ATTRIBUTES; break;
229 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
232 if (sd)
234 const SID *owner = sd_get_owner( sd );
235 if (!owner)
236 owner = token_get_user( current->process->token );
237 mode = sd_to_mode( sd, owner );
239 else if (options & FILE_DIRECTORY_FILE)
240 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
241 else
242 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
244 if (is_file_executable( name ))
246 if (mode & S_IRUSR)
247 mode |= S_IXUSR;
248 if (mode & S_IRGRP)
249 mode |= S_IXGRP;
250 if (mode & S_IROTH)
251 mode |= S_IXOTH;
254 access = generic_file_map_access( access );
256 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
257 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
258 if (!fd) goto done;
260 if (S_ISDIR(mode))
261 obj = create_dir_obj( fd, access, mode );
262 else if (S_ISCHR(mode) && is_serial_fd( fd ))
263 obj = create_serial( fd );
264 else
265 obj = create_file_obj( fd, access, mode );
267 release_object( fd );
269 done:
270 free( name );
271 return obj;
274 static void file_dump( struct object *obj, int verbose )
276 struct file *file = (struct file *)obj;
277 assert( obj->ops == &file_ops );
278 fprintf( stderr, "File fd=%p\n", file->fd );
281 struct object_type *file_get_type( struct object *obj )
283 static const WCHAR name[] = {'F','i','l','e'};
284 static const struct unicode_str str = { name, sizeof(name) };
285 return get_object_type( &str );
288 static int file_get_poll_events( struct fd *fd )
290 struct file *file = get_fd_user( fd );
291 int events = 0;
292 assert( file->obj.ops == &file_ops );
293 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
294 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
295 return events;
298 static enum server_fd_type file_get_fd_type( struct fd *fd )
300 struct file *file = get_fd_user( fd );
302 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
303 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
304 return FD_TYPE_CHAR;
307 static struct fd *file_get_fd( struct object *obj )
309 struct file *file = (struct file *)obj;
310 assert( obj->ops == &file_ops );
311 return (struct fd *)grab_object( file->fd );
314 static unsigned int generic_file_map_access( unsigned int access )
316 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
317 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
318 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
319 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
320 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
323 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
325 struct security_descriptor *sd;
326 size_t dacl_size;
327 ACE_HEADER *current_ace;
328 ACCESS_ALLOWED_ACE *aaa;
329 ACL *dacl;
330 SID *sid;
331 char *ptr;
332 const SID *world_sid = security_world_sid;
333 const SID *local_system_sid = security_local_system_sid;
335 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
336 security_sid_len( local_system_sid );
337 if (mode & S_IRWXU)
338 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
339 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
340 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
341 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
342 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
343 if (mode & S_IRWXO)
344 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
346 sd = mem_alloc( sizeof(struct security_descriptor) +
347 security_sid_len( user ) + security_sid_len( group ) +
348 dacl_size );
349 if (!sd) return sd;
351 sd->control = SE_DACL_PRESENT;
352 sd->owner_len = security_sid_len( user );
353 sd->group_len = security_sid_len( group );
354 sd->sacl_len = 0;
355 sd->dacl_len = dacl_size;
357 ptr = (char *)(sd + 1);
358 memcpy( ptr, user, sd->owner_len );
359 ptr += sd->owner_len;
360 memcpy( ptr, group, sd->group_len );
361 ptr += sd->group_len;
363 dacl = (ACL *)ptr;
364 dacl->AclRevision = ACL_REVISION;
365 dacl->Sbz1 = 0;
366 dacl->AclSize = dacl_size;
367 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
368 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
369 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
370 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
371 dacl->AceCount++;
372 dacl->Sbz2 = 0;
374 /* always give FILE_ALL_ACCESS for Local System */
375 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
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( local_system_sid );
380 aaa->Mask = FILE_ALL_ACCESS;
381 sid = (SID *)&aaa->SidStart;
382 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
384 if (mode & S_IRWXU)
386 /* appropriate access rights for the user */
387 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
388 current_ace = &aaa->Header;
389 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
390 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
391 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
392 aaa->Mask = WRITE_DAC | WRITE_OWNER;
393 if (mode & S_IRUSR)
394 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
395 if (mode & S_IWUSR)
396 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
397 sid = (SID *)&aaa->SidStart;
398 memcpy( sid, user, security_sid_len( user ));
400 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
401 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
402 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
404 /* deny just in case the user is a member of the group */
405 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
406 current_ace = &ada->Header;
407 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
408 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
409 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
410 ada->Mask = 0;
411 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
412 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
413 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
414 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
415 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
416 sid = (SID *)&ada->SidStart;
417 memcpy( sid, user, security_sid_len( user ));
419 if (mode & S_IRWXO)
421 /* appropriate access rights for Everyone */
422 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
423 current_ace = &aaa->Header;
424 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
425 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
426 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
427 aaa->Mask = 0;
428 if (mode & S_IROTH)
429 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
430 if (mode & S_IWOTH)
431 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
432 sid = (SID *)&aaa->SidStart;
433 memcpy( sid, world_sid, security_sid_len( world_sid ));
436 return sd;
439 static struct security_descriptor *file_get_sd( struct object *obj )
441 struct file *file = (struct file *)obj;
442 struct stat st;
443 int unix_fd;
444 struct security_descriptor *sd;
446 assert( obj->ops == &file_ops );
448 unix_fd = get_file_unix_fd( file );
450 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
451 return obj->sd;
453 /* mode and uid the same? if so, no need to re-generate security descriptor */
454 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
455 (st.st_uid == file->uid))
456 return obj->sd;
458 sd = mode_to_sd( st.st_mode,
459 security_unix_uid_to_sid( st.st_uid ),
460 token_get_primary_group( current->process->token ));
461 if (!sd) return obj->sd;
463 file->mode = st.st_mode;
464 file->uid = st.st_uid;
465 free( obj->sd );
466 obj->sd = sd;
467 return sd;
470 static mode_t file_access_to_mode( unsigned int access )
472 mode_t mode = 0;
474 access = generic_file_map_access( access );
475 if (access & FILE_READ_DATA) mode |= 4;
476 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
477 if (access & FILE_EXECUTE) mode |= 1;
478 return mode;
481 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
483 mode_t new_mode = 0;
484 mode_t bits_to_set = ~0;
485 mode_t mode;
486 int present;
487 const ACL *dacl = sd_get_dacl( sd, &present );
488 const SID *user = token_get_user( current->process->token );
489 if (present && dacl)
491 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
492 ULONG i;
493 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
495 const ACCESS_ALLOWED_ACE *aa_ace;
496 const ACCESS_DENIED_ACE *ad_ace;
497 const SID *sid;
499 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
501 switch (ace->AceType)
503 case ACCESS_DENIED_ACE_TYPE:
504 ad_ace = (const ACCESS_DENIED_ACE *)ace;
505 sid = (const SID *)&ad_ace->SidStart;
506 mode = file_access_to_mode( ad_ace->Mask );
507 if (security_equal_sid( sid, security_world_sid ))
509 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
511 else if ((security_equal_sid( user, owner ) &&
512 token_sid_present( current->process->token, sid, TRUE )))
514 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
516 else if (security_equal_sid( sid, owner ))
518 bits_to_set &= ~(mode << 6); /* user only */
520 break;
521 case ACCESS_ALLOWED_ACE_TYPE:
522 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
523 sid = (const SID *)&aa_ace->SidStart;
524 mode = file_access_to_mode( aa_ace->Mask );
525 if (security_equal_sid( sid, security_world_sid ))
527 mode = (mode << 6) | (mode << 3) | mode; /* all */
528 new_mode |= mode & bits_to_set;
529 bits_to_set &= ~mode;
531 else if ((security_equal_sid( user, owner ) &&
532 token_sid_present( current->process->token, sid, FALSE )))
534 mode = (mode << 6) | (mode << 3); /* user + group */
535 new_mode |= mode & bits_to_set;
536 bits_to_set &= ~mode;
538 else if (security_equal_sid( sid, owner ))
540 mode = (mode << 6); /* user only */
541 new_mode |= mode & bits_to_set;
542 bits_to_set &= ~mode;
544 break;
548 else
549 /* no ACL means full access rights to anyone */
550 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
552 return new_mode;
555 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
556 unsigned int set_info )
558 struct file *file = (struct file *)obj;
559 const SID *owner;
560 struct stat st;
561 mode_t mode;
562 int unix_fd;
564 assert( obj->ops == &file_ops );
566 unix_fd = get_file_unix_fd( file );
568 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
570 if (set_info & OWNER_SECURITY_INFORMATION)
572 owner = sd_get_owner( sd );
573 if (!owner)
575 set_error( STATUS_INVALID_SECURITY_DESCR );
576 return 0;
578 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
580 /* FIXME: get Unix uid and call fchown */
583 else if (obj->sd)
584 owner = sd_get_owner( obj->sd );
585 else
586 owner = token_get_user( current->process->token );
588 /* group and sacl not supported */
590 if (set_info & DACL_SECURITY_INFORMATION)
592 /* keep the bits that we don't map to access rights in the ACL */
593 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
594 mode |= sd_to_mode( sd, owner );
596 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
598 file_set_error();
599 return 0;
602 return 1;
605 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
607 if (!name || !name->len) return NULL; /* open the file itself */
609 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
610 return NULL;
613 static struct object *file_open_file( struct object *obj, unsigned int access,
614 unsigned int sharing, unsigned int options )
616 struct file *file = (struct file *)obj;
617 struct object *new_file = NULL;
618 char *unix_name;
620 assert( obj->ops == &file_ops );
622 if ((unix_name = dup_fd_name( file->fd, "" )))
624 new_file = create_file( NULL, unix_name, strlen(unix_name), access,
625 sharing, FILE_OPEN, options, 0, NULL );
626 free( unix_name );
628 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
629 return new_file;
632 static struct list *file_get_kernel_obj_list( struct object *obj )
634 struct file *file = (struct file *)obj;
635 return &file->kernel_object;
638 static void file_destroy( struct object *obj )
640 struct file *file = (struct file *)obj;
641 assert( obj->ops == &file_ops );
643 if (file->fd) release_object( file->fd );
646 /* set the last error depending on errno */
647 void file_set_error(void)
649 switch (errno)
651 case ETXTBSY:
652 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
653 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
654 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
655 case EACCES:
656 case ESRCH:
657 case EROFS:
658 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
659 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
660 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
661 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
662 case ENFILE:
663 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
664 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
665 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
666 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
667 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
668 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
669 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
670 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
671 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
672 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
673 case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
674 case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
675 #ifdef EOVERFLOW
676 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
677 #endif
678 default:
679 perror("wineserver: file_set_error() can't map error");
680 set_error( STATUS_UNSUCCESSFUL );
681 break;
685 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
687 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
690 int get_file_unix_fd( struct file *file )
692 return get_unix_fd( file->fd );
695 /* create a file */
696 DECL_HANDLER(create_file)
698 struct object *file;
699 struct fd *root_fd = NULL;
700 struct unicode_str unicode_name;
701 const struct security_descriptor *sd;
702 const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
703 const char *name;
704 data_size_t name_len;
706 if (!objattr) return;
708 /* name is transferred in the unix codepage outside of the objattr structure */
709 if (unicode_name.len)
711 set_error( STATUS_INVALID_PARAMETER );
712 return;
715 if (objattr->rootdir)
717 struct dir *root;
719 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
720 root_fd = get_obj_fd( (struct object *)root );
721 release_object( root );
722 if (!root_fd) return;
725 name = get_req_data_after_objattr( objattr, &name_len );
727 reply->handle = 0;
728 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
729 req->create, req->options, req->attrs, sd )))
731 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
732 release_object( file );
734 if (root_fd) release_object( root_fd );
737 /* allocate a file handle for a Unix fd */
738 DECL_HANDLER(alloc_file_handle)
740 struct file *file;
741 int fd;
743 reply->handle = 0;
744 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
746 set_error( STATUS_INVALID_HANDLE );
747 return;
749 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
751 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
752 release_object( file );
756 /* lock a region of a file */
757 DECL_HANDLER(lock_file)
759 struct file *file;
761 if ((file = get_file_obj( current->process, req->handle, 0 )))
763 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
764 reply->overlapped = is_fd_overlapped( file->fd );
765 release_object( file );
769 /* unlock a region of a file */
770 DECL_HANDLER(unlock_file)
772 struct file *file;
774 if ((file = get_file_obj( current->process, req->handle, 0 )))
776 unlock_fd( file->fd, req->offset, req->count );
777 release_object( file );