winemac: Make show_window() and hide_window() tolerate being called when there's...
[wine.git] / server / file.c
blobdacb24a3c461cc463c3dcc7c6d04ca3120dc20e2
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 */
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 obj_handle_t file_flush( struct fd *fd, const async_data_t *async, int blocking );
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 */
83 file_dump, /* dump */
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_ioctl, /* ioctl */
111 default_fd_queue_async, /* queue_async */
112 default_fd_reselect_async, /* reselect_async */
113 default_fd_cancel_async /* cancel_async */
116 static inline int is_overlapped( const struct file *file )
118 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
121 /* create a file from a file descriptor */
122 /* if the function fails the fd is closed */
123 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
125 struct file *file;
126 struct stat st;
128 if (fstat( fd, &st ) == -1)
130 file_set_error();
131 close( fd );
132 return NULL;
135 if (!(file = alloc_object( &file_ops )))
137 close( fd );
138 return NULL;
141 file->mode = st.st_mode;
142 file->access = default_fd_map_access( &file->obj, access );
143 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
144 FILE_SYNCHRONOUS_IO_NONALERT )))
146 release_object( file );
147 return NULL;
149 allow_fd_caching( file->fd );
150 return file;
153 /* create a file by duplicating an fd object */
154 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
156 struct file *file;
157 struct stat st;
159 if (fstat( get_unix_fd(fd), &st ) == -1)
161 file_set_error();
162 return NULL;
165 if ((file = alloc_object( &file_ops )))
167 file->mode = st.st_mode;
168 file->access = default_fd_map_access( &file->obj, access );
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 grab_object( fd );
189 set_fd_user( fd, &file_fd_ops, &file->obj );
190 return &file->obj;
193 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
194 unsigned int access, unsigned int sharing, int create,
195 unsigned int options, unsigned int attrs,
196 const struct security_descriptor *sd )
198 struct object *obj = NULL;
199 struct fd *fd;
200 int flags;
201 char *name;
202 mode_t mode;
204 if (!len || ((nameptr[0] == '/') ^ !root))
206 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
207 return NULL;
209 if (!(name = mem_alloc( len + 1 ))) return NULL;
210 memcpy( name, nameptr, len );
211 name[len] = 0;
213 switch(create)
215 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
216 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
217 access |= FILE_WRITE_ATTRIBUTES;
218 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
219 case FILE_OPEN: flags = 0; break;
220 case FILE_OPEN_IF: flags = O_CREAT; break;
221 case FILE_OVERWRITE: flags = O_TRUNC;
222 access |= FILE_WRITE_ATTRIBUTES; break;
223 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
226 if (sd)
228 const SID *owner = sd_get_owner( sd );
229 if (!owner)
230 owner = token_get_user( current->process->token );
231 mode = sd_to_mode( sd, owner );
233 else if (options & FILE_DIRECTORY_FILE)
234 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
235 else
236 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
238 if (len >= 4 &&
239 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
241 if (mode & S_IRUSR)
242 mode |= S_IXUSR;
243 if (mode & S_IRGRP)
244 mode |= S_IXGRP;
245 if (mode & S_IROTH)
246 mode |= S_IXOTH;
249 access = generic_file_map_access( access );
251 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
252 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
253 if (!fd) goto done;
255 if (S_ISDIR(mode))
256 obj = create_dir_obj( fd, access, mode );
257 else if (S_ISCHR(mode) && is_serial_fd( fd ))
258 obj = create_serial( fd );
259 else
260 obj = create_file_obj( fd, access, mode );
262 release_object( fd );
264 done:
265 free( name );
266 return obj;
269 /* check if two file objects point to the same file */
270 int is_same_file( struct file *file1, struct file *file2 )
272 return is_same_file_fd( file1->fd, file2->fd );
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 static 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 obj_handle_t file_flush( struct fd *fd, const async_data_t *async, int blocking )
301 int unix_fd = get_unix_fd( fd );
302 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
303 return 0;
306 static enum server_fd_type file_get_fd_type( struct fd *fd )
308 struct file *file = get_fd_user( fd );
310 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
311 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
312 return FD_TYPE_CHAR;
315 static struct fd *file_get_fd( struct object *obj )
317 struct file *file = (struct file *)obj;
318 assert( obj->ops == &file_ops );
319 return (struct fd *)grab_object( file->fd );
322 static unsigned int generic_file_map_access( unsigned int access )
324 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
325 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
326 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
327 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
328 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
331 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
333 struct security_descriptor *sd;
334 size_t dacl_size;
335 ACE_HEADER *current_ace;
336 ACCESS_ALLOWED_ACE *aaa;
337 ACL *dacl;
338 SID *sid;
339 char *ptr;
340 const SID *world_sid = security_world_sid;
341 const SID *local_system_sid = security_local_system_sid;
343 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
344 security_sid_len( local_system_sid );
345 if (mode & S_IRWXU)
346 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
347 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
348 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
349 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
350 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
351 if (mode & S_IRWXO)
352 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
354 sd = mem_alloc( sizeof(struct security_descriptor) +
355 security_sid_len( user ) + security_sid_len( group ) +
356 dacl_size );
357 if (!sd) return sd;
359 sd->control = SE_DACL_PRESENT;
360 sd->owner_len = security_sid_len( user );
361 sd->group_len = security_sid_len( group );
362 sd->sacl_len = 0;
363 sd->dacl_len = dacl_size;
365 ptr = (char *)(sd + 1);
366 memcpy( ptr, user, sd->owner_len );
367 ptr += sd->owner_len;
368 memcpy( ptr, group, sd->group_len );
369 ptr += sd->group_len;
371 dacl = (ACL *)ptr;
372 dacl->AclRevision = ACL_REVISION;
373 dacl->Sbz1 = 0;
374 dacl->AclSize = dacl_size;
375 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
376 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
377 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
378 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
379 dacl->AceCount++;
380 dacl->Sbz2 = 0;
382 /* always give FILE_ALL_ACCESS for Local System */
383 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
384 current_ace = &aaa->Header;
385 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
386 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
387 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
388 aaa->Mask = FILE_ALL_ACCESS;
389 sid = (SID *)&aaa->SidStart;
390 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
392 if (mode & S_IRWXU)
394 /* appropriate access rights for the user */
395 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
396 current_ace = &aaa->Header;
397 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
398 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
399 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
400 aaa->Mask = WRITE_DAC | WRITE_OWNER;
401 if (mode & S_IRUSR)
402 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
403 if (mode & S_IWUSR)
404 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
405 sid = (SID *)&aaa->SidStart;
406 memcpy( sid, user, security_sid_len( user ));
408 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
409 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
410 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
412 /* deny just in case the user is a member of the group */
413 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
414 current_ace = &ada->Header;
415 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
416 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
417 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
418 ada->Mask = 0;
419 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
420 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
421 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
422 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
423 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
424 sid = (SID *)&ada->SidStart;
425 memcpy( sid, user, security_sid_len( user ));
427 if (mode & S_IRWXO)
429 /* appropriate access rights for Everyone */
430 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
431 current_ace = &aaa->Header;
432 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
433 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
434 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
435 aaa->Mask = 0;
436 if (mode & S_IROTH)
437 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
438 if (mode & S_IWOTH)
439 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
440 sid = (SID *)&aaa->SidStart;
441 memcpy( sid, world_sid, security_sid_len( world_sid ));
444 return sd;
447 static struct security_descriptor *file_get_sd( struct object *obj )
449 struct file *file = (struct file *)obj;
450 struct stat st;
451 int unix_fd;
452 struct security_descriptor *sd;
454 assert( obj->ops == &file_ops );
456 unix_fd = get_file_unix_fd( file );
458 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
459 return obj->sd;
461 /* mode and uid the same? if so, no need to re-generate security descriptor */
462 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
463 (st.st_uid == file->uid))
464 return obj->sd;
466 sd = mode_to_sd( st.st_mode,
467 security_unix_uid_to_sid( st.st_uid ),
468 token_get_primary_group( current->process->token ));
469 if (!sd) return obj->sd;
471 file->mode = st.st_mode;
472 file->uid = st.st_uid;
473 free( obj->sd );
474 obj->sd = sd;
475 return sd;
478 static mode_t file_access_to_mode( unsigned int access )
480 mode_t mode = 0;
482 access = generic_file_map_access( access );
483 if (access & FILE_READ_DATA) mode |= 4;
484 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
485 if (access & FILE_EXECUTE) mode |= 1;
486 return mode;
489 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
491 mode_t new_mode = 0;
492 mode_t bits_to_set = ~0;
493 mode_t mode;
494 int present;
495 const ACL *dacl = sd_get_dacl( sd, &present );
496 const SID *user = token_get_user( current->process->token );
497 if (present && dacl)
499 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
500 ULONG i;
501 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
503 const ACCESS_ALLOWED_ACE *aa_ace;
504 const ACCESS_DENIED_ACE *ad_ace;
505 const SID *sid;
507 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
509 switch (ace->AceType)
511 case ACCESS_DENIED_ACE_TYPE:
512 ad_ace = (const ACCESS_DENIED_ACE *)ace;
513 sid = (const SID *)&ad_ace->SidStart;
514 mode = file_access_to_mode( ad_ace->Mask );
515 if (security_equal_sid( sid, security_world_sid ))
517 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
519 else if ((security_equal_sid( user, owner ) &&
520 token_sid_present( current->process->token, sid, TRUE )))
522 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
524 else if (security_equal_sid( sid, owner ))
526 bits_to_set &= ~(mode << 6); /* user only */
528 break;
529 case ACCESS_ALLOWED_ACE_TYPE:
530 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
531 sid = (const SID *)&aa_ace->SidStart;
532 mode = file_access_to_mode( aa_ace->Mask );
533 if (security_equal_sid( sid, security_world_sid ))
535 mode = (mode << 6) | (mode << 3) | mode; /* all */
536 new_mode |= mode & bits_to_set;
537 bits_to_set &= ~mode;
539 else if ((security_equal_sid( user, owner ) &&
540 token_sid_present( current->process->token, sid, FALSE )))
542 mode = (mode << 6) | (mode << 3); /* user + group */
543 new_mode |= mode & bits_to_set;
544 bits_to_set &= ~mode;
546 else if (security_equal_sid( sid, owner ))
548 mode = (mode << 6); /* user only */
549 new_mode |= mode & bits_to_set;
550 bits_to_set &= ~mode;
552 break;
556 else
557 /* no ACL means full access rights to anyone */
558 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
560 return new_mode;
563 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
564 unsigned int set_info )
566 struct file *file = (struct file *)obj;
567 const SID *owner;
568 struct stat st;
569 mode_t mode;
570 int unix_fd;
572 assert( obj->ops == &file_ops );
574 unix_fd = get_file_unix_fd( file );
576 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
578 if (set_info & OWNER_SECURITY_INFORMATION)
580 owner = sd_get_owner( sd );
581 if (!owner)
583 set_error( STATUS_INVALID_SECURITY_DESCR );
584 return 0;
586 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
588 /* FIXME: get Unix uid and call fchown */
591 else if (obj->sd)
592 owner = sd_get_owner( obj->sd );
593 else
594 owner = token_get_user( current->process->token );
596 /* group and sacl not supported */
598 if (set_info & DACL_SECURITY_INFORMATION)
600 /* keep the bits that we don't map to access rights in the ACL */
601 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
602 mode |= sd_to_mode( sd, owner );
604 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
606 file_set_error();
607 return 0;
610 return 1;
613 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
615 if (!name || !name->len) return NULL; /* open the file itself */
617 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
618 return NULL;
621 static struct object *file_open_file( struct object *obj, unsigned int access,
622 unsigned int sharing, unsigned int options )
624 struct file *file = (struct file *)obj;
625 struct object *new_file = NULL;
626 char *unix_name;
628 assert( obj->ops == &file_ops );
630 if ((unix_name = dup_fd_name( file->fd, "" )))
632 new_file = create_file( NULL, unix_name, strlen(unix_name), access,
633 sharing, FILE_OPEN, options, 0, NULL );
634 free( unix_name );
636 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
637 return new_file;
640 static void file_destroy( struct object *obj )
642 struct file *file = (struct file *)obj;
643 assert( obj->ops == &file_ops );
645 if (file->fd) release_object( file->fd );
648 /* set the last error depending on errno */
649 void file_set_error(void)
651 switch (errno)
653 case ETXTBSY:
654 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
655 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
656 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
657 case EACCES:
658 case ESRCH:
659 case EROFS:
660 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
661 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
662 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
663 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
664 case ENFILE:
665 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
666 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
667 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
668 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
669 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
670 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
671 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
672 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
673 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
674 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
675 #ifdef EOVERFLOW
676 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
677 #endif
678 default:
679 perror("wineserver: file_set_error() can't map error");
680 set_error( STATUS_UNSUCCESSFUL );
681 break;
685 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
687 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
690 int get_file_unix_fd( struct file *file )
692 return get_unix_fd( file->fd );
695 /* create a file */
696 DECL_HANDLER(create_file)
698 struct object *file;
699 struct fd *root_fd = NULL;
700 struct unicode_str unicode_name;
701 const struct security_descriptor *sd;
702 const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
703 const char *name;
704 data_size_t name_len;
706 if (!objattr) return;
708 /* name is transferred in the unix codepage outside of the objattr structure */
709 if (unicode_name.len)
711 set_error( STATUS_INVALID_PARAMETER );
712 return;
715 if (objattr->rootdir)
717 struct dir *root;
719 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
720 root_fd = get_obj_fd( (struct object *)root );
721 release_object( root );
722 if (!root_fd) return;
725 name = get_req_data_after_objattr( objattr, &name_len );
727 reply->handle = 0;
728 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
729 req->create, req->options, req->attrs, sd )))
731 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
732 release_object( file );
734 if (root_fd) release_object( root_fd );
737 /* allocate a file handle for a Unix fd */
738 DECL_HANDLER(alloc_file_handle)
740 struct file *file;
741 int fd;
743 reply->handle = 0;
744 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
746 set_error( STATUS_INVALID_HANDLE );
747 return;
749 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
751 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
752 release_object( file );
756 /* lock a region of a file */
757 DECL_HANDLER(lock_file)
759 struct file *file;
761 if ((file = get_file_obj( current->process, req->handle, 0 )))
763 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
764 reply->overlapped = is_overlapped( file );
765 release_object( file );
769 /* unlock a region of a file */
770 DECL_HANDLER(unlock_file)
772 struct file *file;
774 if ((file = get_file_obj( current->process, req->handle, 0 )))
776 unlock_fd( file->fd, req->offset, req->count );
777 release_object( file );