d2d1: Make d2d_geometry_sink_vtbl static const.
[wine.git] / server / file.c
blobe70304f0265fa22e687a98d7abe25ed6f5114850
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 void file_destroy( struct object *obj );
73 static int file_get_poll_events( struct fd *fd );
74 static obj_handle_t file_flush( struct fd *fd, const async_data_t *async, int blocking );
75 static enum server_fd_type file_get_fd_type( struct fd *fd );
77 static const struct object_ops file_ops =
79 sizeof(struct file), /* size */
80 file_dump, /* dump */
81 file_get_type, /* get_type */
82 add_queue, /* add_queue */
83 remove_queue, /* remove_queue */
84 default_fd_signaled, /* signaled */
85 no_satisfied, /* satisfied */
86 no_signal, /* signal */
87 file_get_fd, /* get_fd */
88 default_fd_map_access, /* map_access */
89 file_get_sd, /* get_sd */
90 file_set_sd, /* set_sd */
91 no_lookup_name, /* lookup_name */
92 no_open_file, /* open_file */
93 fd_close_handle, /* close_handle */
94 file_destroy /* destroy */
97 static const struct fd_ops file_fd_ops =
99 file_get_poll_events, /* get_poll_events */
100 default_poll_event, /* poll_event */
101 file_get_fd_type, /* get_fd_type */
102 no_fd_read, /* read */
103 no_fd_write, /* write */
104 file_flush, /* flush */
105 default_fd_ioctl, /* ioctl */
106 default_fd_queue_async, /* queue_async */
107 default_fd_reselect_async, /* reselect_async */
108 default_fd_cancel_async /* cancel_async */
111 static inline int is_overlapped( const struct file *file )
113 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
116 /* create a file from a file descriptor */
117 /* if the function fails the fd is closed */
118 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
120 struct file *file;
121 struct stat st;
123 if (fstat( fd, &st ) == -1)
125 file_set_error();
126 close( fd );
127 return NULL;
130 if (!(file = alloc_object( &file_ops )))
132 close( fd );
133 return NULL;
136 file->mode = st.st_mode;
137 file->access = default_fd_map_access( &file->obj, access );
138 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
139 FILE_SYNCHRONOUS_IO_NONALERT )))
141 release_object( file );
142 return NULL;
144 allow_fd_caching( file->fd );
145 return file;
148 /* create a file by duplicating an fd object */
149 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
151 struct file *file;
152 struct stat st;
154 if (fstat( get_unix_fd(fd), &st ) == -1)
156 file_set_error();
157 return NULL;
160 if ((file = alloc_object( &file_ops )))
162 file->mode = st.st_mode;
163 file->access = default_fd_map_access( &file->obj, access );
164 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
166 release_object( file );
167 return NULL;
169 set_fd_user( file->fd, &file_fd_ops, &file->obj );
171 return file;
174 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
176 struct file *file = alloc_object( &file_ops );
178 if (!file) return NULL;
179 file->access = access;
180 file->mode = mode;
181 file->uid = ~(uid_t)0;
182 file->fd = fd;
183 grab_object( fd );
184 set_fd_user( fd, &file_fd_ops, &file->obj );
185 return &file->obj;
188 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
189 unsigned int access, unsigned int sharing, int create,
190 unsigned int options, unsigned int attrs,
191 const struct security_descriptor *sd )
193 struct object *obj = NULL;
194 struct fd *fd;
195 int flags;
196 char *name;
197 mode_t mode;
199 if (!len || ((nameptr[0] == '/') ^ !root))
201 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
202 return NULL;
204 if (!(name = mem_alloc( len + 1 ))) return NULL;
205 memcpy( name, nameptr, len );
206 name[len] = 0;
208 switch(create)
210 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
211 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
212 access |= FILE_WRITE_ATTRIBUTES;
213 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
214 case FILE_OPEN: flags = 0; break;
215 case FILE_OPEN_IF: flags = O_CREAT; break;
216 case FILE_OVERWRITE: flags = O_TRUNC;
217 access |= FILE_WRITE_ATTRIBUTES; break;
218 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
221 if (sd)
223 const SID *owner = sd_get_owner( sd );
224 if (!owner)
225 owner = token_get_user( current->process->token );
226 mode = sd_to_mode( sd, owner );
228 else if (options & FILE_DIRECTORY_FILE)
229 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
230 else
231 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
233 if (len >= 4 &&
234 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
236 if (mode & S_IRUSR)
237 mode |= S_IXUSR;
238 if (mode & S_IRGRP)
239 mode |= S_IXGRP;
240 if (mode & S_IROTH)
241 mode |= S_IXOTH;
244 access = generic_file_map_access( access );
246 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
247 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
248 if (!fd) goto done;
250 if (S_ISDIR(mode))
251 obj = create_dir_obj( fd, access, mode );
252 else if (S_ISCHR(mode) && is_serial_fd( fd ))
253 obj = create_serial( fd );
254 else
255 obj = create_file_obj( fd, access, mode );
257 release_object( fd );
259 done:
260 free( name );
261 return obj;
264 /* check if two file objects point to the same file */
265 int is_same_file( struct file *file1, struct file *file2 )
267 return is_same_file_fd( file1->fd, file2->fd );
270 static void file_dump( struct object *obj, int verbose )
272 struct file *file = (struct file *)obj;
273 assert( obj->ops == &file_ops );
274 fprintf( stderr, "File fd=%p\n", file->fd );
277 static struct object_type *file_get_type( struct object *obj )
279 static const WCHAR name[] = {'F','i','l','e'};
280 static const struct unicode_str str = { name, sizeof(name) };
281 return get_object_type( &str );
284 static int file_get_poll_events( struct fd *fd )
286 struct file *file = get_fd_user( fd );
287 int events = 0;
288 assert( file->obj.ops == &file_ops );
289 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
290 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
291 return events;
294 static obj_handle_t file_flush( struct fd *fd, const async_data_t *async, int blocking )
296 int unix_fd = get_unix_fd( fd );
297 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
298 return 0;
301 static enum server_fd_type file_get_fd_type( struct fd *fd )
303 struct file *file = get_fd_user( fd );
305 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
306 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
307 return FD_TYPE_CHAR;
310 static struct fd *file_get_fd( struct object *obj )
312 struct file *file = (struct file *)obj;
313 assert( obj->ops == &file_ops );
314 return (struct fd *)grab_object( file->fd );
317 static unsigned int generic_file_map_access( unsigned int access )
319 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
320 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
321 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
322 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
323 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
326 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
328 struct security_descriptor *sd;
329 size_t dacl_size;
330 ACE_HEADER *current_ace;
331 ACCESS_ALLOWED_ACE *aaa;
332 ACL *dacl;
333 SID *sid;
334 char *ptr;
335 const SID *world_sid = security_world_sid;
336 const SID *local_system_sid = security_local_system_sid;
338 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
339 security_sid_len( local_system_sid );
340 if (mode & S_IRWXU)
341 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
342 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
343 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
344 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
345 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
346 if (mode & S_IRWXO)
347 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
349 sd = mem_alloc( sizeof(struct security_descriptor) +
350 security_sid_len( user ) + security_sid_len( group ) +
351 dacl_size );
352 if (!sd) return sd;
354 sd->control = SE_DACL_PRESENT;
355 sd->owner_len = security_sid_len( user );
356 sd->group_len = security_sid_len( group );
357 sd->sacl_len = 0;
358 sd->dacl_len = dacl_size;
360 ptr = (char *)(sd + 1);
361 memcpy( ptr, user, sd->owner_len );
362 ptr += sd->owner_len;
363 memcpy( ptr, group, sd->group_len );
364 ptr += sd->group_len;
366 dacl = (ACL *)ptr;
367 dacl->AclRevision = ACL_REVISION;
368 dacl->Sbz1 = 0;
369 dacl->AclSize = dacl_size;
370 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
371 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
372 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
373 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
374 dacl->AceCount++;
375 dacl->Sbz2 = 0;
377 /* always give FILE_ALL_ACCESS for Local System */
378 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
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( local_system_sid );
383 aaa->Mask = FILE_ALL_ACCESS;
384 sid = (SID *)&aaa->SidStart;
385 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
387 if (mode & S_IRWXU)
389 /* appropriate access rights for the user */
390 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
391 current_ace = &aaa->Header;
392 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
393 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
394 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
395 aaa->Mask = WRITE_DAC | WRITE_OWNER;
396 if (mode & S_IRUSR)
397 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
398 if (mode & S_IWUSR)
399 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
400 sid = (SID *)&aaa->SidStart;
401 memcpy( sid, user, security_sid_len( user ));
403 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
404 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
405 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
407 /* deny just in case the user is a member of the group */
408 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
409 current_ace = &ada->Header;
410 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
411 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
412 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
413 ada->Mask = 0;
414 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
415 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
416 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
417 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
418 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
419 sid = (SID *)&ada->SidStart;
420 memcpy( sid, user, security_sid_len( user ));
422 if (mode & S_IRWXO)
424 /* appropriate access rights for Everyone */
425 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
426 current_ace = &aaa->Header;
427 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
428 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
429 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
430 aaa->Mask = 0;
431 if (mode & S_IROTH)
432 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
433 if (mode & S_IWOTH)
434 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
435 sid = (SID *)&aaa->SidStart;
436 memcpy( sid, world_sid, security_sid_len( world_sid ));
439 return sd;
442 static struct security_descriptor *file_get_sd( struct object *obj )
444 struct file *file = (struct file *)obj;
445 struct stat st;
446 int unix_fd;
447 struct security_descriptor *sd;
449 assert( obj->ops == &file_ops );
451 unix_fd = get_file_unix_fd( file );
453 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
454 return obj->sd;
456 /* mode and uid the same? if so, no need to re-generate security descriptor */
457 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
458 (st.st_uid == file->uid))
459 return obj->sd;
461 sd = mode_to_sd( st.st_mode,
462 security_unix_uid_to_sid( st.st_uid ),
463 token_get_primary_group( current->process->token ));
464 if (!sd) return obj->sd;
466 file->mode = st.st_mode;
467 file->uid = st.st_uid;
468 free( obj->sd );
469 obj->sd = sd;
470 return sd;
473 static mode_t file_access_to_mode( unsigned int access )
475 mode_t mode = 0;
477 access = generic_file_map_access( access );
478 if (access & FILE_READ_DATA) mode |= 4;
479 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
480 if (access & FILE_EXECUTE) mode |= 1;
481 return mode;
484 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
486 mode_t new_mode = 0;
487 mode_t bits_to_set = ~0;
488 mode_t mode;
489 int present;
490 const ACL *dacl = sd_get_dacl( sd, &present );
491 const SID *user = token_get_user( current->process->token );
492 if (present && dacl)
494 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
495 ULONG i;
496 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
498 const ACCESS_ALLOWED_ACE *aa_ace;
499 const ACCESS_DENIED_ACE *ad_ace;
500 const SID *sid;
502 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
504 switch (ace->AceType)
506 case ACCESS_DENIED_ACE_TYPE:
507 ad_ace = (const ACCESS_DENIED_ACE *)ace;
508 sid = (const SID *)&ad_ace->SidStart;
509 mode = file_access_to_mode( ad_ace->Mask );
510 if (security_equal_sid( sid, security_world_sid ))
512 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
514 else if ((security_equal_sid( user, owner ) &&
515 token_sid_present( current->process->token, sid, TRUE )))
517 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
519 else if (security_equal_sid( sid, owner ))
521 bits_to_set &= ~(mode << 6); /* user only */
523 break;
524 case ACCESS_ALLOWED_ACE_TYPE:
525 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
526 sid = (const SID *)&aa_ace->SidStart;
527 mode = file_access_to_mode( aa_ace->Mask );
528 if (security_equal_sid( sid, security_world_sid ))
530 mode = (mode << 6) | (mode << 3) | mode; /* all */
531 new_mode |= mode & bits_to_set;
532 bits_to_set &= ~mode;
534 else if ((security_equal_sid( user, owner ) &&
535 token_sid_present( current->process->token, sid, FALSE )))
537 mode = (mode << 6) | (mode << 3); /* user + group */
538 new_mode |= mode & bits_to_set;
539 bits_to_set &= ~mode;
541 else if (security_equal_sid( sid, owner ))
543 mode = (mode << 6); /* user only */
544 new_mode |= mode & bits_to_set;
545 bits_to_set &= ~mode;
547 break;
551 else
552 /* no ACL means full access rights to anyone */
553 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
555 return new_mode;
558 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
559 unsigned int set_info )
561 struct file *file = (struct file *)obj;
562 const SID *owner;
563 struct stat st;
564 mode_t mode;
565 int unix_fd;
567 assert( obj->ops == &file_ops );
569 unix_fd = get_file_unix_fd( file );
571 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
573 if (set_info & OWNER_SECURITY_INFORMATION)
575 owner = sd_get_owner( sd );
576 if (!owner)
578 set_error( STATUS_INVALID_SECURITY_DESCR );
579 return 0;
581 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
583 /* FIXME: get Unix uid and call fchown */
586 else if (obj->sd)
587 owner = sd_get_owner( obj->sd );
588 else
589 owner = token_get_user( current->process->token );
591 /* group and sacl not supported */
593 if (set_info & DACL_SECURITY_INFORMATION)
595 /* keep the bits that we don't map to access rights in the ACL */
596 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
597 mode |= sd_to_mode( sd, owner );
599 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
601 file_set_error();
602 return 0;
605 return 1;
608 static void file_destroy( struct object *obj )
610 struct file *file = (struct file *)obj;
611 assert( obj->ops == &file_ops );
613 if (file->fd) release_object( file->fd );
616 /* set the last error depending on errno */
617 void file_set_error(void)
619 switch (errno)
621 case ETXTBSY:
622 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
623 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
624 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
625 case EACCES:
626 case ESRCH:
627 case EROFS:
628 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
629 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
630 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
631 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
632 case ENFILE:
633 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
634 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
635 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
636 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
637 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
638 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
639 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
640 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
641 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
642 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
643 #ifdef EOVERFLOW
644 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
645 #endif
646 default:
647 perror("wineserver: file_set_error() can't map error");
648 set_error( STATUS_UNSUCCESSFUL );
649 break;
653 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
655 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
658 int get_file_unix_fd( struct file *file )
660 return get_unix_fd( file->fd );
663 /* create a file */
664 DECL_HANDLER(create_file)
666 struct object *file;
667 struct fd *root_fd = NULL;
668 const struct object_attributes *objattr = get_req_data();
669 const struct security_descriptor *sd;
670 const char *name;
671 data_size_t name_len;
673 reply->handle = 0;
675 if (!objattr_is_valid( objattr, get_req_data_size() ))
676 return;
677 /* name is transferred in the unix codepage outside of the objattr structure */
678 if (objattr->name_len)
680 set_error( STATUS_INVALID_PARAMETER );
681 return;
684 if (objattr->rootdir)
686 struct dir *root;
688 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
689 root_fd = get_obj_fd( (struct object *)root );
690 release_object( root );
691 if (!root_fd) return;
694 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
696 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
697 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
699 reply->handle = 0;
700 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
701 req->create, req->options, req->attrs, sd )))
703 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
704 release_object( file );
706 if (root_fd) release_object( root_fd );
709 /* allocate a file handle for a Unix fd */
710 DECL_HANDLER(alloc_file_handle)
712 struct file *file;
713 int fd;
715 reply->handle = 0;
716 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
718 set_error( STATUS_INVALID_HANDLE );
719 return;
721 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
723 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
724 release_object( file );
728 /* lock a region of a file */
729 DECL_HANDLER(lock_file)
731 struct file *file;
733 if ((file = get_file_obj( current->process, req->handle, 0 )))
735 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
736 reply->overlapped = is_overlapped( file );
737 release_object( file );
741 /* unlock a region of a file */
742 DECL_HANDLER(unlock_file)
744 struct file *file;
746 if ((file = get_file_obj( current->process, req->handle, 0 )))
748 unlock_fd( file->fd, req->offset, req->count );
749 release_object( file );