po: Update German translation.
[wine.git] / server / file.c
blob3809012dc8f5600ec76be69069cd3202135940c8
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_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 void file_destroy( struct object *obj );
76 static int file_get_poll_events( struct fd *fd );
77 static int file_flush( struct fd *fd, struct async *async );
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 fd_close_handle, /* close_handle */
99 file_destroy /* destroy */
102 static const struct fd_ops file_fd_ops =
104 file_get_poll_events, /* get_poll_events */
105 default_poll_event, /* poll_event */
106 file_get_fd_type, /* get_fd_type */
107 no_fd_read, /* read */
108 no_fd_write, /* write */
109 file_flush, /* flush */
110 default_fd_ioctl, /* ioctl */
111 default_fd_queue_async, /* queue_async */
112 default_fd_reselect_async /* reselect_async */
115 static inline int is_overlapped( const struct file *file )
117 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
120 /* create a file from a file descriptor */
121 /* if the function fails the fd is closed */
122 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
124 struct file *file;
125 struct stat st;
127 if (fstat( fd, &st ) == -1)
129 file_set_error();
130 close( fd );
131 return NULL;
134 if (!(file = alloc_object( &file_ops )))
136 close( fd );
137 return NULL;
140 file->mode = st.st_mode;
141 file->access = default_fd_map_access( &file->obj, access );
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 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 grab_object( fd );
188 set_fd_user( fd, &file_fd_ops, &file->obj );
189 return &file->obj;
192 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
193 unsigned int access, unsigned int sharing, int create,
194 unsigned int options, unsigned int attrs,
195 const struct security_descriptor *sd )
197 struct object *obj = NULL;
198 struct fd *fd;
199 int flags;
200 char *name;
201 mode_t mode;
203 if (!len || ((nameptr[0] == '/') ^ !root))
205 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
206 return NULL;
208 if (!(name = mem_alloc( len + 1 ))) return NULL;
209 memcpy( name, nameptr, len );
210 name[len] = 0;
212 switch(create)
214 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
215 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
216 access |= FILE_WRITE_ATTRIBUTES;
217 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
218 case FILE_OPEN: flags = 0; break;
219 case FILE_OPEN_IF: flags = O_CREAT; break;
220 case FILE_OVERWRITE: flags = O_TRUNC;
221 access |= FILE_WRITE_ATTRIBUTES; break;
222 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
225 if (sd)
227 const SID *owner = sd_get_owner( sd );
228 if (!owner)
229 owner = token_get_user( current->process->token );
230 mode = sd_to_mode( sd, owner );
232 else if (options & FILE_DIRECTORY_FILE)
233 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
234 else
235 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
237 if (len >= 4 &&
238 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
240 if (mode & S_IRUSR)
241 mode |= S_IXUSR;
242 if (mode & S_IRGRP)
243 mode |= S_IXGRP;
244 if (mode & S_IROTH)
245 mode |= S_IXOTH;
248 access = generic_file_map_access( access );
250 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
251 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
252 if (!fd) goto done;
254 if (S_ISDIR(mode))
255 obj = create_dir_obj( fd, access, mode );
256 else if (S_ISCHR(mode) && is_serial_fd( fd ))
257 obj = create_serial( fd );
258 else
259 obj = create_file_obj( fd, access, mode );
261 release_object( fd );
263 done:
264 free( name );
265 return obj;
268 /* check if two file objects point to the same file */
269 int is_same_file( struct file *file1, struct file *file2 )
271 return is_same_file_fd( file1->fd, file2->fd );
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 static 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 int file_flush( struct fd *fd, struct async *async )
300 int unix_fd = get_unix_fd( fd );
301 if (unix_fd != -1 && fsync( unix_fd ) == -1)
303 file_set_error();
304 return 0;
306 return 1;
309 static enum server_fd_type file_get_fd_type( struct fd *fd )
311 struct file *file = get_fd_user( fd );
313 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
314 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
315 return FD_TYPE_CHAR;
318 static struct fd *file_get_fd( struct object *obj )
320 struct file *file = (struct file *)obj;
321 assert( obj->ops == &file_ops );
322 return (struct fd *)grab_object( file->fd );
325 static unsigned int generic_file_map_access( unsigned int access )
327 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
328 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
329 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
330 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
331 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
334 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
336 struct security_descriptor *sd;
337 size_t dacl_size;
338 ACE_HEADER *current_ace;
339 ACCESS_ALLOWED_ACE *aaa;
340 ACL *dacl;
341 SID *sid;
342 char *ptr;
343 const SID *world_sid = security_world_sid;
344 const SID *local_system_sid = security_local_system_sid;
346 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
347 security_sid_len( local_system_sid );
348 if (mode & S_IRWXU)
349 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
350 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
351 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
352 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
353 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
354 if (mode & S_IRWXO)
355 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
357 sd = mem_alloc( sizeof(struct security_descriptor) +
358 security_sid_len( user ) + security_sid_len( group ) +
359 dacl_size );
360 if (!sd) return sd;
362 sd->control = SE_DACL_PRESENT;
363 sd->owner_len = security_sid_len( user );
364 sd->group_len = security_sid_len( group );
365 sd->sacl_len = 0;
366 sd->dacl_len = dacl_size;
368 ptr = (char *)(sd + 1);
369 memcpy( ptr, user, sd->owner_len );
370 ptr += sd->owner_len;
371 memcpy( ptr, group, sd->group_len );
372 ptr += sd->group_len;
374 dacl = (ACL *)ptr;
375 dacl->AclRevision = ACL_REVISION;
376 dacl->Sbz1 = 0;
377 dacl->AclSize = dacl_size;
378 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
379 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
380 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
381 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
382 dacl->AceCount++;
383 dacl->Sbz2 = 0;
385 /* always give FILE_ALL_ACCESS for Local System */
386 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
387 current_ace = &aaa->Header;
388 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
389 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
390 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
391 aaa->Mask = FILE_ALL_ACCESS;
392 sid = (SID *)&aaa->SidStart;
393 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
395 if (mode & S_IRWXU)
397 /* appropriate access rights for the user */
398 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
399 current_ace = &aaa->Header;
400 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
401 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
402 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
403 aaa->Mask = WRITE_DAC | WRITE_OWNER;
404 if (mode & S_IRUSR)
405 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
406 if (mode & S_IWUSR)
407 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
408 sid = (SID *)&aaa->SidStart;
409 memcpy( sid, user, security_sid_len( user ));
411 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
412 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
413 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
415 /* deny just in case the user is a member of the group */
416 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
417 current_ace = &ada->Header;
418 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
419 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
420 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
421 ada->Mask = 0;
422 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
423 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
424 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
425 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
426 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
427 sid = (SID *)&ada->SidStart;
428 memcpy( sid, user, security_sid_len( user ));
430 if (mode & S_IRWXO)
432 /* appropriate access rights for Everyone */
433 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
434 current_ace = &aaa->Header;
435 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
436 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
437 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
438 aaa->Mask = 0;
439 if (mode & S_IROTH)
440 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
441 if (mode & S_IWOTH)
442 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
443 sid = (SID *)&aaa->SidStart;
444 memcpy( sid, world_sid, security_sid_len( world_sid ));
447 return sd;
450 static struct security_descriptor *file_get_sd( struct object *obj )
452 struct file *file = (struct file *)obj;
453 struct stat st;
454 int unix_fd;
455 struct security_descriptor *sd;
457 assert( obj->ops == &file_ops );
459 unix_fd = get_file_unix_fd( file );
461 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
462 return obj->sd;
464 /* mode and uid the same? if so, no need to re-generate security descriptor */
465 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
466 (st.st_uid == file->uid))
467 return obj->sd;
469 sd = mode_to_sd( st.st_mode,
470 security_unix_uid_to_sid( st.st_uid ),
471 token_get_primary_group( current->process->token ));
472 if (!sd) return obj->sd;
474 file->mode = st.st_mode;
475 file->uid = st.st_uid;
476 free( obj->sd );
477 obj->sd = sd;
478 return sd;
481 static mode_t file_access_to_mode( unsigned int access )
483 mode_t mode = 0;
485 access = generic_file_map_access( access );
486 if (access & FILE_READ_DATA) mode |= 4;
487 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
488 if (access & FILE_EXECUTE) mode |= 1;
489 return mode;
492 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
494 mode_t new_mode = 0;
495 mode_t bits_to_set = ~0;
496 mode_t mode;
497 int present;
498 const ACL *dacl = sd_get_dacl( sd, &present );
499 const SID *user = token_get_user( current->process->token );
500 if (present && dacl)
502 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
503 ULONG i;
504 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
506 const ACCESS_ALLOWED_ACE *aa_ace;
507 const ACCESS_DENIED_ACE *ad_ace;
508 const SID *sid;
510 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
512 switch (ace->AceType)
514 case ACCESS_DENIED_ACE_TYPE:
515 ad_ace = (const ACCESS_DENIED_ACE *)ace;
516 sid = (const SID *)&ad_ace->SidStart;
517 mode = file_access_to_mode( ad_ace->Mask );
518 if (security_equal_sid( sid, security_world_sid ))
520 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
522 else if ((security_equal_sid( user, owner ) &&
523 token_sid_present( current->process->token, sid, TRUE )))
525 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
527 else if (security_equal_sid( sid, owner ))
529 bits_to_set &= ~(mode << 6); /* user only */
531 break;
532 case ACCESS_ALLOWED_ACE_TYPE:
533 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
534 sid = (const SID *)&aa_ace->SidStart;
535 mode = file_access_to_mode( aa_ace->Mask );
536 if (security_equal_sid( sid, security_world_sid ))
538 mode = (mode << 6) | (mode << 3) | mode; /* all */
539 new_mode |= mode & bits_to_set;
540 bits_to_set &= ~mode;
542 else if ((security_equal_sid( user, owner ) &&
543 token_sid_present( current->process->token, sid, FALSE )))
545 mode = (mode << 6) | (mode << 3); /* user + group */
546 new_mode |= mode & bits_to_set;
547 bits_to_set &= ~mode;
549 else if (security_equal_sid( sid, owner ))
551 mode = (mode << 6); /* user only */
552 new_mode |= mode & bits_to_set;
553 bits_to_set &= ~mode;
555 break;
559 else
560 /* no ACL means full access rights to anyone */
561 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
563 return new_mode;
566 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
567 unsigned int set_info )
569 struct file *file = (struct file *)obj;
570 const SID *owner;
571 struct stat st;
572 mode_t mode;
573 int unix_fd;
575 assert( obj->ops == &file_ops );
577 unix_fd = get_file_unix_fd( file );
579 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
581 if (set_info & OWNER_SECURITY_INFORMATION)
583 owner = sd_get_owner( sd );
584 if (!owner)
586 set_error( STATUS_INVALID_SECURITY_DESCR );
587 return 0;
589 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
591 /* FIXME: get Unix uid and call fchown */
594 else if (obj->sd)
595 owner = sd_get_owner( obj->sd );
596 else
597 owner = token_get_user( current->process->token );
599 /* group and sacl not supported */
601 if (set_info & DACL_SECURITY_INFORMATION)
603 /* keep the bits that we don't map to access rights in the ACL */
604 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
605 mode |= sd_to_mode( sd, owner );
607 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
609 file_set_error();
610 return 0;
613 return 1;
616 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
618 if (!name || !name->len) return NULL; /* open the file itself */
620 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
621 return NULL;
624 static struct object *file_open_file( struct object *obj, unsigned int access,
625 unsigned int sharing, unsigned int options )
627 struct file *file = (struct file *)obj;
628 struct object *new_file = NULL;
629 char *unix_name;
631 assert( obj->ops == &file_ops );
633 if ((unix_name = dup_fd_name( file->fd, "" )))
635 new_file = create_file( NULL, unix_name, strlen(unix_name), access,
636 sharing, FILE_OPEN, options, 0, NULL );
637 free( unix_name );
639 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
640 return new_file;
643 static void file_destroy( struct object *obj )
645 struct file *file = (struct file *)obj;
646 assert( obj->ops == &file_ops );
648 if (file->fd) release_object( file->fd );
651 /* set the last error depending on errno */
652 void file_set_error(void)
654 switch (errno)
656 case ETXTBSY:
657 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
658 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
659 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
660 case EACCES:
661 case ESRCH:
662 case EROFS:
663 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
664 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
665 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
666 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
667 case ENFILE:
668 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
669 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
670 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
671 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
672 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
673 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
674 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
675 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
676 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
677 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
678 #ifdef EOVERFLOW
679 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
680 #endif
681 default:
682 perror("wineserver: file_set_error() can't map error");
683 set_error( STATUS_UNSUCCESSFUL );
684 break;
688 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
690 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
693 int get_file_unix_fd( struct file *file )
695 return get_unix_fd( file->fd );
698 /* create a file */
699 DECL_HANDLER(create_file)
701 struct object *file;
702 struct fd *root_fd = NULL;
703 struct unicode_str unicode_name;
704 const struct security_descriptor *sd;
705 const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
706 const char *name;
707 data_size_t name_len;
709 if (!objattr) return;
711 /* name is transferred in the unix codepage outside of the objattr structure */
712 if (unicode_name.len)
714 set_error( STATUS_INVALID_PARAMETER );
715 return;
718 if (objattr->rootdir)
720 struct dir *root;
722 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
723 root_fd = get_obj_fd( (struct object *)root );
724 release_object( root );
725 if (!root_fd) return;
728 name = get_req_data_after_objattr( objattr, &name_len );
730 reply->handle = 0;
731 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
732 req->create, req->options, req->attrs, sd )))
734 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
735 release_object( file );
737 if (root_fd) release_object( root_fd );
740 /* allocate a file handle for a Unix fd */
741 DECL_HANDLER(alloc_file_handle)
743 struct file *file;
744 int fd;
746 reply->handle = 0;
747 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
749 set_error( STATUS_INVALID_HANDLE );
750 return;
752 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
754 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
755 release_object( file );
759 /* lock a region of a file */
760 DECL_HANDLER(lock_file)
762 struct file *file;
764 if ((file = get_file_obj( current->process, req->handle, 0 )))
766 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
767 reply->overlapped = is_overlapped( file );
768 release_object( file );
772 /* unlock a region of a file */
773 DECL_HANDLER(unlock_file)
775 struct file *file;
777 if ((file = get_file_obj( current->process, req->handle, 0 )))
779 unlock_fd( file->fd, req->offset, req->count );
780 release_object( file );