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_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
72 static struct object
*file_open_file( struct object
*obj
, unsigned int access
,
73 unsigned int sharing
, unsigned int options
);
74 static void file_destroy( struct object
*obj
);
76 static int file_get_poll_events( struct fd
*fd
);
77 static int file_flush( struct fd
*fd
, struct async
*async
);
78 static enum server_fd_type
file_get_fd_type( struct fd
*fd
);
80 static const struct object_ops file_ops
=
82 sizeof(struct file
), /* size */
84 file_get_type
, /* get_type */
85 add_queue
, /* add_queue */
86 remove_queue
, /* remove_queue */
87 default_fd_signaled
, /* signaled */
88 no_satisfied
, /* satisfied */
89 no_signal
, /* signal */
90 file_get_fd
, /* get_fd */
91 default_fd_map_access
, /* map_access */
92 file_get_sd
, /* get_sd */
93 file_set_sd
, /* set_sd */
94 file_lookup_name
, /* lookup_name */
95 no_link_name
, /* link_name */
96 NULL
, /* unlink_name */
97 file_open_file
, /* open_file */
98 fd_close_handle
, /* close_handle */
99 file_destroy
/* destroy */
102 static const struct fd_ops file_fd_ops
=
104 file_get_poll_events
, /* get_poll_events */
105 default_poll_event
, /* poll_event */
106 file_get_fd_type
, /* get_fd_type */
107 no_fd_read
, /* read */
108 no_fd_write
, /* write */
109 file_flush
, /* flush */
110 default_fd_get_file_info
, /* get_file_info */
111 no_fd_get_volume_info
, /* get_volume_info */
112 default_fd_ioctl
, /* ioctl */
113 default_fd_queue_async
, /* queue_async */
114 default_fd_reselect_async
/* reselect_async */
117 /* create a file from a file descriptor */
118 /* if the function fails the fd is closed */
119 struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
)
124 if (fstat( fd
, &st
) == -1)
131 if (!(file
= alloc_object( &file_ops
)))
137 file
->mode
= st
.st_mode
;
138 file
->access
= default_fd_map_access( &file
->obj
, access
);
139 if (!(file
->fd
= create_anonymous_fd( &file_fd_ops
, fd
, &file
->obj
,
140 FILE_SYNCHRONOUS_IO_NONALERT
)))
142 release_object( file
);
145 allow_fd_caching( file
->fd
);
149 /* create a file by duplicating an fd object */
150 struct file
*create_file_for_fd_obj( struct fd
*fd
, unsigned int access
, unsigned int sharing
)
155 if (fstat( get_unix_fd(fd
), &st
) == -1)
161 if ((file
= alloc_object( &file_ops
)))
163 file
->mode
= st
.st_mode
;
164 file
->access
= default_fd_map_access( &file
->obj
, access
);
165 if (!(file
->fd
= dup_fd_object( fd
, access
, sharing
, FILE_SYNCHRONOUS_IO_NONALERT
)))
167 release_object( file
);
170 set_fd_user( file
->fd
, &file_fd_ops
, &file
->obj
);
175 static struct object
*create_file_obj( struct fd
*fd
, unsigned int access
, mode_t mode
)
177 struct file
*file
= alloc_object( &file_ops
);
179 if (!file
) return NULL
;
180 file
->access
= access
;
182 file
->uid
= ~(uid_t
)0;
185 set_fd_user( fd
, &file_fd_ops
, &file
->obj
);
189 static struct object
*create_file( struct fd
*root
, const char *nameptr
, data_size_t len
,
190 unsigned int access
, unsigned int sharing
, int create
,
191 unsigned int options
, unsigned int attrs
,
192 const struct security_descriptor
*sd
)
194 struct object
*obj
= NULL
;
200 if (!len
|| ((nameptr
[0] == '/') ^ !root
))
202 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
205 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
206 memcpy( name
, nameptr
, len
);
211 case FILE_CREATE
: flags
= O_CREAT
| O_EXCL
; break;
212 case FILE_OVERWRITE_IF
: /* FIXME: the difference is whether we trash existing attr or not */
213 access
|= FILE_WRITE_ATTRIBUTES
;
214 case FILE_SUPERSEDE
: flags
= O_CREAT
| O_TRUNC
; break;
215 case FILE_OPEN
: flags
= 0; break;
216 case FILE_OPEN_IF
: flags
= O_CREAT
; break;
217 case FILE_OVERWRITE
: flags
= O_TRUNC
;
218 access
|= FILE_WRITE_ATTRIBUTES
; break;
219 default: set_error( STATUS_INVALID_PARAMETER
); goto done
;
224 const SID
*owner
= sd_get_owner( sd
);
226 owner
= token_get_user( current
->process
->token
);
227 mode
= sd_to_mode( sd
, owner
);
229 else if (options
& FILE_DIRECTORY_FILE
)
230 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0555 : 0777;
232 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
235 (!strcasecmp( name
+ len
- 4, ".exe" ) || !strcasecmp( name
+ len
- 4, ".com" )))
245 access
= generic_file_map_access( access
);
247 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
248 fd
= open_fd( root
, name
, flags
| O_NONBLOCK
| O_LARGEFILE
, &mode
, access
, sharing
, options
);
252 obj
= create_dir_obj( fd
, access
, mode
);
253 else if (S_ISCHR(mode
) && is_serial_fd( fd
))
254 obj
= create_serial( fd
);
256 obj
= create_file_obj( fd
, access
, mode
);
258 release_object( fd
);
265 static void file_dump( struct object
*obj
, int verbose
)
267 struct file
*file
= (struct file
*)obj
;
268 assert( obj
->ops
== &file_ops
);
269 fprintf( stderr
, "File fd=%p\n", file
->fd
);
272 static struct object_type
*file_get_type( struct object
*obj
)
274 static const WCHAR name
[] = {'F','i','l','e'};
275 static const struct unicode_str str
= { name
, sizeof(name
) };
276 return get_object_type( &str
);
279 static int file_get_poll_events( struct fd
*fd
)
281 struct file
*file
= get_fd_user( fd
);
283 assert( file
->obj
.ops
== &file_ops
);
284 if (file
->access
& FILE_UNIX_READ_ACCESS
) events
|= POLLIN
;
285 if (file
->access
& FILE_UNIX_WRITE_ACCESS
) events
|= POLLOUT
;
289 static int file_flush( struct fd
*fd
, struct async
*async
)
291 int unix_fd
= get_unix_fd( fd
);
292 if (unix_fd
!= -1 && fsync( unix_fd
) == -1)
300 static enum server_fd_type
file_get_fd_type( struct fd
*fd
)
302 struct file
*file
= get_fd_user( fd
);
304 if (S_ISREG(file
->mode
) || S_ISBLK(file
->mode
)) return FD_TYPE_FILE
;
305 if (S_ISDIR(file
->mode
)) return FD_TYPE_DIR
;
309 static struct fd
*file_get_fd( struct object
*obj
)
311 struct file
*file
= (struct file
*)obj
;
312 assert( obj
->ops
== &file_ops
);
313 return (struct fd
*)grab_object( file
->fd
);
316 static unsigned int generic_file_map_access( unsigned int access
)
318 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
319 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
320 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
321 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
322 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
325 struct security_descriptor
*mode_to_sd( mode_t mode
, const SID
*user
, const SID
*group
)
327 struct security_descriptor
*sd
;
329 ACE_HEADER
*current_ace
;
330 ACCESS_ALLOWED_ACE
*aaa
;
334 const SID
*world_sid
= security_world_sid
;
335 const SID
*local_system_sid
= security_local_system_sid
;
337 dacl_size
= sizeof(ACL
) + FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) +
338 security_sid_len( local_system_sid
);
340 dacl_size
+= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( user
);
341 if ((!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
))) ||
342 (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IWOTH
))) ||
343 (!(mode
& S_IXUSR
) && (mode
& (S_IXGRP
|S_IXOTH
))))
344 dacl_size
+= FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
) + security_sid_len( user
);
346 dacl_size
+= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( world_sid
);
348 sd
= mem_alloc( sizeof(struct security_descriptor
) +
349 security_sid_len( user
) + security_sid_len( group
) +
353 sd
->control
= SE_DACL_PRESENT
;
354 sd
->owner_len
= security_sid_len( user
);
355 sd
->group_len
= security_sid_len( group
);
357 sd
->dacl_len
= dacl_size
;
359 ptr
= (char *)(sd
+ 1);
360 memcpy( ptr
, user
, sd
->owner_len
);
361 ptr
+= sd
->owner_len
;
362 memcpy( ptr
, group
, sd
->group_len
);
363 ptr
+= sd
->group_len
;
366 dacl
->AclRevision
= ACL_REVISION
;
368 dacl
->AclSize
= dacl_size
;
369 dacl
->AceCount
= 1 + (mode
& S_IRWXU
? 1 : 0) + (mode
& S_IRWXO
? 1 : 0);
370 if ((!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
))) ||
371 (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IWOTH
))) ||
372 (!(mode
& S_IXUSR
) && (mode
& (S_IXGRP
|S_IXOTH
))))
376 /* always give FILE_ALL_ACCESS for Local System */
377 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
378 current_ace
= &aaa
->Header
;
379 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
380 aaa
->Header
.AceFlags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
381 aaa
->Header
.AceSize
= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( local_system_sid
);
382 aaa
->Mask
= FILE_ALL_ACCESS
;
383 sid
= (SID
*)&aaa
->SidStart
;
384 memcpy( sid
, local_system_sid
, security_sid_len( local_system_sid
));
388 /* appropriate access rights for the user */
389 aaa
= (ACCESS_ALLOWED_ACE
*)ace_next( current_ace
);
390 current_ace
= &aaa
->Header
;
391 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
392 aaa
->Header
.AceFlags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
393 aaa
->Header
.AceSize
= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( user
);
394 aaa
->Mask
= WRITE_DAC
| WRITE_OWNER
;
396 aaa
->Mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
398 aaa
->Mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
399 sid
= (SID
*)&aaa
->SidStart
;
400 memcpy( sid
, user
, security_sid_len( user
));
402 if ((!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
))) ||
403 (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IWOTH
))) ||
404 (!(mode
& S_IXUSR
) && (mode
& (S_IXGRP
|S_IXOTH
))))
406 /* deny just in case the user is a member of the group */
407 ACCESS_DENIED_ACE
*ada
= (ACCESS_DENIED_ACE
*)ace_next( current_ace
);
408 current_ace
= &ada
->Header
;
409 ada
->Header
.AceType
= ACCESS_DENIED_ACE_TYPE
;
410 ada
->Header
.AceFlags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
411 ada
->Header
.AceSize
= FIELD_OFFSET(ACCESS_DENIED_ACE
, SidStart
) + security_sid_len( user
);
413 if (!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
)))
414 ada
->Mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
415 if (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IROTH
)))
416 ada
->Mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
417 ada
->Mask
&= ~STANDARD_RIGHTS_ALL
; /* never deny standard rights */
418 sid
= (SID
*)&ada
->SidStart
;
419 memcpy( sid
, user
, security_sid_len( user
));
423 /* appropriate access rights for Everyone */
424 aaa
= (ACCESS_ALLOWED_ACE
*)ace_next( current_ace
);
425 current_ace
= &aaa
->Header
;
426 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
427 aaa
->Header
.AceFlags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
428 aaa
->Header
.AceSize
= FIELD_OFFSET(ACCESS_ALLOWED_ACE
, SidStart
) + security_sid_len( world_sid
);
431 aaa
->Mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
433 aaa
->Mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
434 sid
= (SID
*)&aaa
->SidStart
;
435 memcpy( sid
, world_sid
, security_sid_len( world_sid
));
441 static struct security_descriptor
*file_get_sd( struct object
*obj
)
443 struct file
*file
= (struct file
*)obj
;
446 struct security_descriptor
*sd
;
448 assert( obj
->ops
== &file_ops
);
450 unix_fd
= get_file_unix_fd( file
);
452 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1)
455 /* mode and uid the same? if so, no need to re-generate security descriptor */
456 if (obj
->sd
&& (st
.st_mode
& (S_IRWXU
|S_IRWXO
)) == (file
->mode
& (S_IRWXU
|S_IRWXO
)) &&
457 (st
.st_uid
== file
->uid
))
460 sd
= mode_to_sd( st
.st_mode
,
461 security_unix_uid_to_sid( st
.st_uid
),
462 token_get_primary_group( current
->process
->token
));
463 if (!sd
) return obj
->sd
;
465 file
->mode
= st
.st_mode
;
466 file
->uid
= st
.st_uid
;
472 static mode_t
file_access_to_mode( unsigned int access
)
476 access
= generic_file_map_access( access
);
477 if (access
& FILE_READ_DATA
) mode
|= 4;
478 if (access
& (FILE_WRITE_DATA
|FILE_APPEND_DATA
)) mode
|= 2;
479 if (access
& FILE_EXECUTE
) mode
|= 1;
483 mode_t
sd_to_mode( const struct security_descriptor
*sd
, const SID
*owner
)
486 mode_t bits_to_set
= ~0;
489 const ACL
*dacl
= sd_get_dacl( sd
, &present
);
490 const SID
*user
= token_get_user( current
->process
->token
);
493 const ACE_HEADER
*ace
= (const ACE_HEADER
*)(dacl
+ 1);
495 for (i
= 0; i
< dacl
->AceCount
; i
++, ace
= ace_next( ace
))
497 const ACCESS_ALLOWED_ACE
*aa_ace
;
498 const ACCESS_DENIED_ACE
*ad_ace
;
501 if (ace
->AceFlags
& INHERIT_ONLY_ACE
) continue;
503 switch (ace
->AceType
)
505 case ACCESS_DENIED_ACE_TYPE
:
506 ad_ace
= (const ACCESS_DENIED_ACE
*)ace
;
507 sid
= (const SID
*)&ad_ace
->SidStart
;
508 mode
= file_access_to_mode( ad_ace
->Mask
);
509 if (security_equal_sid( sid
, security_world_sid
))
511 bits_to_set
&= ~((mode
<< 6) | (mode
<< 3) | mode
); /* all */
513 else if ((security_equal_sid( user
, owner
) &&
514 token_sid_present( current
->process
->token
, sid
, TRUE
)))
516 bits_to_set
&= ~((mode
<< 6) | (mode
<< 3)); /* user + group */
518 else if (security_equal_sid( sid
, owner
))
520 bits_to_set
&= ~(mode
<< 6); /* user only */
523 case ACCESS_ALLOWED_ACE_TYPE
:
524 aa_ace
= (const ACCESS_ALLOWED_ACE
*)ace
;
525 sid
= (const SID
*)&aa_ace
->SidStart
;
526 mode
= file_access_to_mode( aa_ace
->Mask
);
527 if (security_equal_sid( sid
, security_world_sid
))
529 mode
= (mode
<< 6) | (mode
<< 3) | mode
; /* all */
530 new_mode
|= mode
& bits_to_set
;
531 bits_to_set
&= ~mode
;
533 else if ((security_equal_sid( user
, owner
) &&
534 token_sid_present( current
->process
->token
, sid
, FALSE
)))
536 mode
= (mode
<< 6) | (mode
<< 3); /* user + group */
537 new_mode
|= mode
& bits_to_set
;
538 bits_to_set
&= ~mode
;
540 else if (security_equal_sid( sid
, owner
))
542 mode
= (mode
<< 6); /* user only */
543 new_mode
|= mode
& bits_to_set
;
544 bits_to_set
&= ~mode
;
551 /* no ACL means full access rights to anyone */
552 new_mode
= S_IRWXU
| S_IRWXG
| S_IRWXO
;
557 static int file_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
558 unsigned int set_info
)
560 struct file
*file
= (struct file
*)obj
;
566 assert( obj
->ops
== &file_ops
);
568 unix_fd
= get_file_unix_fd( file
);
570 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1) return 1;
572 if (set_info
& OWNER_SECURITY_INFORMATION
)
574 owner
= sd_get_owner( sd
);
577 set_error( STATUS_INVALID_SECURITY_DESCR
);
580 if (!obj
->sd
|| !security_equal_sid( owner
, sd_get_owner( obj
->sd
) ))
582 /* FIXME: get Unix uid and call fchown */
586 owner
= sd_get_owner( obj
->sd
);
588 owner
= token_get_user( current
->process
->token
);
590 /* group and sacl not supported */
592 if (set_info
& DACL_SECURITY_INFORMATION
)
594 /* keep the bits that we don't map to access rights in the ACL */
595 mode
= st
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
);
596 mode
|= sd_to_mode( sd
, owner
);
598 if (((st
.st_mode
^ mode
) & (S_IRWXU
|S_IRWXG
|S_IRWXO
)) && fchmod( unix_fd
, mode
) == -1)
607 static struct object
*file_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
609 if (!name
|| !name
->len
) return NULL
; /* open the file itself */
611 set_error( STATUS_OBJECT_PATH_NOT_FOUND
);
615 static struct object
*file_open_file( struct object
*obj
, unsigned int access
,
616 unsigned int sharing
, unsigned int options
)
618 struct file
*file
= (struct file
*)obj
;
619 struct object
*new_file
= NULL
;
622 assert( obj
->ops
== &file_ops
);
624 if ((unix_name
= dup_fd_name( file
->fd
, "" )))
626 new_file
= create_file( NULL
, unix_name
, strlen(unix_name
), access
,
627 sharing
, FILE_OPEN
, options
, 0, NULL
);
630 else set_error( STATUS_OBJECT_TYPE_MISMATCH
);
634 static void file_destroy( struct object
*obj
)
636 struct file
*file
= (struct file
*)obj
;
637 assert( obj
->ops
== &file_ops
);
639 if (file
->fd
) release_object( file
->fd
);
642 /* set the last error depending on errno */
643 void file_set_error(void)
648 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
649 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
650 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
654 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
655 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
656 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
657 case EISDIR
: set_error( STATUS_FILE_IS_A_DIRECTORY
); break;
659 case EMFILE
: set_error( STATUS_TOO_MANY_OPENED_FILES
); break;
660 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
661 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
662 case ESPIPE
: set_error( STATUS_ILLEGAL_FUNCTION
); break;
663 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
664 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
665 case ENOTDIR
: set_error( STATUS_NOT_A_DIRECTORY
); break;
666 case EFBIG
: set_error( STATUS_SECTION_TOO_BIG
); break;
667 case ENODEV
: set_error( STATUS_NO_SUCH_DEVICE
); break;
668 case ENXIO
: set_error( STATUS_NO_SUCH_DEVICE
); break;
669 case EXDEV
: set_error( STATUS_NOT_SAME_DEVICE
); break;
671 case EOVERFLOW
: set_error( STATUS_INVALID_PARAMETER
); break;
674 perror("wineserver: file_set_error() can't map error");
675 set_error( STATUS_UNSUCCESSFUL
);
680 struct file
*get_file_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
682 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
685 int get_file_unix_fd( struct file
*file
)
687 return get_unix_fd( file
->fd
);
691 DECL_HANDLER(create_file
)
694 struct fd
*root_fd
= NULL
;
695 struct unicode_str unicode_name
;
696 const struct security_descriptor
*sd
;
697 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &unicode_name
, NULL
);
699 data_size_t name_len
;
701 if (!objattr
) return;
703 /* name is transferred in the unix codepage outside of the objattr structure */
704 if (unicode_name
.len
)
706 set_error( STATUS_INVALID_PARAMETER
);
710 if (objattr
->rootdir
)
714 if (!(root
= get_dir_obj( current
->process
, objattr
->rootdir
, 0 ))) return;
715 root_fd
= get_obj_fd( (struct object
*)root
);
716 release_object( root
);
717 if (!root_fd
) return;
720 name
= get_req_data_after_objattr( objattr
, &name_len
);
723 if ((file
= create_file( root_fd
, name
, name_len
, req
->access
, req
->sharing
,
724 req
->create
, req
->options
, req
->attrs
, sd
)))
726 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, objattr
->attributes
);
727 release_object( file
);
729 if (root_fd
) release_object( root_fd
);
732 /* allocate a file handle for a Unix fd */
733 DECL_HANDLER(alloc_file_handle
)
739 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
741 set_error( STATUS_INVALID_HANDLE
);
744 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
)))
746 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->attributes
);
747 release_object( file
);
751 /* lock a region of a file */
752 DECL_HANDLER(lock_file
)
756 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
758 reply
->handle
= lock_fd( file
->fd
, req
->offset
, req
->count
, req
->shared
, req
->wait
);
759 reply
->overlapped
= is_fd_overlapped( file
->fd
);
760 release_object( file
);
764 /* unlock a region of a file */
765 DECL_HANDLER(unlock_file
)
769 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
771 unlock_fd( file
->fd
, req
->offset
, req
->count
);
772 release_object( file
);