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
22 #include "wine/port.h"
33 #include <sys/types.h>
44 #define WIN32_NO_STATUS
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 struct object
*file_open_file( struct object
*obj
, unsigned int access
,
72 unsigned int sharing
, unsigned int options
);
73 static void file_destroy( struct object
*obj
);
75 static int file_get_poll_events( struct fd
*fd
);
76 static obj_handle_t
file_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
);
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 */
83 file_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 file_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_get_fd_type
, /* get_fd_type */
104 no_fd_read
, /* read */
105 no_fd_write
, /* write */
106 file_flush
, /* flush */
107 default_fd_ioctl
, /* ioctl */
108 default_fd_queue_async
, /* queue_async */
109 default_fd_reselect_async
, /* reselect_async */
110 default_fd_cancel_async
/* cancel_async */
113 static inline int is_overlapped( const struct file
*file
)
115 return !(get_fd_options( file
->fd
) & (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
118 /* create a file from a file descriptor */
119 /* if the function fails the fd is closed */
120 struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
)
125 if (fstat( fd
, &st
) == -1)
132 if (!(file
= alloc_object( &file_ops
)))
138 file
->mode
= st
.st_mode
;
139 file
->access
= default_fd_map_access( &file
->obj
, access
);
140 if (!(file
->fd
= create_anonymous_fd( &file_fd_ops
, fd
, &file
->obj
,
141 FILE_SYNCHRONOUS_IO_NONALERT
)))
143 release_object( file
);
146 allow_fd_caching( file
->fd
);
150 /* create a file by duplicating an fd object */
151 struct file
*create_file_for_fd_obj( struct fd
*fd
, unsigned int access
, unsigned int sharing
)
156 if (fstat( get_unix_fd(fd
), &st
) == -1)
162 if ((file
= alloc_object( &file_ops
)))
164 file
->mode
= st
.st_mode
;
165 file
->access
= default_fd_map_access( &file
->obj
, access
);
166 if (!(file
->fd
= dup_fd_object( fd
, access
, sharing
, FILE_SYNCHRONOUS_IO_NONALERT
)))
168 release_object( file
);
171 set_fd_user( file
->fd
, &file_fd_ops
, &file
->obj
);
176 static struct object
*create_file_obj( struct fd
*fd
, unsigned int access
, mode_t mode
)
178 struct file
*file
= alloc_object( &file_ops
);
180 if (!file
) return NULL
;
181 file
->access
= access
;
183 file
->uid
= ~(uid_t
)0;
186 set_fd_user( fd
, &file_fd_ops
, &file
->obj
);
190 static struct object
*create_file( struct fd
*root
, const char *nameptr
, data_size_t len
,
191 unsigned int access
, unsigned int sharing
, int create
,
192 unsigned int options
, unsigned int attrs
,
193 const struct security_descriptor
*sd
)
195 struct object
*obj
= NULL
;
201 if (!len
|| ((nameptr
[0] == '/') ^ !root
))
203 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
206 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
207 memcpy( name
, nameptr
, len
);
212 case FILE_CREATE
: flags
= O_CREAT
| O_EXCL
; break;
213 case FILE_OVERWRITE_IF
: /* FIXME: the difference is whether we trash existing attr or not */
214 access
|= FILE_WRITE_ATTRIBUTES
;
215 case FILE_SUPERSEDE
: flags
= O_CREAT
| O_TRUNC
; break;
216 case FILE_OPEN
: flags
= 0; break;
217 case FILE_OPEN_IF
: flags
= O_CREAT
; break;
218 case FILE_OVERWRITE
: flags
= O_TRUNC
;
219 access
|= FILE_WRITE_ATTRIBUTES
; break;
220 default: set_error( STATUS_INVALID_PARAMETER
); goto done
;
225 const SID
*owner
= sd_get_owner( sd
);
227 owner
= token_get_user( current
->process
->token
);
228 mode
= sd_to_mode( sd
, owner
);
230 else if (options
& FILE_DIRECTORY_FILE
)
231 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0555 : 0777;
233 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
236 (!strcasecmp( name
+ len
- 4, ".exe" ) || !strcasecmp( name
+ len
- 4, ".com" )))
246 access
= generic_file_map_access( access
);
248 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
249 fd
= open_fd( root
, name
, flags
| O_NONBLOCK
| O_LARGEFILE
, &mode
, access
, sharing
, options
);
253 obj
= create_dir_obj( fd
, access
, mode
);
254 else if (S_ISCHR(mode
) && is_serial_fd( fd
))
255 obj
= create_serial( fd
);
257 obj
= create_file_obj( fd
, access
, mode
);
259 release_object( fd
);
266 /* check if two file objects point to the same file */
267 int is_same_file( struct file
*file1
, struct file
*file2
)
269 return is_same_file_fd( file1
->fd
, file2
->fd
);
272 static void file_dump( struct object
*obj
, int verbose
)
274 struct file
*file
= (struct file
*)obj
;
275 assert( obj
->ops
== &file_ops
);
276 fprintf( stderr
, "File fd=%p\n", file
->fd
);
279 static struct object_type
*file_get_type( struct object
*obj
)
281 static const WCHAR name
[] = {'F','i','l','e'};
282 static const struct unicode_str str
= { name
, sizeof(name
) };
283 return get_object_type( &str
);
286 static int file_get_poll_events( struct fd
*fd
)
288 struct file
*file
= get_fd_user( fd
);
290 assert( file
->obj
.ops
== &file_ops
);
291 if (file
->access
& FILE_UNIX_READ_ACCESS
) events
|= POLLIN
;
292 if (file
->access
& FILE_UNIX_WRITE_ACCESS
) events
|= POLLOUT
;
296 static obj_handle_t
file_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
)
298 int unix_fd
= get_unix_fd( fd
);
299 if (unix_fd
!= -1 && fsync( unix_fd
) == -1) file_set_error();
303 static enum server_fd_type
file_get_fd_type( struct fd
*fd
)
305 struct file
*file
= get_fd_user( fd
);
307 if (S_ISREG(file
->mode
) || S_ISBLK(file
->mode
)) return FD_TYPE_FILE
;
308 if (S_ISDIR(file
->mode
)) return FD_TYPE_DIR
;
312 static struct fd
*file_get_fd( struct object
*obj
)
314 struct file
*file
= (struct file
*)obj
;
315 assert( obj
->ops
== &file_ops
);
316 return (struct fd
*)grab_object( file
->fd
);
319 static unsigned int generic_file_map_access( unsigned int access
)
321 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
322 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
323 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
324 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
325 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
328 struct security_descriptor
*mode_to_sd( mode_t mode
, const SID
*user
, const SID
*group
)
330 struct security_descriptor
*sd
;
332 ACE_HEADER
*current_ace
;
333 ACCESS_ALLOWED_ACE
*aaa
;
337 const SID
*world_sid
= security_world_sid
;
338 const SID
*local_system_sid
= security_local_system_sid
;
340 dacl_size
= sizeof(ACL
) + FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) +
341 security_sid_len( local_system_sid
);
343 dacl_size
+= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( user
);
344 if ((!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
))) ||
345 (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IWOTH
))) ||
346 (!(mode
& S_IXUSR
) && (mode
& (S_IXGRP
|S_IXOTH
))))
347 dacl_size
+= FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
) + security_sid_len( user
);
349 dacl_size
+= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( world_sid
);
351 sd
= mem_alloc( sizeof(struct security_descriptor
) +
352 security_sid_len( user
) + security_sid_len( group
) +
356 sd
->control
= SE_DACL_PRESENT
;
357 sd
->owner_len
= security_sid_len( user
);
358 sd
->group_len
= security_sid_len( group
);
360 sd
->dacl_len
= dacl_size
;
362 ptr
= (char *)(sd
+ 1);
363 memcpy( ptr
, user
, sd
->owner_len
);
364 ptr
+= sd
->owner_len
;
365 memcpy( ptr
, group
, sd
->group_len
);
366 ptr
+= sd
->group_len
;
369 dacl
->AclRevision
= ACL_REVISION
;
371 dacl
->AclSize
= dacl_size
;
372 dacl
->AceCount
= 1 + (mode
& S_IRWXU
? 1 : 0) + (mode
& S_IRWXO
? 1 : 0);
373 if ((!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
))) ||
374 (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IWOTH
))) ||
375 (!(mode
& S_IXUSR
) && (mode
& (S_IXGRP
|S_IXOTH
))))
379 /* always give FILE_ALL_ACCESS for Local System */
380 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
381 current_ace
= &aaa
->Header
;
382 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
383 aaa
->Header
.AceFlags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
384 aaa
->Header
.AceSize
= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( local_system_sid
);
385 aaa
->Mask
= FILE_ALL_ACCESS
;
386 sid
= (SID
*)&aaa
->SidStart
;
387 memcpy( sid
, local_system_sid
, security_sid_len( local_system_sid
));
391 /* appropriate access rights for the user */
392 aaa
= (ACCESS_ALLOWED_ACE
*)ace_next( current_ace
);
393 current_ace
= &aaa
->Header
;
394 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
395 aaa
->Header
.AceFlags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
396 aaa
->Header
.AceSize
= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( user
);
397 aaa
->Mask
= WRITE_DAC
| WRITE_OWNER
;
399 aaa
->Mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
401 aaa
->Mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
402 sid
= (SID
*)&aaa
->SidStart
;
403 memcpy( sid
, user
, security_sid_len( user
));
405 if ((!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
))) ||
406 (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IWOTH
))) ||
407 (!(mode
& S_IXUSR
) && (mode
& (S_IXGRP
|S_IXOTH
))))
409 /* deny just in case the user is a member of the group */
410 ACCESS_DENIED_ACE
*ada
= (ACCESS_DENIED_ACE
*)ace_next( current_ace
);
411 current_ace
= &ada
->Header
;
412 ada
->Header
.AceType
= ACCESS_DENIED_ACE_TYPE
;
413 ada
->Header
.AceFlags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
414 ada
->Header
.AceSize
= FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
) + security_sid_len( user
);
416 if (!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
)))
417 ada
->Mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
418 if (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IROTH
)))
419 ada
->Mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
420 ada
->Mask
&= ~STANDARD_RIGHTS_ALL
; /* never deny standard rights */
421 sid
= (SID
*)&ada
->SidStart
;
422 memcpy( sid
, user
, security_sid_len( user
));
426 /* appropriate access rights for Everyone */
427 aaa
= (ACCESS_ALLOWED_ACE
*)ace_next( current_ace
);
428 current_ace
= &aaa
->Header
;
429 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
430 aaa
->Header
.AceFlags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
431 aaa
->Header
.AceSize
= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( world_sid
);
434 aaa
->Mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
436 aaa
->Mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
437 sid
= (SID
*)&aaa
->SidStart
;
438 memcpy( sid
, world_sid
, security_sid_len( world_sid
));
444 static struct security_descriptor
*file_get_sd( struct object
*obj
)
446 struct file
*file
= (struct file
*)obj
;
449 struct security_descriptor
*sd
;
451 assert( obj
->ops
== &file_ops
);
453 unix_fd
= get_file_unix_fd( file
);
455 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1)
458 /* mode and uid the same? if so, no need to re-generate security descriptor */
459 if (obj
->sd
&& (st
.st_mode
& (S_IRWXU
|S_IRWXO
)) == (file
->mode
& (S_IRWXU
|S_IRWXO
)) &&
460 (st
.st_uid
== file
->uid
))
463 sd
= mode_to_sd( st
.st_mode
,
464 security_unix_uid_to_sid( st
.st_uid
),
465 token_get_primary_group( current
->process
->token
));
466 if (!sd
) return obj
->sd
;
468 file
->mode
= st
.st_mode
;
469 file
->uid
= st
.st_uid
;
475 static mode_t
file_access_to_mode( unsigned int access
)
479 access
= generic_file_map_access( access
);
480 if (access
& FILE_READ_DATA
) mode
|= 4;
481 if (access
& (FILE_WRITE_DATA
|FILE_APPEND_DATA
)) mode
|= 2;
482 if (access
& FILE_EXECUTE
) mode
|= 1;
486 mode_t
sd_to_mode( const struct security_descriptor
*sd
, const SID
*owner
)
489 mode_t bits_to_set
= ~0;
492 const ACL
*dacl
= sd_get_dacl( sd
, &present
);
493 const SID
*user
= token_get_user( current
->process
->token
);
496 const ACE_HEADER
*ace
= (const ACE_HEADER
*)(dacl
+ 1);
498 for (i
= 0; i
< dacl
->AceCount
; i
++, ace
= ace_next( ace
))
500 const ACCESS_ALLOWED_ACE
*aa_ace
;
501 const ACCESS_DENIED_ACE
*ad_ace
;
504 if (ace
->AceFlags
& INHERIT_ONLY_ACE
) continue;
506 switch (ace
->AceType
)
508 case ACCESS_DENIED_ACE_TYPE
:
509 ad_ace
= (const ACCESS_DENIED_ACE
*)ace
;
510 sid
= (const SID
*)&ad_ace
->SidStart
;
511 mode
= file_access_to_mode( ad_ace
->Mask
);
512 if (security_equal_sid( sid
, security_world_sid
))
514 bits_to_set
&= ~((mode
<< 6) | (mode
<< 3) | mode
); /* all */
516 else if ((security_equal_sid( user
, owner
) &&
517 token_sid_present( current
->process
->token
, sid
, TRUE
)))
519 bits_to_set
&= ~((mode
<< 6) | (mode
<< 3)); /* user + group */
521 else if (security_equal_sid( sid
, owner
))
523 bits_to_set
&= ~(mode
<< 6); /* user only */
526 case ACCESS_ALLOWED_ACE_TYPE
:
527 aa_ace
= (const ACCESS_ALLOWED_ACE
*)ace
;
528 sid
= (const SID
*)&aa_ace
->SidStart
;
529 mode
= file_access_to_mode( aa_ace
->Mask
);
530 if (security_equal_sid( sid
, security_world_sid
))
532 mode
= (mode
<< 6) | (mode
<< 3) | mode
; /* all */
533 new_mode
|= mode
& bits_to_set
;
534 bits_to_set
&= ~mode
;
536 else if ((security_equal_sid( user
, owner
) &&
537 token_sid_present( current
->process
->token
, sid
, FALSE
)))
539 mode
= (mode
<< 6) | (mode
<< 3); /* user + group */
540 new_mode
|= mode
& bits_to_set
;
541 bits_to_set
&= ~mode
;
543 else if (security_equal_sid( sid
, owner
))
545 mode
= (mode
<< 6); /* user only */
546 new_mode
|= mode
& bits_to_set
;
547 bits_to_set
&= ~mode
;
554 /* no ACL means full access rights to anyone */
555 new_mode
= S_IRWXU
| S_IRWXG
| S_IRWXO
;
560 static int file_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
561 unsigned int set_info
)
563 struct file
*file
= (struct file
*)obj
;
569 assert( obj
->ops
== &file_ops
);
571 unix_fd
= get_file_unix_fd( file
);
573 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1) return 1;
575 if (set_info
& OWNER_SECURITY_INFORMATION
)
577 owner
= sd_get_owner( sd
);
580 set_error( STATUS_INVALID_SECURITY_DESCR
);
583 if (!obj
->sd
|| !security_equal_sid( owner
, sd_get_owner( obj
->sd
) ))
585 /* FIXME: get Unix uid and call fchown */
589 owner
= sd_get_owner( obj
->sd
);
591 owner
= token_get_user( current
->process
->token
);
593 /* group and sacl not supported */
595 if (set_info
& DACL_SECURITY_INFORMATION
)
597 /* keep the bits that we don't map to access rights in the ACL */
598 mode
= st
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
);
599 mode
|= sd_to_mode( sd
, owner
);
601 if (((st
.st_mode
^ mode
) & (S_IRWXU
|S_IRWXG
|S_IRWXO
)) && fchmod( unix_fd
, mode
) == -1)
610 static struct object
*file_open_file( struct object
*obj
, unsigned int access
,
611 unsigned int sharing
, unsigned int options
)
613 struct file
*file
= (struct file
*)obj
;
614 struct object
*new_file
= NULL
;
617 assert( obj
->ops
== &file_ops
);
619 if ((unix_name
= dup_fd_name( file
->fd
, "" )))
621 new_file
= create_file( NULL
, unix_name
, strlen(unix_name
), access
,
622 sharing
, FILE_OPEN
, options
, 0, NULL
);
625 else set_error( STATUS_OBJECT_TYPE_MISMATCH
);
629 static void file_destroy( struct object
*obj
)
631 struct file
*file
= (struct file
*)obj
;
632 assert( obj
->ops
== &file_ops
);
634 if (file
->fd
) release_object( file
->fd
);
637 /* set the last error depending on errno */
638 void file_set_error(void)
643 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
644 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
645 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
649 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
650 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
651 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
652 case EISDIR
: set_error( STATUS_FILE_IS_A_DIRECTORY
); break;
654 case EMFILE
: set_error( STATUS_TOO_MANY_OPENED_FILES
); break;
655 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
656 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
657 case ESPIPE
: set_error( STATUS_ILLEGAL_FUNCTION
); break;
658 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
659 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
660 case ENOTDIR
: set_error( STATUS_NOT_A_DIRECTORY
); break;
661 case EFBIG
: set_error( STATUS_SECTION_TOO_BIG
); break;
662 case ENODEV
: set_error( STATUS_NO_SUCH_DEVICE
); break;
663 case ENXIO
: set_error( STATUS_NO_SUCH_DEVICE
); break;
665 case EOVERFLOW
: set_error( STATUS_INVALID_PARAMETER
); break;
668 perror("wineserver: file_set_error() can't map error");
669 set_error( STATUS_UNSUCCESSFUL
);
674 struct file
*get_file_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
676 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
679 int get_file_unix_fd( struct file
*file
)
681 return get_unix_fd( file
->fd
);
685 DECL_HANDLER(create_file
)
688 struct fd
*root_fd
= NULL
;
689 const struct object_attributes
*objattr
= get_req_data();
690 const struct security_descriptor
*sd
;
692 data_size_t name_len
;
696 if (!objattr_is_valid( objattr
, get_req_data_size() ))
698 /* name is transferred in the unix codepage outside of the objattr structure */
699 if (objattr
->name_len
)
701 set_error( STATUS_INVALID_PARAMETER
);
705 if (objattr
->rootdir
)
709 if (!(root
= get_dir_obj( current
->process
, objattr
->rootdir
, 0 ))) return;
710 root_fd
= get_obj_fd( (struct object
*)root
);
711 release_object( root
);
712 if (!root_fd
) return;
715 sd
= objattr
->sd_len
? (const struct security_descriptor
*)(objattr
+ 1) : NULL
;
717 name
= (const char *)get_req_data() + sizeof(*objattr
) + objattr
->sd_len
;
718 name_len
= get_req_data_size() - sizeof(*objattr
) - objattr
->sd_len
;
721 if ((file
= create_file( root_fd
, name
, name_len
, req
->access
, req
->sharing
,
722 req
->create
, req
->options
, req
->attrs
, sd
)))
724 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->attributes
);
725 release_object( file
);
727 if (root_fd
) release_object( root_fd
);
730 /* allocate a file handle for a Unix fd */
731 DECL_HANDLER(alloc_file_handle
)
737 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
739 set_error( STATUS_INVALID_HANDLE
);
742 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
)))
744 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->attributes
);
745 release_object( file
);
749 /* lock a region of a file */
750 DECL_HANDLER(lock_file
)
754 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
756 reply
->handle
= lock_fd( file
->fd
, req
->offset
, req
->count
, req
->shared
, req
->wait
);
757 reply
->overlapped
= is_overlapped( file
);
758 release_object( file
);
762 /* unlock a region of a file */
763 DECL_HANDLER(unlock_file
)
767 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
769 unlock_fd( file
->fd
, req
->offset
, req
->count
);
770 release_object( file
);