wined3d: Correctly translate between coordinate systems in swapchain_blit().
[wine/hramrach.git] / server / file.c
blob105c3396728ec4d4f74b773245baab40753a9391
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;
140 return file;
143 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
145 struct file *file = alloc_object( &file_ops );
147 if (!file) return NULL;
148 file->access = access;
149 file->mode = mode;
150 file->uid = ~(uid_t)0;
151 file->fd = fd;
152 grab_object( fd );
153 set_fd_user( fd, &file_fd_ops, &file->obj );
154 return &file->obj;
157 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
158 unsigned int access, unsigned int sharing, int create,
159 unsigned int options, unsigned int attrs,
160 const struct security_descriptor *sd )
162 struct object *obj = NULL;
163 struct fd *fd;
164 int flags;
165 char *name;
166 mode_t mode;
168 if (!len || ((nameptr[0] == '/') ^ !root))
170 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
171 return NULL;
173 if (!(name = mem_alloc( len + 1 ))) return NULL;
174 memcpy( name, nameptr, len );
175 name[len] = 0;
177 switch(create)
179 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
180 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
181 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
182 case FILE_OPEN: flags = 0; break;
183 case FILE_OPEN_IF: flags = O_CREAT; break;
184 case FILE_OVERWRITE: flags = O_TRUNC; break;
185 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
188 if (sd)
190 const SID *owner = sd_get_owner( sd );
191 if (!owner)
192 owner = token_get_user( current->process->token );
193 mode = sd_to_mode( sd, owner );
195 else
196 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
198 if (len >= 4 &&
199 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
201 if (mode & S_IRUSR)
202 mode |= S_IXUSR;
203 if (mode & S_IRGRP)
204 mode |= S_IXGRP;
205 if (mode & S_IROTH)
206 mode |= S_IXOTH;
209 access = generic_file_map_access( access );
211 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
212 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
213 if (!fd) goto done;
215 if (S_ISDIR(mode))
216 obj = create_dir_obj( fd, access, mode );
217 else if (S_ISCHR(mode) && is_serial_fd( fd ))
218 obj = create_serial( fd );
219 else
220 obj = create_file_obj( fd, access, mode );
222 release_object( fd );
224 done:
225 free( name );
226 return obj;
229 /* check if two file objects point to the same file */
230 int is_same_file( struct file *file1, struct file *file2 )
232 return is_same_file_fd( file1->fd, file2->fd );
235 static void file_dump( struct object *obj, int verbose )
237 struct file *file = (struct file *)obj;
238 assert( obj->ops == &file_ops );
239 fprintf( stderr, "File fd=%p\n", file->fd );
242 static int file_get_poll_events( struct fd *fd )
244 struct file *file = get_fd_user( fd );
245 int events = 0;
246 assert( file->obj.ops == &file_ops );
247 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
248 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
249 return events;
252 static void file_flush( struct fd *fd, struct event **event )
254 int unix_fd = get_unix_fd( fd );
255 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
258 static enum server_fd_type file_get_fd_type( struct fd *fd )
260 struct file *file = get_fd_user( fd );
262 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
263 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
264 return FD_TYPE_CHAR;
267 static struct fd *file_get_fd( struct object *obj )
269 struct file *file = (struct file *)obj;
270 assert( obj->ops == &file_ops );
271 return (struct fd *)grab_object( file->fd );
274 static unsigned int generic_file_map_access( unsigned int access )
276 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
277 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
278 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
279 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
280 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
283 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
285 struct security_descriptor *sd;
286 size_t dacl_size;
287 ACE_HEADER *current_ace;
288 ACCESS_ALLOWED_ACE *aaa;
289 ACL *dacl;
290 SID *sid;
291 char *ptr;
292 const SID *world_sid = security_world_sid;
293 const SID *local_system_sid = security_local_system_sid;
295 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
296 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
297 if (mode & S_IRWXU)
298 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
299 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
300 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
301 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
302 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
303 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
304 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
305 if (mode & S_IRWXO)
306 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
307 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
309 sd = mem_alloc( sizeof(struct security_descriptor) +
310 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
311 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
312 dacl_size );
313 if (!sd) return sd;
315 sd->control = SE_DACL_PRESENT;
316 sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
317 sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
318 sd->sacl_len = 0;
319 sd->dacl_len = dacl_size;
321 ptr = (char *)(sd + 1);
322 memcpy( ptr, user, sd->owner_len );
323 ptr += sd->owner_len;
324 memcpy( ptr, group, sd->group_len );
325 ptr += sd->group_len;
327 dacl = (ACL *)ptr;
328 dacl->AclRevision = ACL_REVISION;
329 dacl->Sbz1 = 0;
330 dacl->AclSize = dacl_size;
331 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
332 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
333 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
334 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
335 dacl->AceCount++;
336 dacl->Sbz2 = 0;
338 /* always give FILE_ALL_ACCESS for Local System */
339 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
340 current_ace = &aaa->Header;
341 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
342 aaa->Header.AceFlags = 0;
343 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
344 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
345 aaa->Mask = FILE_ALL_ACCESS;
346 sid = (SID *)&aaa->SidStart;
347 memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
349 if (mode & S_IRWXU)
351 /* appropriate access rights for the user */
352 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
353 current_ace = &aaa->Header;
354 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
355 aaa->Header.AceFlags = 0;
356 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
357 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
358 aaa->Mask = WRITE_DAC | WRITE_OWNER;
359 if (mode & S_IRUSR)
360 aaa->Mask |= FILE_GENERIC_READ;
361 if (mode & S_IWUSR)
362 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
363 if (mode & S_IXUSR)
364 aaa->Mask |= FILE_GENERIC_EXECUTE;
365 sid = (SID *)&aaa->SidStart;
366 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
368 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
369 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
370 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
372 /* deny just in case the user is a member of the group */
373 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
374 current_ace = &ada->Header;
375 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
376 ada->Header.AceFlags = 0;
377 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
378 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
379 ada->Mask = 0;
380 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
381 ada->Mask |= FILE_GENERIC_READ;
382 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
383 ada->Mask |= FILE_GENERIC_WRITE | DELETE;
384 if (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))
385 ada->Mask |= FILE_GENERIC_EXECUTE;
386 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
387 sid = (SID *)&ada->SidStart;
388 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
390 if (mode & S_IRWXO)
392 /* appropriate access rights for Everyone */
393 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
394 current_ace = &aaa->Header;
395 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
396 aaa->Header.AceFlags = 0;
397 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
398 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
399 aaa->Mask = 0;
400 if (mode & S_IROTH)
401 aaa->Mask |= FILE_GENERIC_READ;
402 if (mode & S_IWOTH)
403 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
404 if (mode & S_IXOTH)
405 aaa->Mask |= FILE_GENERIC_EXECUTE;
406 sid = (SID *)&aaa->SidStart;
407 memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
410 return sd;
413 static struct security_descriptor *file_get_sd( struct object *obj )
415 struct file *file = (struct file *)obj;
416 struct stat st;
417 int unix_fd;
418 struct security_descriptor *sd;
420 assert( obj->ops == &file_ops );
422 unix_fd = get_file_unix_fd( file );
424 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
425 return obj->sd;
427 /* mode and uid the same? if so, no need to re-generate security descriptor */
428 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
429 (st.st_uid == file->uid))
430 return obj->sd;
432 sd = mode_to_sd( st.st_mode,
433 security_unix_uid_to_sid( st.st_uid ),
434 token_get_primary_group( current->process->token ));
435 if (!sd) return obj->sd;
437 file->mode = st.st_mode;
438 file->uid = st.st_uid;
439 free( obj->sd );
440 obj->sd = sd;
441 return sd;
444 static mode_t file_access_to_mode( unsigned int access )
446 mode_t mode = 0;
448 access = generic_file_map_access( access );
449 if (access & FILE_READ_DATA) mode |= 4;
450 if (access & FILE_WRITE_DATA) mode |= 2;
451 if (access & FILE_EXECUTE) mode |= 1;
452 return mode;
455 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
457 mode_t new_mode = 0;
458 mode_t denied_mode = 0;
459 mode_t mode;
460 int present;
461 const ACL *dacl = sd_get_dacl( sd, &present );
462 const SID *user = token_get_user( current->process->token );
463 if (present && dacl)
465 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
466 ULONG i;
467 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
469 const ACCESS_ALLOWED_ACE *aa_ace;
470 const ACCESS_DENIED_ACE *ad_ace;
471 const SID *sid;
473 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
475 switch (ace->AceType)
477 case ACCESS_DENIED_ACE_TYPE:
478 ad_ace = (const ACCESS_DENIED_ACE *)ace;
479 sid = (const SID *)&ad_ace->SidStart;
480 mode = file_access_to_mode( ad_ace->Mask );
481 if (security_equal_sid( sid, security_world_sid ))
483 denied_mode |= (mode << 6) | (mode << 3) | mode; /* all */
485 else if (security_equal_sid( sid, owner ))
487 denied_mode |= (mode << 6); /* user only */
489 else if ((security_equal_sid( user, owner ) &&
490 token_sid_present( current->process->token, sid, TRUE )))
492 denied_mode |= (mode << 6) | (mode << 3); /* user + group */
494 break;
495 case ACCESS_ALLOWED_ACE_TYPE:
496 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
497 sid = (const SID *)&aa_ace->SidStart;
498 mode = file_access_to_mode( aa_ace->Mask );
499 if (security_equal_sid( sid, security_world_sid ))
501 new_mode |= (mode << 6) | (mode << 3) | mode; /* all */
503 else if (security_equal_sid( sid, owner ))
505 new_mode |= (mode << 6); /* user only */
507 else if ((security_equal_sid( user, owner ) &&
508 token_sid_present( current->process->token, sid, FALSE )))
510 new_mode |= (mode << 6) | (mode << 3); /* user + group */
512 break;
516 else
517 /* no ACL means full access rights to anyone */
518 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
520 return new_mode & ~denied_mode;
523 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
524 unsigned int set_info )
526 struct file *file = (struct file *)obj;
527 const SID *owner;
528 struct stat st;
529 mode_t mode;
530 int unix_fd;
532 assert( obj->ops == &file_ops );
534 unix_fd = get_file_unix_fd( file );
536 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
538 if (set_info & OWNER_SECURITY_INFORMATION)
540 owner = sd_get_owner( sd );
541 if (!owner)
543 set_error( STATUS_INVALID_SECURITY_DESCR );
544 return 0;
546 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
548 /* FIXME: get Unix uid and call fchown */
551 else if (obj->sd)
552 owner = sd_get_owner( obj->sd );
553 else
554 owner = token_get_user( current->process->token );
556 /* group and sacl not supported */
558 if (set_info & DACL_SECURITY_INFORMATION)
560 /* keep the bits that we don't map to access rights in the ACL */
561 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
562 mode |= sd_to_mode( sd, owner );
564 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
566 file_set_error();
567 return 0;
570 return 1;
573 static void file_destroy( struct object *obj )
575 struct file *file = (struct file *)obj;
576 assert( obj->ops == &file_ops );
578 if (file->fd) release_object( file->fd );
581 /* set the last error depending on errno */
582 void file_set_error(void)
584 switch (errno)
586 case ETXTBSY:
587 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
588 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
589 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
590 case EACCES:
591 case ESRCH:
592 case EROFS:
593 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
594 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
595 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
596 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
597 case ENFILE:
598 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
599 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
600 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
601 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
602 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
603 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
604 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
605 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
606 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
607 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
608 #ifdef EOVERFLOW
609 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
610 #endif
611 default:
612 perror("wineserver: file_set_error() can't map error");
613 set_error( STATUS_UNSUCCESSFUL );
614 break;
618 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
620 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
623 int get_file_unix_fd( struct file *file )
625 return get_unix_fd( file->fd );
628 struct file *grab_file_unless_removable( struct file *file )
630 if (is_fd_removable( file->fd )) return NULL;
631 return (struct file *)grab_object( file );
634 /* create a file */
635 DECL_HANDLER(create_file)
637 struct object *file;
638 struct fd *root_fd = NULL;
639 const struct object_attributes *objattr = get_req_data();
640 const struct security_descriptor *sd;
641 const char *name;
642 data_size_t name_len;
644 reply->handle = 0;
646 if (!objattr_is_valid( objattr, get_req_data_size() ))
647 return;
648 /* name is transferred in the unix codepage outside of the objattr structure */
649 if (objattr->name_len)
651 set_error( STATUS_INVALID_PARAMETER );
652 return;
655 if (objattr->rootdir)
657 struct dir *root;
659 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
660 root_fd = get_obj_fd( (struct object *)root );
661 release_object( root );
662 if (!root_fd) return;
665 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
667 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
668 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
670 reply->handle = 0;
671 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
672 req->create, req->options, req->attrs, sd )))
674 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
675 release_object( file );
677 if (root_fd) release_object( root_fd );
680 /* allocate a file handle for a Unix fd */
681 DECL_HANDLER(alloc_file_handle)
683 struct file *file;
684 int fd;
686 reply->handle = 0;
687 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
689 set_error( STATUS_INVALID_HANDLE );
690 return;
692 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
694 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
695 release_object( file );
699 /* lock a region of a file */
700 DECL_HANDLER(lock_file)
702 struct file *file;
704 if ((file = get_file_obj( current->process, req->handle, 0 )))
706 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
707 reply->overlapped = is_overlapped( file );
708 release_object( file );
712 /* unlock a region of a file */
713 DECL_HANDLER(unlock_file)
715 struct file *file;
717 if ((file = get_file_obj( current->process, req->handle, 0 )))
719 unlock_fd( file->fd, req->offset, req->count );
720 release_object( file );