user32/tests: Don't test function directly when reporting GetLastError().
[wine/multimedia.git] / server / file.c
blobabda2c38e24b7f0f92dd5eb5d09b063a18b81faa
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 void file_flush( struct fd *fd, struct event **event );
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_flush, /* flush */
101 file_get_fd_type, /* get_fd_type */
102 default_fd_ioctl, /* ioctl */
103 default_fd_queue_async, /* queue_async */
104 default_fd_reselect_async, /* reselect_async */
105 default_fd_cancel_async /* cancel_async */
108 static inline int is_overlapped( const struct file *file )
110 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
113 /* create a file from a file descriptor */
114 /* if the function fails the fd is closed */
115 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
117 struct file *file;
118 struct stat st;
120 if (fstat( fd, &st ) == -1)
122 file_set_error();
123 close( fd );
124 return NULL;
127 if (!(file = alloc_object( &file_ops )))
129 close( fd );
130 return NULL;
133 file->mode = st.st_mode;
134 file->access = default_fd_map_access( &file->obj, access );
135 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
136 FILE_SYNCHRONOUS_IO_NONALERT )))
138 release_object( file );
139 return NULL;
141 allow_fd_caching( file->fd );
142 return file;
145 /* create a file by duplicating an fd object */
146 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
148 struct file *file;
149 struct stat st;
151 if (fstat( get_unix_fd(fd), &st ) == -1)
153 file_set_error();
154 return NULL;
157 if ((file = alloc_object( &file_ops )))
159 file->mode = st.st_mode;
160 file->access = default_fd_map_access( &file->obj, access );
161 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
163 release_object( file );
164 return NULL;
166 set_fd_user( file->fd, &file_fd_ops, &file->obj );
168 return file;
171 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
173 struct file *file = alloc_object( &file_ops );
175 if (!file) return NULL;
176 file->access = access;
177 file->mode = mode;
178 file->uid = ~(uid_t)0;
179 file->fd = fd;
180 grab_object( fd );
181 set_fd_user( fd, &file_fd_ops, &file->obj );
182 return &file->obj;
185 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
186 unsigned int access, unsigned int sharing, int create,
187 unsigned int options, unsigned int attrs,
188 const struct security_descriptor *sd )
190 struct object *obj = NULL;
191 struct fd *fd;
192 int flags;
193 char *name;
194 mode_t mode;
196 if (!len || ((nameptr[0] == '/') ^ !root))
198 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
199 return NULL;
201 if (!(name = mem_alloc( len + 1 ))) return NULL;
202 memcpy( name, nameptr, len );
203 name[len] = 0;
205 switch(create)
207 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
208 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
209 access |= FILE_WRITE_ATTRIBUTES;
210 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
211 case FILE_OPEN: flags = 0; break;
212 case FILE_OPEN_IF: flags = O_CREAT; break;
213 case FILE_OVERWRITE: flags = O_TRUNC;
214 access |= FILE_WRITE_ATTRIBUTES; break;
215 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
218 if (sd)
220 const SID *owner = sd_get_owner( sd );
221 if (!owner)
222 owner = token_get_user( current->process->token );
223 mode = sd_to_mode( sd, owner );
225 else if (options & FILE_DIRECTORY_FILE)
226 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
227 else
228 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
230 if (len >= 4 &&
231 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
233 if (mode & S_IRUSR)
234 mode |= S_IXUSR;
235 if (mode & S_IRGRP)
236 mode |= S_IXGRP;
237 if (mode & S_IROTH)
238 mode |= S_IXOTH;
241 access = generic_file_map_access( access );
243 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
244 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
245 if (!fd) goto done;
247 if (S_ISDIR(mode))
248 obj = create_dir_obj( fd, access, mode );
249 else if (S_ISCHR(mode) && is_serial_fd( fd ))
250 obj = create_serial( fd );
251 else
252 obj = create_file_obj( fd, access, mode );
254 release_object( fd );
256 done:
257 free( name );
258 return obj;
261 /* check if two file objects point to the same file */
262 int is_same_file( struct file *file1, struct file *file2 )
264 return is_same_file_fd( file1->fd, file2->fd );
267 static void file_dump( struct object *obj, int verbose )
269 struct file *file = (struct file *)obj;
270 assert( obj->ops == &file_ops );
271 fprintf( stderr, "File fd=%p\n", file->fd );
274 static int file_get_poll_events( struct fd *fd )
276 struct file *file = get_fd_user( fd );
277 int events = 0;
278 assert( file->obj.ops == &file_ops );
279 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
280 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
281 return events;
284 static void file_flush( struct fd *fd, struct event **event )
286 int unix_fd = get_unix_fd( fd );
287 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
290 static enum server_fd_type file_get_fd_type( struct fd *fd )
292 struct file *file = get_fd_user( fd );
294 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
295 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
296 return FD_TYPE_CHAR;
299 static struct fd *file_get_fd( struct object *obj )
301 struct file *file = (struct file *)obj;
302 assert( obj->ops == &file_ops );
303 return (struct fd *)grab_object( file->fd );
306 static unsigned int generic_file_map_access( unsigned int access )
308 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
309 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
310 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
311 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
312 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
315 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
317 struct security_descriptor *sd;
318 size_t dacl_size;
319 ACE_HEADER *current_ace;
320 ACCESS_ALLOWED_ACE *aaa;
321 ACL *dacl;
322 SID *sid;
323 char *ptr;
324 const SID *world_sid = security_world_sid;
325 const SID *local_system_sid = security_local_system_sid;
327 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
328 security_sid_len( local_system_sid );
329 if (mode & S_IRWXU)
330 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
331 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
332 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
333 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
334 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
335 if (mode & S_IRWXO)
336 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
338 sd = mem_alloc( sizeof(struct security_descriptor) +
339 security_sid_len( user ) + security_sid_len( group ) +
340 dacl_size );
341 if (!sd) return sd;
343 sd->control = SE_DACL_PRESENT;
344 sd->owner_len = security_sid_len( user );
345 sd->group_len = security_sid_len( group );
346 sd->sacl_len = 0;
347 sd->dacl_len = dacl_size;
349 ptr = (char *)(sd + 1);
350 memcpy( ptr, user, sd->owner_len );
351 ptr += sd->owner_len;
352 memcpy( ptr, group, sd->group_len );
353 ptr += sd->group_len;
355 dacl = (ACL *)ptr;
356 dacl->AclRevision = ACL_REVISION;
357 dacl->Sbz1 = 0;
358 dacl->AclSize = dacl_size;
359 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
360 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
361 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
362 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
363 dacl->AceCount++;
364 dacl->Sbz2 = 0;
366 /* always give FILE_ALL_ACCESS for Local System */
367 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
368 current_ace = &aaa->Header;
369 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
370 aaa->Header.AceFlags = 0;
371 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
372 aaa->Mask = FILE_ALL_ACCESS;
373 sid = (SID *)&aaa->SidStart;
374 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
376 if (mode & S_IRWXU)
378 /* appropriate access rights for the user */
379 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
380 current_ace = &aaa->Header;
381 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
382 aaa->Header.AceFlags = 0;
383 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
384 aaa->Mask = WRITE_DAC | WRITE_OWNER;
385 if (mode & S_IRUSR)
386 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
387 if (mode & S_IWUSR)
388 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
389 sid = (SID *)&aaa->SidStart;
390 memcpy( sid, user, security_sid_len( user ));
392 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
393 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
394 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
396 /* deny just in case the user is a member of the group */
397 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
398 current_ace = &ada->Header;
399 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
400 ada->Header.AceFlags = 0;
401 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
402 ada->Mask = 0;
403 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
404 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
405 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
406 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
407 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
408 sid = (SID *)&ada->SidStart;
409 memcpy( sid, user, security_sid_len( user ));
411 if (mode & S_IRWXO)
413 /* appropriate access rights for Everyone */
414 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
415 current_ace = &aaa->Header;
416 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
417 aaa->Header.AceFlags = 0;
418 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
419 aaa->Mask = 0;
420 if (mode & S_IROTH)
421 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
422 if (mode & S_IWOTH)
423 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
424 sid = (SID *)&aaa->SidStart;
425 memcpy( sid, world_sid, security_sid_len( world_sid ));
428 return sd;
431 static struct security_descriptor *file_get_sd( struct object *obj )
433 struct file *file = (struct file *)obj;
434 struct stat st;
435 int unix_fd;
436 struct security_descriptor *sd;
438 assert( obj->ops == &file_ops );
440 unix_fd = get_file_unix_fd( file );
442 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
443 return obj->sd;
445 /* mode and uid the same? if so, no need to re-generate security descriptor */
446 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
447 (st.st_uid == file->uid))
448 return obj->sd;
450 sd = mode_to_sd( st.st_mode,
451 security_unix_uid_to_sid( st.st_uid ),
452 token_get_primary_group( current->process->token ));
453 if (!sd) return obj->sd;
455 file->mode = st.st_mode;
456 file->uid = st.st_uid;
457 free( obj->sd );
458 obj->sd = sd;
459 return sd;
462 static mode_t file_access_to_mode( unsigned int access )
464 mode_t mode = 0;
466 access = generic_file_map_access( access );
467 if (access & FILE_READ_DATA) mode |= 4;
468 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
469 if (access & FILE_EXECUTE) mode |= 1;
470 return mode;
473 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
475 mode_t new_mode = 0;
476 mode_t denied_mode = 0;
477 mode_t mode;
478 int present;
479 const ACL *dacl = sd_get_dacl( sd, &present );
480 const SID *user = token_get_user( current->process->token );
481 if (present && dacl)
483 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
484 ULONG i;
485 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
487 const ACCESS_ALLOWED_ACE *aa_ace;
488 const ACCESS_DENIED_ACE *ad_ace;
489 const SID *sid;
491 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
493 switch (ace->AceType)
495 case ACCESS_DENIED_ACE_TYPE:
496 ad_ace = (const ACCESS_DENIED_ACE *)ace;
497 sid = (const SID *)&ad_ace->SidStart;
498 mode = file_access_to_mode( ad_ace->Mask );
499 if (security_equal_sid( sid, security_world_sid ))
501 denied_mode |= (mode << 6) | (mode << 3) | mode; /* all */
503 else if ((security_equal_sid( user, owner ) &&
504 token_sid_present( current->process->token, sid, TRUE )))
506 denied_mode |= (mode << 6) | (mode << 3); /* user + group */
508 else if (security_equal_sid( sid, owner ))
510 denied_mode |= (mode << 6); /* user only */
512 break;
513 case ACCESS_ALLOWED_ACE_TYPE:
514 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
515 sid = (const SID *)&aa_ace->SidStart;
516 mode = file_access_to_mode( aa_ace->Mask );
517 if (security_equal_sid( sid, security_world_sid ))
519 new_mode |= (mode << 6) | (mode << 3) | mode; /* all */
521 else if ((security_equal_sid( user, owner ) &&
522 token_sid_present( current->process->token, sid, FALSE )))
524 new_mode |= (mode << 6) | (mode << 3); /* user + group */
526 else if (security_equal_sid( sid, owner ))
528 new_mode |= (mode << 6); /* user only */
530 break;
534 else
535 /* no ACL means full access rights to anyone */
536 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
538 return new_mode & ~denied_mode;
541 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
542 unsigned int set_info )
544 struct file *file = (struct file *)obj;
545 const SID *owner;
546 struct stat st;
547 mode_t mode;
548 int unix_fd;
550 assert( obj->ops == &file_ops );
552 unix_fd = get_file_unix_fd( file );
554 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
556 if (set_info & OWNER_SECURITY_INFORMATION)
558 owner = sd_get_owner( sd );
559 if (!owner)
561 set_error( STATUS_INVALID_SECURITY_DESCR );
562 return 0;
564 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
566 /* FIXME: get Unix uid and call fchown */
569 else if (obj->sd)
570 owner = sd_get_owner( obj->sd );
571 else
572 owner = token_get_user( current->process->token );
574 /* group and sacl not supported */
576 if (set_info & DACL_SECURITY_INFORMATION)
578 /* keep the bits that we don't map to access rights in the ACL */
579 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
580 mode |= sd_to_mode( sd, owner );
582 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
584 file_set_error();
585 return 0;
588 return 1;
591 static void file_destroy( struct object *obj )
593 struct file *file = (struct file *)obj;
594 assert( obj->ops == &file_ops );
596 if (file->fd) release_object( file->fd );
599 /* set the last error depending on errno */
600 void file_set_error(void)
602 switch (errno)
604 case ETXTBSY:
605 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
606 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
607 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
608 case EACCES:
609 case ESRCH:
610 case EROFS:
611 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
612 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
613 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
614 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
615 case ENFILE:
616 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
617 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
618 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
619 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
620 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
621 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
622 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
623 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
624 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
625 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
626 #ifdef EOVERFLOW
627 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
628 #endif
629 default:
630 perror("wineserver: file_set_error() can't map error");
631 set_error( STATUS_UNSUCCESSFUL );
632 break;
636 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
638 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
641 int get_file_unix_fd( struct file *file )
643 return get_unix_fd( file->fd );
646 /* create a file */
647 DECL_HANDLER(create_file)
649 struct object *file;
650 struct fd *root_fd = NULL;
651 const struct object_attributes *objattr = get_req_data();
652 const struct security_descriptor *sd;
653 const char *name;
654 data_size_t name_len;
656 reply->handle = 0;
658 if (!objattr_is_valid( objattr, get_req_data_size() ))
659 return;
660 /* name is transferred in the unix codepage outside of the objattr structure */
661 if (objattr->name_len)
663 set_error( STATUS_INVALID_PARAMETER );
664 return;
667 if (objattr->rootdir)
669 struct dir *root;
671 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
672 root_fd = get_obj_fd( (struct object *)root );
673 release_object( root );
674 if (!root_fd) return;
677 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
679 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
680 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
682 reply->handle = 0;
683 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
684 req->create, req->options, req->attrs, sd )))
686 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
687 release_object( file );
689 if (root_fd) release_object( root_fd );
692 /* allocate a file handle for a Unix fd */
693 DECL_HANDLER(alloc_file_handle)
695 struct file *file;
696 int fd;
698 reply->handle = 0;
699 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
701 set_error( STATUS_INVALID_HANDLE );
702 return;
704 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
706 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
707 release_object( file );
711 /* lock a region of a file */
712 DECL_HANDLER(lock_file)
714 struct file *file;
716 if ((file = get_file_obj( current->process, req->handle, 0 )))
718 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
719 reply->overlapped = is_overlapped( file );
720 release_object( file );
724 /* unlock a region of a file */
725 DECL_HANDLER(unlock_file)
727 struct file *file;
729 if ((file = get_file_obj( current->process, req->handle, 0 )))
731 unlock_fd( file->fd, req->offset, req->count );
732 release_object( file );