winex11.drv: Initialize display devices before getting primary desktop rectangle.
[wine.git] / server / file.c
blob4f3485751499821b9eee190292af7aa46676c17a
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 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
195 unsigned int access, unsigned int sharing, int create,
196 unsigned int options, unsigned int attrs,
197 const struct security_descriptor *sd )
199 struct object *obj = NULL;
200 struct fd *fd;
201 int flags;
202 char *name;
203 mode_t mode;
205 if (!len || ((nameptr[0] == '/') ^ !root))
207 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
208 return NULL;
210 if (!(name = mem_alloc( len + 1 ))) return NULL;
211 memcpy( name, nameptr, len );
212 name[len] = 0;
214 switch(create)
216 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
217 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
218 access |= FILE_WRITE_ATTRIBUTES;
219 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
220 case FILE_OPEN: flags = 0; break;
221 case FILE_OPEN_IF: flags = O_CREAT; break;
222 case FILE_OVERWRITE: flags = O_TRUNC;
223 access |= FILE_WRITE_ATTRIBUTES; break;
224 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
227 if (sd)
229 const SID *owner = sd_get_owner( sd );
230 if (!owner)
231 owner = token_get_user( current->process->token );
232 mode = sd_to_mode( sd, owner );
234 else if (options & FILE_DIRECTORY_FILE)
235 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
236 else
237 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
239 if (len >= 4 &&
240 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
242 if (mode & S_IRUSR)
243 mode |= S_IXUSR;
244 if (mode & S_IRGRP)
245 mode |= S_IXGRP;
246 if (mode & S_IROTH)
247 mode |= S_IXOTH;
250 access = generic_file_map_access( access );
252 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
253 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
254 if (!fd) goto done;
256 if (S_ISDIR(mode))
257 obj = create_dir_obj( fd, access, mode );
258 else if (S_ISCHR(mode) && is_serial_fd( fd ))
259 obj = create_serial( fd );
260 else
261 obj = create_file_obj( fd, access, mode );
263 release_object( fd );
265 done:
266 free( name );
267 return obj;
270 static void file_dump( struct object *obj, int verbose )
272 struct file *file = (struct file *)obj;
273 assert( obj->ops == &file_ops );
274 fprintf( stderr, "File fd=%p\n", file->fd );
277 struct object_type *file_get_type( struct object *obj )
279 static const WCHAR name[] = {'F','i','l','e'};
280 static const struct unicode_str str = { name, sizeof(name) };
281 return get_object_type( &str );
284 static int file_get_poll_events( struct fd *fd )
286 struct file *file = get_fd_user( fd );
287 int events = 0;
288 assert( file->obj.ops == &file_ops );
289 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
290 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
291 return events;
294 static int file_flush( struct fd *fd, struct async *async )
296 int unix_fd = get_unix_fd( fd );
297 if (unix_fd != -1 && fsync( unix_fd ) == -1)
299 file_set_error();
300 return 0;
302 return 1;
305 static enum server_fd_type file_get_fd_type( struct fd *fd )
307 struct file *file = get_fd_user( fd );
309 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
310 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
311 return FD_TYPE_CHAR;
314 static struct fd *file_get_fd( struct object *obj )
316 struct file *file = (struct file *)obj;
317 assert( obj->ops == &file_ops );
318 return (struct fd *)grab_object( file->fd );
321 static unsigned int generic_file_map_access( unsigned int access )
323 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
324 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
325 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
326 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
327 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
330 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
332 struct security_descriptor *sd;
333 size_t dacl_size;
334 ACE_HEADER *current_ace;
335 ACCESS_ALLOWED_ACE *aaa;
336 ACL *dacl;
337 SID *sid;
338 char *ptr;
339 const SID *world_sid = security_world_sid;
340 const SID *local_system_sid = security_local_system_sid;
342 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
343 security_sid_len( local_system_sid );
344 if (mode & S_IRWXU)
345 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
346 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
347 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
348 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
349 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
350 if (mode & S_IRWXO)
351 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
353 sd = mem_alloc( sizeof(struct security_descriptor) +
354 security_sid_len( user ) + security_sid_len( group ) +
355 dacl_size );
356 if (!sd) return sd;
358 sd->control = SE_DACL_PRESENT;
359 sd->owner_len = security_sid_len( user );
360 sd->group_len = security_sid_len( group );
361 sd->sacl_len = 0;
362 sd->dacl_len = dacl_size;
364 ptr = (char *)(sd + 1);
365 memcpy( ptr, user, sd->owner_len );
366 ptr += sd->owner_len;
367 memcpy( ptr, group, sd->group_len );
368 ptr += sd->group_len;
370 dacl = (ACL *)ptr;
371 dacl->AclRevision = ACL_REVISION;
372 dacl->Sbz1 = 0;
373 dacl->AclSize = dacl_size;
374 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
375 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
376 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
377 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
378 dacl->AceCount++;
379 dacl->Sbz2 = 0;
381 /* always give FILE_ALL_ACCESS for Local System */
382 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
383 current_ace = &aaa->Header;
384 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
385 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
386 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
387 aaa->Mask = FILE_ALL_ACCESS;
388 sid = (SID *)&aaa->SidStart;
389 memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
391 if (mode & S_IRWXU)
393 /* appropriate access rights for the user */
394 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
395 current_ace = &aaa->Header;
396 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
397 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
398 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
399 aaa->Mask = WRITE_DAC | WRITE_OWNER;
400 if (mode & S_IRUSR)
401 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
402 if (mode & S_IWUSR)
403 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
404 sid = (SID *)&aaa->SidStart;
405 memcpy( sid, user, security_sid_len( user ));
407 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
408 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
409 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
411 /* deny just in case the user is a member of the group */
412 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
413 current_ace = &ada->Header;
414 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
415 ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
416 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
417 ada->Mask = 0;
418 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
419 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
420 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
421 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
422 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
423 sid = (SID *)&ada->SidStart;
424 memcpy( sid, user, security_sid_len( user ));
426 if (mode & S_IRWXO)
428 /* appropriate access rights for Everyone */
429 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
430 current_ace = &aaa->Header;
431 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
432 aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
433 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
434 aaa->Mask = 0;
435 if (mode & S_IROTH)
436 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
437 if (mode & S_IWOTH)
438 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
439 sid = (SID *)&aaa->SidStart;
440 memcpy( sid, world_sid, security_sid_len( world_sid ));
443 return sd;
446 static struct security_descriptor *file_get_sd( struct object *obj )
448 struct file *file = (struct file *)obj;
449 struct stat st;
450 int unix_fd;
451 struct security_descriptor *sd;
453 assert( obj->ops == &file_ops );
455 unix_fd = get_file_unix_fd( file );
457 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
458 return obj->sd;
460 /* mode and uid the same? if so, no need to re-generate security descriptor */
461 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
462 (st.st_uid == file->uid))
463 return obj->sd;
465 sd = mode_to_sd( st.st_mode,
466 security_unix_uid_to_sid( st.st_uid ),
467 token_get_primary_group( current->process->token ));
468 if (!sd) return obj->sd;
470 file->mode = st.st_mode;
471 file->uid = st.st_uid;
472 free( obj->sd );
473 obj->sd = sd;
474 return sd;
477 static mode_t file_access_to_mode( unsigned int access )
479 mode_t mode = 0;
481 access = generic_file_map_access( access );
482 if (access & FILE_READ_DATA) mode |= 4;
483 if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
484 if (access & FILE_EXECUTE) mode |= 1;
485 return mode;
488 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
490 mode_t new_mode = 0;
491 mode_t bits_to_set = ~0;
492 mode_t mode;
493 int present;
494 const ACL *dacl = sd_get_dacl( sd, &present );
495 const SID *user = token_get_user( current->process->token );
496 if (present && dacl)
498 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
499 ULONG i;
500 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
502 const ACCESS_ALLOWED_ACE *aa_ace;
503 const ACCESS_DENIED_ACE *ad_ace;
504 const SID *sid;
506 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
508 switch (ace->AceType)
510 case ACCESS_DENIED_ACE_TYPE:
511 ad_ace = (const ACCESS_DENIED_ACE *)ace;
512 sid = (const SID *)&ad_ace->SidStart;
513 mode = file_access_to_mode( ad_ace->Mask );
514 if (security_equal_sid( sid, security_world_sid ))
516 bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
518 else if ((security_equal_sid( user, owner ) &&
519 token_sid_present( current->process->token, sid, TRUE )))
521 bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
523 else if (security_equal_sid( sid, owner ))
525 bits_to_set &= ~(mode << 6); /* user only */
527 break;
528 case ACCESS_ALLOWED_ACE_TYPE:
529 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
530 sid = (const SID *)&aa_ace->SidStart;
531 mode = file_access_to_mode( aa_ace->Mask );
532 if (security_equal_sid( sid, security_world_sid ))
534 mode = (mode << 6) | (mode << 3) | mode; /* all */
535 new_mode |= mode & bits_to_set;
536 bits_to_set &= ~mode;
538 else if ((security_equal_sid( user, owner ) &&
539 token_sid_present( current->process->token, sid, FALSE )))
541 mode = (mode << 6) | (mode << 3); /* user + group */
542 new_mode |= mode & bits_to_set;
543 bits_to_set &= ~mode;
545 else if (security_equal_sid( sid, owner ))
547 mode = (mode << 6); /* user only */
548 new_mode |= mode & bits_to_set;
549 bits_to_set &= ~mode;
551 break;
555 else
556 /* no ACL means full access rights to anyone */
557 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
559 return new_mode;
562 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
563 unsigned int set_info )
565 struct file *file = (struct file *)obj;
566 const SID *owner;
567 struct stat st;
568 mode_t mode;
569 int unix_fd;
571 assert( obj->ops == &file_ops );
573 unix_fd = get_file_unix_fd( file );
575 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
577 if (set_info & OWNER_SECURITY_INFORMATION)
579 owner = sd_get_owner( sd );
580 if (!owner)
582 set_error( STATUS_INVALID_SECURITY_DESCR );
583 return 0;
585 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
587 /* FIXME: get Unix uid and call fchown */
590 else if (obj->sd)
591 owner = sd_get_owner( obj->sd );
592 else
593 owner = token_get_user( current->process->token );
595 /* group and sacl not supported */
597 if (set_info & DACL_SECURITY_INFORMATION)
599 /* keep the bits that we don't map to access rights in the ACL */
600 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
601 mode |= sd_to_mode( sd, owner );
603 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
605 file_set_error();
606 return 0;
609 return 1;
612 static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
614 if (!name || !name->len) return NULL; /* open the file itself */
616 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
617 return NULL;
620 static struct object *file_open_file( struct object *obj, unsigned int access,
621 unsigned int sharing, unsigned int options )
623 struct file *file = (struct file *)obj;
624 struct object *new_file = NULL;
625 char *unix_name;
627 assert( obj->ops == &file_ops );
629 if ((unix_name = dup_fd_name( file->fd, "" )))
631 new_file = create_file( NULL, unix_name, strlen(unix_name), access,
632 sharing, FILE_OPEN, options, 0, NULL );
633 free( unix_name );
635 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
636 return new_file;
639 static struct list *file_get_kernel_obj_list( struct object *obj )
641 struct file *file = (struct file *)obj;
642 return &file->kernel_object;
645 static void file_destroy( struct object *obj )
647 struct file *file = (struct file *)obj;
648 assert( obj->ops == &file_ops );
650 if (file->fd) release_object( file->fd );
653 /* set the last error depending on errno */
654 void file_set_error(void)
656 switch (errno)
658 case ETXTBSY:
659 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
660 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
661 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
662 case EACCES:
663 case ESRCH:
664 case EROFS:
665 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
666 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
667 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
668 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
669 case ENFILE:
670 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
671 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
672 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
673 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
674 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
675 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
676 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
677 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
678 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
679 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
680 case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
681 #ifdef EOVERFLOW
682 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
683 #endif
684 default:
685 perror("wineserver: file_set_error() can't map error");
686 set_error( STATUS_UNSUCCESSFUL );
687 break;
691 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
693 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
696 int get_file_unix_fd( struct file *file )
698 return get_unix_fd( file->fd );
701 /* create a file */
702 DECL_HANDLER(create_file)
704 struct object *file;
705 struct fd *root_fd = NULL;
706 struct unicode_str unicode_name;
707 const struct security_descriptor *sd;
708 const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
709 const char *name;
710 data_size_t name_len;
712 if (!objattr) return;
714 /* name is transferred in the unix codepage outside of the objattr structure */
715 if (unicode_name.len)
717 set_error( STATUS_INVALID_PARAMETER );
718 return;
721 if (objattr->rootdir)
723 struct dir *root;
725 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
726 root_fd = get_obj_fd( (struct object *)root );
727 release_object( root );
728 if (!root_fd) return;
731 name = get_req_data_after_objattr( objattr, &name_len );
733 reply->handle = 0;
734 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
735 req->create, req->options, req->attrs, sd )))
737 reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
738 release_object( file );
740 if (root_fd) release_object( root_fd );
743 /* allocate a file handle for a Unix fd */
744 DECL_HANDLER(alloc_file_handle)
746 struct file *file;
747 int fd;
749 reply->handle = 0;
750 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
752 set_error( STATUS_INVALID_HANDLE );
753 return;
755 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
757 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
758 release_object( file );
762 /* lock a region of a file */
763 DECL_HANDLER(lock_file)
765 struct file *file;
767 if ((file = get_file_obj( current->process, req->handle, 0 )))
769 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
770 reply->overlapped = is_fd_overlapped( file->fd );
771 release_object( file );
775 /* unlock a region of a file */
776 DECL_HANDLER(unlock_file)
778 struct file *file;
780 if ((file = get_file_obj( current->process, req->handle, 0 )))
782 unlock_fd( file->fd, req->offset, req->count );
783 release_object( file );