comctl32/treeview: Always return state from TVM_GETITEM handler.
[wine/multimedia.git] / server / file.c
blobfcfd572c2b17afea3de55fe91777e62ee8f94da9
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 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
33 #endif
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_UTIME_H
40 #include <utime.h>
41 #endif
42 #ifdef HAVE_POLL_H
43 #include <poll.h>
44 #endif
46 #include "ntstatus.h"
47 #define WIN32_NO_STATUS
48 #include "windef.h"
49 #include "winternl.h"
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
55 #include "process.h"
56 #include "security.h"
58 struct file
60 struct object obj; /* object header */
61 struct fd *fd; /* file descriptor for this file */
62 unsigned int access; /* file access (FILE_READ_DATA etc.) */
63 mode_t mode; /* file stat.st_mode */
64 uid_t uid; /* file stat.st_uid */
67 static unsigned int generic_file_map_access( unsigned int access );
69 static void file_dump( struct object *obj, int verbose );
70 static struct fd *file_get_fd( struct object *obj );
71 static struct security_descriptor *file_get_sd( struct object *obj );
72 static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
73 static void file_destroy( struct object *obj );
75 static int file_get_poll_events( struct fd *fd );
76 static void file_flush( struct fd *fd, struct event **event );
77 static enum server_fd_type file_get_fd_type( struct fd *fd );
79 static const struct object_ops file_ops =
81 sizeof(struct file), /* size */
82 file_dump, /* dump */
83 no_get_type, /* get_type */
84 add_queue, /* add_queue */
85 remove_queue, /* remove_queue */
86 default_fd_signaled, /* signaled */
87 no_satisfied, /* satisfied */
88 no_signal, /* signal */
89 file_get_fd, /* get_fd */
90 default_fd_map_access, /* map_access */
91 file_get_sd, /* get_sd */
92 file_set_sd, /* set_sd */
93 no_lookup_name, /* lookup_name */
94 no_open_file, /* open_file */
95 fd_close_handle, /* close_handle */
96 file_destroy /* destroy */
99 static const struct fd_ops file_fd_ops =
101 file_get_poll_events, /* get_poll_events */
102 default_poll_event, /* poll_event */
103 file_flush, /* flush */
104 file_get_fd_type, /* get_fd_type */
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 return NULL;
129 if ((file = alloc_object( &file_ops )))
131 file->mode = st.st_mode;
132 file->access = default_fd_map_access( &file->obj, access );
133 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
134 FILE_SYNCHRONOUS_IO_NONALERT )))
136 release_object( file );
137 return NULL;
139 allow_fd_caching( file->fd );
141 return file;
144 /* create a file by duplicating an fd object */
145 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
147 struct file *file;
148 struct stat st;
150 if (fstat( get_unix_fd(fd), &st ) == -1)
152 file_set_error();
153 return NULL;
156 if ((file = alloc_object( &file_ops )))
158 file->mode = st.st_mode;
159 file->access = default_fd_map_access( &file->obj, access );
160 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
162 release_object( file );
163 return NULL;
165 set_fd_user( file->fd, &file_fd_ops, &file->obj );
167 return file;
170 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
172 struct file *file = alloc_object( &file_ops );
174 if (!file) return NULL;
175 file->access = access;
176 file->mode = mode;
177 file->uid = ~(uid_t)0;
178 file->fd = fd;
179 grab_object( fd );
180 set_fd_user( fd, &file_fd_ops, &file->obj );
181 return &file->obj;
184 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
185 unsigned int access, unsigned int sharing, int create,
186 unsigned int options, unsigned int attrs,
187 const struct security_descriptor *sd )
189 struct object *obj = NULL;
190 struct fd *fd;
191 int flags;
192 char *name;
193 mode_t mode;
195 if (!len || ((nameptr[0] == '/') ^ !root))
197 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
198 return NULL;
200 if (!(name = mem_alloc( len + 1 ))) return NULL;
201 memcpy( name, nameptr, len );
202 name[len] = 0;
204 switch(create)
206 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
207 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
208 access |= FILE_WRITE_ATTRIBUTES;
209 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
210 case FILE_OPEN: flags = 0; break;
211 case FILE_OPEN_IF: flags = O_CREAT; break;
212 case FILE_OVERWRITE: flags = O_TRUNC;
213 access |= FILE_WRITE_ATTRIBUTES; break;
214 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
217 if (sd)
219 const SID *owner = sd_get_owner( sd );
220 if (!owner)
221 owner = token_get_user( current->process->token );
222 mode = sd_to_mode( sd, owner );
224 else
225 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
227 if (len >= 4 &&
228 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
230 if (mode & S_IRUSR)
231 mode |= S_IXUSR;
232 if (mode & S_IRGRP)
233 mode |= S_IXGRP;
234 if (mode & S_IROTH)
235 mode |= S_IXOTH;
238 access = generic_file_map_access( access );
240 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
241 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
242 if (!fd) goto done;
244 if (S_ISDIR(mode))
245 obj = create_dir_obj( fd, access, mode );
246 else if (S_ISCHR(mode) && is_serial_fd( fd ))
247 obj = create_serial( fd );
248 else
249 obj = create_file_obj( fd, access, mode );
251 release_object( fd );
253 done:
254 free( name );
255 return obj;
258 /* check if two file objects point to the same file */
259 int is_same_file( struct file *file1, struct file *file2 )
261 return is_same_file_fd( file1->fd, file2->fd );
264 static void file_dump( struct object *obj, int verbose )
266 struct file *file = (struct file *)obj;
267 assert( obj->ops == &file_ops );
268 fprintf( stderr, "File fd=%p\n", file->fd );
271 static int file_get_poll_events( struct fd *fd )
273 struct file *file = get_fd_user( fd );
274 int events = 0;
275 assert( file->obj.ops == &file_ops );
276 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
277 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
278 return events;
281 static void file_flush( struct fd *fd, struct event **event )
283 int unix_fd = get_unix_fd( fd );
284 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
287 static enum server_fd_type file_get_fd_type( struct fd *fd )
289 struct file *file = get_fd_user( fd );
291 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
292 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
293 return FD_TYPE_CHAR;
296 static struct fd *file_get_fd( struct object *obj )
298 struct file *file = (struct file *)obj;
299 assert( obj->ops == &file_ops );
300 return (struct fd *)grab_object( file->fd );
303 static unsigned int generic_file_map_access( unsigned int access )
305 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
306 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
307 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
308 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
309 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
312 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
314 struct security_descriptor *sd;
315 size_t dacl_size;
316 ACE_HEADER *current_ace;
317 ACCESS_ALLOWED_ACE *aaa;
318 ACL *dacl;
319 SID *sid;
320 char *ptr;
321 const SID *world_sid = security_world_sid;
322 const SID *local_system_sid = security_local_system_sid;
324 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
325 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
326 if (mode & S_IRWXU)
327 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
328 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
329 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
330 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
331 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
332 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
333 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
334 if (mode & S_IRWXO)
335 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
336 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
338 sd = mem_alloc( sizeof(struct security_descriptor) +
339 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
340 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
341 dacl_size );
342 if (!sd) return sd;
344 sd->control = SE_DACL_PRESENT;
345 sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
346 sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
347 sd->sacl_len = 0;
348 sd->dacl_len = dacl_size;
350 ptr = (char *)(sd + 1);
351 memcpy( ptr, user, sd->owner_len );
352 ptr += sd->owner_len;
353 memcpy( ptr, group, sd->group_len );
354 ptr += sd->group_len;
356 dacl = (ACL *)ptr;
357 dacl->AclRevision = ACL_REVISION;
358 dacl->Sbz1 = 0;
359 dacl->AclSize = dacl_size;
360 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
361 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
362 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
363 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
364 dacl->AceCount++;
365 dacl->Sbz2 = 0;
367 /* always give FILE_ALL_ACCESS for Local System */
368 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
369 current_ace = &aaa->Header;
370 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
371 aaa->Header.AceFlags = 0;
372 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
373 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
374 aaa->Mask = FILE_ALL_ACCESS;
375 sid = (SID *)&aaa->SidStart;
376 memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
378 if (mode & S_IRWXU)
380 /* appropriate access rights for the user */
381 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
382 current_ace = &aaa->Header;
383 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
384 aaa->Header.AceFlags = 0;
385 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
386 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
387 aaa->Mask = WRITE_DAC | WRITE_OWNER;
388 if (mode & S_IRUSR)
389 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
390 if (mode & S_IWUSR)
391 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
392 sid = (SID *)&aaa->SidStart;
393 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
395 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
396 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
397 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
399 /* deny just in case the user is a member of the group */
400 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
401 current_ace = &ada->Header;
402 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
403 ada->Header.AceFlags = 0;
404 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
405 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
406 ada->Mask = 0;
407 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
408 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
409 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
410 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
411 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
412 sid = (SID *)&ada->SidStart;
413 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
415 if (mode & S_IRWXO)
417 /* appropriate access rights for Everyone */
418 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
419 current_ace = &aaa->Header;
420 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
421 aaa->Header.AceFlags = 0;
422 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
423 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
424 aaa->Mask = 0;
425 if (mode & S_IROTH)
426 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
427 if (mode & S_IWOTH)
428 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
429 sid = (SID *)&aaa->SidStart;
430 memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
433 return sd;
436 static struct security_descriptor *file_get_sd( struct object *obj )
438 struct file *file = (struct file *)obj;
439 struct stat st;
440 int unix_fd;
441 struct security_descriptor *sd;
443 assert( obj->ops == &file_ops );
445 unix_fd = get_file_unix_fd( file );
447 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
448 return obj->sd;
450 /* mode and uid the same? if so, no need to re-generate security descriptor */
451 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
452 (st.st_uid == file->uid))
453 return obj->sd;
455 sd = mode_to_sd( st.st_mode,
456 security_unix_uid_to_sid( st.st_uid ),
457 token_get_primary_group( current->process->token ));
458 if (!sd) return obj->sd;
460 file->mode = st.st_mode;
461 file->uid = st.st_uid;
462 free( obj->sd );
463 obj->sd = sd;
464 return sd;
467 static mode_t file_access_to_mode( unsigned int access )
469 mode_t mode = 0;
471 access = generic_file_map_access( access );
472 if (access & FILE_READ_DATA) mode |= 4;
473 if (access & FILE_WRITE_DATA) mode |= 2;
474 if (access & FILE_EXECUTE) mode |= 1;
475 return mode;
478 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
480 mode_t new_mode = 0;
481 mode_t denied_mode = 0;
482 mode_t mode;
483 int present;
484 const ACL *dacl = sd_get_dacl( sd, &present );
485 const SID *user = token_get_user( current->process->token );
486 if (present && dacl)
488 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
489 ULONG i;
490 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
492 const ACCESS_ALLOWED_ACE *aa_ace;
493 const ACCESS_DENIED_ACE *ad_ace;
494 const SID *sid;
496 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
498 switch (ace->AceType)
500 case ACCESS_DENIED_ACE_TYPE:
501 ad_ace = (const ACCESS_DENIED_ACE *)ace;
502 sid = (const SID *)&ad_ace->SidStart;
503 mode = file_access_to_mode( ad_ace->Mask );
504 if (security_equal_sid( sid, security_world_sid ))
506 denied_mode |= (mode << 6) | (mode << 3) | mode; /* all */
508 else if (security_equal_sid( sid, owner ))
510 denied_mode |= (mode << 6); /* user only */
512 else if ((security_equal_sid( user, owner ) &&
513 token_sid_present( current->process->token, sid, TRUE )))
515 denied_mode |= (mode << 6) | (mode << 3); /* user + group */
517 break;
518 case ACCESS_ALLOWED_ACE_TYPE:
519 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
520 sid = (const SID *)&aa_ace->SidStart;
521 mode = file_access_to_mode( aa_ace->Mask );
522 if (security_equal_sid( sid, security_world_sid ))
524 new_mode |= (mode << 6) | (mode << 3) | mode; /* all */
526 else if (security_equal_sid( sid, owner ))
528 new_mode |= (mode << 6); /* user only */
530 else if ((security_equal_sid( user, owner ) &&
531 token_sid_present( current->process->token, sid, FALSE )))
533 new_mode |= (mode << 6) | (mode << 3); /* user + group */
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 & ~denied_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 void file_destroy( struct object *obj )
598 struct file *file = (struct file *)obj;
599 assert( obj->ops == &file_ops );
601 if (file->fd) release_object( file->fd );
604 /* set the last error depending on errno */
605 void file_set_error(void)
607 switch (errno)
609 case ETXTBSY:
610 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
611 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
612 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
613 case EACCES:
614 case ESRCH:
615 case EROFS:
616 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
617 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
618 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
619 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
620 case ENFILE:
621 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
622 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
623 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
624 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
625 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
626 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
627 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
628 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
629 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
630 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
631 #ifdef EOVERFLOW
632 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
633 #endif
634 default:
635 perror("wineserver: file_set_error() can't map error");
636 set_error( STATUS_UNSUCCESSFUL );
637 break;
641 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
643 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
646 int get_file_unix_fd( struct file *file )
648 return get_unix_fd( file->fd );
651 /* create a file */
652 DECL_HANDLER(create_file)
654 struct object *file;
655 struct fd *root_fd = NULL;
656 const struct object_attributes *objattr = get_req_data();
657 const struct security_descriptor *sd;
658 const char *name;
659 data_size_t name_len;
661 reply->handle = 0;
663 if (!objattr_is_valid( objattr, get_req_data_size() ))
664 return;
665 /* name is transferred in the unix codepage outside of the objattr structure */
666 if (objattr->name_len)
668 set_error( STATUS_INVALID_PARAMETER );
669 return;
672 if (objattr->rootdir)
674 struct dir *root;
676 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
677 root_fd = get_obj_fd( (struct object *)root );
678 release_object( root );
679 if (!root_fd) return;
682 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
684 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
685 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
687 reply->handle = 0;
688 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
689 req->create, req->options, req->attrs, sd )))
691 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
692 release_object( file );
694 if (root_fd) release_object( root_fd );
697 /* allocate a file handle for a Unix fd */
698 DECL_HANDLER(alloc_file_handle)
700 struct file *file;
701 int fd;
703 reply->handle = 0;
704 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
706 set_error( STATUS_INVALID_HANDLE );
707 return;
709 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
711 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
712 release_object( file );
716 /* lock a region of a file */
717 DECL_HANDLER(lock_file)
719 struct file *file;
721 if ((file = get_file_obj( current->process, req->handle, 0 )))
723 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
724 reply->overlapped = is_overlapped( file );
725 release_object( file );
729 /* unlock a region of a file */
730 DECL_HANDLER(unlock_file)
732 struct file *file;
734 if ((file = get_file_obj( current->process, req->handle, 0 )))
736 unlock_fd( file->fd, req->offset, req->count );
737 release_object( file );