ntoskrnl: Add support for read and write requests.
[wine/multimedia.git] / server / file.c
blob792bbe09b4e6059e49ac3c8f942d99d3fe5aa0a7
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 fd *file_get_fd( struct object *obj );
68 static struct security_descriptor *file_get_sd( struct object *obj );
69 static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
70 static void file_destroy( struct object *obj );
72 static int file_get_poll_events( struct fd *fd );
73 static obj_handle_t file_flush( struct fd *fd, const async_data_t *async, int blocking );
74 static enum server_fd_type file_get_fd_type( struct fd *fd );
76 static const struct object_ops file_ops =
78 sizeof(struct file), /* size */
79 file_dump, /* dump */
80 no_get_type, /* get_type */
81 add_queue, /* add_queue */
82 remove_queue, /* remove_queue */
83 default_fd_signaled, /* signaled */
84 no_satisfied, /* satisfied */
85 no_signal, /* signal */
86 file_get_fd, /* get_fd */
87 default_fd_map_access, /* map_access */
88 file_get_sd, /* get_sd */
89 file_set_sd, /* set_sd */
90 no_lookup_name, /* lookup_name */
91 no_open_file, /* open_file */
92 fd_close_handle, /* close_handle */
93 file_destroy /* destroy */
96 static const struct fd_ops file_fd_ops =
98 file_get_poll_events, /* get_poll_events */
99 default_poll_event, /* poll_event */
100 file_get_fd_type, /* get_fd_type */
101 no_fd_read, /* read */
102 no_fd_write, /* write */
103 file_flush, /* flush */
104 default_fd_ioctl, /* ioctl */
105 default_fd_queue_async, /* queue_async */
106 default_fd_reselect_async, /* reselect_async */
107 default_fd_cancel_async /* cancel_async */
110 static inline int is_overlapped( const struct file *file )
112 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
115 /* create a file from a file descriptor */
116 /* if the function fails the fd is closed */
117 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
119 struct file *file;
120 struct stat st;
122 if (fstat( fd, &st ) == -1)
124 file_set_error();
125 close( fd );
126 return NULL;
129 if (!(file = alloc_object( &file_ops )))
131 close( fd );
132 return NULL;
135 file->mode = st.st_mode;
136 file->access = default_fd_map_access( &file->obj, access );
137 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
138 FILE_SYNCHRONOUS_IO_NONALERT )))
140 release_object( file );
141 return NULL;
143 allow_fd_caching( file->fd );
144 return file;
147 /* create a file by duplicating an fd object */
148 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
150 struct file *file;
151 struct stat st;
153 if (fstat( get_unix_fd(fd), &st ) == -1)
155 file_set_error();
156 return NULL;
159 if ((file = alloc_object( &file_ops )))
161 file->mode = st.st_mode;
162 file->access = default_fd_map_access( &file->obj, access );
163 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
165 release_object( file );
166 return NULL;
168 set_fd_user( file->fd, &file_fd_ops, &file->obj );
170 return file;
173 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
175 struct file *file = alloc_object( &file_ops );
177 if (!file) return NULL;
178 file->access = access;
179 file->mode = mode;
180 file->uid = ~(uid_t)0;
181 file->fd = fd;
182 grab_object( fd );
183 set_fd_user( fd, &file_fd_ops, &file->obj );
184 return &file->obj;
187 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
188 unsigned int access, unsigned int sharing, int create,
189 unsigned int options, unsigned int attrs,
190 const struct security_descriptor *sd )
192 struct object *obj = NULL;
193 struct fd *fd;
194 int flags;
195 char *name;
196 mode_t mode;
198 if (!len || ((nameptr[0] == '/') ^ !root))
200 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
201 return NULL;
203 if (!(name = mem_alloc( len + 1 ))) return NULL;
204 memcpy( name, nameptr, len );
205 name[len] = 0;
207 switch(create)
209 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
210 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
211 access |= FILE_WRITE_ATTRIBUTES;
212 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
213 case FILE_OPEN: flags = 0; break;
214 case FILE_OPEN_IF: flags = O_CREAT; break;
215 case FILE_OVERWRITE: flags = O_TRUNC;
216 access |= FILE_WRITE_ATTRIBUTES; break;
217 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
220 if (sd)
222 const SID *owner = sd_get_owner( sd );
223 if (!owner)
224 owner = token_get_user( current->process->token );
225 mode = sd_to_mode( sd, owner );
227 else if (options & FILE_DIRECTORY_FILE)
228 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
229 else
230 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
232 if (len >= 4 &&
233 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
235 if (mode & S_IRUSR)
236 mode |= S_IXUSR;
237 if (mode & S_IRGRP)
238 mode |= S_IXGRP;
239 if (mode & S_IROTH)
240 mode |= S_IXOTH;
243 access = generic_file_map_access( access );
245 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
246 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
247 if (!fd) goto done;
249 if (S_ISDIR(mode))
250 obj = create_dir_obj( fd, access, mode );
251 else if (S_ISCHR(mode) && is_serial_fd( fd ))
252 obj = create_serial( fd );
253 else
254 obj = create_file_obj( fd, access, mode );
256 release_object( fd );
258 done:
259 free( name );
260 return obj;
263 /* check if two file objects point to the same file */
264 int is_same_file( struct file *file1, struct file *file2 )
266 return is_same_file_fd( file1->fd, file2->fd );
269 static void file_dump( struct object *obj, int verbose )
271 struct file *file = (struct file *)obj;
272 assert( obj->ops == &file_ops );
273 fprintf( stderr, "File fd=%p\n", file->fd );
276 static int file_get_poll_events( struct fd *fd )
278 struct file *file = get_fd_user( fd );
279 int events = 0;
280 assert( file->obj.ops == &file_ops );
281 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
282 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
283 return events;
286 static obj_handle_t file_flush( struct fd *fd, const async_data_t *async, int blocking )
288 int unix_fd = get_unix_fd( fd );
289 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
290 return 0;
293 static enum server_fd_type file_get_fd_type( struct fd *fd )
295 struct file *file = get_fd_user( fd );
297 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
298 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
299 return FD_TYPE_CHAR;
302 static struct fd *file_get_fd( struct object *obj )
304 struct file *file = (struct file *)obj;
305 assert( obj->ops == &file_ops );
306 return (struct fd *)grab_object( file->fd );
309 static unsigned int generic_file_map_access( unsigned int access )
311 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
312 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
313 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
314 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
315 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
318 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
320 struct security_descriptor *sd;
321 size_t dacl_size;
322 ACE_HEADER *current_ace;
323 ACCESS_ALLOWED_ACE *aaa;
324 ACL *dacl;
325 SID *sid;
326 char *ptr;
327 const SID *world_sid = security_world_sid;
328 const SID *local_system_sid = security_local_system_sid;
330 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
331 security_sid_len( local_system_sid );
332 if (mode & S_IRWXU)
333 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
334 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
335 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
336 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
337 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
338 if (mode & S_IRWXO)
339 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
341 sd = mem_alloc( sizeof(struct security_descriptor) +
342 security_sid_len( user ) + security_sid_len( group ) +
343 dacl_size );
344 if (!sd) return sd;
346 sd->control = SE_DACL_PRESENT;
347 sd->owner_len = security_sid_len( user );
348 sd->group_len = security_sid_len( group );
349 sd->sacl_len = 0;
350 sd->dacl_len = dacl_size;
352 ptr = (char *)(sd + 1);
353 memcpy( ptr, user, sd->owner_len );
354 ptr += sd->owner_len;
355 memcpy( ptr, group, sd->group_len );
356 ptr += sd->group_len;
358 dacl = (ACL *)ptr;
359 dacl->AclRevision = ACL_REVISION;
360 dacl->Sbz1 = 0;
361 dacl->AclSize = dacl_size;
362 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
363 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
364 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
365 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
366 dacl->AceCount++;
367 dacl->Sbz2 = 0;
369 /* always give FILE_ALL_ACCESS for Local System */
370 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
371 current_ace = &aaa->Header;
372 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
373 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
374 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
375 aaa->Mask = FILE_ALL_ACCESS;
376 sid = (SID *)&aaa->SidStart;
377 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
379 if (mode & S_IRWXU)
381 /* appropriate access rights for the user */
382 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
383 current_ace = &aaa->Header;
384 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
385 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
386 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
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, security_sid_len( user ));
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 = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
404 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
405 ada->Mask = 0;
406 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
407 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
408 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
409 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
410 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
411 sid = (SID *)&ada->SidStart;
412 memcpy( sid, user, security_sid_len( user ));
414 if (mode & S_IRWXO)
416 /* appropriate access rights for Everyone */
417 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
418 current_ace = &aaa->Header;
419 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
420 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
421 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
422 aaa->Mask = 0;
423 if (mode & S_IROTH)
424 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
425 if (mode & S_IWOTH)
426 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
427 sid = (SID *)&aaa->SidStart;
428 memcpy( sid, world_sid, security_sid_len( world_sid ));
431 return sd;
434 static struct security_descriptor *file_get_sd( struct object *obj )
436 struct file *file = (struct file *)obj;
437 struct stat st;
438 int unix_fd;
439 struct security_descriptor *sd;
441 assert( obj->ops == &file_ops );
443 unix_fd = get_file_unix_fd( file );
445 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
446 return obj->sd;
448 /* mode and uid the same? if so, no need to re-generate security descriptor */
449 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
450 (st.st_uid == file->uid))
451 return obj->sd;
453 sd = mode_to_sd( st.st_mode,
454 security_unix_uid_to_sid( st.st_uid ),
455 token_get_primary_group( current->process->token ));
456 if (!sd) return obj->sd;
458 file->mode = st.st_mode;
459 file->uid = st.st_uid;
460 free( obj->sd );
461 obj->sd = sd;
462 return sd;
465 static mode_t file_access_to_mode( unsigned int access )
467 mode_t mode = 0;
469 access = generic_file_map_access( access );
470 if (access & FILE_READ_DATA) mode |= 4;
471 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
472 if (access & FILE_EXECUTE) mode |= 1;
473 return mode;
476 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
478 mode_t new_mode = 0;
479 mode_t bits_to_set = ~0;
480 mode_t mode;
481 int present;
482 const ACL *dacl = sd_get_dacl( sd, &present );
483 const SID *user = token_get_user( current->process->token );
484 if (present && dacl)
486 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
487 ULONG i;
488 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
490 const ACCESS_ALLOWED_ACE *aa_ace;
491 const ACCESS_DENIED_ACE *ad_ace;
492 const SID *sid;
494 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
496 switch (ace->AceType)
498 case ACCESS_DENIED_ACE_TYPE:
499 ad_ace = (const ACCESS_DENIED_ACE *)ace;
500 sid = (const SID *)&ad_ace->SidStart;
501 mode = file_access_to_mode( ad_ace->Mask );
502 if (security_equal_sid( sid, security_world_sid ))
504 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
506 else if ((security_equal_sid( user, owner ) &&
507 token_sid_present( current->process->token, sid, TRUE )))
509 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
511 else if (security_equal_sid( sid, owner ))
513 bits_to_set &= ~(mode << 6); /* user only */
515 break;
516 case ACCESS_ALLOWED_ACE_TYPE:
517 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
518 sid = (const SID *)&aa_ace->SidStart;
519 mode = file_access_to_mode( aa_ace->Mask );
520 if (security_equal_sid( sid, security_world_sid ))
522 mode = (mode << 6) | (mode << 3) | mode; /* all */
523 new_mode |= mode & bits_to_set;
524 bits_to_set &= ~mode;
526 else if ((security_equal_sid( user, owner ) &&
527 token_sid_present( current->process->token, sid, FALSE )))
529 mode = (mode << 6) | (mode << 3); /* user + group */
530 new_mode |= mode & bits_to_set;
531 bits_to_set &= ~mode;
533 else if (security_equal_sid( sid, owner ))
535 mode = (mode << 6); /* user only */
536 new_mode |= mode & bits_to_set;
537 bits_to_set &= ~mode;
539 break;
543 else
544 /* no ACL means full access rights to anyone */
545 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
547 return new_mode;
550 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
551 unsigned int set_info )
553 struct file *file = (struct file *)obj;
554 const SID *owner;
555 struct stat st;
556 mode_t mode;
557 int unix_fd;
559 assert( obj->ops == &file_ops );
561 unix_fd = get_file_unix_fd( file );
563 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
565 if (set_info & OWNER_SECURITY_INFORMATION)
567 owner = sd_get_owner( sd );
568 if (!owner)
570 set_error( STATUS_INVALID_SECURITY_DESCR );
571 return 0;
573 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
575 /* FIXME: get Unix uid and call fchown */
578 else if (obj->sd)
579 owner = sd_get_owner( obj->sd );
580 else
581 owner = token_get_user( current->process->token );
583 /* group and sacl not supported */
585 if (set_info & DACL_SECURITY_INFORMATION)
587 /* keep the bits that we don't map to access rights in the ACL */
588 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
589 mode |= sd_to_mode( sd, owner );
591 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
593 file_set_error();
594 return 0;
597 return 1;
600 static void file_destroy( struct object *obj )
602 struct file *file = (struct file *)obj;
603 assert( obj->ops == &file_ops );
605 if (file->fd) release_object( file->fd );
608 /* set the last error depending on errno */
609 void file_set_error(void)
611 switch (errno)
613 case ETXTBSY:
614 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
615 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
616 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
617 case EACCES:
618 case ESRCH:
619 case EROFS:
620 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
621 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
622 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
623 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
624 case ENFILE:
625 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
626 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
627 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
628 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
629 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
630 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
631 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
632 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
633 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
634 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
635 #ifdef EOVERFLOW
636 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
637 #endif
638 default:
639 perror("wineserver: file_set_error() can't map error");
640 set_error( STATUS_UNSUCCESSFUL );
641 break;
645 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
647 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
650 int get_file_unix_fd( struct file *file )
652 return get_unix_fd( file->fd );
655 /* create a file */
656 DECL_HANDLER(create_file)
658 struct object *file;
659 struct fd *root_fd = NULL;
660 const struct object_attributes *objattr = get_req_data();
661 const struct security_descriptor *sd;
662 const char *name;
663 data_size_t name_len;
665 reply->handle = 0;
667 if (!objattr_is_valid( objattr, get_req_data_size() ))
668 return;
669 /* name is transferred in the unix codepage outside of the objattr structure */
670 if (objattr->name_len)
672 set_error( STATUS_INVALID_PARAMETER );
673 return;
676 if (objattr->rootdir)
678 struct dir *root;
680 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
681 root_fd = get_obj_fd( (struct object *)root );
682 release_object( root );
683 if (!root_fd) return;
686 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
688 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
689 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
691 reply->handle = 0;
692 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
693 req->create, req->options, req->attrs, sd )))
695 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
696 release_object( file );
698 if (root_fd) release_object( root_fd );
701 /* allocate a file handle for a Unix fd */
702 DECL_HANDLER(alloc_file_handle)
704 struct file *file;
705 int fd;
707 reply->handle = 0;
708 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
710 set_error( STATUS_INVALID_HANDLE );
711 return;
713 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
715 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
716 release_object( file );
720 /* lock a region of a file */
721 DECL_HANDLER(lock_file)
723 struct file *file;
725 if ((file = get_file_obj( current->process, req->handle, 0 )))
727 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
728 reply->overlapped = is_overlapped( file );
729 release_object( file );
733 /* unlock a region of a file */
734 DECL_HANDLER(unlock_file)
736 struct file *file;
738 if ((file = get_file_obj( current->process, req->handle, 0 )))
740 unlock_fd( file->fd, req->offset, req->count );
741 release_object( file );