server: Store a mapping instead of a file for process dlls.
[wine] / server / file.c
1 /*
2  * Server-side file management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
45
46 #include "ntstatus.h"
47 #define WIN32_NO_STATUS
48 #include "windef.h"
49 #include "winternl.h"
50
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
55 #include "process.h"
56 #include "security.h"
57
58 struct file
59 {
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 */
65 };
66
67 static unsigned int generic_file_map_access( unsigned int access );
68
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 );
74
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
79 static const struct object_ops file_ops =
80 {
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 */
97 };
98
99 static const struct fd_ops file_fd_ops =
100 {
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 */
109 };
110
111 static inline int is_overlapped( const struct file *file )
112 {
113     return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
114 }
115
116 /* create a file from a file descriptor */
117 /* if the function fails the fd is closed */
118 struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
119 {
120     struct file *file;
121     struct stat st;
122
123     if (fstat( fd, &st ) == -1)
124     {
125         file_set_error();
126         return NULL;
127     }
128
129     if ((file = alloc_object( &file_ops )))
130     {
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 )))
135         {
136             release_object( file );
137             return NULL;
138         }
139         allow_fd_caching( file->fd );
140     }
141     return file;
142 }
143
144 /* create a file by duplicating an fd object */
145 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
146 {
147     struct file *file;
148     struct stat st;
149
150     if (fstat( get_unix_fd(fd), &st ) == -1)
151     {
152         file_set_error();
153         return NULL;
154     }
155
156     if ((file = alloc_object( &file_ops )))
157     {
158         file->mode = st.st_mode;
159         file->access = default_fd_map_access( &file->obj, access );
160         if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
161         {
162             release_object( file );
163             return NULL;
164         }
165     }
166     return file;
167 }
168
169 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
170 {
171     struct file *file = alloc_object( &file_ops );
172
173     if (!file) return NULL;
174     file->access  = access;
175     file->mode    = mode;
176     file->uid     = ~(uid_t)0;
177     file->fd      = fd;
178     grab_object( fd );
179     set_fd_user( fd, &file_fd_ops, &file->obj );
180     return &file->obj;
181 }
182
183 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
184                                    unsigned int access, unsigned int sharing, int create,
185                                    unsigned int options, unsigned int attrs,
186                                    const struct security_descriptor *sd )
187 {
188     struct object *obj = NULL;
189     struct fd *fd;
190     int flags;
191     char *name;
192     mode_t mode;
193
194     if (!len || ((nameptr[0] == '/') ^ !root))
195     {
196         set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
197         return NULL;
198     }
199     if (!(name = mem_alloc( len + 1 ))) return NULL;
200     memcpy( name, nameptr, len );
201     name[len] = 0;
202
203     switch(create)
204     {
205     case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
206     case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
207                             access |= FILE_WRITE_ATTRIBUTES;
208     case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
209     case FILE_OPEN:         flags = 0; break;
210     case FILE_OPEN_IF:      flags = O_CREAT; break;
211     case FILE_OVERWRITE:    flags = O_TRUNC;
212                             access |= FILE_WRITE_ATTRIBUTES; break;
213     default:                set_error( STATUS_INVALID_PARAMETER ); goto done;
214     }
215
216     if (sd)
217     {
218         const SID *owner = sd_get_owner( sd );
219         if (!owner)
220             owner = token_get_user( current->process->token );
221         mode = sd_to_mode( sd, owner );
222     }
223     else
224         mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
225
226     if (len >= 4 &&
227         (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
228     {
229         if (mode & S_IRUSR)
230             mode |= S_IXUSR;
231         if (mode & S_IRGRP)
232             mode |= S_IXGRP;
233         if (mode & S_IROTH)
234             mode |= S_IXOTH;
235     }
236
237     access = generic_file_map_access( access );
238
239     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
240     fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
241     if (!fd) goto done;
242
243     if (S_ISDIR(mode))
244         obj = create_dir_obj( fd, access, mode );
245     else if (S_ISCHR(mode) && is_serial_fd( fd ))
246         obj = create_serial( fd );
247     else
248         obj = create_file_obj( fd, access, mode );
249
250     release_object( fd );
251
252 done:
253     free( name );
254     return obj;
255 }
256
257 /* check if two file objects point to the same file */
258 int is_same_file( struct file *file1, struct file *file2 )
259 {
260     return is_same_file_fd( file1->fd, file2->fd );
261 }
262
263 static void file_dump( struct object *obj, int verbose )
264 {
265     struct file *file = (struct file *)obj;
266     assert( obj->ops == &file_ops );
267     fprintf( stderr, "File fd=%p\n", file->fd );
268 }
269
270 static int file_get_poll_events( struct fd *fd )
271 {
272     struct file *file = get_fd_user( fd );
273     int events = 0;
274     assert( file->obj.ops == &file_ops );
275     if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
276     if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
277     return events;
278 }
279
280 static void file_flush( struct fd *fd, struct event **event )
281 {
282     int unix_fd = get_unix_fd( fd );
283     if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
284 }
285
286 static enum server_fd_type file_get_fd_type( struct fd *fd )
287 {
288     struct file *file = get_fd_user( fd );
289
290     if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
291     if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
292     return FD_TYPE_CHAR;
293 }
294
295 static struct fd *file_get_fd( struct object *obj )
296 {
297     struct file *file = (struct file *)obj;
298     assert( obj->ops == &file_ops );
299     return (struct fd *)grab_object( file->fd );
300 }
301
302 static unsigned int generic_file_map_access( unsigned int access )
303 {
304     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
305     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
306     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
307     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
308     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
309 }
310
311 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
312 {
313     struct security_descriptor *sd;
314     size_t dacl_size;
315     ACE_HEADER *current_ace;
316     ACCESS_ALLOWED_ACE *aaa;
317     ACL *dacl;
318     SID *sid;
319     char *ptr;
320     const SID *world_sid = security_world_sid;
321     const SID *local_system_sid = security_local_system_sid;
322
323     dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
324         FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
325     if (mode & S_IRWXU)
326         dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
327             FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
328     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
329         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
330         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
331         dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
332             FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
333     if (mode & S_IRWXO)
334         dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
335             FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
336
337     sd = mem_alloc( sizeof(struct security_descriptor) +
338                     FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
339                     FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
340                     dacl_size );
341     if (!sd) return sd;
342
343     sd->control = SE_DACL_PRESENT;
344     sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
345     sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
346     sd->sacl_len = 0;
347     sd->dacl_len = dacl_size;
348
349     ptr = (char *)(sd + 1);
350     memcpy( ptr, user, sd->owner_len );
351     ptr += sd->owner_len;
352     memcpy( ptr, group, sd->group_len );
353     ptr += sd->group_len;
354
355     dacl = (ACL *)ptr;
356     dacl->AclRevision = ACL_REVISION;
357     dacl->Sbz1 = 0;
358     dacl->AclSize = dacl_size;
359     dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
360     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
361         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
362         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
363         dacl->AceCount++;
364     dacl->Sbz2 = 0;
365
366     /* always give FILE_ALL_ACCESS for Local System */
367     aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
368     current_ace = &aaa->Header;
369     aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
370     aaa->Header.AceFlags = 0;
371     aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
372         FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
373     aaa->Mask = FILE_ALL_ACCESS;
374     sid = (SID *)&aaa->SidStart;
375     memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
376
377     if (mode & S_IRWXU)
378     {
379         /* appropriate access rights for the user */
380         aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
381         current_ace = &aaa->Header;
382         aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
383         aaa->Header.AceFlags = 0;
384         aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
385                               FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
386         aaa->Mask = WRITE_DAC | WRITE_OWNER;
387         if (mode & S_IRUSR)
388             aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
389         if (mode & S_IWUSR)
390             aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
391         sid = (SID *)&aaa->SidStart;
392         memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
393     }
394     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
395         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
396         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
397     {
398         /* deny just in case the user is a member of the group */
399         ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
400         current_ace = &ada->Header;
401         ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
402         ada->Header.AceFlags = 0;
403         ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
404                               FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
405         ada->Mask = 0;
406         if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
407             ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
408         if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
409             ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
410         ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
411         sid = (SID *)&ada->SidStart;
412         memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
413     }
414     if (mode & S_IRWXO)
415     {
416         /* appropriate access rights for Everyone */
417         aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
418         current_ace = &aaa->Header;
419         aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
420         aaa->Header.AceFlags = 0;
421         aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
422                              FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
423         aaa->Mask = 0;
424         if (mode & S_IROTH)
425             aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
426         if (mode & S_IWOTH)
427             aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
428         sid = (SID *)&aaa->SidStart;
429         memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
430     }
431
432     return sd;
433 }
434
435 static struct security_descriptor *file_get_sd( struct object *obj )
436 {
437     struct file *file = (struct file *)obj;
438     struct stat st;
439     int unix_fd;
440     struct security_descriptor *sd;
441
442     assert( obj->ops == &file_ops );
443
444     unix_fd = get_file_unix_fd( file );
445
446     if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
447         return obj->sd;
448
449     /* mode and uid the same? if so, no need to re-generate security descriptor */
450     if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
451         (st.st_uid == file->uid))
452         return obj->sd;
453
454     sd = mode_to_sd( st.st_mode,
455                      security_unix_uid_to_sid( st.st_uid ),
456                      token_get_primary_group( current->process->token ));
457     if (!sd) return obj->sd;
458
459     file->mode = st.st_mode;
460     file->uid = st.st_uid;
461     free( obj->sd );
462     obj->sd = sd;
463     return sd;
464 }
465
466 static mode_t file_access_to_mode( unsigned int access )
467 {
468     mode_t mode = 0;
469
470     access = generic_file_map_access( access );
471     if (access & FILE_READ_DATA)  mode |= 4;
472     if (access & FILE_WRITE_DATA) mode |= 2;
473     if (access & FILE_EXECUTE)    mode |= 1;
474     return mode;
475 }
476
477 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
478 {
479     mode_t new_mode = 0;
480     mode_t denied_mode = 0;
481     mode_t mode;
482     int present;
483     const ACL *dacl = sd_get_dacl( sd, &present );
484     const SID *user = token_get_user( current->process->token );
485     if (present && dacl)
486     {
487         const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
488         ULONG i;
489         for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
490         {
491             const ACCESS_ALLOWED_ACE *aa_ace;
492             const ACCESS_DENIED_ACE *ad_ace;
493             const SID *sid;
494
495             if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
496
497             switch (ace->AceType)
498             {
499                 case ACCESS_DENIED_ACE_TYPE:
500                     ad_ace = (const ACCESS_DENIED_ACE *)ace;
501                     sid = (const SID *)&ad_ace->SidStart;
502                     mode = file_access_to_mode( ad_ace->Mask );
503                     if (security_equal_sid( sid, security_world_sid ))
504                     {
505                         denied_mode |= (mode << 6) | (mode << 3) | mode; /* all */
506                     }
507                     else if (security_equal_sid( sid, owner ))
508                     {
509                         denied_mode |= (mode << 6);  /* user only */
510                     }
511                     else if ((security_equal_sid( user, owner ) &&
512                               token_sid_present( current->process->token, sid, TRUE )))
513                     {
514                         denied_mode |= (mode << 6) | (mode << 3);  /* user + group */
515                     }
516                     break;
517                 case ACCESS_ALLOWED_ACE_TYPE:
518                     aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
519                     sid = (const SID *)&aa_ace->SidStart;
520                     mode = file_access_to_mode( aa_ace->Mask );
521                     if (security_equal_sid( sid, security_world_sid ))
522                     {
523                         new_mode |= (mode << 6) | (mode << 3) | mode;  /* all */
524                     }
525                     else if (security_equal_sid( sid, owner ))
526                     {
527                         new_mode |= (mode << 6);  /* user only */
528                     }
529                     else if ((security_equal_sid( user, owner ) &&
530                               token_sid_present( current->process->token, sid, FALSE )))
531                     {
532                         new_mode |= (mode << 6) | (mode << 3);  /* user + group */
533                     }
534                     break;
535             }
536         }
537     }
538     else
539         /* no ACL means full access rights to anyone */
540         new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
541
542     return new_mode & ~denied_mode;
543 }
544
545 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
546                         unsigned int set_info )
547 {
548     struct file *file = (struct file *)obj;
549     const SID *owner;
550     struct stat st;
551     mode_t mode;
552     int unix_fd;
553
554     assert( obj->ops == &file_ops );
555
556     unix_fd = get_file_unix_fd( file );
557
558     if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
559
560     if (set_info & OWNER_SECURITY_INFORMATION)
561     {
562         owner = sd_get_owner( sd );
563         if (!owner)
564         {
565             set_error( STATUS_INVALID_SECURITY_DESCR );
566             return 0;
567         }
568         if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
569         {
570             /* FIXME: get Unix uid and call fchown */
571         }
572     }
573     else if (obj->sd)
574         owner = sd_get_owner( obj->sd );
575     else
576         owner = token_get_user( current->process->token );
577
578     /* group and sacl not supported */
579
580     if (set_info & DACL_SECURITY_INFORMATION)
581     {
582         /* keep the bits that we don't map to access rights in the ACL */
583         mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
584         mode |= sd_to_mode( sd, owner );
585
586         if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
587         {
588             file_set_error();
589             return 0;
590         }
591     }
592     return 1;
593 }
594
595 static void file_destroy( struct object *obj )
596 {
597     struct file *file = (struct file *)obj;
598     assert( obj->ops == &file_ops );
599
600     if (file->fd) release_object( file->fd );
601 }
602
603 /* set the last error depending on errno */
604 void file_set_error(void)
605 {
606     switch (errno)
607     {
608     case ETXTBSY:
609     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
610     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
611     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
612     case EACCES:
613     case ESRCH:
614     case EROFS:
615     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
616     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
617     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
618     case EISDIR:    set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
619     case ENFILE:
620     case EMFILE:    set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
621     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
622     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
623     case ESPIPE:    set_error( STATUS_ILLEGAL_FUNCTION ); break;
624     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
625     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
626     case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
627     case EFBIG:     set_error( STATUS_SECTION_TOO_BIG ); break;
628     case ENODEV:    set_error( STATUS_NO_SUCH_DEVICE ); break;
629     case ENXIO:     set_error( STATUS_NO_SUCH_DEVICE ); break;
630 #ifdef EOVERFLOW
631     case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
632 #endif
633     default:
634         perror("wineserver: file_set_error() can't map error");
635         set_error( STATUS_UNSUCCESSFUL );
636         break;
637     }
638 }
639
640 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
641 {
642     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
643 }
644
645 int get_file_unix_fd( struct file *file )
646 {
647     return get_unix_fd( file->fd );
648 }
649
650 /* create a file */
651 DECL_HANDLER(create_file)
652 {
653     struct object *file;
654     struct fd *root_fd = NULL;
655     const struct object_attributes *objattr = get_req_data();
656     const struct security_descriptor *sd;
657     const char *name;
658     data_size_t name_len;
659
660     reply->handle = 0;
661
662     if (!objattr_is_valid( objattr, get_req_data_size() ))
663         return;
664     /* name is transferred in the unix codepage outside of the objattr structure */
665     if (objattr->name_len)
666     {
667         set_error( STATUS_INVALID_PARAMETER );
668         return;
669     }
670
671     if (objattr->rootdir)
672     {
673         struct dir *root;
674
675         if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
676         root_fd = get_obj_fd( (struct object *)root );
677         release_object( root );
678         if (!root_fd) return;
679     }
680
681     sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
682
683     name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
684     name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
685
686     reply->handle = 0;
687     if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
688                              req->create, req->options, req->attrs, sd )))
689     {
690         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
691         release_object( file );
692     }
693     if (root_fd) release_object( root_fd );
694 }
695
696 /* allocate a file handle for a Unix fd */
697 DECL_HANDLER(alloc_file_handle)
698 {
699     struct file *file;
700     int fd;
701
702     reply->handle = 0;
703     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
704     {
705         set_error( STATUS_INVALID_HANDLE );
706         return;
707     }
708     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
709     {
710         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
711         release_object( file );
712     }
713 }
714
715 /* lock a region of a file */
716 DECL_HANDLER(lock_file)
717 {
718     struct file *file;
719
720     if ((file = get_file_obj( current->process, req->handle, 0 )))
721     {
722         reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
723         reply->overlapped = is_overlapped( file );
724         release_object( file );
725     }
726 }
727
728 /* unlock a region of a file */
729 DECL_HANDLER(unlock_file)
730 {
731     struct file *file;
732
733     if ((file = get_file_obj( current->process, req->handle, 0 )))
734     {
735         unlock_fd( file->fd, req->offset, req->count );
736         release_object( file );
737     }
738 }