Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side file management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 21 | #include "config.h" |
Marcus Meissner | 3f1ed52 | 2001-05-14 20:09:37 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <fcntl.h> |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 26 | #include <stdarg.h> |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
David Luyer | ee517e8 | 1999-02-28 12:27:56 +0000 | [diff] [blame] | 28 | #include <string.h> |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
Marcus Meissner | 8ba68fb | 1999-01-01 18:42:17 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 31 | #ifdef HAVE_SYS_ERRNO_H |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 32 | #include <sys/errno.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 33 | #endif |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 34 | #include <sys/stat.h> |
| 35 | #include <sys/time.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <time.h> |
| 38 | #include <unistd.h> |
Steven Edwards | 037c8a1 | 2003-02-11 22:27:13 +0000 | [diff] [blame] | 39 | #ifdef HAVE_UTIME_H |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 40 | #include <utime.h> |
Steven Edwards | 037c8a1 | 2003-02-11 22:27:13 +0000 | [diff] [blame] | 41 | #endif |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 42 | |
| 43 | #include "winerror.h" |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 44 | #include "windef.h" |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 45 | #include "winbase.h" |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 46 | #include "winreg.h" |
| 47 | #include "winternl.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 48 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 49 | #include "file.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 50 | #include "handle.h" |
| 51 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 52 | #include "request.h" |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 53 | #include "async.h" |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 54 | |
| 55 | struct file |
| 56 | { |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 57 | struct object obj; /* object header */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 58 | struct fd *fd; /* file descriptor for this file */ |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 59 | unsigned int access; /* file access (GENERIC_READ/WRITE) */ |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 60 | unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */ |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 61 | struct async_queue read_q; |
| 62 | struct async_queue write_q; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static void file_dump( struct object *obj, int verbose ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 66 | static struct fd *file_get_fd( struct object *obj ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 67 | static void file_destroy( struct object *obj ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 68 | |
| 69 | static int file_get_poll_events( struct fd *fd ); |
| 70 | static void file_poll_event( struct fd *fd, int event ); |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 71 | static int file_flush( struct fd *fd, struct event **event ); |
Alexandre Julliard | 18c08d3 | 2004-04-08 19:09:04 +0000 | [diff] [blame] | 72 | static int file_get_info( struct fd *fd, int *flags ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 73 | static void file_queue_async( struct fd *fd, void *ptr, unsigned int status, int type, int count ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 74 | |
| 75 | static const struct object_ops file_ops = |
| 76 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 77 | sizeof(struct file), /* size */ |
| 78 | file_dump, /* dump */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 79 | default_fd_add_queue, /* add_queue */ |
| 80 | default_fd_remove_queue, /* remove_queue */ |
| 81 | default_fd_signaled, /* signaled */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 82 | no_satisfied, /* satisfied */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 83 | file_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 84 | file_destroy /* destroy */ |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 87 | static const struct fd_ops file_fd_ops = |
| 88 | { |
| 89 | file_get_poll_events, /* get_poll_events */ |
| 90 | file_poll_event, /* poll_event */ |
| 91 | file_flush, /* flush */ |
| 92 | file_get_info, /* get_file_info */ |
| 93 | file_queue_async /* queue_async */ |
| 94 | }; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 95 | |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 96 | static inline int is_overlapped( const struct file *file ) |
| 97 | { |
| 98 | return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)); |
| 99 | } |
| 100 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 101 | /* create a file from a file descriptor */ |
| 102 | /* if the function fails the fd is closed */ |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 103 | static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing ) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 104 | { |
| 105 | struct file *file; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 106 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 107 | if ((file = alloc_object( &file_ops ))) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 108 | { |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 109 | file->access = access; |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 110 | file->options = FILE_SYNCHRONOUS_IO_NONALERT; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 111 | if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj ))) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 112 | { |
| 113 | release_object( file ); |
| 114 | return NULL; |
| 115 | } |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 116 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 117 | return file; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 120 | |
Alexandre Julliard | adc0610 | 2004-03-18 04:08:48 +0000 | [diff] [blame] | 121 | static struct object *create_file( const char *nameptr, size_t len, unsigned int access, |
| 122 | unsigned int sharing, int create, unsigned int options, |
Alexandre Julliard | 49b2f6d | 2004-04-06 23:41:01 +0000 | [diff] [blame] | 123 | unsigned int attrs ) |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 124 | { |
| 125 | struct file *file; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 126 | int flags; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 127 | char *name; |
Bernhard Rosenkraenzer | 5dda1f7 | 2001-07-23 18:09:41 +0000 | [diff] [blame] | 128 | mode_t mode; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 129 | |
| 130 | if (!(name = mem_alloc( len + 1 ))) return NULL; |
| 131 | memcpy( name, nameptr, len ); |
| 132 | name[len] = 0; |
| 133 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 134 | switch(create) |
| 135 | { |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 136 | case FILE_CREATE: flags = O_CREAT | O_EXCL; break; |
| 137 | case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */ |
| 138 | case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break; |
| 139 | case FILE_OPEN: flags = 0; break; |
| 140 | case FILE_OPEN_IF: flags = O_CREAT; break; |
| 141 | case FILE_OVERWRITE: flags = O_TRUNC; break; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 142 | default: set_error( STATUS_INVALID_PARAMETER ); goto error; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 143 | } |
| 144 | switch(access & (GENERIC_READ | GENERIC_WRITE)) |
| 145 | { |
| 146 | case 0: break; |
| 147 | case GENERIC_READ: flags |= O_RDONLY; break; |
| 148 | case GENERIC_WRITE: flags |= O_WRONLY; break; |
| 149 | case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break; |
| 150 | } |
Bernhard Rosenkraenzer | 5dda1f7 | 2001-07-23 18:09:41 +0000 | [diff] [blame] | 151 | mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666; |
| 152 | |
| 153 | if (len >= 4 && |
| 154 | (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" ))) |
| 155 | mode |= 0111; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 156 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 157 | if (!(file = alloc_object( &file_ops ))) goto error; |
| 158 | |
| 159 | file->access = access; |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 160 | file->options = options; |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 161 | if (is_overlapped( file )) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 162 | { |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 163 | init_async_queue (&file->read_q); |
| 164 | init_async_queue (&file->write_q); |
Marcus Meissner | 3f1ed52 | 2001-05-14 20:09:37 +0000 | [diff] [blame] | 165 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 166 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 167 | /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */ |
| 168 | if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) || |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 169 | !(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE, |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 170 | &mode, access, sharing, options ))) |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 171 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 172 | free( name ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 173 | release_object( file ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 174 | return NULL; |
| 175 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 176 | free( name ); |
| 177 | |
Alexandre Julliard | adc0610 | 2004-03-18 04:08:48 +0000 | [diff] [blame] | 178 | /* check for serial port */ |
| 179 | if (S_ISCHR(mode) && is_serial_fd( file->fd )) |
| 180 | { |
| 181 | struct object *obj = create_serial( file->fd, file->options ); |
| 182 | release_object( file ); |
| 183 | return obj; |
| 184 | } |
| 185 | |
| 186 | return &file->obj; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 187 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 188 | error: |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 189 | free( name ); |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Alexandre Julliard | 84fdfd0 | 2001-04-13 22:38:39 +0000 | [diff] [blame] | 193 | /* check if two file objects point to the same file */ |
| 194 | int is_same_file( struct file *file1, struct file *file2 ) |
| 195 | { |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 196 | return is_same_file_fd( file1->fd, file2->fd ); |
Alexandre Julliard | 84fdfd0 | 2001-04-13 22:38:39 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 199 | /* create a temp file for anonymous mappings */ |
| 200 | struct file *create_temp_file( int access ) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 201 | { |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 202 | char tmpfn[16]; |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 203 | int fd; |
| 204 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 205 | sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */ |
Alexandre Julliard | 2ee8b5b | 2003-03-20 21:07:49 +0000 | [diff] [blame] | 206 | fd = mkstemps( tmpfn, 0 ); |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 207 | if (fd == -1) |
| 208 | { |
| 209 | file_set_error(); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 210 | return NULL; |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 211 | } |
Marcus Meissner | 786d249 | 2002-07-29 23:55:39 +0000 | [diff] [blame] | 212 | unlink( tmpfn ); |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 213 | return create_file_for_fd( fd, access, 0 ); |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 216 | static void file_dump( struct object *obj, int verbose ) |
| 217 | { |
| 218 | struct file *file = (struct file *)obj; |
| 219 | assert( obj->ops == &file_ops ); |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 220 | fprintf( stderr, "File fd=%p options=%08x\n", file->fd, file->options ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 223 | static int file_get_poll_events( struct fd *fd ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 224 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 225 | struct file *file = get_fd_user( fd ); |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 226 | int events = 0; |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 227 | assert( file->obj.ops == &file_ops ); |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 228 | if (file->access & GENERIC_READ) events |= POLLIN; |
| 229 | if (file->access & GENERIC_WRITE) events |= POLLOUT; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 230 | return events; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 233 | static void file_poll_event( struct fd *fd, int event ) |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 234 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 235 | struct file *file = get_fd_user( fd ); |
| 236 | assert( file->obj.ops == &file_ops ); |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 237 | if (is_overlapped( file )) |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 238 | { |
| 239 | if( IS_READY(file->read_q) && (POLLIN & event) ) |
| 240 | { |
| 241 | async_notify(file->read_q.head, STATUS_ALERTED); |
| 242 | return; |
| 243 | } |
| 244 | if( IS_READY(file->write_q) && (POLLOUT & event) ) |
| 245 | { |
| 246 | async_notify(file->write_q.head, STATUS_ALERTED); |
| 247 | return; |
| 248 | } |
| 249 | } |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 250 | default_poll_event( fd, event ); |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 254 | static int file_flush( struct fd *fd, struct event **event ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 255 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 256 | int ret = (fsync( get_unix_fd(fd) ) != -1); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 257 | if (!ret) file_set_error(); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 258 | return ret; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Alexandre Julliard | 18c08d3 | 2004-04-08 19:09:04 +0000 | [diff] [blame] | 261 | static int file_get_info( struct fd *fd, int *flags ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 262 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 263 | struct file *file = get_fd_user( fd ); |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 264 | |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 265 | *flags = 0; |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 266 | if (is_overlapped( file )) *flags |= FD_FLAG_OVERLAPPED; |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 267 | return FD_TYPE_DEFAULT; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 270 | static void file_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count) |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 271 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 272 | struct file *file = get_fd_user( fd ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 273 | struct async *async; |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 274 | struct async_queue *q; |
| 275 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 276 | assert( file->obj.ops == &file_ops ); |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 277 | |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 278 | if (!is_overlapped( file )) |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 279 | { |
| 280 | set_error ( STATUS_INVALID_HANDLE ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 281 | return; |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | switch(type) |
| 285 | { |
| 286 | case ASYNC_TYPE_READ: |
| 287 | q = &file->read_q; |
| 288 | break; |
| 289 | case ASYNC_TYPE_WRITE: |
| 290 | q = &file->write_q; |
| 291 | break; |
| 292 | default: |
| 293 | set_error( STATUS_INVALID_PARAMETER ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 294 | return; |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 297 | async = find_async ( q, current, ptr ); |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 298 | |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 299 | if ( status == STATUS_PENDING ) |
| 300 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 301 | int events; |
Martin Wilck | ff1f320 | 2002-04-26 18:31:19 +0000 | [diff] [blame] | 302 | |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 303 | if ( !async ) |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 304 | async = create_async ( &file->obj, current, ptr ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 305 | if ( !async ) |
| 306 | return; |
| 307 | |
| 308 | async->status = STATUS_PENDING; |
| 309 | if ( !async->q ) |
| 310 | async_insert( q, async ); |
Martin Wilck | ff1f320 | 2002-04-26 18:31:19 +0000 | [diff] [blame] | 311 | |
| 312 | /* Check if the new pending request can be served immediately */ |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 313 | events = check_fd_events( fd, file_get_poll_events( fd ) ); |
| 314 | if (events) file_poll_event ( fd, events ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 315 | } |
| 316 | else if ( async ) destroy_async ( async ); |
| 317 | else set_error ( STATUS_INVALID_PARAMETER ); |
| 318 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 319 | set_fd_events( fd, file_get_poll_events( fd )); |
| 320 | } |
| 321 | |
| 322 | static struct fd *file_get_fd( struct object *obj ) |
| 323 | { |
| 324 | struct file *file = (struct file *)obj; |
| 325 | assert( obj->ops == &file_ops ); |
| 326 | return (struct fd *)grab_object( file->fd ); |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 329 | static void file_destroy( struct object *obj ) |
| 330 | { |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 331 | struct file *file = (struct file *)obj; |
| 332 | assert( obj->ops == &file_ops ); |
| 333 | |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 334 | if (is_overlapped( file )) |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 335 | { |
| 336 | destroy_async_queue (&file->read_q); |
| 337 | destroy_async_queue (&file->write_q); |
| 338 | } |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 339 | if (file->fd) release_object( file->fd ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 342 | /* set the last error depending on errno */ |
| 343 | void file_set_error(void) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 344 | { |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 345 | switch (errno) |
| 346 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 347 | case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break; |
| 348 | case EBADF: set_error( STATUS_INVALID_HANDLE ); break; |
| 349 | case ENOSPC: set_error( STATUS_DISK_FULL ); break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 350 | case EACCES: |
Alexandre Julliard | 5f258c6 | 2001-07-14 00:50:30 +0000 | [diff] [blame] | 351 | case ESRCH: |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 352 | case EPERM: set_error( STATUS_ACCESS_DENIED ); break; |
| 353 | case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break; |
| 354 | case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break; |
| 355 | case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break; |
Alexandre Julliard | 716878c | 2004-04-16 23:32:40 +0000 | [diff] [blame] | 356 | case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 357 | case ENFILE: |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 358 | case EMFILE: set_error( STATUS_NO_MORE_FILES ); break; |
| 359 | case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break; |
| 360 | case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break; |
Alexandre Julliard | d5b4232 | 2003-12-10 01:12:18 +0000 | [diff] [blame] | 361 | case ESPIPE: set_win32_error( ERROR_SEEK ); break; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 362 | case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break; |
| 363 | case EIO: set_error( STATUS_ACCESS_VIOLATION ); break; |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 364 | case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break; |
Alexandre Julliard | 72f87b8 | 2004-05-01 02:50:06 +0000 | [diff] [blame] | 365 | case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break; |
Wim Lewis | 99a01c0 | 2004-01-02 20:11:35 +0000 | [diff] [blame] | 366 | #ifdef EOVERFLOW |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 367 | case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break; |
Wim Lewis | 99a01c0 | 2004-01-02 20:11:35 +0000 | [diff] [blame] | 368 | #endif |
Filip Navara | 2392a36 | 2004-04-12 23:15:11 +0000 | [diff] [blame] | 369 | default: perror("file_set_error"); set_error( STATUS_UNSUCCESSFUL ); break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 370 | } |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 373 | struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 374 | { |
Alexandre Julliard | 61ec6c1 | 1999-11-29 02:17:08 +0000 | [diff] [blame] | 375 | return (struct file *)get_handle_obj( process, handle, access, &file_ops ); |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 376 | } |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 377 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 378 | int get_file_unix_fd( struct file *file ) |
| 379 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 380 | return get_unix_fd( file->fd ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 383 | /* extend a file beyond the current end of file */ |
| 384 | static int extend_file( struct file *file, off_t size ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 385 | { |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 386 | static const char zero; |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 387 | int unix_fd = get_file_unix_fd( file ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 388 | |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 389 | /* extend the file one byte beyond the requested size and then truncate it */ |
| 390 | /* this should work around ftruncate implementations that can't extend files */ |
Alexandre Julliard | 72f87b8 | 2004-05-01 02:50:06 +0000 | [diff] [blame] | 391 | if (pwrite( unix_fd, &zero, 1, size ) != -1) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 392 | { |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 393 | ftruncate( unix_fd, size ); |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 394 | return 1; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 395 | } |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 396 | file_set_error(); |
| 397 | return 0; |
| 398 | } |
| 399 | |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 400 | /* try to grow the file to the specified size */ |
| 401 | int grow_file( struct file *file, int size_high, int size_low ) |
| 402 | { |
Marcus Meissner | 6bb990f | 2001-05-29 20:55:21 +0000 | [diff] [blame] | 403 | struct stat st; |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 404 | int unix_fd = get_file_unix_fd( file ); |
Alexandre Julliard | 72f87b8 | 2004-05-01 02:50:06 +0000 | [diff] [blame] | 405 | off_t size = size_low + (((off_t)size_high)<<32); |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 406 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 407 | if (fstat( unix_fd, &st ) == -1) |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 408 | { |
| 409 | file_set_error(); |
| 410 | return 0; |
| 411 | } |
Marcus Meissner | 3f1ed52 | 2001-05-14 20:09:37 +0000 | [diff] [blame] | 412 | if (st.st_size >= size) return 1; /* already large enough */ |
Alexandre Julliard | 72f87b8 | 2004-05-01 02:50:06 +0000 | [diff] [blame] | 413 | return extend_file( file, size ); |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 416 | /* create a file */ |
| 417 | DECL_HANDLER(create_file) |
| 418 | { |
Alexandre Julliard | adc0610 | 2004-03-18 04:08:48 +0000 | [diff] [blame] | 419 | struct object *file; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 420 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 421 | reply->handle = 0; |
| 422 | if ((file = create_file( get_req_data(), get_req_data_size(), req->access, |
Alexandre Julliard | 49b2f6d | 2004-04-06 23:41:01 +0000 | [diff] [blame] | 423 | req->sharing, req->create, req->options, req->attrs ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 424 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 425 | reply->handle = alloc_handle( current->process, file, req->access, req->inherit ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 426 | release_object( file ); |
| 427 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* allocate a file handle for a Unix fd */ |
| 431 | DECL_HANDLER(alloc_file_handle) |
| 432 | { |
| 433 | struct file *file; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 434 | int fd; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 435 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 436 | reply->handle = 0; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 437 | if ((fd = thread_get_inflight_fd( current, req->fd )) == -1) |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 438 | { |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 439 | set_error( STATUS_INVALID_HANDLE ); |
| 440 | return; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 441 | } |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 442 | if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE ))) |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 443 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 444 | reply->handle = alloc_handle( current->process, file, req->access, req->inherit ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 445 | release_object( file ); |
| 446 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 449 | /* lock a region of a file */ |
| 450 | DECL_HANDLER(lock_file) |
| 451 | { |
| 452 | struct file *file; |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 453 | file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low; |
| 454 | file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 455 | |
| 456 | if ((file = get_file_obj( current->process, req->handle, 0 ))) |
| 457 | { |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 458 | reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait ); |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 459 | reply->overlapped = is_overlapped( file ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 460 | release_object( file ); |
| 461 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* unlock a region of a file */ |
| 465 | DECL_HANDLER(unlock_file) |
| 466 | { |
| 467 | struct file *file; |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 468 | file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low; |
| 469 | file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 470 | |
| 471 | if ((file = get_file_obj( current->process, req->handle, 0 ))) |
| 472 | { |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 473 | unlock_fd( file->fd, offset, count ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 474 | release_object( file ); |
| 475 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 476 | } |