wined3d: Fix handling of a special case in IWineD3DImpl_FillGLCaps() and adjust type...
[wine/wine64.git] / server / file.c
blobad98d2129bd5497bdf545c134c6bcb166c18f745
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 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
33 #endif
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_UTIME_H
40 #include <utime.h>
41 #endif
42 #ifdef HAVE_POLL_H
43 #include <poll.h>
44 #endif
46 #include "ntstatus.h"
47 #define WIN32_NO_STATUS
48 #include "windef.h"
49 #include "winternl.h"
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
55 #include "process.h"
56 #include "security.h"
58 struct file
60 struct object obj; /* object header */
61 struct fd *fd; /* file descriptor for this file */
62 unsigned int access; /* file access (FILE_READ_DATA etc.) */
63 mode_t mode; /* file stat.st_mode */
64 uid_t uid; /* file stat.st_uid */
67 static unsigned int generic_file_map_access( unsigned int access );
69 static void file_dump( struct object *obj, int verbose );
70 static struct fd *file_get_fd( struct object *obj );
71 static struct security_descriptor *file_get_sd( struct object *obj );
72 static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
73 static void file_destroy( struct object *obj );
75 static int file_get_poll_events( struct fd *fd );
76 static void file_flush( struct fd *fd, struct event **event );
77 static enum server_fd_type file_get_fd_type( struct fd *fd );
78 static mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner );
80 static const struct object_ops file_ops =
82 sizeof(struct file), /* size */
83 file_dump, /* dump */
84 add_queue, /* add_queue */
85 remove_queue, /* remove_queue */
86 default_fd_signaled, /* signaled */
87 no_satisfied, /* satisfied */
88 no_signal, /* signal */
89 file_get_fd, /* get_fd */
90 default_fd_map_access, /* map_access */
91 file_get_sd, /* get_sd */
92 file_set_sd, /* set_sd */
93 no_lookup_name, /* lookup_name */
94 no_open_file, /* open_file */
95 fd_close_handle, /* close_handle */
96 file_destroy /* destroy */
99 static const struct fd_ops file_fd_ops =
101 file_get_poll_events, /* get_poll_events */
102 default_poll_event, /* poll_event */
103 file_flush, /* flush */
104 file_get_fd_type, /* get_fd_type */
105 default_fd_ioctl, /* ioctl */
106 default_fd_queue_async, /* queue_async */
107 default_fd_reselect_async, /* reselect_async */
108 default_fd_cancel_async /* cancel_async */
111 static inline int is_overlapped( const struct file *file )
113 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
116 /* create a file from a file descriptor */
117 /* if the function fails the fd is closed */
118 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
120 struct file *file;
121 struct stat st;
123 if (fstat( fd, &st ) == -1)
125 file_set_error();
126 return NULL;
129 if ((file = alloc_object( &file_ops )))
131 file->mode = st.st_mode;
132 file->access = default_fd_map_access( &file->obj, access );
133 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
134 FILE_SYNCHRONOUS_IO_NONALERT )))
136 release_object( file );
137 return NULL;
140 return file;
143 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
145 struct file *file = alloc_object( &file_ops );
147 if (!file) return NULL;
148 file->access = access;
149 file->mode = mode;
150 file->fd = fd;
151 grab_object( fd );
152 set_fd_user( fd, &file_fd_ops, &file->obj );
153 return &file->obj;
156 static struct object *create_file( const char *nameptr, data_size_t len, unsigned int access,
157 unsigned int sharing, int create, unsigned int options,
158 unsigned int attrs, const struct security_descriptor *sd )
160 struct object *obj = NULL;
161 struct fd *fd;
162 int flags;
163 char *name;
164 mode_t mode;
166 if (!(name = mem_alloc( len + 1 ))) return NULL;
167 memcpy( name, nameptr, len );
168 name[len] = 0;
170 switch(create)
172 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
173 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
174 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
175 case FILE_OPEN: flags = 0; break;
176 case FILE_OPEN_IF: flags = O_CREAT; break;
177 case FILE_OVERWRITE: flags = O_TRUNC; break;
178 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
181 if (sd)
183 const SID *owner = sd_get_owner( sd );
184 if (!owner)
185 owner = token_get_user( current->process->token );
186 mode = sd_to_mode( sd, owner );
188 else
189 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
191 if (len >= 4 &&
192 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
194 if (mode & S_IRUSR)
195 mode |= S_IXUSR;
196 if (mode & S_IRGRP)
197 mode |= S_IXGRP;
198 if (mode & S_IROTH)
199 mode |= S_IXOTH;
202 access = generic_file_map_access( access );
204 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
205 fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
206 if (!fd) goto done;
208 if (S_ISDIR(mode))
209 obj = create_dir_obj( fd );
210 else if (S_ISCHR(mode) && is_serial_fd( fd ))
211 obj = create_serial( fd );
212 else
213 obj = create_file_obj( fd, access, mode );
215 release_object( fd );
217 done:
218 free( name );
219 return obj;
222 /* check if two file objects point to the same file */
223 int is_same_file( struct file *file1, struct file *file2 )
225 return is_same_file_fd( file1->fd, file2->fd );
228 /* create a temp file for anonymous mappings */
229 struct file *create_temp_file( int access )
231 char tmpfn[16];
232 int fd;
234 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
235 fd = mkstemps( tmpfn, 0 );
236 if (fd == -1)
238 file_set_error();
239 return NULL;
241 unlink( tmpfn );
242 return create_file_for_fd( fd, access, 0 );
245 static void file_dump( struct object *obj, int verbose )
247 struct file *file = (struct file *)obj;
248 assert( obj->ops == &file_ops );
249 fprintf( stderr, "File fd=%p\n", file->fd );
252 static int file_get_poll_events( struct fd *fd )
254 struct file *file = get_fd_user( fd );
255 int events = 0;
256 assert( file->obj.ops == &file_ops );
257 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
258 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
259 return events;
262 static void file_flush( struct fd *fd, struct event **event )
264 int unix_fd = get_unix_fd( fd );
265 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
268 static enum server_fd_type file_get_fd_type( struct fd *fd )
270 struct file *file = get_fd_user( fd );
272 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
273 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
274 return FD_TYPE_CHAR;
277 static struct fd *file_get_fd( struct object *obj )
279 struct file *file = (struct file *)obj;
280 assert( obj->ops == &file_ops );
281 return (struct fd *)grab_object( file->fd );
284 static unsigned int generic_file_map_access( unsigned int access )
286 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
287 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
288 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
289 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
290 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
293 static struct security_descriptor *file_get_sd( struct object *obj )
295 struct file *file = (struct file *)obj;
296 struct stat st;
297 int unix_fd;
298 struct security_descriptor *sd;
299 const SID *user;
300 const SID *group;
301 size_t dacl_size;
302 ACE_HEADER *current_ace;
303 ACCESS_ALLOWED_ACE *aaa;
304 ACL *dacl;
305 SID *sid;
306 char *ptr;
307 const SID *world_sid = security_world_sid;
308 const SID *local_system_sid = security_local_system_sid;
310 assert( obj->ops == &file_ops );
312 unix_fd = get_file_unix_fd( file );
314 if (unix_fd == -1) return obj->sd;
316 if (fstat( unix_fd, &st ) == -1)
317 return obj->sd;
319 /* mode and uid the same? if so, no need to re-generate security descriptor */
320 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
321 (st.st_uid == file->uid))
322 return obj->sd;
324 user = security_unix_uid_to_sid( st.st_uid );
325 group = token_get_primary_group( current->process->token );
327 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
328 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
329 if (st.st_mode & S_IRWXU)
330 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
331 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
332 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
333 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
334 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
335 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
336 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
337 if (st.st_mode & S_IRWXO)
338 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
339 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
341 sd = mem_alloc( sizeof(struct security_descriptor) +
342 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
343 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
344 dacl_size );
345 if (!sd) return obj->sd;
347 sd->control = SE_DACL_PRESENT;
348 sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
349 sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
350 sd->sacl_len = 0;
351 sd->dacl_len = dacl_size;
353 ptr = (char *)(sd + 1);
354 memcpy( ptr, user, sd->owner_len );
355 ptr += sd->owner_len;
356 memcpy( ptr, group, sd->group_len );
357 ptr += sd->group_len;
359 dacl = (ACL *)ptr;
360 dacl->AclRevision = ACL_REVISION;
361 dacl->Sbz1 = 0;
362 dacl->AclSize = dacl_size;
363 dacl->AceCount = 1 + (st.st_mode & S_IRWXU ? 1 : 0) + (st.st_mode & S_IRWXO ? 1 : 0);
364 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
365 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
366 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
367 dacl->AceCount++;
368 dacl->Sbz2 = 0;
370 /* always give FILE_ALL_ACCESS for Local System */
371 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
372 current_ace = &aaa->Header;
373 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
374 aaa->Header.AceFlags = 0;
375 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
376 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
377 aaa->Mask = FILE_ALL_ACCESS;
378 sid = (SID *)&aaa->SidStart;
379 memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
381 if (st.st_mode & S_IRWXU)
383 /* appropriate access rights for the user */
384 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
385 current_ace = &aaa->Header;
386 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
387 aaa->Header.AceFlags = 0;
388 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
389 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
390 aaa->Mask = WRITE_DAC | WRITE_OWNER;
391 if (st.st_mode & S_IRUSR)
392 aaa->Mask |= FILE_GENERIC_READ;
393 if (st.st_mode & S_IWUSR)
394 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
395 if (st.st_mode & S_IXUSR)
396 aaa->Mask |= FILE_GENERIC_EXECUTE;
397 sid = (SID *)&aaa->SidStart;
398 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
400 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
401 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
402 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
404 /* deny just in case the user is a member of the group */
405 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
406 current_ace = &ada->Header;
407 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
408 ada->Header.AceFlags = 0;
409 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
410 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
411 ada->Mask = 0;
412 if (!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH)))
413 ada->Mask |= FILE_GENERIC_READ;
414 if (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IROTH)))
415 ada->Mask |= FILE_GENERIC_WRITE | DELETE;
416 if (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH)))
417 ada->Mask |= FILE_GENERIC_EXECUTE;
418 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
419 sid = (SID *)&ada->SidStart;
420 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
422 if (st.st_mode & S_IRWXO)
424 /* appropriate access rights for Everyone */
425 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
426 current_ace = &aaa->Header;
427 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
428 aaa->Header.AceFlags = 0;
429 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
430 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
431 aaa->Mask = 0;
432 if (st.st_mode & S_IROTH)
433 aaa->Mask |= FILE_GENERIC_READ;
434 if (st.st_mode & S_IWOTH)
435 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
436 if (st.st_mode & S_IXOTH)
437 aaa->Mask |= FILE_GENERIC_EXECUTE;
438 sid = (SID *)&aaa->SidStart;
439 memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
442 file->mode = st.st_mode;
443 file->uid = st.st_uid;
444 free( obj->sd );
445 obj->sd = sd;
446 return sd;
449 static mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
451 mode_t new_mode = 0;
452 mode_t denied_mode = 0;
453 int present;
454 const ACL *dacl = sd_get_dacl( sd, &present );
455 if (present && dacl)
457 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
458 ULONG i;
459 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
461 const ACCESS_ALLOWED_ACE *aa_ace;
462 const ACCESS_DENIED_ACE *ad_ace;
463 const SID *sid;
465 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
467 switch (ace->AceType)
469 case ACCESS_DENIED_ACE_TYPE:
470 ad_ace = (const ACCESS_DENIED_ACE *)ace;
471 sid = (const SID *)&ad_ace->SidStart;
472 if (security_equal_sid( sid, security_world_sid ))
474 unsigned int access = generic_file_map_access( ad_ace->Mask );
475 if (access & FILE_READ_DATA)
476 denied_mode |= S_IRUSR|S_IRGRP|S_IROTH;
477 if (access & FILE_WRITE_DATA)
478 denied_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
479 if (access & FILE_EXECUTE)
480 denied_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
482 else if (security_equal_sid( sid, owner ))
484 unsigned int access = generic_file_map_access( ad_ace->Mask );
485 if (access & FILE_READ_DATA)
486 denied_mode |= S_IRUSR;
487 if (access & FILE_WRITE_DATA)
488 denied_mode |= S_IWUSR;
489 if (access & FILE_EXECUTE)
490 denied_mode |= S_IXUSR;
492 break;
493 case ACCESS_ALLOWED_ACE_TYPE:
494 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
495 sid = (const SID *)&aa_ace->SidStart;
496 if (security_equal_sid( sid, security_world_sid ))
498 unsigned int access = generic_file_map_access( aa_ace->Mask );
499 if (access & FILE_READ_DATA)
500 new_mode |= S_IRUSR|S_IRGRP|S_IROTH;
501 if (access & FILE_WRITE_DATA)
502 new_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
503 if (access & FILE_EXECUTE)
504 new_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
506 else if (security_equal_sid( sid, owner ))
508 unsigned int access = generic_file_map_access( aa_ace->Mask );
509 if (access & FILE_READ_DATA)
510 new_mode |= S_IRUSR;
511 if (access & FILE_WRITE_DATA)
512 new_mode |= S_IWUSR;
513 if (access & FILE_EXECUTE)
514 new_mode |= S_IXUSR;
516 break;
520 else
521 /* no ACL means full access rights to anyone */
522 new_mode = S_IRWXU | S_IRWXO;
524 return new_mode & ~denied_mode;
527 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
528 unsigned int set_info )
530 struct file *file = (struct file *)obj;
531 const SID *owner;
532 mode_t mode;
533 int unix_fd;
535 assert( obj->ops == &file_ops );
537 unix_fd = get_file_unix_fd( file );
539 if (unix_fd == -1) return 1;
541 if (set_info & OWNER_SECURITY_INFORMATION)
543 owner = sd_get_owner( sd );
544 if (!owner)
546 set_error( STATUS_INVALID_SECURITY_DESCR );
547 return 0;
549 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
551 /* FIXME: get Unix uid and call fchown */
554 else if (obj->sd)
555 owner = sd_get_owner( obj->sd );
556 else
557 owner = token_get_user( current->process->token );
559 /* group and sacl not supported */
561 if (set_info & DACL_SECURITY_INFORMATION)
563 /* keep the bits that we don't map to access rights in the ACL */
564 mode = file->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
565 mode |= sd_to_mode( sd, owner );
567 if (file->mode != mode)
569 if (fchmod( unix_fd, mode ) == -1)
571 file_set_error();
572 return 0;
575 file->mode = mode;
578 return 1;
581 static void file_destroy( struct object *obj )
583 struct file *file = (struct file *)obj;
584 assert( obj->ops == &file_ops );
586 if (file->fd) release_object( file->fd );
589 /* set the last error depending on errno */
590 void file_set_error(void)
592 switch (errno)
594 case ETXTBSY:
595 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
596 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
597 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
598 case EACCES:
599 case ESRCH:
600 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
601 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
602 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
603 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
604 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
605 case ENFILE:
606 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
607 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
608 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
609 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
610 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
611 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
612 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
613 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
614 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
615 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
616 #ifdef EOVERFLOW
617 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
618 #endif
619 default:
620 perror("wineserver: file_set_error() can't map error");
621 set_error( STATUS_UNSUCCESSFUL );
622 break;
626 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
628 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
631 int get_file_unix_fd( struct file *file )
633 return get_unix_fd( file->fd );
636 struct file *grab_file_unless_removable( struct file *file )
638 if (is_fd_removable( file->fd )) return NULL;
639 return (struct file *)grab_object( file );
642 /* extend a file beyond the current end of file */
643 static int extend_file( struct file *file, file_pos_t new_size )
645 static const char zero;
646 int unix_fd = get_file_unix_fd( file );
647 off_t size = new_size;
649 if (unix_fd == -1) return 0;
651 if (sizeof(new_size) > sizeof(size) && size != new_size)
653 set_error( STATUS_INVALID_PARAMETER );
654 return 0;
656 /* extend the file one byte beyond the requested size and then truncate it */
657 /* this should work around ftruncate implementations that can't extend files */
658 if (pwrite( unix_fd, &zero, 1, size ) != -1)
660 ftruncate( unix_fd, size );
661 return 1;
663 file_set_error();
664 return 0;
667 /* try to grow the file to the specified size */
668 int grow_file( struct file *file, file_pos_t size )
670 struct stat st;
671 int unix_fd = get_file_unix_fd( file );
673 if (unix_fd == -1) return 0;
675 if (fstat( unix_fd, &st ) == -1)
677 file_set_error();
678 return 0;
680 if (st.st_size >= size) return 1; /* already large enough */
681 return extend_file( file, size );
684 /* create a file */
685 DECL_HANDLER(create_file)
687 struct object *file;
688 const struct object_attributes *objattr = get_req_data();
689 const struct security_descriptor *sd;
690 const char *name;
691 data_size_t name_len;
693 reply->handle = 0;
695 if (!objattr_is_valid( objattr, get_req_data_size() ))
696 return;
697 /* name is transferred in the unix codepage outside of the objattr structure */
698 if (objattr->name_len)
700 set_error( STATUS_INVALID_PARAMETER );
701 return;
704 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
706 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
707 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
709 reply->handle = 0;
710 if ((file = create_file( name, name_len, req->access,
711 req->sharing, req->create, req->options,
712 req->attrs, sd )))
714 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
715 release_object( file );
719 /* allocate a file handle for a Unix fd */
720 DECL_HANDLER(alloc_file_handle)
722 struct file *file;
723 int fd;
725 reply->handle = 0;
726 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
728 set_error( STATUS_INVALID_HANDLE );
729 return;
731 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
733 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
734 release_object( file );
738 /* lock a region of a file */
739 DECL_HANDLER(lock_file)
741 struct file *file;
743 if ((file = get_file_obj( current->process, req->handle, 0 )))
745 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
746 reply->overlapped = is_overlapped( file );
747 release_object( file );
751 /* unlock a region of a file */
752 DECL_HANDLER(unlock_file)
754 struct file *file;
756 if ((file = get_file_obj( current->process, req->handle, 0 )))
758 unlock_fd( file->fd, req->offset, req->count );
759 release_object( file );