advapi32: Implement CredReadDomainCredentials stub and tests.
[wine/multimedia.git] / server / file.c
blobdc93b4bcfdf047f302e5bfd3a2fa2266da3efaa5
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 );
79 static const struct object_ops file_ops =
81 sizeof(struct file), /* size */
82 file_dump, /* dump */
83 no_get_type, /* get_type */
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 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
295 struct security_descriptor *sd;
296 size_t dacl_size;
297 ACE_HEADER *current_ace;
298 ACCESS_ALLOWED_ACE *aaa;
299 ACL *dacl;
300 SID *sid;
301 char *ptr;
302 const SID *world_sid = security_world_sid;
303 const SID *local_system_sid = security_local_system_sid;
305 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
306 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
307 if (mode & S_IRWXU)
308 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
309 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
310 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
311 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
312 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
313 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
314 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
315 if (mode & S_IRWXO)
316 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
317 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
319 sd = mem_alloc( sizeof(struct security_descriptor) +
320 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
321 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
322 dacl_size );
323 if (!sd) return sd;
325 sd->control = SE_DACL_PRESENT;
326 sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
327 sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
328 sd->sacl_len = 0;
329 sd->dacl_len = dacl_size;
331 ptr = (char *)(sd + 1);
332 memcpy( ptr, user, sd->owner_len );
333 ptr += sd->owner_len;
334 memcpy( ptr, group, sd->group_len );
335 ptr += sd->group_len;
337 dacl = (ACL *)ptr;
338 dacl->AclRevision = ACL_REVISION;
339 dacl->Sbz1 = 0;
340 dacl->AclSize = dacl_size;
341 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
342 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
343 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
344 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
345 dacl->AceCount++;
346 dacl->Sbz2 = 0;
348 /* always give FILE_ALL_ACCESS for Local System */
349 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
350 current_ace = &aaa->Header;
351 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
352 aaa->Header.AceFlags = 0;
353 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
354 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
355 aaa->Mask = FILE_ALL_ACCESS;
356 sid = (SID *)&aaa->SidStart;
357 memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
359 if (mode & S_IRWXU)
361 /* appropriate access rights for the user */
362 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
363 current_ace = &aaa->Header;
364 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
365 aaa->Header.AceFlags = 0;
366 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
367 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
368 aaa->Mask = WRITE_DAC | WRITE_OWNER;
369 if (mode & S_IRUSR)
370 aaa->Mask |= FILE_GENERIC_READ;
371 if (mode & S_IWUSR)
372 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
373 if (mode & S_IXUSR)
374 aaa->Mask |= FILE_GENERIC_EXECUTE;
375 sid = (SID *)&aaa->SidStart;
376 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
378 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
379 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
380 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
382 /* deny just in case the user is a member of the group */
383 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
384 current_ace = &ada->Header;
385 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
386 ada->Header.AceFlags = 0;
387 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
388 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
389 ada->Mask = 0;
390 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
391 ada->Mask |= FILE_GENERIC_READ;
392 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
393 ada->Mask |= FILE_GENERIC_WRITE | DELETE;
394 if (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))
395 ada->Mask |= FILE_GENERIC_EXECUTE;
396 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
397 sid = (SID *)&ada->SidStart;
398 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
400 if (mode & S_IRWXO)
402 /* appropriate access rights for Everyone */
403 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
404 current_ace = &aaa->Header;
405 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
406 aaa->Header.AceFlags = 0;
407 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
408 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
409 aaa->Mask = 0;
410 if (mode & S_IROTH)
411 aaa->Mask |= FILE_GENERIC_READ;
412 if (mode & S_IWOTH)
413 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
414 if (mode & S_IXOTH)
415 aaa->Mask |= FILE_GENERIC_EXECUTE;
416 sid = (SID *)&aaa->SidStart;
417 memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
420 return sd;
423 static struct security_descriptor *file_get_sd( struct object *obj )
425 struct file *file = (struct file *)obj;
426 struct stat st;
427 int unix_fd;
428 struct security_descriptor *sd;
430 assert( obj->ops == &file_ops );
432 unix_fd = get_file_unix_fd( file );
434 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
435 return obj->sd;
437 /* mode and uid the same? if so, no need to re-generate security descriptor */
438 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
439 (st.st_uid == file->uid))
440 return obj->sd;
442 sd = mode_to_sd( st.st_mode,
443 security_unix_uid_to_sid( st.st_uid ),
444 token_get_primary_group( current->process->token ));
445 if (!sd) return obj->sd;
447 file->mode = st.st_mode;
448 file->uid = st.st_uid;
449 free( obj->sd );
450 obj->sd = sd;
451 return sd;
454 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
456 mode_t new_mode = 0;
457 mode_t denied_mode = 0;
458 int present;
459 const ACL *dacl = sd_get_dacl( sd, &present );
460 if (present && dacl)
462 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
463 ULONG i;
464 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
466 const ACCESS_ALLOWED_ACE *aa_ace;
467 const ACCESS_DENIED_ACE *ad_ace;
468 const SID *sid;
470 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
472 switch (ace->AceType)
474 case ACCESS_DENIED_ACE_TYPE:
475 ad_ace = (const ACCESS_DENIED_ACE *)ace;
476 sid = (const SID *)&ad_ace->SidStart;
477 if (security_equal_sid( sid, security_world_sid ))
479 unsigned int access = generic_file_map_access( ad_ace->Mask );
480 if (access & FILE_READ_DATA)
481 denied_mode |= S_IRUSR|S_IRGRP|S_IROTH;
482 if (access & FILE_WRITE_DATA)
483 denied_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
484 if (access & FILE_EXECUTE)
485 denied_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
487 else if (security_equal_sid( sid, owner ))
489 unsigned int access = generic_file_map_access( ad_ace->Mask );
490 if (access & FILE_READ_DATA)
491 denied_mode |= S_IRUSR;
492 if (access & FILE_WRITE_DATA)
493 denied_mode |= S_IWUSR;
494 if (access & FILE_EXECUTE)
495 denied_mode |= S_IXUSR;
497 break;
498 case ACCESS_ALLOWED_ACE_TYPE:
499 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
500 sid = (const SID *)&aa_ace->SidStart;
501 if (security_equal_sid( sid, security_world_sid ))
503 unsigned int access = generic_file_map_access( aa_ace->Mask );
504 if (access & FILE_READ_DATA)
505 new_mode |= S_IRUSR|S_IRGRP|S_IROTH;
506 if (access & FILE_WRITE_DATA)
507 new_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
508 if (access & FILE_EXECUTE)
509 new_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
511 else if (security_equal_sid( sid, owner ))
513 unsigned int access = generic_file_map_access( aa_ace->Mask );
514 if (access & FILE_READ_DATA)
515 new_mode |= S_IRUSR;
516 if (access & FILE_WRITE_DATA)
517 new_mode |= S_IWUSR;
518 if (access & FILE_EXECUTE)
519 new_mode |= S_IXUSR;
521 break;
525 else
526 /* no ACL means full access rights to anyone */
527 new_mode = S_IRWXU | S_IRWXO;
529 return new_mode & ~denied_mode;
532 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
533 unsigned int set_info )
535 struct file *file = (struct file *)obj;
536 const SID *owner;
537 mode_t mode;
538 int unix_fd;
540 assert( obj->ops == &file_ops );
542 unix_fd = get_file_unix_fd( file );
544 if (unix_fd == -1) return 1;
546 if (set_info & OWNER_SECURITY_INFORMATION)
548 owner = sd_get_owner( sd );
549 if (!owner)
551 set_error( STATUS_INVALID_SECURITY_DESCR );
552 return 0;
554 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
556 /* FIXME: get Unix uid and call fchown */
559 else if (obj->sd)
560 owner = sd_get_owner( obj->sd );
561 else
562 owner = token_get_user( current->process->token );
564 /* group and sacl not supported */
566 if (set_info & DACL_SECURITY_INFORMATION)
568 /* keep the bits that we don't map to access rights in the ACL */
569 mode = file->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
570 mode |= sd_to_mode( sd, owner );
572 if (file->mode != mode)
574 if (fchmod( unix_fd, mode ) == -1)
576 file_set_error();
577 return 0;
580 file->mode = mode;
583 return 1;
586 static void file_destroy( struct object *obj )
588 struct file *file = (struct file *)obj;
589 assert( obj->ops == &file_ops );
591 if (file->fd) release_object( file->fd );
594 /* set the last error depending on errno */
595 void file_set_error(void)
597 switch (errno)
599 case ETXTBSY:
600 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
601 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
602 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
603 case EACCES:
604 case ESRCH:
605 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
606 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
607 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
608 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
609 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
610 case ENFILE:
611 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
612 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
613 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
614 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
615 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
616 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
617 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
618 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
619 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
620 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
621 #ifdef EOVERFLOW
622 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
623 #endif
624 default:
625 perror("wineserver: file_set_error() can't map error");
626 set_error( STATUS_UNSUCCESSFUL );
627 break;
631 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
633 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
636 int get_file_unix_fd( struct file *file )
638 return get_unix_fd( file->fd );
641 struct file *grab_file_unless_removable( struct file *file )
643 if (is_fd_removable( file->fd )) return NULL;
644 return (struct file *)grab_object( file );
647 /* extend a file beyond the current end of file */
648 static int extend_file( struct file *file, file_pos_t new_size )
650 static const char zero;
651 int unix_fd = get_file_unix_fd( file );
652 off_t size = new_size;
654 if (unix_fd == -1) return 0;
656 if (sizeof(new_size) > sizeof(size) && size != new_size)
658 set_error( STATUS_INVALID_PARAMETER );
659 return 0;
661 /* extend the file one byte beyond the requested size and then truncate it */
662 /* this should work around ftruncate implementations that can't extend files */
663 if (pwrite( unix_fd, &zero, 1, size ) != -1)
665 ftruncate( unix_fd, size );
666 return 1;
668 file_set_error();
669 return 0;
672 /* try to grow the file to the specified size */
673 int grow_file( struct file *file, file_pos_t size )
675 struct stat st;
676 int unix_fd = get_file_unix_fd( file );
678 if (unix_fd == -1) return 0;
680 if (fstat( unix_fd, &st ) == -1)
682 file_set_error();
683 return 0;
685 if (st.st_size >= size) return 1; /* already large enough */
686 return extend_file( file, size );
689 /* create a file */
690 DECL_HANDLER(create_file)
692 struct object *file;
693 const struct object_attributes *objattr = get_req_data();
694 const struct security_descriptor *sd;
695 const char *name;
696 data_size_t name_len;
698 reply->handle = 0;
700 if (!objattr_is_valid( objattr, get_req_data_size() ))
701 return;
702 /* name is transferred in the unix codepage outside of the objattr structure */
703 if (objattr->name_len)
705 set_error( STATUS_INVALID_PARAMETER );
706 return;
709 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
711 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
712 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
714 reply->handle = 0;
715 if ((file = create_file( name, name_len, req->access,
716 req->sharing, req->create, req->options,
717 req->attrs, sd )))
719 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
720 release_object( file );
724 /* allocate a file handle for a Unix fd */
725 DECL_HANDLER(alloc_file_handle)
727 struct file *file;
728 int fd;
730 reply->handle = 0;
731 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
733 set_error( STATUS_INVALID_HANDLE );
734 return;
736 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
738 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
739 release_object( file );
743 /* lock a region of a file */
744 DECL_HANDLER(lock_file)
746 struct file *file;
748 if ((file = get_file_obj( current->process, req->handle, 0 )))
750 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
751 reply->overlapped = is_overlapped( file );
752 release_object( file );
756 /* unlock a region of a file */
757 DECL_HANDLER(unlock_file)
759 struct file *file;
761 if ((file = get_file_obj( current->process, req->handle, 0 )))
763 unlock_fd( file->fd, req->offset, req->count );
764 release_object( file );