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> |
| 26 | #include <stdio.h> |
David Luyer | ee517e8 | 1999-02-28 12:27:56 +0000 | [diff] [blame] | 27 | #include <string.h> |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
Marcus Meissner | 8ba68fb | 1999-01-01 18:42:17 +0000 | [diff] [blame] | 29 | #include <errno.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 30 | #ifdef HAVE_SYS_ERRNO_H |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 31 | #include <sys/errno.h> |
Howard Abrams | 1327748 | 1999-07-10 13:16:29 +0000 | [diff] [blame] | 32 | #endif |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <sys/time.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <time.h> |
| 37 | #include <unistd.h> |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 38 | #include <utime.h> |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 39 | |
| 40 | #include "winerror.h" |
Michael Veksler | f935c59 | 1999-02-09 15:49:39 +0000 | [diff] [blame] | 41 | #include "winbase.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 42 | |
| 43 | #include "handle.h" |
| 44 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 45 | #include "request.h" |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 46 | #include "async.h" |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 47 | |
| 48 | struct file |
| 49 | { |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 50 | struct object obj; /* object header */ |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 51 | struct file *next; /* next file in hashing list */ |
| 52 | char *name; /* file name */ |
| 53 | unsigned int access; /* file access (GENERIC_READ/WRITE) */ |
| 54 | unsigned int flags; /* flags (FILE_FLAG_*) */ |
| 55 | unsigned int sharing; /* file sharing mode */ |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 56 | int drive_type; /* type of drive the file is on */ |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 57 | struct async_queue read_q; |
| 58 | struct async_queue write_q; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 61 | #define NAME_HASH_SIZE 37 |
| 62 | |
| 63 | static struct file *file_hash[NAME_HASH_SIZE]; |
| 64 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 65 | static void file_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 66 | static int file_get_poll_events( struct object *obj ); |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 67 | static void file_poll_event( struct object *obj, int event ); |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 68 | static int file_get_fd( struct object *obj ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 69 | static int file_flush( struct object *obj ); |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 70 | static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 71 | static void file_destroy( struct object *obj ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 72 | static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 73 | |
| 74 | static const struct object_ops file_ops = |
| 75 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 76 | sizeof(struct file), /* size */ |
| 77 | file_dump, /* dump */ |
| 78 | default_poll_add_queue, /* add_queue */ |
| 79 | default_poll_remove_queue, /* remove_queue */ |
| 80 | default_poll_signaled, /* signaled */ |
| 81 | no_satisfied, /* satisfied */ |
| 82 | file_get_poll_events, /* get_poll_events */ |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 83 | file_poll_event, /* poll_event */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 84 | file_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 85 | file_flush, /* flush */ |
| 86 | file_get_info, /* get_file_info */ |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 87 | file_queue_async, /* queue_async */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 88 | file_destroy /* destroy */ |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 91 | |
| 92 | static int get_name_hash( const char *name ) |
| 93 | { |
| 94 | int hash = 0; |
Eric Pouech | bf2b765 | 1999-11-15 00:07:30 +0000 | [diff] [blame] | 95 | while (*name) hash ^= (unsigned char)*name++; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 96 | return hash % NAME_HASH_SIZE; |
| 97 | } |
| 98 | |
| 99 | /* check if the desired access is possible without violating */ |
| 100 | /* the sharing mode of other opens of the same file */ |
| 101 | static int check_sharing( const char *name, int hash, unsigned int access, |
| 102 | unsigned int sharing ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 103 | { |
| 104 | struct file *file; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 105 | unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 106 | unsigned int existing_access = 0; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 107 | |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 108 | for (file = file_hash[hash]; file; file = file->next) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 109 | { |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 110 | if (strcmp( file->name, name )) continue; |
| 111 | existing_sharing &= file->sharing; |
| 112 | existing_access |= file->access; |
| 113 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 114 | if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error; |
| 115 | if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error; |
| 116 | if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error; |
| 117 | if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 118 | return 1; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 119 | error: |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 120 | set_error( STATUS_SHARING_VIOLATION ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 121 | return 0; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 124 | /* create a file from a file descriptor */ |
| 125 | /* if the function fails the fd is closed */ |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 126 | static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing, |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 127 | unsigned int attrs, int drive_type ) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 128 | { |
| 129 | struct file *file; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 130 | if ((file = alloc_object( &file_ops, fd ))) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 131 | { |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 132 | file->name = NULL; |
| 133 | file->next = NULL; |
| 134 | file->access = access; |
| 135 | file->flags = attrs; |
| 136 | file->sharing = sharing; |
| 137 | file->drive_type = drive_type; |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 138 | if (file->flags & FILE_FLAG_OVERLAPPED) |
| 139 | { |
| 140 | init_async_queue (&file->read_q); |
| 141 | init_async_queue (&file->write_q); |
| 142 | } |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 143 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 144 | return file; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 147 | |
| 148 | static struct file *create_file( const char *nameptr, size_t len, unsigned int access, |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 149 | unsigned int sharing, int create, unsigned int attrs, |
| 150 | int drive_type ) |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 151 | { |
| 152 | struct file *file; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 153 | int hash, flags; |
| 154 | struct stat st; |
| 155 | char *name; |
| 156 | int fd = -1; |
Bernhard Rosenkraenzer | 5dda1f7 | 2001-07-23 18:09:41 +0000 | [diff] [blame] | 157 | mode_t mode; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 158 | |
| 159 | if (!(name = mem_alloc( len + 1 ))) return NULL; |
| 160 | memcpy( name, nameptr, len ); |
| 161 | name[len] = 0; |
| 162 | |
| 163 | /* check sharing mode */ |
| 164 | hash = get_name_hash( name ); |
| 165 | if (!check_sharing( name, hash, access, sharing )) goto error; |
| 166 | |
| 167 | switch(create) |
| 168 | { |
| 169 | case CREATE_NEW: flags = O_CREAT | O_EXCL; break; |
| 170 | case CREATE_ALWAYS: flags = O_CREAT | O_TRUNC; break; |
| 171 | case OPEN_ALWAYS: flags = O_CREAT; break; |
| 172 | case TRUNCATE_EXISTING: flags = O_TRUNC; break; |
| 173 | case OPEN_EXISTING: flags = 0; break; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 174 | default: set_error( STATUS_INVALID_PARAMETER ); goto error; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 175 | } |
| 176 | switch(access & (GENERIC_READ | GENERIC_WRITE)) |
| 177 | { |
| 178 | case 0: break; |
| 179 | case GENERIC_READ: flags |= O_RDONLY; break; |
| 180 | case GENERIC_WRITE: flags |= O_WRONLY; break; |
| 181 | case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break; |
| 182 | } |
Bernhard Rosenkraenzer | 5dda1f7 | 2001-07-23 18:09:41 +0000 | [diff] [blame] | 183 | mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666; |
| 184 | |
| 185 | if (len >= 4 && |
| 186 | (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" ))) |
| 187 | mode |= 0111; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 188 | |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 189 | /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */ |
Bernhard Rosenkraenzer | 5dda1f7 | 2001-07-23 18:09:41 +0000 | [diff] [blame] | 190 | if ((fd = open( name, flags | O_NONBLOCK | O_LARGEFILE, mode )) == -1 ) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 191 | goto file_error; |
| 192 | /* refuse to open a directory */ |
| 193 | if (fstat( fd, &st ) == -1) goto file_error; |
| 194 | if (S_ISDIR(st.st_mode)) |
| 195 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 196 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 197 | goto error; |
Marcus Meissner | 3f1ed52 | 2001-05-14 20:09:37 +0000 | [diff] [blame] | 198 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 199 | |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 200 | if (!(file = create_file_for_fd( fd, access, sharing, attrs, drive_type ))) |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 201 | { |
| 202 | free( name ); |
| 203 | return NULL; |
| 204 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 205 | file->name = name; |
| 206 | file->next = file_hash[hash]; |
| 207 | file_hash[hash] = file; |
| 208 | return file; |
| 209 | |
| 210 | file_error: |
| 211 | file_set_error(); |
| 212 | error: |
| 213 | if (fd != -1) close( fd ); |
| 214 | free( name ); |
| 215 | return NULL; |
| 216 | } |
| 217 | |
Alexandre Julliard | 84fdfd0 | 2001-04-13 22:38:39 +0000 | [diff] [blame] | 218 | /* check if two file objects point to the same file */ |
| 219 | int is_same_file( struct file *file1, struct file *file2 ) |
| 220 | { |
| 221 | return !strcmp( file1->name, file2->name ); |
| 222 | } |
| 223 | |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 224 | /* get the type of drive the file is on */ |
| 225 | int get_file_drive_type( struct file *file ) |
| 226 | { |
| 227 | return file->drive_type; |
| 228 | } |
| 229 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 230 | /* Create an anonymous Unix file */ |
| 231 | int create_anonymous_file(void) |
| 232 | { |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 233 | char *name; |
| 234 | int fd; |
| 235 | |
| 236 | do |
| 237 | { |
| 238 | if (!(name = tmpnam(NULL))) |
| 239 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 240 | set_error( STATUS_TOO_MANY_OPENED_FILES ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 241 | return -1; |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 242 | } |
| 243 | fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 ); |
| 244 | } while ((fd == -1) && (errno == EEXIST)); |
| 245 | if (fd == -1) |
| 246 | { |
| 247 | file_set_error(); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 248 | return -1; |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 249 | } |
| 250 | unlink( name ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 251 | return fd; |
| 252 | } |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 253 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 254 | /* Create a temp file for anonymous mappings */ |
| 255 | struct file *create_temp_file( int access ) |
| 256 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 257 | int fd; |
| 258 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 259 | if ((fd = create_anonymous_file()) == -1) return NULL; |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 260 | return create_file_for_fd( fd, access, 0, 0, DRIVE_FIXED ); |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 263 | static void file_dump( struct object *obj, int verbose ) |
| 264 | { |
| 265 | struct file *file = (struct file *)obj; |
| 266 | assert( obj->ops == &file_ops ); |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 267 | fprintf( stderr, "File fd=%d flags=%08x name='%s'\n", file->obj.fd, file->flags, file->name ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 270 | static int file_get_poll_events( struct object *obj ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 271 | { |
| 272 | struct file *file = (struct file *)obj; |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 273 | int events = 0; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 274 | assert( obj->ops == &file_ops ); |
Alexandre Julliard | 247b8ae | 1999-12-13 00:16:44 +0000 | [diff] [blame] | 275 | if (file->access & GENERIC_READ) events |= POLLIN; |
| 276 | if (file->access & GENERIC_WRITE) events |= POLLOUT; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 277 | return events; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 280 | static void file_poll_event( struct object *obj, int event ) |
| 281 | { |
| 282 | struct file *file = (struct file *)obj; |
| 283 | assert( obj->ops == &file_ops ); |
| 284 | if ( file->flags & FILE_FLAG_OVERLAPPED ) |
| 285 | { |
| 286 | if( IS_READY(file->read_q) && (POLLIN & event) ) |
| 287 | { |
| 288 | async_notify(file->read_q.head, STATUS_ALERTED); |
| 289 | return; |
| 290 | } |
| 291 | if( IS_READY(file->write_q) && (POLLOUT & event) ) |
| 292 | { |
| 293 | async_notify(file->write_q.head, STATUS_ALERTED); |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | default_poll_event( obj, event ); |
| 298 | } |
| 299 | |
| 300 | |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 301 | static int file_get_fd( struct object *obj ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 302 | { |
| 303 | struct file *file = (struct file *)obj; |
| 304 | assert( obj->ops == &file_ops ); |
Alexandre Julliard | 63411db | 2000-12-22 21:12:36 +0000 | [diff] [blame] | 305 | return file->obj.fd; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static int file_flush( struct object *obj ) |
| 309 | { |
| 310 | int ret; |
| 311 | struct file *file = (struct file *)grab_object(obj); |
| 312 | assert( obj->ops == &file_ops ); |
| 313 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 314 | ret = (fsync( file->obj.fd ) != -1); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 315 | if (!ret) file_set_error(); |
| 316 | release_object( file ); |
| 317 | return ret; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 320 | static int file_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 321 | { |
Marcus Meissner | 6bb990f | 2001-05-29 20:55:21 +0000 | [diff] [blame] | 322 | struct stat st; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 323 | struct file *file = (struct file *)obj; |
| 324 | assert( obj->ops == &file_ops ); |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 325 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 326 | if (reply) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 327 | { |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 328 | if (fstat( file->obj.fd, &st ) == -1) |
| 329 | { |
| 330 | file_set_error(); |
| 331 | return FD_TYPE_INVALID; |
| 332 | } |
| 333 | if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) || |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 334 | S_ISSOCK(st.st_mode) || isatty(file->obj.fd)) reply->type = FILE_TYPE_CHAR; |
| 335 | else reply->type = FILE_TYPE_DISK; |
| 336 | if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY; |
| 337 | else reply->attr = FILE_ATTRIBUTE_ARCHIVE; |
| 338 | if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY; |
| 339 | reply->access_time = st.st_atime; |
| 340 | reply->write_time = st.st_mtime; |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 341 | if (S_ISDIR(st.st_mode)) |
| 342 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 343 | reply->size_high = 0; |
| 344 | reply->size_low = 0; |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 345 | } |
| 346 | else |
| 347 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 348 | reply->size_high = st.st_size >> 32; |
| 349 | reply->size_low = st.st_size & 0xffffffff; |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 350 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 351 | reply->links = st.st_nlink; |
| 352 | reply->index_high = st.st_dev; |
| 353 | reply->index_low = st.st_ino; |
| 354 | reply->serial = 0; /* FIXME */ |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 355 | } |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 356 | *flags = 0; |
| 357 | if (file->flags & FILE_FLAG_OVERLAPPED) *flags |= FD_FLAG_OVERLAPPED; |
Mike McCormack | ff58be5 | 2001-10-04 16:18:15 +0000 | [diff] [blame] | 358 | return FD_TYPE_DEFAULT; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 361 | static void file_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count) |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 362 | { |
| 363 | struct file *file = (struct file *)obj; |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 364 | struct async *async; |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 365 | struct async_queue *q; |
| 366 | |
| 367 | assert( obj->ops == &file_ops ); |
| 368 | |
| 369 | if ( !(file->flags & FILE_FLAG_OVERLAPPED) ) |
| 370 | { |
| 371 | set_error ( STATUS_INVALID_HANDLE ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 372 | return; |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | switch(type) |
| 376 | { |
| 377 | case ASYNC_TYPE_READ: |
| 378 | q = &file->read_q; |
| 379 | break; |
| 380 | case ASYNC_TYPE_WRITE: |
| 381 | q = &file->write_q; |
| 382 | break; |
| 383 | default: |
| 384 | set_error( STATUS_INVALID_PARAMETER ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 385 | return; |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 388 | async = find_async ( q, current, ptr ); |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 389 | |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 390 | if ( status == STATUS_PENDING ) |
| 391 | { |
Martin Wilck | ff1f320 | 2002-04-26 18:31:19 +0000 | [diff] [blame] | 392 | struct pollfd pfd; |
| 393 | |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 394 | if ( !async ) |
| 395 | async = create_async ( obj, current, ptr ); |
| 396 | if ( !async ) |
| 397 | return; |
| 398 | |
| 399 | async->status = STATUS_PENDING; |
| 400 | if ( !async->q ) |
| 401 | async_insert( q, async ); |
Martin Wilck | ff1f320 | 2002-04-26 18:31:19 +0000 | [diff] [blame] | 402 | |
| 403 | /* Check if the new pending request can be served immediately */ |
| 404 | pfd.fd = obj->fd; |
| 405 | pfd.events = file_get_poll_events ( obj ); |
| 406 | pfd.revents = 0; |
| 407 | poll ( &pfd, 1, 0 ); |
| 408 | |
| 409 | if ( pfd.revents ) |
| 410 | file_poll_event ( obj, pfd.revents ); |
Martin Wilck | 54ba272 | 2002-04-24 21:29:54 +0000 | [diff] [blame] | 411 | } |
| 412 | else if ( async ) destroy_async ( async ); |
| 413 | else set_error ( STATUS_INVALID_PARAMETER ); |
| 414 | |
| 415 | set_select_events ( obj, file_get_poll_events ( obj )); |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 418 | static void file_destroy( struct object *obj ) |
| 419 | { |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 420 | struct file *file = (struct file *)obj; |
| 421 | assert( obj->ops == &file_ops ); |
| 422 | |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 423 | if (file->name) |
| 424 | { |
| 425 | /* remove it from the hashing list */ |
| 426 | struct file **pptr = &file_hash[get_name_hash( file->name )]; |
| 427 | while (*pptr && *pptr != file) pptr = &(*pptr)->next; |
| 428 | assert( *pptr ); |
| 429 | *pptr = (*pptr)->next; |
Juergen Schmied | cddfcce | 1999-02-14 11:20:07 +0000 | [diff] [blame] | 430 | if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name ); |
Alexandre Julliard | 638f169 | 1999-01-17 16:32:32 +0000 | [diff] [blame] | 431 | free( file->name ); |
| 432 | } |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 433 | if (file->flags & FILE_FLAG_OVERLAPPED) |
| 434 | { |
| 435 | destroy_async_queue (&file->read_q); |
| 436 | destroy_async_queue (&file->write_q); |
| 437 | } |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 440 | /* set the last error depending on errno */ |
| 441 | void file_set_error(void) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 442 | { |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 443 | switch (errno) |
| 444 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 445 | case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break; |
| 446 | case EBADF: set_error( STATUS_INVALID_HANDLE ); break; |
| 447 | case ENOSPC: set_error( STATUS_DISK_FULL ); break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 448 | case EACCES: |
Alexandre Julliard | 5f258c6 | 2001-07-14 00:50:30 +0000 | [diff] [blame] | 449 | case ESRCH: |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 450 | case EPERM: set_error( STATUS_ACCESS_DENIED ); break; |
| 451 | case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break; |
| 452 | case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break; |
| 453 | case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break; |
| 454 | case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 455 | case ENFILE: |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 456 | case EMFILE: set_error( STATUS_NO_MORE_FILES ); break; |
| 457 | case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break; |
| 458 | case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break; |
| 459 | case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break; |
| 460 | case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break; |
| 461 | case EIO: set_error( STATUS_ACCESS_VIOLATION ); break; |
| 462 | default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 463 | } |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 466 | 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] | 467 | { |
Alexandre Julliard | 61ec6c1 | 1999-11-29 02:17:08 +0000 | [diff] [blame] | 468 | return (struct file *)get_handle_obj( process, handle, access, &file_ops ); |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 469 | } |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 470 | |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 471 | static int set_file_pointer( obj_handle_t handle, unsigned int *low, int *high, int whence ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 472 | { |
| 473 | struct file *file; |
Marcus Meissner | 6bb990f | 2001-05-29 20:55:21 +0000 | [diff] [blame] | 474 | off_t result,xto; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 475 | |
Marcus Meissner | 6bb990f | 2001-05-29 20:55:21 +0000 | [diff] [blame] | 476 | xto = *low+((off_t)*high<<32); |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 477 | if (!(file = get_file_obj( current->process, handle, 0 ))) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 478 | return 0; |
Marcus Meissner | 6bb990f | 2001-05-29 20:55:21 +0000 | [diff] [blame] | 479 | if ((result = lseek(file->obj.fd,xto,whence))==-1) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 480 | { |
| 481 | /* Check for seek before start of file */ |
Andreas Mohr | 22c80a2 | 2001-01-12 23:02:39 +0000 | [diff] [blame] | 482 | |
| 483 | /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */ |
| 484 | if (((errno == EINVAL) || (errno == EPERM)) |
Bill Medland | e81bf3f | 2001-06-19 03:30:53 +0000 | [diff] [blame] | 485 | && (whence != SEEK_SET) && (*high < 0)) |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 486 | set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ ); |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 487 | else |
| 488 | file_set_error(); |
| 489 | release_object( file ); |
| 490 | return 0; |
| 491 | } |
Marcus Meissner | 3f1ed52 | 2001-05-14 20:09:37 +0000 | [diff] [blame] | 492 | *low = result & 0xffffffff; |
| 493 | *high = result >> 32; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 494 | release_object( file ); |
| 495 | return 1; |
| 496 | } |
| 497 | |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 498 | /* extend a file beyond the current end of file */ |
| 499 | static int extend_file( struct file *file, off_t size ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 500 | { |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 501 | static const char zero; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 502 | |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 503 | /* extend the file one byte beyond the requested size and then truncate it */ |
| 504 | /* this should work around ftruncate implementations that can't extend files */ |
| 505 | if ((lseek( file->obj.fd, size, SEEK_SET ) != -1) && |
| 506 | (write( file->obj.fd, &zero, 1 ) != -1)) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 507 | { |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 508 | ftruncate( file->obj.fd, size ); |
| 509 | return 1; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 510 | } |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 511 | file_set_error(); |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | /* truncate file at current position */ |
| 516 | static int truncate_file( struct file *file ) |
| 517 | { |
| 518 | int ret = 0; |
| 519 | off_t pos = lseek( file->obj.fd, 0, SEEK_CUR ); |
| 520 | off_t eof = lseek( file->obj.fd, 0, SEEK_END ); |
| 521 | |
| 522 | if (eof < pos) ret = extend_file( file, pos ); |
| 523 | else |
| 524 | { |
| 525 | if (ftruncate( file->obj.fd, pos ) != -1) ret = 1; |
| 526 | else file_set_error(); |
| 527 | } |
| 528 | lseek( file->obj.fd, pos, SEEK_SET ); /* restore file pos */ |
| 529 | return ret; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 532 | /* try to grow the file to the specified size */ |
| 533 | int grow_file( struct file *file, int size_high, int size_low ) |
| 534 | { |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 535 | int ret = 0; |
Marcus Meissner | 6bb990f | 2001-05-29 20:55:21 +0000 | [diff] [blame] | 536 | struct stat st; |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 537 | off_t old_pos, size = size_low + (((off_t)size_high)<<32); |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 538 | |
Marcus Meissner | 6bb990f | 2001-05-29 20:55:21 +0000 | [diff] [blame] | 539 | if (fstat( file->obj.fd, &st ) == -1) |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 540 | { |
| 541 | file_set_error(); |
| 542 | return 0; |
| 543 | } |
Marcus Meissner | 3f1ed52 | 2001-05-14 20:09:37 +0000 | [diff] [blame] | 544 | if (st.st_size >= size) return 1; /* already large enough */ |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 545 | old_pos = lseek( file->obj.fd, 0, SEEK_CUR ); /* save old pos */ |
| 546 | ret = extend_file( file, size ); |
| 547 | lseek( file->obj.fd, old_pos, SEEK_SET ); /* restore file pos */ |
| 548 | return ret; |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 551 | static int set_file_time( obj_handle_t handle, time_t access_time, time_t write_time ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 552 | { |
| 553 | struct file *file; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 554 | struct utimbuf utimbuf; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 555 | |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 556 | if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE ))) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 557 | return 0; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 558 | if (!file->name) |
| 559 | { |
| 560 | set_error( STATUS_INVALID_HANDLE ); |
| 561 | release_object( file ); |
| 562 | return 0; |
| 563 | } |
Alexandre Julliard | a27b48b | 1999-01-31 15:08:31 +0000 | [diff] [blame] | 564 | if (!access_time || !write_time) |
| 565 | { |
| 566 | struct stat st; |
| 567 | if (stat( file->name, &st ) == -1) goto error; |
| 568 | if (!access_time) access_time = st.st_atime; |
| 569 | if (!write_time) write_time = st.st_mtime; |
| 570 | } |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 571 | utimbuf.actime = access_time; |
| 572 | utimbuf.modtime = write_time; |
Alexandre Julliard | a27b48b | 1999-01-31 15:08:31 +0000 | [diff] [blame] | 573 | if (utime( file->name, &utimbuf ) == -1) goto error; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 574 | release_object( file ); |
| 575 | return 1; |
Alexandre Julliard | a27b48b | 1999-01-31 15:08:31 +0000 | [diff] [blame] | 576 | error: |
| 577 | file_set_error(); |
| 578 | release_object( file ); |
| 579 | return 0; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 580 | } |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 581 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 582 | static int file_lock( struct file *file, int offset_high, int offset_low, |
| 583 | int count_high, int count_low ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 584 | { |
| 585 | /* FIXME: implement this */ |
| 586 | return 1; |
| 587 | } |
| 588 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 589 | static int file_unlock( struct file *file, int offset_high, int offset_low, |
| 590 | int count_high, int count_low ) |
Alexandre Julliard | 62a8b43 | 1999-01-19 17:48:23 +0000 | [diff] [blame] | 591 | { |
| 592 | /* FIXME: implement this */ |
| 593 | return 1; |
| 594 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 595 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 596 | /* create a file */ |
| 597 | DECL_HANDLER(create_file) |
| 598 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 599 | struct file *file; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 600 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 601 | reply->handle = 0; |
| 602 | if ((file = create_file( get_req_data(), get_req_data_size(), req->access, |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 603 | req->sharing, req->create, req->attrs, req->drive_type ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 604 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 605 | reply->handle = alloc_handle( current->process, file, req->access, req->inherit ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 606 | release_object( file ); |
| 607 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* allocate a file handle for a Unix fd */ |
| 611 | DECL_HANDLER(alloc_file_handle) |
| 612 | { |
| 613 | struct file *file; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 614 | int fd; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 615 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 616 | reply->handle = 0; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 617 | if ((fd = thread_get_inflight_fd( current, req->fd )) == -1) |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 618 | { |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 619 | set_error( STATUS_INVALID_HANDLE ); |
| 620 | return; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 621 | } |
Ove Kaaven | 708a846 | 2001-10-24 00:23:25 +0000 | [diff] [blame] | 622 | if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 623 | 0, DRIVE_UNKNOWN ))) |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 624 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 625 | reply->handle = alloc_handle( current->process, file, req->access, req->inherit ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 626 | release_object( file ); |
| 627 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 630 | /* get a Unix fd to access a file */ |
| 631 | DECL_HANDLER(get_handle_fd) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 632 | { |
| 633 | struct object *obj; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 634 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 635 | reply->fd = -1; |
| 636 | reply->type = FD_TYPE_INVALID; |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 637 | if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 638 | { |
Alexandre Julliard | 63411db | 2000-12-22 21:12:36 +0000 | [diff] [blame] | 639 | int fd = get_handle_fd( current->process, req->handle, req->access ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 640 | if (fd != -1) reply->fd = fd; |
Alexandre Julliard | 63411db | 2000-12-22 21:12:36 +0000 | [diff] [blame] | 641 | else if (!get_error()) |
| 642 | { |
| 643 | if ((fd = obj->ops->get_fd( obj )) != -1) |
Alexandre Julliard | 8859d77 | 2001-03-01 22:13:49 +0000 | [diff] [blame] | 644 | send_client_fd( current->process, fd, req->handle ); |
Alexandre Julliard | 63411db | 2000-12-22 21:12:36 +0000 | [diff] [blame] | 645 | } |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 646 | reply->type = obj->ops->get_file_info( obj, NULL, &reply->flags ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 647 | release_object( obj ); |
| 648 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* set a file current position */ |
| 652 | DECL_HANDLER(set_file_pointer) |
| 653 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 654 | int high = req->high; |
| 655 | int low = req->low; |
| 656 | set_file_pointer( req->handle, &low, &high, req->whence ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 657 | reply->new_low = low; |
| 658 | reply->new_high = high; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* truncate (or extend) a file */ |
| 662 | DECL_HANDLER(truncate_file) |
| 663 | { |
Alexandre Julliard | 86e5efb | 2002-01-29 02:51:12 +0000 | [diff] [blame] | 664 | struct file *file; |
| 665 | |
| 666 | if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE ))) |
| 667 | { |
| 668 | truncate_file( file ); |
| 669 | release_object( file ); |
| 670 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* flush a file buffers */ |
| 674 | DECL_HANDLER(flush_file) |
| 675 | { |
| 676 | struct object *obj; |
| 677 | |
Marcus Meissner | 9d60e35 | 1999-12-04 04:00:16 +0000 | [diff] [blame] | 678 | if ((obj = get_handle_obj( current->process, req->handle, 0, NULL ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 679 | { |
| 680 | obj->ops->flush( obj ); |
| 681 | release_object( obj ); |
| 682 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* set a file access and modification times */ |
| 686 | DECL_HANDLER(set_file_time) |
| 687 | { |
| 688 | set_file_time( req->handle, req->access_time, req->write_time ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* get a file information */ |
| 692 | DECL_HANDLER(get_file_info) |
| 693 | { |
| 694 | struct object *obj; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 695 | |
| 696 | if ((obj = get_handle_obj( current->process, req->handle, 0, NULL ))) |
| 697 | { |
Martin Wilck | 88cd32b | 2002-01-09 20:30:51 +0000 | [diff] [blame] | 698 | int flags; |
| 699 | obj->ops->get_file_info( obj, reply, &flags ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 700 | release_object( obj ); |
| 701 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* lock a region of a file */ |
| 705 | DECL_HANDLER(lock_file) |
| 706 | { |
| 707 | struct file *file; |
| 708 | |
| 709 | if ((file = get_file_obj( current->process, req->handle, 0 ))) |
| 710 | { |
| 711 | file_lock( file, req->offset_high, req->offset_low, |
| 712 | req->count_high, req->count_low ); |
| 713 | release_object( file ); |
| 714 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /* unlock a region of a file */ |
| 718 | DECL_HANDLER(unlock_file) |
| 719 | { |
| 720 | struct file *file; |
| 721 | |
| 722 | if ((file = get_file_obj( current->process, req->handle, 0 ))) |
| 723 | { |
| 724 | file_unlock( file, req->offset_high, req->offset_low, |
| 725 | req->count_high, req->count_low ); |
| 726 | release_object( file ); |
| 727 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 728 | } |