blob: 0c8aaae3792c0548a7d61525b88b1c980ed3c59b [file] [log] [blame]
Alexandre Julliard338e7571998-12-27 15:28:54 +00001/*
2 * Server-side file management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
5 */
6
Patrik Stridvall96336321999-10-24 22:13:47 +00007#include "config.h"
8
Alexandre Julliard338e7571998-12-27 15:28:54 +00009#include <assert.h>
10#include <fcntl.h>
11#include <stdio.h>
David Luyeree517e81999-02-28 12:27:56 +000012#include <string.h>
Alexandre Julliard338e7571998-12-27 15:28:54 +000013#include <stdlib.h>
Marcus Meissner8ba68fb1999-01-01 18:42:17 +000014#include <errno.h>
Howard Abrams13277481999-07-10 13:16:29 +000015#ifdef HAVE_SYS_ERRNO_H
Alexandre Julliard338e7571998-12-27 15:28:54 +000016#include <sys/errno.h>
Howard Abrams13277481999-07-10 13:16:29 +000017#endif
Alexandre Julliard338e7571998-12-27 15:28:54 +000018#include <sys/stat.h>
19#include <sys/time.h>
20#include <sys/types.h>
21#include <time.h>
22#include <unistd.h>
Alexandre Julliard05625391999-01-03 11:55:56 +000023#include <utime.h>
Alexandre Julliard338e7571998-12-27 15:28:54 +000024
25#include "winerror.h"
Michael Vekslerf935c591999-02-09 15:49:39 +000026#include "winbase.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000027
28#include "handle.h"
29#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000030#include "request.h"
Alexandre Julliard338e7571998-12-27 15:28:54 +000031
32struct file
33{
Alexandre Julliard57e11311999-05-16 16:59:38 +000034 struct object obj; /* object header */
Alexandre Julliard57e11311999-05-16 16:59:38 +000035 struct file *next; /* next file in hashing list */
36 char *name; /* file name */
37 unsigned int access; /* file access (GENERIC_READ/WRITE) */
38 unsigned int flags; /* flags (FILE_FLAG_*) */
39 unsigned int sharing; /* file sharing mode */
Alexandre Julliard338e7571998-12-27 15:28:54 +000040};
41
Alexandre Julliard05625391999-01-03 11:55:56 +000042#define NAME_HASH_SIZE 37
43
44static struct file *file_hash[NAME_HASH_SIZE];
45
Alexandre Julliard338e7571998-12-27 15:28:54 +000046static void file_dump( struct object *obj, int verbose );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000047static int file_get_poll_events( struct object *obj );
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000048static int file_get_fd( struct object *obj );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +000049static int file_flush( struct object *obj );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000050static int file_get_info( struct object *obj, struct get_file_info_request *req );
Alexandre Julliard338e7571998-12-27 15:28:54 +000051static void file_destroy( struct object *obj );
52
53static const struct object_ops file_ops =
54{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000055 sizeof(struct file), /* size */
56 file_dump, /* dump */
57 default_poll_add_queue, /* add_queue */
58 default_poll_remove_queue, /* remove_queue */
59 default_poll_signaled, /* signaled */
60 no_satisfied, /* satisfied */
61 file_get_poll_events, /* get_poll_events */
62 default_poll_event, /* poll_event */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000063 file_get_fd, /* get_fd */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000064 file_flush, /* flush */
65 file_get_info, /* get_file_info */
66 file_destroy /* destroy */
Alexandre Julliard338e7571998-12-27 15:28:54 +000067};
68
Alexandre Julliard05625391999-01-03 11:55:56 +000069
70static int get_name_hash( const char *name )
71{
72 int hash = 0;
Eric Pouechbf2b7651999-11-15 00:07:30 +000073 while (*name) hash ^= (unsigned char)*name++;
Alexandre Julliard05625391999-01-03 11:55:56 +000074 return hash % NAME_HASH_SIZE;
75}
76
77/* check if the desired access is possible without violating */
78/* the sharing mode of other opens of the same file */
79static int check_sharing( const char *name, int hash, unsigned int access,
80 unsigned int sharing )
Alexandre Julliard338e7571998-12-27 15:28:54 +000081{
82 struct file *file;
Alexandre Julliard05625391999-01-03 11:55:56 +000083 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
84 unsigned int existing_access = 0;
Alexandre Julliard338e7571998-12-27 15:28:54 +000085
Alexandre Julliard05625391999-01-03 11:55:56 +000086 for (file = file_hash[hash]; file; file = file->next)
Alexandre Julliard338e7571998-12-27 15:28:54 +000087 {
Alexandre Julliard05625391999-01-03 11:55:56 +000088 if (strcmp( file->name, name )) continue;
89 existing_sharing &= file->sharing;
90 existing_access |= file->access;
91 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +000092 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) goto error;
93 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) goto error;
94 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) goto error;
95 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) goto error;
Alexandre Julliard05625391999-01-03 11:55:56 +000096 return 1;
Alexandre Julliard5bc78081999-06-22 17:26:53 +000097 error:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000098 set_error( STATUS_SHARING_VIOLATION );
Alexandre Julliard5bc78081999-06-22 17:26:53 +000099 return 0;
Alexandre Julliard05625391999-01-03 11:55:56 +0000100}
101
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000102/* create a file from a file descriptor */
103/* if the function fails the fd is closed */
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000104static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing,
105 unsigned int attrs )
Alexandre Julliard05625391999-01-03 11:55:56 +0000106{
107 struct file *file;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000108 if ((file = alloc_object( &file_ops, fd )))
Alexandre Julliard05625391999-01-03 11:55:56 +0000109 {
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000110 file->name = NULL;
111 file->next = NULL;
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000112 file->access = access;
113 file->flags = attrs;
114 file->sharing = sharing;
Alexandre Julliard05625391999-01-03 11:55:56 +0000115 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000116 return file;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000117}
118
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000119
120static struct file *create_file( const char *nameptr, size_t len, unsigned int access,
121 unsigned int sharing, int create, unsigned int attrs )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000122{
123 struct file *file;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000124 int hash, flags;
125 struct stat st;
126 char *name;
127 int fd = -1;
128
129 if (!(name = mem_alloc( len + 1 ))) return NULL;
130 memcpy( name, nameptr, len );
131 name[len] = 0;
132
133 /* check sharing mode */
134 hash = get_name_hash( name );
135 if (!check_sharing( name, hash, access, sharing )) goto error;
136
137 switch(create)
138 {
139 case CREATE_NEW: flags = O_CREAT | O_EXCL; break;
140 case CREATE_ALWAYS: flags = O_CREAT | O_TRUNC; break;
141 case OPEN_ALWAYS: flags = O_CREAT; break;
142 case TRUNCATE_EXISTING: flags = O_TRUNC; break;
143 case OPEN_EXISTING: flags = 0; break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000144 default: set_error( STATUS_INVALID_PARAMETER ); goto error;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000145 }
146 switch(access & (GENERIC_READ | GENERIC_WRITE))
147 {
148 case 0: break;
149 case GENERIC_READ: flags |= O_RDONLY; break;
150 case GENERIC_WRITE: flags |= O_WRONLY; break;
151 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
152 }
153
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000154 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000155 if ((fd = open( name, flags | O_NONBLOCK,
156 (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666 )) == -1)
157 goto file_error;
158 /* refuse to open a directory */
159 if (fstat( fd, &st ) == -1) goto file_error;
160 if (S_ISDIR(st.st_mode))
161 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000162 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000163 goto error;
164 }
165
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000166 if (!(file = create_file_for_fd( fd, access, sharing, attrs )))
167 {
168 free( name );
169 return NULL;
170 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000171 file->name = name;
172 file->next = file_hash[hash];
173 file_hash[hash] = file;
174 return file;
175
176 file_error:
177 file_set_error();
178 error:
179 if (fd != -1) close( fd );
180 free( name );
181 return NULL;
182}
183
184/* Create an anonymous Unix file */
185int create_anonymous_file(void)
186{
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000187 char *name;
188 int fd;
189
190 do
191 {
192 if (!(name = tmpnam(NULL)))
193 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000194 set_error( STATUS_TOO_MANY_OPENED_FILES );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000195 return -1;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000196 }
197 fd = open( name, O_CREAT | O_EXCL | O_RDWR, 0600 );
198 } while ((fd == -1) && (errno == EEXIST));
199 if (fd == -1)
200 {
201 file_set_error();
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000202 return -1;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000203 }
204 unlink( name );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000205 return fd;
206}
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000207
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000208/* Create a temp file for anonymous mappings */
209struct file *create_temp_file( int access )
210{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000211 int fd;
212
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000213 if ((fd = create_anonymous_file()) == -1) return NULL;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000214 return create_file_for_fd( fd, access, 0, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000215}
216
Alexandre Julliard338e7571998-12-27 15:28:54 +0000217static void file_dump( struct object *obj, int verbose )
218{
219 struct file *file = (struct file *)obj;
220 assert( obj->ops == &file_ops );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000221 fprintf( stderr, "File fd=%d flags=%08x name='%s'\n", file->obj.fd, file->flags, file->name );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000222}
223
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000224static int file_get_poll_events( struct object *obj )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000225{
226 struct file *file = (struct file *)obj;
Alexandre Julliard57e11311999-05-16 16:59:38 +0000227 int events = 0;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000228 assert( obj->ops == &file_ops );
Alexandre Julliard247b8ae1999-12-13 00:16:44 +0000229 if (file->access & GENERIC_READ) events |= POLLIN;
230 if (file->access & GENERIC_WRITE) events |= POLLOUT;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000231 return events;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000232}
233
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000234static int file_get_fd( struct object *obj )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000235{
236 struct file *file = (struct file *)obj;
237 assert( obj->ops == &file_ops );
Alexandre Julliard63411db2000-12-22 21:12:36 +0000238 return file->obj.fd;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000239}
240
241static int file_flush( struct object *obj )
242{
243 int ret;
244 struct file *file = (struct file *)grab_object(obj);
245 assert( obj->ops == &file_ops );
246
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000247 ret = (fsync( file->obj.fd ) != -1);
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000248 if (!ret) file_set_error();
249 release_object( file );
250 return ret;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000251}
252
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000253static int file_get_info( struct object *obj, struct get_file_info_request *req )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000254{
Alexandre Julliard05625391999-01-03 11:55:56 +0000255 struct stat st;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000256 struct file *file = (struct file *)obj;
257 assert( obj->ops == &file_ops );
Alexandre Julliard05625391999-01-03 11:55:56 +0000258
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000259 if (fstat( file->obj.fd, &st ) == -1)
Alexandre Julliard05625391999-01-03 11:55:56 +0000260 {
261 file_set_error();
262 return 0;
263 }
264 if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000265 S_ISSOCK(st.st_mode) || isatty(file->obj.fd)) req->type = FILE_TYPE_CHAR;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000266 else req->type = FILE_TYPE_DISK;
267 if (S_ISDIR(st.st_mode)) req->attr = FILE_ATTRIBUTE_DIRECTORY;
268 else req->attr = FILE_ATTRIBUTE_ARCHIVE;
269 if (!(st.st_mode & S_IWUSR)) req->attr |= FILE_ATTRIBUTE_READONLY;
270 req->access_time = st.st_atime;
271 req->write_time = st.st_mtime;
272 req->size_high = 0;
273 req->size_low = S_ISDIR(st.st_mode) ? 0 : st.st_size;
274 req->links = st.st_nlink;
275 req->index_high = st.st_dev;
276 req->index_low = st.st_ino;
277 req->serial = 0; /* FIXME */
Alexandre Julliard05625391999-01-03 11:55:56 +0000278 return 1;
279}
280
281static void file_destroy( struct object *obj )
282{
Alexandre Julliard05625391999-01-03 11:55:56 +0000283 struct file *file = (struct file *)obj;
284 assert( obj->ops == &file_ops );
285
Alexandre Julliard638f1691999-01-17 16:32:32 +0000286 if (file->name)
287 {
288 /* remove it from the hashing list */
289 struct file **pptr = &file_hash[get_name_hash( file->name )];
290 while (*pptr && *pptr != file) pptr = &(*pptr)->next;
291 assert( *pptr );
292 *pptr = (*pptr)->next;
Juergen Schmiedcddfcce1999-02-14 11:20:07 +0000293 if (file->flags & FILE_FLAG_DELETE_ON_CLOSE) unlink( file->name );
Alexandre Julliard638f1691999-01-17 16:32:32 +0000294 free( file->name );
295 }
Alexandre Julliard338e7571998-12-27 15:28:54 +0000296}
297
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000298/* set the last error depending on errno */
299void file_set_error(void)
Alexandre Julliard338e7571998-12-27 15:28:54 +0000300{
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000301 switch (errno)
302 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000303 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
304 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
305 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000306 case EACCES:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000307 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
308 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
309 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
310 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
311 case EISDIR: set_error( 0xc0010000 | ERROR_CANNOT_MAKE /* FIXME */ ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000312 case ENFILE:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000313 case EMFILE: set_error( STATUS_NO_MORE_FILES ); break;
314 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
315 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
316 case ESPIPE: set_error( 0xc0010000 | ERROR_SEEK /* FIXME */ ); break;
317 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
318 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
319 default: perror("file_set_error"); set_error( ERROR_UNKNOWN /* FIXME */ ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000320 }
Alexandre Julliard338e7571998-12-27 15:28:54 +0000321}
322
Alexandre Julliard61ec6c11999-11-29 02:17:08 +0000323struct file *get_file_obj( struct process *process, int handle, unsigned int access )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000324{
Alexandre Julliard61ec6c11999-11-29 02:17:08 +0000325 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000326}
Alexandre Julliard338e7571998-12-27 15:28:54 +0000327
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000328static int set_file_pointer( int handle, int *low, int *high, int whence )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000329{
330 struct file *file;
331 int result;
332
Hidenori Takeshima66791af2000-07-31 23:26:50 +0000333 if ((*low >= 0 && *high != 0) || (*low < 0 && *high != -1))
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000334 {
Hidenori Takeshima66791af2000-07-31 23:26:50 +0000335 fprintf( stderr, "set_file_pointer: offset > 2Gb not supported yet\n" );
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000336 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000337 return 0;
338 }
339
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000340 if (!(file = get_file_obj( current->process, handle, 0 )))
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000341 return 0;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000342 if ((result = lseek( file->obj.fd, *low, whence )) == -1)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000343 {
344 /* Check for seek before start of file */
345 if ((errno == EINVAL) && (whence != SEEK_SET) && (*low < 0))
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000346 set_error( 0xc0010000 | ERROR_NEGATIVE_SEEK /* FIXME */ );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000347 else
348 file_set_error();
349 release_object( file );
350 return 0;
351 }
352 *low = result;
353 release_object( file );
354 return 1;
355}
356
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000357static int truncate_file( int handle )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000358{
359 struct file *file;
360 int result;
361
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000362 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000363 return 0;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000364 if (((result = lseek( file->obj.fd, 0, SEEK_CUR )) == -1) ||
365 (ftruncate( file->obj.fd, result ) == -1))
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000366 {
367 file_set_error();
368 release_object( file );
369 return 0;
370 }
371 release_object( file );
372 return 1;
373
374}
375
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000376/* try to grow the file to the specified size */
377int grow_file( struct file *file, int size_high, int size_low )
378{
379 struct stat st;
380
381 if (size_high)
382 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000383 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000384 return 0;
385 }
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000386 if (fstat( file->obj.fd, &st ) == -1)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000387 {
388 file_set_error();
389 return 0;
390 }
391 if (st.st_size >= size_low) return 1; /* already large enough */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000392 if (ftruncate( file->obj.fd, size_low ) != -1) return 1;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000393 file_set_error();
394 return 0;
395}
396
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000397static int set_file_time( int handle, time_t access_time, time_t write_time )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000398{
399 struct file *file;
Alexandre Julliard05625391999-01-03 11:55:56 +0000400 struct utimbuf utimbuf;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000401
Alexandre Julliard05625391999-01-03 11:55:56 +0000402 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE )))
Alexandre Julliard338e7571998-12-27 15:28:54 +0000403 return 0;
Alexandre Julliarda27b48b1999-01-31 15:08:31 +0000404 if (!access_time || !write_time)
405 {
406 struct stat st;
407 if (stat( file->name, &st ) == -1) goto error;
408 if (!access_time) access_time = st.st_atime;
409 if (!write_time) write_time = st.st_mtime;
410 }
Alexandre Julliard05625391999-01-03 11:55:56 +0000411 utimbuf.actime = access_time;
412 utimbuf.modtime = write_time;
Alexandre Julliarda27b48b1999-01-31 15:08:31 +0000413 if (utime( file->name, &utimbuf ) == -1) goto error;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000414 release_object( file );
415 return 1;
Alexandre Julliarda27b48b1999-01-31 15:08:31 +0000416 error:
417 file_set_error();
418 release_object( file );
419 return 0;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000420}
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000421
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000422static int file_lock( struct file *file, int offset_high, int offset_low,
423 int count_high, int count_low )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000424{
425 /* FIXME: implement this */
426 return 1;
427}
428
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000429static int file_unlock( struct file *file, int offset_high, int offset_low,
430 int count_high, int count_low )
Alexandre Julliard62a8b431999-01-19 17:48:23 +0000431{
432 /* FIXME: implement this */
433 return 1;
434}
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000435
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000436/* create a file */
437DECL_HANDLER(create_file)
438{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000439 struct file *file;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000440
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000441 req->handle = -1;
Alexandre Julliard92643002000-08-31 01:59:51 +0000442 if ((file = create_file( get_req_data(req), get_req_data_size(req), req->access,
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000443 req->sharing, req->create, req->attrs )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000444 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000445 req->handle = alloc_handle( current->process, file, req->access, req->inherit );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000446 release_object( file );
447 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000448}
449
450/* allocate a file handle for a Unix fd */
451DECL_HANDLER(alloc_file_handle)
452{
453 struct file *file;
454
455 req->handle = -1;
Alexandre Julliardea0d0282000-03-10 22:16:10 +0000456 if (current->pass_fd != -1)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000457 {
Alexandre Julliardea0d0282000-03-10 22:16:10 +0000458 if ((file = create_file_for_fd( current->pass_fd, req->access,
459 FILE_SHARE_READ | FILE_SHARE_WRITE, 0 )))
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000460 {
461 req->handle = alloc_handle( current->process, file, req->access, 0 );
462 release_object( file );
463 }
Alexandre Julliardea0d0282000-03-10 22:16:10 +0000464 current->pass_fd = -1;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000465 }
Alexandre Julliardea0d0282000-03-10 22:16:10 +0000466 else set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000467}
468
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000469/* get a Unix fd to access a file */
470DECL_HANDLER(get_handle_fd)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000471{
472 struct object *obj;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000473
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000474 req->fd = -1;
475 if ((obj = get_handle_obj( current->process, req->handle, req->access, NULL )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000476 {
Alexandre Julliard63411db2000-12-22 21:12:36 +0000477 int fd = get_handle_fd( current->process, req->handle, req->access );
478 if (fd != -1) req->fd = fd;
479 else if (!get_error())
480 {
481 if ((fd = obj->ops->get_fd( obj )) != -1)
482 send_client_fd( current, fd, req->handle );
483 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000484 release_object( obj );
485 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000486}
487
488/* set a file current position */
489DECL_HANDLER(set_file_pointer)
490{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000491 int high = req->high;
492 int low = req->low;
493 set_file_pointer( req->handle, &low, &high, req->whence );
494 req->new_low = low;
495 req->new_high = high;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000496}
497
498/* truncate (or extend) a file */
499DECL_HANDLER(truncate_file)
500{
501 truncate_file( req->handle );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000502}
503
504/* flush a file buffers */
505DECL_HANDLER(flush_file)
506{
507 struct object *obj;
508
Marcus Meissner9d60e351999-12-04 04:00:16 +0000509 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000510 {
511 obj->ops->flush( obj );
512 release_object( obj );
513 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000514}
515
516/* set a file access and modification times */
517DECL_HANDLER(set_file_time)
518{
519 set_file_time( req->handle, req->access_time, req->write_time );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000520}
521
522/* get a file information */
523DECL_HANDLER(get_file_info)
524{
525 struct object *obj;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000526
527 if ((obj = get_handle_obj( current->process, req->handle, 0, NULL )))
528 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000529 obj->ops->get_file_info( obj, req );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000530 release_object( obj );
531 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000532}
533
534/* lock a region of a file */
535DECL_HANDLER(lock_file)
536{
537 struct file *file;
538
539 if ((file = get_file_obj( current->process, req->handle, 0 )))
540 {
541 file_lock( file, req->offset_high, req->offset_low,
542 req->count_high, req->count_low );
543 release_object( file );
544 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000545}
546
547/* unlock a region of a file */
548DECL_HANDLER(unlock_file)
549{
550 struct file *file;
551
552 if ((file = get_file_obj( current->process, req->handle, 0 )))
553 {
554 file_unlock( file, req->offset_high, req->offset_low,
555 req->count_high, req->count_low );
556 release_object( file );
557 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000558}