ntdll: Implement NtDebugActiveProcess() and NtRemoveProcessDebug().
[wine.git] / server / file.c
blobfd8e3f960330afbca49c450dd91a24022590e683
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,
72 unsigned int attr, struct object *root );
73 static struct object *file_open_file( struct object *obj, unsigned int access,
74 unsigned int sharing, unsigned int options );
75 static struct list *file_get_kernel_obj_list( struct object *obj );
76 static void file_destroy( struct object *obj );
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 default_fd_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 enum server_fd_type file_get_fd_type( struct fd *fd )
291 struct file *file = get_fd_user( fd );
293 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
294 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
295 return FD_TYPE_CHAR;
298 static struct fd *file_get_fd( struct object *obj )
300 struct file *file = (struct file *)obj;
301 assert( obj->ops == &file_ops );
302 return (struct fd *)grab_object( file->fd );
305 static unsigned int generic_file_map_access( unsigned int access )
307 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
308 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
309 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
310 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
311 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
314 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
316 struct security_descriptor *sd;
317 size_t dacl_size;
318 ACE_HEADER *current_ace;
319 ACCESS_ALLOWED_ACE *aaa;
320 ACL *dacl;
321 SID *sid;
322 char *ptr;
323 const SID *world_sid = security_world_sid;
324 const SID *local_system_sid = security_local_system_sid;
326 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
327 security_sid_len( local_system_sid );
328 if (mode & S_IRWXU)
329 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
330 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
331 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
332 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
333 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
334 if (mode & S_IRWXO)
335 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
337 sd = mem_alloc( sizeof(struct security_descriptor) +
338 security_sid_len( user ) + security_sid_len( group ) +
339 dacl_size );
340 if (!sd) return sd;
342 sd->control = SE_DACL_PRESENT;
343 sd->owner_len = security_sid_len( user );
344 sd->group_len = security_sid_len( group );
345 sd->sacl_len = 0;
346 sd->dacl_len = dacl_size;
348 ptr = (char *)(sd + 1);
349 memcpy( ptr, user, sd->owner_len );
350 ptr += sd->owner_len;
351 memcpy( ptr, group, sd->group_len );
352 ptr += sd->group_len;
354 dacl = (ACL *)ptr;
355 dacl->AclRevision = ACL_REVISION;
356 dacl->Sbz1 = 0;
357 dacl->AclSize = dacl_size;
358 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
359 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
360 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
361 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
362 dacl->AceCount++;
363 dacl->Sbz2 = 0;
365 /* always give FILE_ALL_ACCESS for Local System */
366 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
367 current_ace = &aaa->Header;
368 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
369 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
370 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
371 aaa->Mask = FILE_ALL_ACCESS;
372 sid = (SID *)&aaa->SidStart;
373 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
375 if (mode & S_IRWXU)
377 /* appropriate access rights for the user */
378 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
379 current_ace = &aaa->Header;
380 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
381 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
382 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
383 aaa->Mask = WRITE_DAC | WRITE_OWNER;
384 if (mode & S_IRUSR)
385 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
386 if (mode & S_IWUSR)
387 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
388 sid = (SID *)&aaa->SidStart;
389 memcpy( sid, user, security_sid_len( user ));
391 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
392 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
393 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
395 /* deny just in case the user is a member of the group */
396 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
397 current_ace = &ada->Header;
398 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
399 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
400 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
401 ada->Mask = 0;
402 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
403 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
404 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
405 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
406 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
407 sid = (SID *)&ada->SidStart;
408 memcpy( sid, user, security_sid_len( user ));
410 if (mode & S_IRWXO)
412 /* appropriate access rights for Everyone */
413 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
414 current_ace = &aaa->Header;
415 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
416 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
417 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
418 aaa->Mask = 0;
419 if (mode & S_IROTH)
420 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
421 if (mode & S_IWOTH)
422 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
423 sid = (SID *)&aaa->SidStart;
424 memcpy( sid, world_sid, security_sid_len( world_sid ));
427 return sd;
430 static struct security_descriptor *file_get_sd( struct object *obj )
432 struct file *file = (struct file *)obj;
433 struct stat st;
434 int unix_fd;
435 struct security_descriptor *sd;
437 assert( obj->ops == &file_ops );
439 unix_fd = get_file_unix_fd( file );
441 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
442 return obj->sd;
444 /* mode and uid the same? if so, no need to re-generate security descriptor */
445 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
446 (st.st_uid == file->uid))
447 return obj->sd;
449 sd = mode_to_sd( st.st_mode,
450 security_unix_uid_to_sid( st.st_uid ),
451 token_get_primary_group( current->process->token ));
452 if (!sd) return obj->sd;
454 file->mode = st.st_mode;
455 file->uid = st.st_uid;
456 free( obj->sd );
457 obj->sd = sd;
458 return sd;
461 static mode_t file_access_to_mode( unsigned int access )
463 mode_t mode = 0;
465 access = generic_file_map_access( access );
466 if (access & FILE_READ_DATA) mode |= 4;
467 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
468 if (access & FILE_EXECUTE) mode |= 1;
469 return mode;
472 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
474 mode_t new_mode = 0;
475 mode_t bits_to_set = ~0;
476 mode_t mode;
477 int present;
478 const ACL *dacl = sd_get_dacl( sd, &present );
479 const SID *user = token_get_user( current->process->token );
480 if (present && dacl)
482 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
483 ULONG i;
484 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
486 const ACCESS_ALLOWED_ACE *aa_ace;
487 const ACCESS_DENIED_ACE *ad_ace;
488 const SID *sid;
490 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
492 switch (ace->AceType)
494 case ACCESS_DENIED_ACE_TYPE:
495 ad_ace = (const ACCESS_DENIED_ACE *)ace;
496 sid = (const SID *)&ad_ace->SidStart;
497 mode = file_access_to_mode( ad_ace->Mask );
498 if (security_equal_sid( sid, security_world_sid ))
500 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
502 else if ((security_equal_sid( user, owner ) &&
503 token_sid_present( current->process->token, sid, TRUE )))
505 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
507 else if (security_equal_sid( sid, owner ))
509 bits_to_set &= ~(mode << 6); /* user only */
511 break;
512 case ACCESS_ALLOWED_ACE_TYPE:
513 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
514 sid = (const SID *)&aa_ace->SidStart;
515 mode = file_access_to_mode( aa_ace->Mask );
516 if (security_equal_sid( sid, security_world_sid ))
518 mode = (mode << 6) | (mode << 3) | mode; /* all */
519 new_mode |= mode & bits_to_set;
520 bits_to_set &= ~mode;
522 else if ((security_equal_sid( user, owner ) &&
523 token_sid_present( current->process->token, sid, FALSE )))
525 mode = (mode << 6) | (mode << 3); /* user + group */
526 new_mode |= mode & bits_to_set;
527 bits_to_set &= ~mode;
529 else if (security_equal_sid( sid, owner ))
531 mode = (mode << 6); /* user only */
532 new_mode |= mode & bits_to_set;
533 bits_to_set &= ~mode;
535 break;
539 else
540 /* no ACL means full access rights to anyone */
541 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
543 return new_mode;
546 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
547 unsigned int set_info )
549 struct file *file = (struct file *)obj;
550 const SID *owner;
551 struct stat st;
552 mode_t mode;
553 int unix_fd;
555 assert( obj->ops == &file_ops );
557 unix_fd = get_file_unix_fd( file );
559 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
561 if (set_info & OWNER_SECURITY_INFORMATION)
563 owner = sd_get_owner( sd );
564 if (!owner)
566 set_error( STATUS_INVALID_SECURITY_DESCR );
567 return 0;
569 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
571 /* FIXME: get Unix uid and call fchown */
574 else if (obj->sd)
575 owner = sd_get_owner( obj->sd );
576 else
577 owner = token_get_user( current->process->token );
579 /* group and sacl not supported */
581 if (set_info & DACL_SECURITY_INFORMATION)
583 /* keep the bits that we don't map to access rights in the ACL */
584 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
585 mode |= sd_to_mode( sd, owner );
587 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
589 file_set_error();
590 return 0;
593 return 1;
596 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
597 unsigned int attr, struct object *root )
599 if (!name || !name->len) return NULL; /* open the file itself */
601 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
602 return NULL;
605 static struct object *file_open_file( struct object *obj, unsigned int access,
606 unsigned int sharing, unsigned int options )
608 struct file *file = (struct file *)obj;
609 struct object *new_file = NULL;
610 char *unix_name;
612 assert( obj->ops == &file_ops );
614 if ((unix_name = dup_fd_name( file->fd, "" )))
616 new_file = create_file( NULL, unix_name, strlen(unix_name), access,
617 sharing, FILE_OPEN, options, 0, NULL );
618 free( unix_name );
620 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
621 return new_file;
624 static struct list *file_get_kernel_obj_list( struct object *obj )
626 struct file *file = (struct file *)obj;
627 return &file->kernel_object;
630 static void file_destroy( struct object *obj )
632 struct file *file = (struct file *)obj;
633 assert( obj->ops == &file_ops );
635 if (file->fd) release_object( file->fd );
638 /* set the last error depending on errno */
639 void file_set_error(void)
641 switch (errno)
643 case ETXTBSY:
644 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
645 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
646 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
647 case EACCES:
648 case ESRCH:
649 case EROFS:
650 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
651 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
652 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
653 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
654 case ENFILE:
655 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
656 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
657 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
658 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
659 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
660 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
661 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
662 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
663 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
664 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
665 case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
666 case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
667 #ifdef EOVERFLOW
668 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
669 #endif
670 default:
671 perror("wineserver: file_set_error() can't map error");
672 set_error( STATUS_UNSUCCESSFUL );
673 break;
677 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
679 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
682 int get_file_unix_fd( struct file *file )
684 return get_unix_fd( file->fd );
687 /* create a file */
688 DECL_HANDLER(create_file)
690 struct object *file;
691 struct fd *root_fd = NULL;
692 struct unicode_str unicode_name;
693 const struct security_descriptor *sd;
694 const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
695 const char *name;
696 data_size_t name_len;
698 if (!objattr) return;
700 /* name is transferred in the unix codepage outside of the objattr structure */
701 if (unicode_name.len)
703 set_error( STATUS_INVALID_PARAMETER );
704 return;
707 if (objattr->rootdir)
709 struct dir *root;
711 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
712 root_fd = get_obj_fd( (struct object *)root );
713 release_object( root );
714 if (!root_fd) return;
717 name = get_req_data_after_objattr( objattr, &name_len );
719 reply->handle = 0;
720 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
721 req->create, req->options, req->attrs, sd )))
723 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
724 release_object( file );
726 if (root_fd) release_object( root_fd );
729 /* allocate a file handle for a Unix fd */
730 DECL_HANDLER(alloc_file_handle)
732 struct file *file;
733 int fd;
735 reply->handle = 0;
736 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
738 set_error( STATUS_INVALID_HANDLE );
739 return;
741 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
743 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
744 release_object( file );
748 /* lock a region of a file */
749 DECL_HANDLER(lock_file)
751 struct file *file;
753 if ((file = get_file_obj( current->process, req->handle, 0 )))
755 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
756 reply->overlapped = is_fd_overlapped( file->fd );
757 release_object( file );
761 /* unlock a region of a file */
762 DECL_HANDLER(unlock_file)
764 struct file *file;
766 if ((file = get_file_obj( current->process, req->handle, 0 )))
768 unlock_fd( file->fd, req->offset, req->count );
769 release_object( file );