ntdll: Load .so builtin modules without using libwine.
[wine/zf.git] / server / file.c
blobbce202138e009be766c4c0a5b123c44bfa9349bc
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 */
62 struct list kernel_object; /* list of kernel object pointers */
65 static unsigned int generic_file_map_access( unsigned int access );
67 static void file_dump( struct object *obj, int verbose );
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 struct list *file_get_kernel_obj_list( struct object *obj );
75 static void file_destroy( struct object *obj );
77 static int file_get_poll_events( struct fd *fd );
78 static int file_flush( struct fd *fd, struct async *async );
79 static enum server_fd_type file_get_fd_type( struct fd *fd );
81 static const struct object_ops file_ops =
83 sizeof(struct file), /* size */
84 file_dump, /* dump */
85 file_get_type, /* get_type */
86 add_queue, /* add_queue */
87 remove_queue, /* remove_queue */
88 default_fd_signaled, /* signaled */
89 no_satisfied, /* satisfied */
90 no_signal, /* signal */
91 file_get_fd, /* get_fd */
92 default_fd_map_access, /* map_access */
93 file_get_sd, /* get_sd */
94 file_set_sd, /* set_sd */
95 file_lookup_name, /* lookup_name */
96 no_link_name, /* link_name */
97 NULL, /* unlink_name */
98 file_open_file, /* open_file */
99 file_get_kernel_obj_list, /* get_kernel_obj_list */
100 fd_close_handle, /* close_handle */
101 file_destroy /* destroy */
104 static const struct fd_ops file_fd_ops =
106 file_get_poll_events, /* get_poll_events */
107 default_poll_event, /* poll_event */
108 file_get_fd_type, /* get_fd_type */
109 no_fd_read, /* read */
110 no_fd_write, /* write */
111 file_flush, /* flush */
112 default_fd_get_file_info, /* get_file_info */
113 no_fd_get_volume_info, /* get_volume_info */
114 default_fd_ioctl, /* ioctl */
115 default_fd_queue_async, /* queue_async */
116 default_fd_reselect_async /* reselect_async */
119 /* create a file from a file descriptor */
120 /* if the function fails the fd is closed */
121 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
123 struct file *file;
124 struct stat st;
126 if (fstat( fd, &st ) == -1)
128 file_set_error();
129 close( fd );
130 return NULL;
133 if (!(file = alloc_object( &file_ops )))
135 close( fd );
136 return NULL;
139 file->mode = st.st_mode;
140 file->access = default_fd_map_access( &file->obj, access );
141 list_init( &file->kernel_object );
142 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
143 FILE_SYNCHRONOUS_IO_NONALERT )))
145 release_object( file );
146 return NULL;
148 allow_fd_caching( file->fd );
149 return file;
152 /* create a file by duplicating an fd object */
153 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
155 struct file *file;
156 struct stat st;
158 if (fstat( get_unix_fd(fd), &st ) == -1)
160 file_set_error();
161 return NULL;
164 if ((file = alloc_object( &file_ops )))
166 file->mode = st.st_mode;
167 file->access = default_fd_map_access( &file->obj, access );
168 list_init( &file->kernel_object );
169 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
171 release_object( file );
172 return NULL;
174 set_fd_user( file->fd, &file_fd_ops, &file->obj );
176 return file;
179 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
181 struct file *file = alloc_object( &file_ops );
183 if (!file) return NULL;
184 file->access = access;
185 file->mode = mode;
186 file->uid = ~(uid_t)0;
187 file->fd = fd;
188 list_init( &file->kernel_object );
189 grab_object( fd );
190 set_fd_user( fd, &file_fd_ops, &file->obj );
191 return &file->obj;
194 int is_file_executable( const char *name )
196 int len = strlen( name );
197 return len >= 4 && (!strcasecmp( name + len - 4, ".exe") || !strcasecmp( name + len - 4, ".com" ));
200 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
201 unsigned int access, unsigned int sharing, int create,
202 unsigned int options, unsigned int attrs,
203 const struct security_descriptor *sd )
205 struct object *obj = NULL;
206 struct fd *fd;
207 int flags;
208 char *name;
209 mode_t mode;
211 if (!len || ((nameptr[0] == '/') ^ !root))
213 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
214 return NULL;
216 if (!(name = mem_alloc( len + 1 ))) return NULL;
217 memcpy( name, nameptr, len );
218 name[len] = 0;
220 switch(create)
222 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
223 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
224 access |= FILE_WRITE_ATTRIBUTES;
225 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
226 case FILE_OPEN: flags = 0; break;
227 case FILE_OPEN_IF: flags = O_CREAT; break;
228 case FILE_OVERWRITE: flags = O_TRUNC;
229 access |= FILE_WRITE_ATTRIBUTES; break;
230 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
233 if (sd)
235 const SID *owner = sd_get_owner( sd );
236 if (!owner)
237 owner = token_get_user( current->process->token );
238 mode = sd_to_mode( sd, owner );
240 else if (options & FILE_DIRECTORY_FILE)
241 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
242 else
243 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
245 if (is_file_executable( name ))
247 if (mode & S_IRUSR)
248 mode |= S_IXUSR;
249 if (mode & S_IRGRP)
250 mode |= S_IXGRP;
251 if (mode & S_IROTH)
252 mode |= S_IXOTH;
255 access = generic_file_map_access( access );
257 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
258 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
259 if (!fd) goto done;
261 if (S_ISDIR(mode))
262 obj = create_dir_obj( fd, access, mode );
263 else if (S_ISCHR(mode) && is_serial_fd( fd ))
264 obj = create_serial( fd );
265 else
266 obj = create_file_obj( fd, access, mode );
268 release_object( fd );
270 done:
271 free( name );
272 return obj;
275 static void file_dump( struct object *obj, int verbose )
277 struct file *file = (struct file *)obj;
278 assert( obj->ops == &file_ops );
279 fprintf( stderr, "File fd=%p\n", file->fd );
282 struct object_type *file_get_type( struct object *obj )
284 static const WCHAR name[] = {'F','i','l','e'};
285 static const struct unicode_str str = { name, sizeof(name) };
286 return get_object_type( &str );
289 static int file_get_poll_events( struct fd *fd )
291 struct file *file = get_fd_user( fd );
292 int events = 0;
293 assert( file->obj.ops == &file_ops );
294 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
295 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
296 return events;
299 static int file_flush( struct fd *fd, struct async *async )
301 int unix_fd = get_unix_fd( fd );
302 if (unix_fd != -1 && fsync( unix_fd ) == -1)
304 file_set_error();
305 return 0;
307 return 1;
310 static enum server_fd_type file_get_fd_type( struct fd *fd )
312 struct file *file = get_fd_user( fd );
314 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
315 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
316 return FD_TYPE_CHAR;
319 static struct fd *file_get_fd( struct object *obj )
321 struct file *file = (struct file *)obj;
322 assert( obj->ops == &file_ops );
323 return (struct fd *)grab_object( file->fd );
326 static unsigned int generic_file_map_access( unsigned int access )
328 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
329 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
330 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
331 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
332 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
335 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
337 struct security_descriptor *sd;
338 size_t dacl_size;
339 ACE_HEADER *current_ace;
340 ACCESS_ALLOWED_ACE *aaa;
341 ACL *dacl;
342 SID *sid;
343 char *ptr;
344 const SID *world_sid = security_world_sid;
345 const SID *local_system_sid = security_local_system_sid;
347 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
348 security_sid_len( local_system_sid );
349 if (mode & S_IRWXU)
350 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
351 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
352 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
353 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
354 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
355 if (mode & S_IRWXO)
356 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
358 sd = mem_alloc( sizeof(struct security_descriptor) +
359 security_sid_len( user ) + security_sid_len( group ) +
360 dacl_size );
361 if (!sd) return sd;
363 sd->control = SE_DACL_PRESENT;
364 sd->owner_len = security_sid_len( user );
365 sd->group_len = security_sid_len( group );
366 sd->sacl_len = 0;
367 sd->dacl_len = dacl_size;
369 ptr = (char *)(sd + 1);
370 memcpy( ptr, user, sd->owner_len );
371 ptr += sd->owner_len;
372 memcpy( ptr, group, sd->group_len );
373 ptr += sd->group_len;
375 dacl = (ACL *)ptr;
376 dacl->AclRevision = ACL_REVISION;
377 dacl->Sbz1 = 0;
378 dacl->AclSize = dacl_size;
379 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
380 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
381 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
382 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
383 dacl->AceCount++;
384 dacl->Sbz2 = 0;
386 /* always give FILE_ALL_ACCESS for Local System */
387 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
388 current_ace = &aaa->Header;
389 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
390 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
391 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
392 aaa->Mask = FILE_ALL_ACCESS;
393 sid = (SID *)&aaa->SidStart;
394 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
396 if (mode & S_IRWXU)
398 /* appropriate access rights for the user */
399 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
400 current_ace = &aaa->Header;
401 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
402 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
403 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
404 aaa->Mask = WRITE_DAC | WRITE_OWNER;
405 if (mode & S_IRUSR)
406 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
407 if (mode & S_IWUSR)
408 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
409 sid = (SID *)&aaa->SidStart;
410 memcpy( sid, user, security_sid_len( user ));
412 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
413 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
414 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
416 /* deny just in case the user is a member of the group */
417 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
418 current_ace = &ada->Header;
419 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
420 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
421 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
422 ada->Mask = 0;
423 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
424 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
425 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
426 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
427 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
428 sid = (SID *)&ada->SidStart;
429 memcpy( sid, user, security_sid_len( user ));
431 if (mode & S_IRWXO)
433 /* appropriate access rights for Everyone */
434 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
435 current_ace = &aaa->Header;
436 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
437 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
438 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
439 aaa->Mask = 0;
440 if (mode & S_IROTH)
441 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
442 if (mode & S_IWOTH)
443 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
444 sid = (SID *)&aaa->SidStart;
445 memcpy( sid, world_sid, security_sid_len( world_sid ));
448 return sd;
451 static struct security_descriptor *file_get_sd( struct object *obj )
453 struct file *file = (struct file *)obj;
454 struct stat st;
455 int unix_fd;
456 struct security_descriptor *sd;
458 assert( obj->ops == &file_ops );
460 unix_fd = get_file_unix_fd( file );
462 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
463 return obj->sd;
465 /* mode and uid the same? if so, no need to re-generate security descriptor */
466 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
467 (st.st_uid == file->uid))
468 return obj->sd;
470 sd = mode_to_sd( st.st_mode,
471 security_unix_uid_to_sid( st.st_uid ),
472 token_get_primary_group( current->process->token ));
473 if (!sd) return obj->sd;
475 file->mode = st.st_mode;
476 file->uid = st.st_uid;
477 free( obj->sd );
478 obj->sd = sd;
479 return sd;
482 static mode_t file_access_to_mode( unsigned int access )
484 mode_t mode = 0;
486 access = generic_file_map_access( access );
487 if (access & FILE_READ_DATA) mode |= 4;
488 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
489 if (access & FILE_EXECUTE) mode |= 1;
490 return mode;
493 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
495 mode_t new_mode = 0;
496 mode_t bits_to_set = ~0;
497 mode_t mode;
498 int present;
499 const ACL *dacl = sd_get_dacl( sd, &present );
500 const SID *user = token_get_user( current->process->token );
501 if (present && dacl)
503 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
504 ULONG i;
505 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
507 const ACCESS_ALLOWED_ACE *aa_ace;
508 const ACCESS_DENIED_ACE *ad_ace;
509 const SID *sid;
511 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
513 switch (ace->AceType)
515 case ACCESS_DENIED_ACE_TYPE:
516 ad_ace = (const ACCESS_DENIED_ACE *)ace;
517 sid = (const SID *)&ad_ace->SidStart;
518 mode = file_access_to_mode( ad_ace->Mask );
519 if (security_equal_sid( sid, security_world_sid ))
521 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
523 else if ((security_equal_sid( user, owner ) &&
524 token_sid_present( current->process->token, sid, TRUE )))
526 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
528 else if (security_equal_sid( sid, owner ))
530 bits_to_set &= ~(mode << 6); /* user only */
532 break;
533 case ACCESS_ALLOWED_ACE_TYPE:
534 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
535 sid = (const SID *)&aa_ace->SidStart;
536 mode = file_access_to_mode( aa_ace->Mask );
537 if (security_equal_sid( sid, security_world_sid ))
539 mode = (mode << 6) | (mode << 3) | mode; /* all */
540 new_mode |= mode & bits_to_set;
541 bits_to_set &= ~mode;
543 else if ((security_equal_sid( user, owner ) &&
544 token_sid_present( current->process->token, sid, FALSE )))
546 mode = (mode << 6) | (mode << 3); /* user + group */
547 new_mode |= mode & bits_to_set;
548 bits_to_set &= ~mode;
550 else if (security_equal_sid( sid, owner ))
552 mode = (mode << 6); /* user only */
553 new_mode |= mode & bits_to_set;
554 bits_to_set &= ~mode;
556 break;
560 else
561 /* no ACL means full access rights to anyone */
562 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
564 return new_mode;
567 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
568 unsigned int set_info )
570 struct file *file = (struct file *)obj;
571 const SID *owner;
572 struct stat st;
573 mode_t mode;
574 int unix_fd;
576 assert( obj->ops == &file_ops );
578 unix_fd = get_file_unix_fd( file );
580 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
582 if (set_info & OWNER_SECURITY_INFORMATION)
584 owner = sd_get_owner( sd );
585 if (!owner)
587 set_error( STATUS_INVALID_SECURITY_DESCR );
588 return 0;
590 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
592 /* FIXME: get Unix uid and call fchown */
595 else if (obj->sd)
596 owner = sd_get_owner( obj->sd );
597 else
598 owner = token_get_user( current->process->token );
600 /* group and sacl not supported */
602 if (set_info & DACL_SECURITY_INFORMATION)
604 /* keep the bits that we don't map to access rights in the ACL */
605 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
606 mode |= sd_to_mode( sd, owner );
608 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
610 file_set_error();
611 return 0;
614 return 1;
617 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
619 if (!name || !name->len) return NULL; /* open the file itself */
621 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
622 return NULL;
625 static struct object *file_open_file( struct object *obj, unsigned int access,
626 unsigned int sharing, unsigned int options )
628 struct file *file = (struct file *)obj;
629 struct object *new_file = NULL;
630 char *unix_name;
632 assert( obj->ops == &file_ops );
634 if ((unix_name = dup_fd_name( file->fd, "" )))
636 new_file = create_file( NULL, unix_name, strlen(unix_name), access,
637 sharing, FILE_OPEN, options, 0, NULL );
638 free( unix_name );
640 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
641 return new_file;
644 static struct list *file_get_kernel_obj_list( struct object *obj )
646 struct file *file = (struct file *)obj;
647 return &file->kernel_object;
650 static void file_destroy( struct object *obj )
652 struct file *file = (struct file *)obj;
653 assert( obj->ops == &file_ops );
655 if (file->fd) release_object( file->fd );
658 /* set the last error depending on errno */
659 void file_set_error(void)
661 switch (errno)
663 case ETXTBSY:
664 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
665 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
666 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
667 case EACCES:
668 case ESRCH:
669 case EROFS:
670 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
671 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
672 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
673 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
674 case ENFILE:
675 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
676 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
677 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
678 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
679 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
680 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
681 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
682 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
683 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
684 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
685 case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
686 case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
687 #ifdef EOVERFLOW
688 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
689 #endif
690 default:
691 perror("wineserver: file_set_error() can't map error");
692 set_error( STATUS_UNSUCCESSFUL );
693 break;
697 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
699 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
702 int get_file_unix_fd( struct file *file )
704 return get_unix_fd( file->fd );
707 /* create a file */
708 DECL_HANDLER(create_file)
710 struct object *file;
711 struct fd *root_fd = NULL;
712 struct unicode_str unicode_name;
713 const struct security_descriptor *sd;
714 const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
715 const char *name;
716 data_size_t name_len;
718 if (!objattr) return;
720 /* name is transferred in the unix codepage outside of the objattr structure */
721 if (unicode_name.len)
723 set_error( STATUS_INVALID_PARAMETER );
724 return;
727 if (objattr->rootdir)
729 struct dir *root;
731 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
732 root_fd = get_obj_fd( (struct object *)root );
733 release_object( root );
734 if (!root_fd) return;
737 name = get_req_data_after_objattr( objattr, &name_len );
739 reply->handle = 0;
740 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
741 req->create, req->options, req->attrs, sd )))
743 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
744 release_object( file );
746 if (root_fd) release_object( root_fd );
749 /* allocate a file handle for a Unix fd */
750 DECL_HANDLER(alloc_file_handle)
752 struct file *file;
753 int fd;
755 reply->handle = 0;
756 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
758 set_error( STATUS_INVALID_HANDLE );
759 return;
761 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
763 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
764 release_object( file );
768 /* lock a region of a file */
769 DECL_HANDLER(lock_file)
771 struct file *file;
773 if ((file = get_file_obj( current->process, req->handle, 0 )))
775 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
776 reply->overlapped = is_fd_overlapped( file->fd );
777 release_object( file );
781 /* unlock a region of a file */
782 DECL_HANDLER(unlock_file)
784 struct file *file;
786 if ((file = get_file_obj( current->process, req->handle, 0 )))
788 unlock_fd( file->fd, req->offset, req->count );
789 release_object( file );