mapi32: Assign to structs instead of using memcpy.
[wine.git] / server / file.c
blob5224e85feaac7d37eed57a74395668e261ce3c2c
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 no_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 no_lookup_name, /* lookup_name */
95 no_open_file, /* open_file */
96 fd_close_handle, /* close_handle */
97 file_destroy /* destroy */
100 static const struct fd_ops file_fd_ops =
102 file_get_poll_events, /* get_poll_events */
103 default_poll_event, /* poll_event */
104 file_flush, /* flush */
105 file_get_fd_type, /* get_fd_type */
106 default_fd_ioctl, /* ioctl */
107 default_fd_queue_async, /* queue_async */
108 default_fd_reselect_async, /* reselect_async */
109 default_fd_cancel_async /* cancel_async */
112 static inline int is_overlapped( const struct file *file )
114 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
117 /* create a file from a file descriptor */
118 /* if the function fails the fd is closed */
119 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
121 struct file *file;
122 struct stat st;
124 if (fstat( fd, &st ) == -1)
126 file_set_error();
127 return NULL;
130 if ((file = alloc_object( &file_ops )))
132 file->mode = st.st_mode;
133 file->access = default_fd_map_access( &file->obj, access );
134 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
135 FILE_SYNCHRONOUS_IO_NONALERT )))
137 release_object( file );
138 return NULL;
141 return file;
144 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
146 struct file *file = alloc_object( &file_ops );
148 if (!file) return NULL;
149 file->access = access;
150 file->mode = mode;
151 file->fd = fd;
152 grab_object( fd );
153 set_fd_user( fd, &file_fd_ops, &file->obj );
154 return &file->obj;
157 static struct object *create_file( const char *nameptr, data_size_t len, unsigned int access,
158 unsigned int sharing, int create, unsigned int options,
159 unsigned int attrs, const struct security_descriptor *sd )
161 struct object *obj = NULL;
162 struct fd *fd;
163 int flags;
164 char *name;
165 mode_t mode;
167 if (!(name = mem_alloc( len + 1 ))) return NULL;
168 memcpy( name, nameptr, len );
169 name[len] = 0;
171 switch(create)
173 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
174 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
175 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
176 case FILE_OPEN: flags = 0; break;
177 case FILE_OPEN_IF: flags = O_CREAT; break;
178 case FILE_OVERWRITE: flags = O_TRUNC; break;
179 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
182 if (sd)
184 const SID *owner = sd_get_owner( sd );
185 if (!owner)
186 owner = token_get_user( current->process->token );
187 mode = sd_to_mode( sd, owner );
189 else
190 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
192 if (len >= 4 &&
193 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
195 if (mode & S_IRUSR)
196 mode |= S_IXUSR;
197 if (mode & S_IRGRP)
198 mode |= S_IXGRP;
199 if (mode & S_IROTH)
200 mode |= S_IXOTH;
203 access = generic_file_map_access( access );
205 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
206 fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
207 if (!fd) goto done;
209 if (S_ISDIR(mode))
210 obj = create_dir_obj( fd );
211 else if (S_ISCHR(mode) && is_serial_fd( fd ))
212 obj = create_serial( fd );
213 else
214 obj = create_file_obj( fd, access, mode );
216 release_object( fd );
218 done:
219 free( name );
220 return obj;
223 /* check if two file objects point to the same file */
224 int is_same_file( struct file *file1, struct file *file2 )
226 return is_same_file_fd( file1->fd, file2->fd );
229 /* create a temp file for anonymous mappings */
230 struct file *create_temp_file( int access )
232 char tmpfn[16];
233 int fd;
235 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
236 fd = mkstemps( tmpfn, 0 );
237 if (fd == -1)
239 file_set_error();
240 return NULL;
242 unlink( tmpfn );
243 return create_file_for_fd( fd, access, 0 );
246 static void file_dump( struct object *obj, int verbose )
248 struct file *file = (struct file *)obj;
249 assert( obj->ops == &file_ops );
250 fprintf( stderr, "File fd=%p\n", file->fd );
253 static int file_get_poll_events( struct fd *fd )
255 struct file *file = get_fd_user( fd );
256 int events = 0;
257 assert( file->obj.ops == &file_ops );
258 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
259 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
260 return events;
263 static void file_flush( struct fd *fd, struct event **event )
265 int unix_fd = get_unix_fd( fd );
266 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
269 static enum server_fd_type file_get_fd_type( struct fd *fd )
271 struct file *file = get_fd_user( fd );
273 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
274 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
275 return FD_TYPE_CHAR;
278 static struct fd *file_get_fd( struct object *obj )
280 struct file *file = (struct file *)obj;
281 assert( obj->ops == &file_ops );
282 return (struct fd *)grab_object( file->fd );
285 static unsigned int generic_file_map_access( unsigned int access )
287 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
288 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
289 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
290 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
291 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
294 static struct security_descriptor *file_get_sd( struct object *obj )
296 struct file *file = (struct file *)obj;
297 struct stat st;
298 int unix_fd;
299 struct security_descriptor *sd;
300 const SID *user;
301 const SID *group;
302 size_t dacl_size;
303 ACE_HEADER *current_ace;
304 ACCESS_ALLOWED_ACE *aaa;
305 ACL *dacl;
306 SID *sid;
307 char *ptr;
308 const SID *world_sid = security_world_sid;
309 const SID *local_system_sid = security_local_system_sid;
311 assert( obj->ops == &file_ops );
313 unix_fd = get_file_unix_fd( file );
315 if (unix_fd == -1) return obj->sd;
317 if (fstat( unix_fd, &st ) == -1)
318 return obj->sd;
320 /* mode and uid the same? if so, no need to re-generate security descriptor */
321 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
322 (st.st_uid == file->uid))
323 return obj->sd;
325 user = security_unix_uid_to_sid( st.st_uid );
326 group = token_get_primary_group( current->process->token );
328 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
329 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
330 if (st.st_mode & S_IRWXU)
331 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
332 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
333 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
334 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
335 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
336 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
337 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
338 if (st.st_mode & S_IRWXO)
339 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
340 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
342 sd = mem_alloc( sizeof(struct security_descriptor) +
343 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
344 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
345 dacl_size );
346 if (!sd) return obj->sd;
348 sd->control = SE_DACL_PRESENT;
349 sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
350 sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
351 sd->sacl_len = 0;
352 sd->dacl_len = dacl_size;
354 ptr = (char *)(sd + 1);
355 memcpy( ptr, user, sd->owner_len );
356 ptr += sd->owner_len;
357 memcpy( ptr, group, sd->group_len );
358 ptr += sd->group_len;
360 dacl = (ACL *)ptr;
361 dacl->AclRevision = ACL_REVISION;
362 dacl->Sbz1 = 0;
363 dacl->AclSize = dacl_size;
364 dacl->AceCount = 1 + (st.st_mode & S_IRWXU ? 1 : 0) + (st.st_mode & S_IRWXO ? 1 : 0);
365 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
366 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
367 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
368 dacl->AceCount++;
369 dacl->Sbz2 = 0;
371 /* always give FILE_ALL_ACCESS for Local System */
372 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
373 current_ace = &aaa->Header;
374 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
375 aaa->Header.AceFlags = 0;
376 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
377 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
378 aaa->Mask = FILE_ALL_ACCESS;
379 sid = (SID *)&aaa->SidStart;
380 memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
382 if (st.st_mode & S_IRWXU)
384 /* appropriate access rights for the user */
385 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
386 current_ace = &aaa->Header;
387 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
388 aaa->Header.AceFlags = 0;
389 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
390 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
391 aaa->Mask = WRITE_DAC | WRITE_OWNER;
392 if (st.st_mode & S_IRUSR)
393 aaa->Mask |= FILE_GENERIC_READ;
394 if (st.st_mode & S_IWUSR)
395 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
396 if (st.st_mode & S_IXUSR)
397 aaa->Mask |= FILE_GENERIC_EXECUTE;
398 sid = (SID *)&aaa->SidStart;
399 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
401 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
402 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
403 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
405 /* deny just in case the user is a member of the group */
406 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
407 current_ace = &ada->Header;
408 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
409 ada->Header.AceFlags = 0;
410 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
411 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
412 ada->Mask = 0;
413 if (!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH)))
414 ada->Mask |= FILE_GENERIC_READ;
415 if (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IROTH)))
416 ada->Mask |= FILE_GENERIC_WRITE | DELETE;
417 if (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH)))
418 ada->Mask |= FILE_GENERIC_EXECUTE;
419 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
420 sid = (SID *)&ada->SidStart;
421 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
423 if (st.st_mode & S_IRWXO)
425 /* appropriate access rights for Everyone */
426 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
427 current_ace = &aaa->Header;
428 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
429 aaa->Header.AceFlags = 0;
430 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
431 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
432 aaa->Mask = 0;
433 if (st.st_mode & S_IROTH)
434 aaa->Mask |= FILE_GENERIC_READ;
435 if (st.st_mode & S_IWOTH)
436 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
437 if (st.st_mode & S_IXOTH)
438 aaa->Mask |= FILE_GENERIC_EXECUTE;
439 sid = (SID *)&aaa->SidStart;
440 memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
443 file->mode = st.st_mode;
444 file->uid = st.st_uid;
445 free( obj->sd );
446 obj->sd = sd;
447 return sd;
450 static mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
452 mode_t new_mode = 0;
453 mode_t denied_mode = 0;
454 int present;
455 const ACL *dacl = sd_get_dacl( sd, &present );
456 if (present && dacl)
458 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
459 ULONG i;
460 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
462 const ACCESS_ALLOWED_ACE *aa_ace;
463 const ACCESS_DENIED_ACE *ad_ace;
464 const SID *sid;
466 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
468 switch (ace->AceType)
470 case ACCESS_DENIED_ACE_TYPE:
471 ad_ace = (const ACCESS_DENIED_ACE *)ace;
472 sid = (const SID *)&ad_ace->SidStart;
473 if (security_equal_sid( sid, security_world_sid ))
475 unsigned int access = generic_file_map_access( ad_ace->Mask );
476 if (access & FILE_READ_DATA)
477 denied_mode |= S_IRUSR|S_IRGRP|S_IROTH;
478 if (access & FILE_WRITE_DATA)
479 denied_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
480 if (access & FILE_EXECUTE)
481 denied_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
483 else if (security_equal_sid( sid, owner ))
485 unsigned int access = generic_file_map_access( ad_ace->Mask );
486 if (access & FILE_READ_DATA)
487 denied_mode |= S_IRUSR;
488 if (access & FILE_WRITE_DATA)
489 denied_mode |= S_IWUSR;
490 if (access & FILE_EXECUTE)
491 denied_mode |= S_IXUSR;
493 break;
494 case ACCESS_ALLOWED_ACE_TYPE:
495 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
496 sid = (const SID *)&aa_ace->SidStart;
497 if (security_equal_sid( sid, security_world_sid ))
499 unsigned int access = generic_file_map_access( aa_ace->Mask );
500 if (access & FILE_READ_DATA)
501 new_mode |= S_IRUSR|S_IRGRP|S_IROTH;
502 if (access & FILE_WRITE_DATA)
503 new_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
504 if (access & FILE_EXECUTE)
505 new_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
507 else if (security_equal_sid( sid, owner ))
509 unsigned int access = generic_file_map_access( aa_ace->Mask );
510 if (access & FILE_READ_DATA)
511 new_mode |= S_IRUSR;
512 if (access & FILE_WRITE_DATA)
513 new_mode |= S_IWUSR;
514 if (access & FILE_EXECUTE)
515 new_mode |= S_IXUSR;
517 break;
521 else
522 /* no ACL means full access rights to anyone */
523 new_mode = S_IRWXU | S_IRWXO;
525 return new_mode & ~denied_mode;
528 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
529 unsigned int set_info )
531 struct file *file = (struct file *)obj;
532 const SID *owner;
533 mode_t mode;
534 int unix_fd;
536 assert( obj->ops == &file_ops );
538 unix_fd = get_file_unix_fd( file );
540 if (unix_fd == -1) return 1;
542 if (set_info & OWNER_SECURITY_INFORMATION)
544 owner = sd_get_owner( sd );
545 if (!owner)
547 set_error( STATUS_INVALID_SECURITY_DESCR );
548 return 0;
550 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
552 /* FIXME: get Unix uid and call fchown */
555 else if (obj->sd)
556 owner = sd_get_owner( obj->sd );
557 else
558 owner = token_get_user( current->process->token );
560 /* group and sacl not supported */
562 if (set_info & DACL_SECURITY_INFORMATION)
564 /* keep the bits that we don't map to access rights in the ACL */
565 mode = file->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
566 mode |= sd_to_mode( sd, owner );
568 if (file->mode != mode)
570 if (fchmod( unix_fd, mode ) == -1)
572 file_set_error();
573 return 0;
576 file->mode = mode;
579 return 1;
582 static void file_destroy( struct object *obj )
584 struct file *file = (struct file *)obj;
585 assert( obj->ops == &file_ops );
587 if (file->fd) release_object( file->fd );
590 /* set the last error depending on errno */
591 void file_set_error(void)
593 switch (errno)
595 case ETXTBSY:
596 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
597 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
598 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
599 case EACCES:
600 case ESRCH:
601 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
602 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
603 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
604 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
605 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
606 case ENFILE:
607 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
608 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
609 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
610 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
611 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
612 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
613 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
614 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
615 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
616 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
617 #ifdef EOVERFLOW
618 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
619 #endif
620 default:
621 perror("wineserver: file_set_error() can't map error");
622 set_error( STATUS_UNSUCCESSFUL );
623 break;
627 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
629 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
632 int get_file_unix_fd( struct file *file )
634 return get_unix_fd( file->fd );
637 struct file *grab_file_unless_removable( struct file *file )
639 if (is_fd_removable( file->fd )) return NULL;
640 return (struct file *)grab_object( file );
643 /* extend a file beyond the current end of file */
644 static int extend_file( struct file *file, file_pos_t new_size )
646 static const char zero;
647 int unix_fd = get_file_unix_fd( file );
648 off_t size = new_size;
650 if (unix_fd == -1) return 0;
652 if (sizeof(new_size) > sizeof(size) && size != new_size)
654 set_error( STATUS_INVALID_PARAMETER );
655 return 0;
657 /* extend the file one byte beyond the requested size and then truncate it */
658 /* this should work around ftruncate implementations that can't extend files */
659 if (pwrite( unix_fd, &zero, 1, size ) != -1)
661 ftruncate( unix_fd, size );
662 return 1;
664 file_set_error();
665 return 0;
668 /* try to grow the file to the specified size */
669 int grow_file( struct file *file, file_pos_t size )
671 struct stat st;
672 int unix_fd = get_file_unix_fd( file );
674 if (unix_fd == -1) return 0;
676 if (fstat( unix_fd, &st ) == -1)
678 file_set_error();
679 return 0;
681 if (st.st_size >= size) return 1; /* already large enough */
682 return extend_file( file, size );
685 /* create a file */
686 DECL_HANDLER(create_file)
688 struct object *file;
689 const struct object_attributes *objattr = get_req_data();
690 const struct security_descriptor *sd;
691 const char *name;
692 data_size_t name_len;
694 reply->handle = 0;
696 if (!objattr_is_valid( objattr, get_req_data_size() ))
697 return;
698 /* name is transferred in the unix codepage outside of the objattr structure */
699 if (objattr->name_len)
701 set_error( STATUS_INVALID_PARAMETER );
702 return;
705 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
707 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
708 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
710 reply->handle = 0;
711 if ((file = create_file( name, name_len, req->access,
712 req->sharing, req->create, req->options,
713 req->attrs, sd )))
715 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
716 release_object( file );
720 /* allocate a file handle for a Unix fd */
721 DECL_HANDLER(alloc_file_handle)
723 struct file *file;
724 int fd;
726 reply->handle = 0;
727 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
729 set_error( STATUS_INVALID_HANDLE );
730 return;
732 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
734 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
735 release_object( file );
739 /* lock a region of a file */
740 DECL_HANDLER(lock_file)
742 struct file *file;
744 if ((file = get_file_obj( current->process, req->handle, 0 )))
746 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
747 reply->overlapped = is_overlapped( file );
748 release_object( file );
752 /* unlock a region of a file */
753 DECL_HANDLER(unlock_file)
755 struct file *file;
757 if ((file = get_file_obj( current->process, req->handle, 0 )))
759 unlock_fd( file->fd, req->offset, req->count );
760 release_object( file );