Workarounded captive_file_file_info_get() compat. for ntfs.sys of NT-5.1sp1
[captive.git] / src / libcaptive / client / file.c
1 /* $Id$
2  * captive vfs 'file' interface to reactos
3  * Copyright (C) 2002-2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "captive/client-file.h"        /* self */
23 #include "file.h"       /* self-priv */
24 #include "directory.h"
25 #include "lib.h"
26 #include <glib/gmessages.h>
27 #include "captive/unicode.h"
28 #include "reactos/ntos/types.h" /* for HANDLE */
29 #include "reactos/ddk/iotypes.h"        /* for IO_STATUS_BLOCK */
30 #include "reactos/ddk/iofuncs.h"        /* for IoCreateFile() */
31 #include "captive/sandbox.h"
32 #include "result.h"
33 #include "captive/client-directory.h"
34 #include "reactos/ddk/obfuncs.h"        /* for ObReferenceObjectByHandle() */
35 #include "captive/macros.h"
36
37
38 static gpointer captive_file_object_parent_class=NULL;
39
40
41 static GnomeVFSResult captive_file_close(CaptiveFileObject *captive_file_object);
42
43 static void captive_file_object_finalize(CaptiveFileObject *captive_file_object)
44 {
45 GnomeVFSResult errvfsresult;
46
47         g_return_if_fail(captive_file_object!=NULL);
48
49         errvfsresult=captive_file_close(captive_file_object);
50         g_assert(errvfsresult==GNOME_VFS_OK);
51
52         G_OBJECT_CLASS(captive_file_object_parent_class)->finalize((GObject *)captive_file_object);
53 }
54
55
56 static void captive_file_object_class_init(CaptiveFileObjectClass *class)
57 {
58 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
59
60         captive_file_object_parent_class=g_type_class_ref(G_TYPE_OBJECT);
61         gobject_class->finalize=(void (*)(GObject *object))captive_file_object_finalize;
62 }
63
64
65 static void captive_file_object_init(CaptiveFileObject *captive_file_object)
66 {
67         captive_file_object->file_Handle=NULL;
68 }
69
70
71 GType captive_file_object_get_type(void)
72 {
73 static GType captive_file_object_type=0;
74
75         if (!captive_file_object_type) {
76 static const GTypeInfo captive_file_object_info={
77                                 sizeof(CaptiveFileObjectClass),
78                                 NULL,   /* base_init */
79                                 NULL,   /* base_finalize */
80                                 (GClassInitFunc)captive_file_object_class_init,
81                                 NULL,   /* class_finalize */
82                                 NULL,   /* class_data */
83                                 sizeof(CaptiveFileObject),
84                                 5,      /* n_preallocs */
85                                 (GInstanceInitFunc)captive_file_object_init,
86                                 };
87
88                 captive_file_object_type=g_type_register_static(G_TYPE_OBJECT,
89                                 "CaptiveFileObject",&captive_file_object_info,0);
90                 }
91
92         return captive_file_object_type;
93 }
94
95
96 static GnomeVFSResult captive_file_new_internal(CaptiveFileObject **captive_file_object_return,
97                 const gchar *pathname,GnomeVFSOpenMode mode,gboolean create,gboolean create_exclusive,guint create_perm)
98 {
99 IO_STATUS_BLOCK file_IoStatusBlock;
100 GnomeVFSResult errvfsresult;
101 OBJECT_ATTRIBUTES file_ObjectAttributes;
102 HANDLE file_Handle;
103 CaptiveFileObject *captive_file_object;
104 NTSTATUS err;
105
106         if (CAPTIVE_IS_SANDBOX_PARENT())
107                 return captive_sandbox_parent_file_new_open(captive_file_object_return,pathname,mode);
108
109         g_return_val_if_fail(captive_file_object_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
110         g_return_val_if_fail(pathname!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
111
112         *captive_file_object_return=NULL;
113
114         errvfsresult=captive_ObjectAttributes_init(pathname,&file_ObjectAttributes);
115         g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
116         
117         /* open */
118         err=IoCreateFile(
119                         &file_Handle,   /* FileHandle */
120                         0
121                                         |(!(mode&GNOME_VFS_OPEN_READ ) ? 0 : FILE_READ_DATA)
122                                         |(!(mode&GNOME_VFS_OPEN_WRITE) ? 0 : FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES)
123                                         |(  mode!=0                    ? 0 : FILE_READ_ATTRIBUTES)
124                                         ,       /* DesiredAccess */
125                         &file_ObjectAttributes, /* ObjectAttributes */
126                         &file_IoStatusBlock,    /* IoStatusBlock */
127                         NULL,   /* AllocationSize; ignored for open */
128                         (!create || create_perm&0200 ? FILE_ATTRIBUTE_NORMAL: FILE_ATTRIBUTE_READONLY), /* FileAttributes; ignored for open */
129                         (!create || !create_exclusive   /* ShareAccess; 0 means exclusive */
130                                         ? (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)
131                                         : 0),
132                         (!create ? FILE_OPEN : FILE_CREATE),    /* CreateDisposition */
133                         /* FILE_SYNCHRONOUS_IO_{,NON}ALERT: We need to allow W32 filesystem
134                                  * any waits to not to let it return STATUS_CANT_WAIT us.
135                                  * Alertability should have only effect on asynchronous events
136                                  * from KeWaitForSingleObject() by setting/clearing its parameter 'Alertable'.
137                                  */
138                         FILE_SYNCHRONOUS_IO_ALERT,      /* CreateOptions */
139                         NULL,   /* EaBuffer */
140                         0,      /* EaLength */
141                         CreateFileTypeNone,     /* CreateFileType */
142                         NULL,   /* ExtraCreateParameters */
143                         0);     /* Options */
144         g_free(file_ObjectAttributes.ObjectName);       /* left from captive_file_uri_parent_init() */
145         g_return_val_if_fail(NT_SUCCESS(err)==NT_SUCCESS(file_IoStatusBlock.Status),GNOME_VFS_ERROR_GENERIC);
146         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
147                 return errvfsresult;
148         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
149         g_return_val_if_fail(file_IoStatusBlock.Information
150                                         ==(!create ? FILE_OPENED : FILE_CREATED),
151                         GNOME_VFS_ERROR_GENERIC);
152
153         captive_file_object=g_object_new(
154                         CAPTIVE_FILE_TYPE_OBJECT,       /* object_type */
155                         NULL);  /* first_property_name; FIXME: support properties */
156
157         captive_file_object->file_Handle=file_Handle;
158         captive_file_object->offset=0;
159
160         *captive_file_object_return=captive_file_object;
161         return GNOME_VFS_OK;
162 }
163
164
165 GnomeVFSResult captive_file_new_open
166                 (CaptiveFileObject **captive_file_object_return,const gchar *pathname,GnomeVFSOpenMode mode)
167 {
168         g_return_val_if_fail(captive_file_object_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
169         g_return_val_if_fail(pathname!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
170
171         return captive_file_new_internal(captive_file_object_return,pathname,mode,
172                         FALSE,  /* create */
173                         FALSE,  /* create_exclusive; ignored */
174                         0);     /* create_perm; ignored */
175 }
176
177
178 GnomeVFSResult captive_file_new_create
179                 (CaptiveFileObject **captive_file_object_return,const gchar *pathname,GnomeVFSOpenMode mode,gboolean exclusive,guint perm)
180 {
181         g_return_val_if_fail(captive_file_object_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
182         g_return_val_if_fail(pathname!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
183
184         return captive_file_new_internal(captive_file_object_return,pathname,mode,
185                         TRUE,   /* create */
186                         exclusive,      /* create_exclusive */
187                         perm);  /* create_perm */
188 }
189
190
191 static GnomeVFSResult captive_file_close(CaptiveFileObject *captive_file_object)
192 {
193 NTSTATUS err;
194 HANDLE file_Handle;
195
196         if (CAPTIVE_IS_SANDBOX_PARENT())
197                 return captive_sandbox_parent_file_close(captive_file_object);
198
199         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
200         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);    /* already closed */
201
202         file_Handle=captive_file_object->file_Handle;
203         captive_file_object->file_Handle=NULL;
204         err=NtClose(file_Handle);
205         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
206
207         return GNOME_VFS_OK;
208 }
209
210
211 GnomeVFSResult captive_file_read(CaptiveFileObject *captive_file_object,
212                 gpointer buffer,GnomeVFSFileSize num_bytes,GnomeVFSFileSize *bytes_read_return)
213 {
214 NTSTATUS err;
215 IO_STATUS_BLOCK file_IoStatusBlock;
216 LARGE_INTEGER file_offset;
217 GnomeVFSResult errvfsresult;
218
219         if (CAPTIVE_IS_SANDBOX_PARENT())
220                 return captive_sandbox_parent_file_read(captive_file_object,buffer,num_bytes,bytes_read_return);
221
222         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
223         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
224         g_return_val_if_fail(buffer!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
225         g_return_val_if_fail(bytes_read_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
226         g_return_val_if_fail(num_bytes==(ULONG)num_bytes,GNOME_VFS_ERROR_BAD_PARAMETERS);
227
228         file_offset.QuadPart=captive_file_object->offset;
229         err=NtReadFile(
230                         captive_file_object->file_Handle,       /* FileHandle */
231                         NULL,   /* EventHandle; completion signalling; optional */
232                         NULL,   /* ApcRoutine; optional */
233                         NULL,   /* ApcContext; optional */
234                         &file_IoStatusBlock,    /* IoStatusBlock */
235                         buffer, /* Buffer */
236                         num_bytes,      /* Length */
237                         &file_offset,   /* ByteOffset */
238                         NULL);  /* Key; NULL means no file locking key */
239         if (err==STATUS_END_OF_FILE) {
240                 g_return_val_if_fail(file_IoStatusBlock.Status==STATUS_END_OF_FILE,GNOME_VFS_ERROR_GENERIC);
241                 g_return_val_if_fail(file_IoStatusBlock.Information==0,GNOME_VFS_ERROR_GENERIC);
242                 *bytes_read_return=0;
243                 return GNOME_VFS_ERROR_EOF;
244                 }
245         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
246                 return errvfsresult;
247         g_return_val_if_fail(NT_SUCCESS(err)==NT_SUCCESS(file_IoStatusBlock.Status),GNOME_VFS_ERROR_GENERIC);
248         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
249         g_return_val_if_fail(file_IoStatusBlock.Information<=num_bytes,GNOME_VFS_ERROR_GENERIC);
250
251         captive_file_object->offset+=file_IoStatusBlock.Information;
252         *bytes_read_return=file_IoStatusBlock.Information;
253         return GNOME_VFS_OK;
254 }
255
256
257 GnomeVFSResult captive_file_write(CaptiveFileObject *captive_file_object,
258                 gconstpointer buffer,GnomeVFSFileSize num_bytes,GnomeVFSFileSize *bytes_written_return)
259 {
260 NTSTATUS err;
261 IO_STATUS_BLOCK file_IoStatusBlock;
262 LARGE_INTEGER file_offset;
263
264         if (CAPTIVE_IS_SANDBOX_PARENT())
265                 return captive_sandbox_parent_file_write(captive_file_object,buffer,num_bytes,bytes_written_return);
266
267         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
268         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
269         g_return_val_if_fail(buffer!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
270         g_return_val_if_fail(bytes_written_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
271         g_return_val_if_fail(num_bytes==(ULONG)num_bytes,GNOME_VFS_ERROR_BAD_PARAMETERS);
272
273         file_offset.QuadPart=captive_file_object->offset;
274         err=NtWriteFile(
275                         captive_file_object->file_Handle,       /* FileHandle */
276                         NULL,   /* Event; completion signalling; optional */
277                         NULL,   /* ApcRoutine; optional */
278                         NULL,   /* ApcContext; optional */
279                         &file_IoStatusBlock,    /* IoStatusBlock */
280                         (gpointer /* de-const */ )buffer,       /* Buffer */
281                         num_bytes,      /* Length */
282                         &file_offset,   /* ByteOffset */
283                         NULL);  /* Key; NULL means no file locking key */
284         g_return_val_if_fail(NT_SUCCESS(err)==NT_SUCCESS(file_IoStatusBlock.Status),GNOME_VFS_ERROR_GENERIC);
285         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
286         g_return_val_if_fail(file_IoStatusBlock.Information==num_bytes,GNOME_VFS_ERROR_GENERIC);
287
288         captive_file_object->offset+=file_IoStatusBlock.Information;
289         *bytes_written_return=file_IoStatusBlock.Information;
290         return GNOME_VFS_OK;
291 }
292
293
294 GnomeVFSResult captive_file_seek
295                 (CaptiveFileObject *captive_file_object,GnomeVFSSeekPosition whence,GnomeVFSFileOffset offset)
296 {
297         if (CAPTIVE_IS_SANDBOX_PARENT())
298                 return captive_sandbox_parent_file_seek(captive_file_object,whence,offset);
299
300         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
301         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
302
303         switch (whence) {
304                 case GNOME_VFS_SEEK_START:
305                         captive_file_object->offset=offset;
306                         break;
307                 case GNOME_VFS_SEEK_CURRENT:
308                         if (0
309                                         || (offset>0 && (captive_file_object->offset+offset)<captive_file_object->offset)
310                                         || (offset<0 && (captive_file_object->offset+offset)>captive_file_object->offset))
311                                 return GNOME_VFS_ERROR_BAD_PARAMETERS;
312                         captive_file_object->offset+=offset;
313                         break;
314                 case GNOME_VFS_SEEK_END:
315                         g_assert_not_reached(); /* NOT IMPLEMENTED YET */
316                         break;
317                 default:
318                         g_return_val_if_reached(GNOME_VFS_ERROR_BAD_PARAMETERS);
319                 }
320
321         return GNOME_VFS_OK;
322 }
323
324
325 GnomeVFSResult captive_file_tell(CaptiveFileObject *captive_file_object,GnomeVFSFileOffset *offset_return)
326 {
327         if (CAPTIVE_IS_SANDBOX_PARENT())
328                 return captive_sandbox_parent_file_tell(captive_file_object,offset_return);
329
330         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
331         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
332         g_return_val_if_fail(offset_return!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
333
334         *offset_return=captive_file_object->offset;
335         return GNOME_VFS_OK;
336 }
337
338
339 GnomeVFSResult captive_file_remove(CaptiveFileObject *captive_file_object)
340 {
341 NTSTATUS err;
342 FILE_DISPOSITION_INFORMATION FileDispositionInformation_struct;
343 IO_STATUS_BLOCK file_IoStatusBlock;
344 GnomeVFSResult errvfsresult;
345
346         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
347         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
348
349         FileDispositionInformation_struct.DoDeleteFile=TRUE;
350
351         err=NtSetInformationFile(
352                         captive_file_object->file_Handle,       /* FileHandle */
353                         &file_IoStatusBlock,    /* IoStatusBlock */
354                         &FileDispositionInformation_struct,     /* FileInformation */
355                         sizeof(FileDispositionInformation_struct),      /* Length */
356                         FileDispositionInformation);    /* FileInformationClass */
357         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
358                 return errvfsresult;
359         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
360
361         return GNOME_VFS_OK;
362 }
363
364
365 /* Use 'FileNameInformationStruct' identifier instead of the logical 'FileNameInformation'
366  * to prevent override of enum member 'FileNameInformation'
367  */
368 static GnomeVFSResult FileNameInformationStruct_to_GnomeVFSFileInfo(GnomeVFSFileInfo *file_info,
369                 FILE_NAME_INFORMATION *FileNameInformationStruct)
370 {
371 UNICODE_STRING FileName_UnicodeString;
372
373         g_return_val_if_fail(file_info!=NULL,GNOME_VFS_ERROR_GENERIC);
374         g_return_val_if_fail(FileNameInformationStruct!=NULL,GNOME_VFS_ERROR_GENERIC);
375
376         FileName_UnicodeString.Length=FileNameInformationStruct->FileNameLength;
377         FileName_UnicodeString.MaximumLength=FileNameInformationStruct->FileNameLength
378                         +sizeof(*FileNameInformationStruct->FileName);  /* 0-terminator */
379
380 #if 0   /* 'IoStatusBlock->Information' is sometimes weird, do not check it */
381         g_assert((gpointer)(((char *)FileNameInformationStruct->FileName)+FileName_UnicodeString.Length)
382                         <=(gpointer)(((char *)FileAllInformationStruct)+IoStatusBlock->Information));
383                                         /* ensure we fit below '->IoStatusBlock->Information' at least without the 0-terminator */
384 #endif
385
386         FileNameInformationStruct->FileName[FileNameInformationStruct->FileNameLength
387                         /sizeof(*FileNameInformationStruct->FileName)]=0;       /* 0-terminate it */
388         FileName_UnicodeString.Buffer=FileNameInformationStruct->FileName;
389         file_info->name=captive_UnicodeString_to_utf8_malloc(&FileName_UnicodeString);
390         /* '->name' assumed for 'file_info->valid_fields' */
391
392         return GNOME_VFS_OK;
393 }
394
395
396 /* Use 'FileBasicInformationStruct' identifier instead of the logical 'FileBasicInformation'
397  * to prevent override of enum member 'FileBasicInformation'
398  */
399 static GnomeVFSResult FileBasicInformationStruct_to_GnomeVFSFileInfo(GnomeVFSFileInfo *file_info,
400                 FILE_BASIC_INFORMATION *FileBasicInformationStruct)
401 {
402 BOOLEAN errBOOLEAN;
403 ULONG tmp_ULONG;
404
405         /* FIXME: What is 'FILE_ATTRIBUTE_NORMAL'? */
406         switch (FileBasicInformationStruct->FileAttributes & (0
407                         | FILE_ATTRIBUTE_DIRECTORY
408                         | FILE_ATTRIBUTE_DEVICE)) {
409                 case 0:                        file_info->type=GNOME_VFS_FILE_TYPE_REGULAR;   break;
410                 case FILE_ATTRIBUTE_DIRECTORY: file_info->type=GNOME_VFS_FILE_TYPE_DIRECTORY; break;
411                 case FILE_ATTRIBUTE_DEVICE:    file_info->type=GNOME_VFS_FILE_TYPE_SOCKET;
412                         /* or GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE or GNOME_VFS_FILE_TYPE_BLOCK_DEVICE ? */
413                         break;
414                 default:                       file_info->type=GNOME_VFS_FILE_TYPE_UNKNOWN;   break;
415                 }
416         file_info->valid_fields|=GNOME_VFS_FILE_INFO_FIELDS_TYPE;
417
418         /* we use 0600 for r/w files, 0400 for FILE_ATTRIBUTE_READONLY */
419         file_info->permissions=GNOME_VFS_PERM_USER_READ;
420         if (file_info->type==GNOME_VFS_FILE_TYPE_DIRECTORY)
421                 file_info->permissions|=GNOME_VFS_PERM_USER_EXEC;
422         if (!(FileBasicInformationStruct->FileAttributes & FILE_ATTRIBUTE_READONLY))
423                 file_info->permissions|=GNOME_VFS_PERM_USER_WRITE;
424         file_info->valid_fields|=GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS;
425
426         if (FileBasicInformationStruct->LastAccessTime.QuadPart) {      /* it may be 0 if not set */
427                 errBOOLEAN=RtlTimeToSecondsSince1970(&FileBasicInformationStruct->LastAccessTime,&tmp_ULONG);
428                 g_assert(errBOOLEAN==TRUE);
429                 file_info->atime=tmp_ULONG;
430                 file_info->valid_fields|=GNOME_VFS_FILE_INFO_FIELDS_ATIME;
431                 }
432
433         /* it may be 0 if not set */
434         if (FileBasicInformationStruct->LastWriteTime.QuadPart || FileBasicInformationStruct->ChangeTime.QuadPart) {
435                 errBOOLEAN=RtlTimeToSecondsSince1970(
436                                 /* take the more recent (==bigger) time: */
437                                 (FileBasicInformationStruct->LastWriteTime.QuadPart > FileBasicInformationStruct->ChangeTime.QuadPart
438                                                 ? &FileBasicInformationStruct->LastWriteTime : &FileBasicInformationStruct->ChangeTime),
439                                 &tmp_ULONG);
440                 g_assert(errBOOLEAN==TRUE);
441                 file_info->mtime=tmp_ULONG;
442                 file_info->valid_fields|=GNOME_VFS_FILE_INFO_FIELDS_MTIME;
443                 }
444
445         if (FileBasicInformationStruct->CreationTime.QuadPart) {        /* it may be 0 if not set */
446                 errBOOLEAN=RtlTimeToSecondsSince1970(&FileBasicInformationStruct->CreationTime,&tmp_ULONG);
447                 g_assert(errBOOLEAN==TRUE);
448                 file_info->ctime=tmp_ULONG;
449                 file_info->valid_fields|=GNOME_VFS_FILE_INFO_FIELDS_CTIME;
450                 }
451
452         return GNOME_VFS_OK;
453 }
454
455
456 /* Use 'FileStandardInformationStruct' identifier instead of the logical 'FileStandardInformation'
457  * to prevent override of enum member 'FileStandardInformation'
458  */
459 static GnomeVFSResult FileStandardInformationStruct_to_GnomeVFSFileInfo(GnomeVFSFileInfo *file_info,
460                 FILE_STANDARD_INFORMATION *FileStandardInformationStruct)
461 {
462         file_info->size=FileStandardInformationStruct->EndOfFile.QuadPart;
463         file_info->valid_fields|=GNOME_VFS_FILE_INFO_FIELDS_SIZE;
464
465         file_info->block_count=FileStandardInformationStruct->AllocationSize.QuadPart/512;
466         file_info->valid_fields|=GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
467
468         return GNOME_VFS_OK;
469 }
470
471
472 GnomeVFSResult captive_file_file_info_get(CaptiveFileObject *captive_file_object,
473                 GnomeVFSFileInfo *file_info)
474 {
475 NTSTATUS err;
476 IO_STATUS_BLOCK file_IoStatusBlock;
477 FILE_NAME_INFORMATION *FileNameInformationStruct;
478 FILE_BASIC_INFORMATION *FileBasicInformationStruct;
479 FILE_STANDARD_INFORMATION *FileStandardInformationStruct;
480 GnomeVFSResult errvfsresult;
481 char QueryFile_buf[sizeof(FILE_NAME_INFORMATION)
482                 +0x1000 /* max 'FileName' length, 255 should be enough */ * sizeof(WCHAR /* *FILE_ALL_INFORMATION.NameInformation.FileName */ )];
483
484         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
485         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
486         g_return_val_if_fail(file_info!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
487
488         /* ntfs.sys of NT-5.1sp1 has *(FILE_BASIC_INFORMATION *)FILE_ALL_INFORMATION.BasicInformation
489          * size 0x28 although reactos includes and all the information says 0x24.
490          * fastfat.sys of NT-5.1sp1 also has the 'official' sizeof 0x24.
491          * It would be a bit of heuristic to correctly detect its expected sizeof.
492          */
493
494         file_info->valid_fields=0;
495
496         file_info->flags=GNOME_VFS_FILE_FLAGS_LOCAL;
497         file_info->valid_fields|=GNOME_VFS_FILE_INFO_FIELDS_FLAGS;
498
499         /* query NameInformation */
500         FileNameInformationStruct=(void *)QueryFile_buf;
501         err=NtQueryInformationFile(
502                         captive_file_object->file_Handle,       /* FileHandle */
503                         &file_IoStatusBlock,    /* IoStatusBlock */
504                         (gpointer)QueryFile_buf,        /* FileInformation */
505                         sizeof(QueryFile_buf)   /* Length */
506                                         -sizeof(*FileNameInformationStruct->FileName),  /* reserve space for a possible 0-terminator */
507                         FileNameInformation);   /* FileInformationClass; =>FILE_NAME_INFORMATION */
508         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
509                 return errvfsresult;
510         g_return_val_if_fail(NT_SUCCESS(err)==NT_SUCCESS(file_IoStatusBlock.Status),GNOME_VFS_ERROR_GENERIC);
511         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
512         errvfsresult=FileNameInformationStruct_to_GnomeVFSFileInfo(file_info,FileNameInformationStruct);
513         g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
514
515         /* query BasicInformation */
516         FileBasicInformationStruct=(void *)QueryFile_buf;
517         err=NtQueryInformationFile(
518                         captive_file_object->file_Handle,       /* FileHandle */
519                         &file_IoStatusBlock,    /* IoStatusBlock */
520                         (gpointer)QueryFile_buf,        /* FileInformation */
521                         sizeof(QueryFile_buf),  /* Length */
522                         FileBasicInformation);  /* FileInformationClass; =>FILE_BASIC_INFORMATION */
523         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
524                 return errvfsresult;
525         g_return_val_if_fail(NT_SUCCESS(err)==NT_SUCCESS(file_IoStatusBlock.Status),GNOME_VFS_ERROR_GENERIC);
526         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
527         errvfsresult=FileBasicInformationStruct_to_GnomeVFSFileInfo(file_info,FileBasicInformationStruct);
528         g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
529
530         /* query StandardInformation */
531         FileStandardInformationStruct=(void *)QueryFile_buf;
532         err=NtQueryInformationFile(
533                         captive_file_object->file_Handle,       /* FileHandle */
534                         &file_IoStatusBlock,    /* IoStatusBlock */
535                         (gpointer)QueryFile_buf,        /* FileInformation */
536                         sizeof(QueryFile_buf),  /* Length */
537                         FileStandardInformation);       /* FileInformationClass; =>FILE_STANDARD_INFORMATION */
538         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
539                 return errvfsresult;
540         g_return_val_if_fail(NT_SUCCESS(err)==NT_SUCCESS(file_IoStatusBlock.Status),GNOME_VFS_ERROR_GENERIC);
541         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
542         errvfsresult=FileStandardInformationStruct_to_GnomeVFSFileInfo(file_info,FileStandardInformationStruct);
543         g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,errvfsresult);
544
545         return GNOME_VFS_OK;
546 }
547
548
549 GnomeVFSResult captive_file_file_info_set(CaptiveFileObject *captive_file_object,
550     const GnomeVFSFileInfo *info,GnomeVFSSetFileInfoMask mask)
551 {
552         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
553         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
554         g_return_val_if_fail(info!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
555
556         if (mask & GNOME_VFS_SET_FILE_INFO_NAME) {
557 gchar *name_dirname,*name_basename;
558 UNICODE_STRING *name_UnicodeString;
559 FILE_RENAME_INFORMATION *FileRenameInformation_structp;
560 gsize FileRenameInformation_struct_len;
561 IO_STATUS_BLOCK file_IoStatusBlock;
562 NTSTATUS err;
563 GnomeVFSResult errvfsresult;
564
565                 /* non-existing dirname assertion */
566                 name_dirname=g_path_get_dirname(info->name);
567                 if (*name_dirname) {
568                         g_assert_not_reached();
569                         errvfsresult=GNOME_VFS_ERROR_BAD_PARAMETERS;
570                         goto err;
571                         }
572
573                 /* fully-matching basename assertion */
574                 name_basename=g_path_get_basename(info->name);
575                 if (strcmp(name_basename,info->name)) {
576                         g_assert_not_reached();
577                         errvfsresult=GNOME_VFS_ERROR_BAD_PARAMETERS;
578                         goto err_free_name_dirname;
579                         }
580
581                 name_UnicodeString=captive_utf8_to_UnicodeString_alloca(info->name);
582                 FileRenameInformation_struct_len=sizeof(*FileRenameInformation_structp)
583                                 +name_UnicodeString->MaximumLength;     /* ==Length+(0-terminator); is the terminator needed? */
584
585                 FileRenameInformation_structp=g_alloca(FileRenameInformation_struct_len);
586                 FileRenameInformation_structp->Replace=FALSE;
587                 FileRenameInformation_structp->RootDir=NULL;    /* we do 'simple rename' here */
588                 FileRenameInformation_structp->FileNameLength=name_UnicodeString->Length;
589                 memcpy(FileRenameInformation_structp->FileName,name_UnicodeString->Buffer,
590                                 name_UnicodeString->MaximumLength);     /* ==Length+(0-terminator); is the terminator needed? */
591
592                 err=NtSetInformationFile(
593                                 captive_file_object->file_Handle,       /* FileHandle */
594                                 &file_IoStatusBlock,    /* IoStatusBlock */
595                                 FileRenameInformation_structp,  /* FileInformation */
596                                 FileRenameInformation_struct_len,       /* Length */
597                                 FileRenameInformation); /* FileInformationClass */
598                 if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
599                         goto err_free_name_basename;
600
601 err_free_name_basename:
602                 g_free(name_basename);
603 err_free_name_dirname:
604                 g_free(name_dirname);
605 err:
606
607                 if (errvfsresult!=GNOME_VFS_OK)
608                         return errvfsresult;
609                 g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
610                 }
611
612         if (mask & (GNOME_VFS_SET_FILE_INFO_PERMISSIONS | GNOME_VFS_SET_FILE_INFO_TIME)) {
613 char FileBasicInformation_buf[sizeof(FILE_BASIC_INFORMATION)+4];
614 FILE_BASIC_INFORMATION *FileBasicInformationp=(FILE_BASIC_INFORMATION *)FileBasicInformation_buf;
615 IO_STATUS_BLOCK file_IoStatusBlock;
616 NTSTATUS err;
617
618                 /* FIXME: Everywhere is is the declaration with 4xLARGE_INTEGER+1xULONG==36 bytes
619                  * but the precompiled ext2fsd.sys wants 40 bytes - why?
620                  * The same case applies to ntfs.sys of NT-5.1sp1.
621                  */
622                 g_assert(sizeof(FILE_BASIC_INFORMATION)==36);
623                 g_assert(sizeof(FileBasicInformation_buf)==40);
624                 err=NtQueryInformationFile(
625                                 captive_file_object->file_Handle,       /* FileHandle */
626                                 &file_IoStatusBlock,    /* IoStatusBlock */
627                                 FileBasicInformation_buf,       /* FileInformation */
628                                 sizeof(FileBasicInformation_buf),       /* Length */
629                                 FileBasicInformation);  /* FileInformationClass */
630                 g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
631
632                 if (mask & GNOME_VFS_SET_FILE_INFO_PERMISSIONS) {
633                         FileBasicInformationp->FileAttributes&=~FILE_ATTRIBUTE_READONLY;
634                         g_assert(info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS);
635                         if (!(info->permissions&0200))
636                                 FileBasicInformationp->FileAttributes|=FILE_ATTRIBUTE_READONLY;
637                         }
638
639                 if (mask & GNOME_VFS_SET_FILE_INFO_TIME) {
640                         g_assert(info->valid_fields & (0
641                                         | GNOME_VFS_FILE_INFO_FIELDS_ATIME
642                                         | GNOME_VFS_FILE_INFO_FIELDS_MTIME
643                                         | GNOME_VFS_FILE_INFO_FIELDS_CTIME));
644                         if (info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_ATIME) {
645                                 RtlSecondsSince1970ToTime(
646                                                 info->atime,    /* SecondsSince1970 */
647                                                 &FileBasicInformationp->LastAccessTime);        /* Time */
648                                 }
649                         if (info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_MTIME) {
650                                 RtlSecondsSince1970ToTime(
651                                                 info->mtime,    /* SecondsSince1970 */
652                                                 &FileBasicInformationp->LastWriteTime); /* Time */
653                                 FileBasicInformationp->ChangeTime=FileBasicInformationp->LastWriteTime;
654                                 }
655                         if (info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_CTIME) {
656                                 RtlSecondsSince1970ToTime(
657                                                 info->ctime,    /* SecondsSince1970 */
658                                                 &FileBasicInformationp->CreationTime);  /* Time */
659                                 }
660                         }
661
662                 err=NtSetInformationFile(
663                                 captive_file_object->file_Handle,       /* FileHandle */
664                                 &file_IoStatusBlock,    /* IoStatusBlock */
665                                 FileBasicInformation_buf,       /* FileInformation */
666                                 sizeof(FileBasicInformation_buf),       /* Length */
667                                 FileBasicInformation);  /* FileInformationClass */
668                 g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
669                 }
670
671         if (mask & GNOME_VFS_SET_FILE_INFO_OWNER) {
672                 /* FIXME: g_assert(info->valid_fields & ???); */
673                 /* owner ignored for W32 */
674                 }
675
676         return GNOME_VFS_OK;
677 }
678
679
680 GnomeVFSResult captive_file_truncate(CaptiveFileObject *captive_file_object,GnomeVFSFileSize file_size)
681 {
682 NTSTATUS err;
683 FILE_END_OF_FILE_INFORMATION FileEndOfFileInformation_struct;
684 IO_STATUS_BLOCK file_IoStatusBlock;
685 GnomeVFSResult errvfsresult;
686
687         g_return_val_if_fail(captive_file_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
688         g_return_val_if_fail(captive_file_object->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
689
690         FileEndOfFileInformation_struct.EndOfFile.QuadPart=file_size;
691
692         err=NtSetInformationFile(
693                         captive_file_object->file_Handle,       /* FileHandle */
694                         &file_IoStatusBlock,    /* IoStatusBlock */
695                         &FileEndOfFileInformation_struct,       /* FileInformation */
696                         sizeof(FileEndOfFileInformation_struct),        /* Length */
697                         FileEndOfFileInformation);      /* FileInformationClass */
698         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
699                 return errvfsresult;
700         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
701
702         return GNOME_VFS_OK;
703 }
704
705
706 GnomeVFSResult captive_file_move(CaptiveFileObject *captive_file_object_old,const gchar *pathname_new,gboolean force_replace)
707 {
708 NTSTATUS err;
709 FILE_RENAME_INFORMATION FileRenameInformation_struct;
710 IO_STATUS_BLOCK file_IoStatusBlock;
711 GnomeVFSResult errvfsresult;
712 gchar *pathname_new_dirname,*pathname_new_basename;
713 CaptiveDirectoryObject *captive_target_directory_object;
714 FILE_OBJECT *target_dir_FileObject;
715 UNICODE_STRING *target_dir_FileObject_FileName_UnicodeString_local;
716 BOOLEAN errboolean;
717
718         g_return_val_if_fail(captive_file_object_old!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
719         g_return_val_if_fail(captive_file_object_old->file_Handle!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
720         g_return_val_if_fail(pathname_new!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
721
722         pathname_new_dirname=g_path_get_dirname(pathname_new);
723         pathname_new_basename=g_path_get_basename(pathname_new);
724
725         errvfsresult=captive_directory_new_open(&captive_target_directory_object,pathname_new_dirname);
726         if (errvfsresult!=GNOME_VFS_OK)
727                 goto err_free_pathnames;
728
729         err=ObReferenceObjectByHandle(
730                         captive_target_directory_object->dir_Handle,    /* Handle */
731                         (FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY),        /* DesiredAccess */
732                         IoFileObjectType,       /* ObjectType */
733                         KernelMode,     /* AccessMode */
734                         (PVOID *)&target_dir_FileObject,        /* Object */
735                         NULL);  /* HandleInfo */
736         if (!NT_SUCCESS(err)) {
737                 g_assert_not_reached();
738                 errvfsresult=GNOME_VFS_ERROR_GENERIC;
739                 goto err_close_captive_target_directory_object;
740                 }
741
742         /* Supply only the 'basename' to 'target_dir_FileObject->FileName'
743          * as the directory is fully determined by its other attributes.
744          */
745         target_dir_FileObject_FileName_UnicodeString_local=captive_utf8_to_UnicodeString_alloca(pathname_new_basename);
746         errboolean=RtlCreateUnicodeString(&target_dir_FileObject->FileName,
747                         target_dir_FileObject_FileName_UnicodeString_local->Buffer);
748         g_assert(errboolean==TRUE);
749
750         ObDereferenceObject(target_dir_FileObject);
751
752         FileRenameInformation_struct.Replace=force_replace;
753         FileRenameInformation_struct.RootDir=captive_target_directory_object->dir_Handle;       /* ->'target_dir_FileObject' */
754         FileRenameInformation_struct.FileNameLength=0;  /* provided by 'FileRenameInformation_struct.RootDir' */
755
756         err=NtSetInformationFile(
757                         captive_file_object_old->file_Handle,   /* FileHandle */
758                         &file_IoStatusBlock,    /* IoStatusBlock */
759                         &FileRenameInformation_struct,  /* FileInformation */
760                         sizeof(FileRenameInformation_struct),   /* Length */
761                         FileRenameInformation); /* FileInformationClass */
762         if (GNOME_VFS_OK!=(errvfsresult=captive_NTSTATUS_to_GnomeVFSResult(err)))
763                 goto err_close_captive_target_directory_object;
764         g_return_val_if_fail(NT_SUCCESS(err),GNOME_VFS_ERROR_GENERIC);
765
766
767 err_close_captive_target_directory_object:
768         g_object_unref(captive_target_directory_object);
769 err_free_pathnames:
770         g_free(pathname_new_dirname);
771         g_free(pathname_new_basename);
772
773         return errvfsresult;
774 }