gdi32/tests: Test substituted face family name.
[wine.git] / server / file.c
blob062322f685daf06fce76776ea71045abed8bfa42
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"
23 #include <assert.h>
24 #include <fcntl.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <time.h>
34 #include <unistd.h>
35 #ifdef HAVE_UTIME_H
36 #include <utime.h>
37 #endif
38 #include <poll.h>
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "windef.h"
43 #include "winternl.h"
45 #include "file.h"
46 #include "handle.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "process.h"
50 #include "security.h"
52 static const WCHAR file_name[] = {'F','i','l','e'};
54 struct type_descr file_type =
56 { file_name, sizeof(file_name) }, /* name */
57 FILE_ALL_ACCESS, /* valid_access */
58 { /* mapping */
59 FILE_GENERIC_READ,
60 FILE_GENERIC_WRITE,
61 FILE_GENERIC_EXECUTE,
62 FILE_ALL_ACCESS
66 struct file
68 struct object obj; /* object header */
69 struct fd *fd; /* file descriptor for this file */
70 unsigned int access; /* file access (FILE_READ_DATA etc.) */
71 mode_t mode; /* file stat.st_mode */
72 uid_t uid; /* file stat.st_uid */
73 struct list kernel_object; /* list of kernel object pointers */
76 static void file_dump( struct object *obj, int verbose );
77 static struct fd *file_get_fd( struct object *obj );
78 static struct security_descriptor *file_get_sd( struct object *obj );
79 static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
80 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
81 unsigned int attr, struct object *root );
82 static struct object *file_open_file( struct object *obj, unsigned int access,
83 unsigned int sharing, unsigned int options );
84 static struct list *file_get_kernel_obj_list( struct object *obj );
85 static void file_destroy( struct object *obj );
87 static enum server_fd_type file_get_fd_type( struct fd *fd );
89 static const struct object_ops file_ops =
91 sizeof(struct file), /* size */
92 &file_type, /* type */
93 file_dump, /* dump */
94 add_queue, /* add_queue */
95 remove_queue, /* remove_queue */
96 default_fd_signaled, /* signaled */
97 no_satisfied, /* satisfied */
98 no_signal, /* signal */
99 file_get_fd, /* get_fd */
100 default_map_access, /* map_access */
101 file_get_sd, /* get_sd */
102 file_set_sd, /* set_sd */
103 no_get_full_name, /* get_full_name */
104 file_lookup_name, /* lookup_name */
105 no_link_name, /* link_name */
106 NULL, /* unlink_name */
107 file_open_file, /* open_file */
108 file_get_kernel_obj_list, /* get_kernel_obj_list */
109 no_close_handle, /* close_handle */
110 file_destroy /* destroy */
113 static const struct fd_ops file_fd_ops =
115 default_fd_get_poll_events, /* get_poll_events */
116 default_poll_event, /* poll_event */
117 file_get_fd_type, /* get_fd_type */
118 no_fd_read, /* read */
119 no_fd_write, /* write */
120 no_fd_flush, /* flush */
121 default_fd_get_file_info, /* get_file_info */
122 no_fd_get_volume_info, /* get_volume_info */
123 default_fd_ioctl, /* ioctl */
124 default_fd_cancel_async, /* cancel_async */
125 default_fd_queue_async, /* queue_async */
126 default_fd_reselect_async /* reselect_async */
129 /* create a file from a file descriptor */
130 /* if the function fails the fd is closed */
131 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
133 struct file *file;
134 struct stat st;
136 if (fstat( fd, &st ) == -1)
138 file_set_error();
139 close( fd );
140 return NULL;
143 if (!(file = alloc_object( &file_ops )))
145 close( fd );
146 return NULL;
149 file->mode = st.st_mode;
150 file->access = default_map_access( &file->obj, access );
151 list_init( &file->kernel_object );
152 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
153 FILE_SYNCHRONOUS_IO_NONALERT )))
155 release_object( file );
156 return NULL;
158 allow_fd_caching( file->fd );
159 return file;
162 /* create a file by duplicating an fd object */
163 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
165 struct file *file;
166 struct stat st;
168 if (fstat( get_unix_fd(fd), &st ) == -1)
170 file_set_error();
171 return NULL;
174 if ((file = alloc_object( &file_ops )))
176 file->mode = st.st_mode;
177 file->access = default_map_access( &file->obj, access );
178 list_init( &file->kernel_object );
179 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
181 release_object( file );
182 return NULL;
184 set_fd_user( file->fd, &file_fd_ops, &file->obj );
186 return file;
189 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
191 struct file *file = alloc_object( &file_ops );
193 if (!file) return NULL;
194 file->access = access;
195 file->mode = mode;
196 file->uid = ~(uid_t)0;
197 file->fd = fd;
198 list_init( &file->kernel_object );
199 grab_object( fd );
200 set_fd_user( fd, &file_fd_ops, &file->obj );
201 return &file->obj;
204 int is_file_executable( const char *name )
206 int len = strlen( name );
207 return len >= 4 && (!strcasecmp( name + len - 4, ".exe") || !strcasecmp( name + len - 4, ".com" ));
210 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
211 struct unicode_str nt_name,
212 unsigned int access, unsigned int sharing, int create,
213 unsigned int options, unsigned int attrs,
214 const struct security_descriptor *sd )
216 struct object *obj = NULL;
217 struct fd *fd;
218 int flags;
219 char *name;
220 mode_t mode;
222 if (!len || ((nameptr[0] == '/') ^ !root) || (nt_name.len && ((nt_name.str[0] == '\\') ^ !root)))
224 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
225 return NULL;
227 if (!(name = mem_alloc( len + 1 ))) return NULL;
228 memcpy( name, nameptr, len );
229 name[len] = 0;
231 switch(create)
233 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
234 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
235 access |= FILE_WRITE_ATTRIBUTES;
236 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
237 case FILE_OPEN: flags = 0; break;
238 case FILE_OPEN_IF: flags = O_CREAT; break;
239 case FILE_OVERWRITE: flags = O_TRUNC;
240 access |= FILE_WRITE_ATTRIBUTES; break;
241 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
244 if (sd)
246 const SID *owner = sd_get_owner( sd );
247 if (!owner)
248 owner = token_get_user( current->process->token );
249 mode = sd_to_mode( sd, owner );
251 else if (options & FILE_DIRECTORY_FILE)
252 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
253 else
254 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
256 if (is_file_executable( name ))
258 if (mode & S_IRUSR)
259 mode |= S_IXUSR;
260 if (mode & S_IRGRP)
261 mode |= S_IXGRP;
262 if (mode & S_IROTH)
263 mode |= S_IXOTH;
266 access = map_access( access, &file_type.mapping );
268 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
269 fd = open_fd( root, name, nt_name, flags | O_NONBLOCK, &mode, access, sharing, options );
270 if (!fd) goto done;
272 if (S_ISDIR(mode))
273 obj = create_dir_obj( fd, access, mode );
274 else if (S_ISCHR(mode) && is_serial_fd( fd ))
275 obj = create_serial( fd );
276 else
277 obj = create_file_obj( fd, access, mode );
279 release_object( fd );
281 done:
282 free( name );
283 return obj;
286 static void file_dump( struct object *obj, int verbose )
288 struct file *file = (struct file *)obj;
289 assert( obj->ops == &file_ops );
290 fprintf( stderr, "File fd=%p\n", file->fd );
293 static enum server_fd_type file_get_fd_type( struct fd *fd )
295 struct file *file = get_fd_user( fd );
297 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
298 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
299 return FD_TYPE_CHAR;
302 static struct fd *file_get_fd( struct object *obj )
304 struct file *file = (struct file *)obj;
305 assert( obj->ops == &file_ops );
306 return (struct fd *)grab_object( file->fd );
309 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
311 struct security_descriptor *sd;
312 size_t dacl_size;
313 ACE_HEADER *current_ace;
314 ACCESS_ALLOWED_ACE *aaa;
315 ACL *dacl;
316 SID *sid;
317 char *ptr;
318 const SID *world_sid = security_world_sid;
319 const SID *local_system_sid = security_local_system_sid;
321 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
322 security_sid_len( local_system_sid );
323 if (mode & S_IRWXU)
324 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
325 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
326 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
327 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
328 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
329 if (mode & S_IRWXO)
330 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
332 sd = mem_alloc( sizeof(struct security_descriptor) +
333 security_sid_len( user ) + security_sid_len( group ) +
334 dacl_size );
335 if (!sd) return sd;
337 sd->control = SE_DACL_PRESENT;
338 sd->owner_len = security_sid_len( user );
339 sd->group_len = security_sid_len( group );
340 sd->sacl_len = 0;
341 sd->dacl_len = dacl_size;
343 ptr = (char *)(sd + 1);
344 memcpy( ptr, user, sd->owner_len );
345 ptr += sd->owner_len;
346 memcpy( ptr, group, sd->group_len );
347 ptr += sd->group_len;
349 dacl = (ACL *)ptr;
350 dacl->AclRevision = ACL_REVISION;
351 dacl->Sbz1 = 0;
352 dacl->AclSize = dacl_size;
353 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
354 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
355 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
356 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
357 dacl->AceCount++;
358 dacl->Sbz2 = 0;
360 /* always give FILE_ALL_ACCESS for Local System */
361 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
362 current_ace = &aaa->Header;
363 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
364 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
365 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
366 aaa->Mask = FILE_ALL_ACCESS;
367 sid = (SID *)&aaa->SidStart;
368 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
370 if (mode & S_IRWXU)
372 /* appropriate access rights for the user */
373 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
374 current_ace = &aaa->Header;
375 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
376 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
377 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
378 aaa->Mask = WRITE_DAC | WRITE_OWNER;
379 if (mode & S_IRUSR)
380 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
381 if (mode & S_IWUSR)
382 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
383 sid = (SID *)&aaa->SidStart;
384 memcpy( sid, user, security_sid_len( user ));
386 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
387 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
388 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
390 /* deny just in case the user is a member of the group */
391 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
392 current_ace = &ada->Header;
393 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
394 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
395 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
396 ada->Mask = 0;
397 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
398 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
399 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
400 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
401 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
402 sid = (SID *)&ada->SidStart;
403 memcpy( sid, user, security_sid_len( user ));
405 if (mode & S_IRWXO)
407 /* appropriate access rights for Everyone */
408 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
409 current_ace = &aaa->Header;
410 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
411 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
412 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
413 aaa->Mask = 0;
414 if (mode & S_IROTH)
415 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
416 if (mode & S_IWOTH)
417 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
418 sid = (SID *)&aaa->SidStart;
419 memcpy( sid, world_sid, security_sid_len( world_sid ));
422 return sd;
425 static struct security_descriptor *file_get_sd( struct object *obj )
427 struct file *file = (struct file *)obj;
428 struct stat st;
429 int unix_fd;
430 struct security_descriptor *sd;
432 assert( obj->ops == &file_ops );
434 unix_fd = get_file_unix_fd( file );
436 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
437 return obj->sd;
439 /* mode and uid the same? if so, no need to re-generate security descriptor */
440 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
441 (st.st_uid == file->uid))
442 return obj->sd;
444 sd = mode_to_sd( st.st_mode,
445 security_unix_uid_to_sid( st.st_uid ),
446 token_get_primary_group( current->process->token ));
447 if (!sd) return obj->sd;
449 file->mode = st.st_mode;
450 file->uid = st.st_uid;
451 free( obj->sd );
452 obj->sd = sd;
453 return sd;
456 static mode_t file_access_to_mode( unsigned int access )
458 mode_t mode = 0;
460 access = map_access( access, &file_type.mapping );
461 if (access & FILE_READ_DATA) mode |= 4;
462 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
463 if (access & FILE_EXECUTE) mode |= 1;
464 return mode;
467 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
469 mode_t new_mode = 0;
470 mode_t bits_to_set = ~0;
471 mode_t mode;
472 int present;
473 const ACL *dacl = sd_get_dacl( sd, &present );
474 if (present && dacl)
476 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
477 ULONG i;
478 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
480 const ACCESS_ALLOWED_ACE *aa_ace;
481 const ACCESS_DENIED_ACE *ad_ace;
482 const SID *sid;
484 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
486 switch (ace->AceType)
488 case ACCESS_DENIED_ACE_TYPE:
489 ad_ace = (const ACCESS_DENIED_ACE *)ace;
490 sid = (const SID *)&ad_ace->SidStart;
491 mode = file_access_to_mode( ad_ace->Mask );
492 if (security_equal_sid( sid, security_world_sid ))
494 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
496 else if (token_sid_present( current->process->token, owner, TRUE ) &&
497 token_sid_present( current->process->token, sid, TRUE ))
499 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
501 else if (security_equal_sid( sid, owner ))
503 bits_to_set &= ~(mode << 6); /* user only */
505 break;
506 case ACCESS_ALLOWED_ACE_TYPE:
507 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
508 sid = (const SID *)&aa_ace->SidStart;
509 mode = file_access_to_mode( aa_ace->Mask );
510 if (security_equal_sid( sid, security_world_sid ))
512 mode = (mode << 6) | (mode << 3) | mode; /* all */
513 new_mode |= mode & bits_to_set;
514 bits_to_set &= ~mode;
516 else if (token_sid_present( current->process->token, owner, FALSE ) &&
517 token_sid_present( current->process->token, sid, FALSE ))
519 mode = (mode << 6) | (mode << 3); /* user + group */
520 new_mode |= mode & bits_to_set;
521 bits_to_set &= ~mode;
523 else if (security_equal_sid( sid, owner ))
525 mode = (mode << 6); /* user only */
526 new_mode |= mode & bits_to_set;
527 bits_to_set &= ~mode;
529 break;
533 else
534 /* no ACL means full access rights to anyone */
535 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
537 return new_mode;
540 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
541 unsigned int set_info )
543 struct file *file = (struct file *)obj;
544 const SID *owner;
545 struct stat st;
546 mode_t mode;
547 int unix_fd;
549 assert( obj->ops == &file_ops );
551 unix_fd = get_file_unix_fd( file );
553 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
555 if (set_info & OWNER_SECURITY_INFORMATION)
557 owner = sd_get_owner( sd );
558 if (!owner)
560 set_error( STATUS_INVALID_SECURITY_DESCR );
561 return 0;
563 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
565 /* FIXME: get Unix uid and call fchown */
568 else if (obj->sd)
569 owner = sd_get_owner( obj->sd );
570 else
571 owner = token_get_user( current->process->token );
573 /* group and sacl not supported */
575 if (set_info & DACL_SECURITY_INFORMATION)
577 /* keep the bits that we don't map to access rights in the ACL */
578 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
579 mode |= sd_to_mode( sd, owner );
581 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
583 file_set_error();
584 return 0;
587 return 1;
590 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
591 unsigned int attr, struct object *root )
593 if (!name || !name->len) return NULL; /* open the file itself */
595 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
596 return NULL;
599 static struct object *file_open_file( struct object *obj, unsigned int access,
600 unsigned int sharing, unsigned int options )
602 struct file *file = (struct file *)obj;
603 struct object *new_file = NULL;
604 struct unicode_str nt_name;
605 char *unix_name;
607 assert( obj->ops == &file_ops );
609 if ((unix_name = dup_fd_name( file->fd, "" )))
611 get_nt_name( file->fd, &nt_name );
612 new_file = create_file( NULL, unix_name, strlen(unix_name), nt_name, access,
613 sharing, FILE_OPEN, options, 0, NULL );
614 free( unix_name );
616 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
617 return new_file;
620 static struct list *file_get_kernel_obj_list( struct object *obj )
622 struct file *file = (struct file *)obj;
623 return &file->kernel_object;
626 static void file_destroy( struct object *obj )
628 struct file *file = (struct file *)obj;
629 assert( obj->ops == &file_ops );
631 if (file->fd) release_object( file->fd );
634 /* set the last error depending on errno */
635 void file_set_error(void)
637 switch (errno)
639 case ETXTBSY:
640 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
641 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
642 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
643 case EACCES:
644 case ESRCH:
645 case EROFS:
646 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
647 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
648 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
649 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
650 case ENFILE:
651 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
652 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
653 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
654 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
655 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
656 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
657 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
658 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
659 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
660 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
661 case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
662 case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
663 #ifdef EOVERFLOW
664 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
665 #endif
666 default:
667 perror("wineserver: file_set_error() can't map error");
668 set_error( STATUS_UNSUCCESSFUL );
669 break;
673 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
675 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
678 int get_file_unix_fd( struct file *file )
680 return get_unix_fd( file->fd );
683 /* create a file */
684 DECL_HANDLER(create_file)
686 struct object *file;
687 struct fd *root_fd = NULL;
688 struct unicode_str nt_name;
689 const struct security_descriptor *sd;
690 const struct object_attributes *objattr = get_req_object_attributes( &sd, &nt_name, NULL );
691 const char *name;
692 data_size_t name_len;
694 if (!objattr) return;
696 if (objattr->rootdir)
698 struct dir *root;
700 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
701 root_fd = get_obj_fd( (struct object *)root );
702 release_object( root );
703 if (!root_fd) return;
706 name = get_req_data_after_objattr( objattr, &name_len );
708 reply->handle = 0;
709 if ((file = create_file( root_fd, name, name_len, nt_name, req->access, req->sharing,
710 req->create, req->options, req->attrs, sd )))
712 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
713 release_object( file );
715 if (root_fd) release_object( root_fd );
718 /* allocate a file handle for a Unix fd */
719 DECL_HANDLER(alloc_file_handle)
721 struct file *file;
722 int fd;
724 reply->handle = 0;
725 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
727 set_error( STATUS_INVALID_HANDLE );
728 return;
730 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
732 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
733 release_object( file );
737 /* lock a region of a file */
738 DECL_HANDLER(lock_file)
740 struct file *file;
742 if ((file = get_file_obj( current->process, req->handle, 0 )))
744 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
745 reply->overlapped = is_fd_overlapped( file->fd );
746 release_object( file );
750 /* unlock a region of a file */
751 DECL_HANDLER(unlock_file)
753 struct file *file;
755 if ((file = get_file_obj( current->process, req->handle, 0 )))
757 unlock_fd( file->fd, req->offset, req->count );
758 release_object( file );