3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
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; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystems/fastfat/create.c
22 * PURPOSE: VFAT Filesystem
23 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
24 * Pierre Schweitzer (pierre@reactos.org)
27 /* INCLUDES *****************************************************************/
34 /* FUNCTIONS *****************************************************************/
38 PFAT_DIR_ENTRY pEntry
,
39 PUNICODE_STRING NameU
)
45 RtlCopyMemory(cString
, pEntry
->ShortName
, 11);
47 if (cString
[0] == 0x05)
52 StringA
.Buffer
= cString
;
53 for (StringA
.Length
= 0;
54 StringA
.Length
< 8 && StringA
.Buffer
[StringA
.Length
] != ' ';
56 StringA
.MaximumLength
= StringA
.Length
;
58 RtlOemStringToUnicodeString(NameU
, &StringA
, FALSE
);
60 if (BooleanFlagOn(pEntry
->lCase
, VFAT_CASE_LOWER_BASE
))
62 RtlDowncaseUnicodeString(NameU
, NameU
, FALSE
);
65 if (cString
[8] != ' ')
67 Length
= NameU
->Length
;
68 NameU
->Buffer
+= Length
/ sizeof(WCHAR
);
69 if (!FAT_ENTRY_VOLUME(pEntry
))
71 Length
+= sizeof(WCHAR
);
72 NameU
->Buffer
[0] = L
'.';
76 NameU
->MaximumLength
-= Length
;
78 StringA
.Buffer
= &cString
[8];
79 for (StringA
.Length
= 0;
80 StringA
.Length
< 3 && StringA
.Buffer
[StringA
.Length
] != ' ';
82 StringA
.MaximumLength
= StringA
.Length
;
83 RtlOemStringToUnicodeString(NameU
, &StringA
, FALSE
);
84 if (BooleanFlagOn(pEntry
->lCase
, VFAT_CASE_LOWER_EXT
))
86 RtlDowncaseUnicodeString(NameU
, NameU
, FALSE
);
88 NameU
->Buffer
-= Length
/ sizeof(WCHAR
);
89 NameU
->Length
+= Length
;
90 NameU
->MaximumLength
+= Length
;
93 NameU
->Buffer
[NameU
->Length
/ sizeof(WCHAR
)] = 0;
94 DPRINT("'%wZ'\n", NameU
);
98 * FUNCTION: Find a file
102 PDEVICE_EXTENSION DeviceExt
,
104 PUNICODE_STRING FileToFindU
,
105 PVFAT_DIRENTRY_CONTEXT DirContext
,
108 PWCHAR PathNameBuffer
;
109 USHORT PathNameBufferLength
;
111 PVOID Context
= NULL
;
115 UNICODE_STRING PathNameU
;
116 UNICODE_STRING FileToFindUpcase
;
118 BOOLEAN IsFatX
= vfatVolumeIsFatX(DeviceExt
);
120 DPRINT("FindFile(Parent %p, FileToFind '%wZ', DirIndex: %u)\n",
121 Parent
, FileToFindU
, DirContext
->DirIndex
);
122 DPRINT("FindFile: Path %wZ\n",&Parent
->PathNameU
);
124 PathNameBufferLength
= LONGNAME_MAX_LENGTH
* sizeof(WCHAR
);
125 PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameBufferLength
+ sizeof(WCHAR
), TAG_VFAT
);
128 return STATUS_INSUFFICIENT_RESOURCES
;
131 PathNameU
.Buffer
= PathNameBuffer
;
132 PathNameU
.Length
= 0;
133 PathNameU
.MaximumLength
= PathNameBufferLength
;
135 DirContext
->LongNameU
.Length
= 0;
136 DirContext
->ShortNameU
.Length
= 0;
138 WildCard
= FsRtlDoesNameContainWildCards(FileToFindU
);
140 if (WildCard
== FALSE
)
142 /* if there is no '*?' in the search name, than look first for an existing fcb */
143 RtlCopyUnicodeString(&PathNameU
, &Parent
->PathNameU
);
144 if (!vfatFCBIsRoot(Parent
))
146 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = L
'\\';
147 PathNameU
.Length
+= sizeof(WCHAR
);
149 RtlAppendUnicodeStringToString(&PathNameU
, FileToFindU
);
150 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = 0;
151 rcFcb
= vfatGrabFCBFromTable(DeviceExt
, &PathNameU
);
154 ULONG startIndex
= rcFcb
->startIndex
;
155 if (IsFatX
&& !vfatFCBIsRoot(Parent
))
159 if(startIndex
>= DirContext
->DirIndex
)
161 RtlCopyUnicodeString(&DirContext
->LongNameU
, &rcFcb
->LongNameU
);
162 RtlCopyUnicodeString(&DirContext
->ShortNameU
, &rcFcb
->ShortNameU
);
163 RtlCopyMemory(&DirContext
->DirEntry
, &rcFcb
->entry
, sizeof(DIR_ENTRY
));
164 DirContext
->StartIndex
= rcFcb
->startIndex
;
165 DirContext
->DirIndex
= rcFcb
->dirIndex
;
166 DPRINT("FindFile: new Name %wZ, DirIndex %u (%u)\n",
167 &DirContext
->LongNameU
, DirContext
->DirIndex
, DirContext
->StartIndex
);
168 Status
= STATUS_SUCCESS
;
172 DPRINT("FCB not found for %wZ\n", &PathNameU
);
173 Status
= STATUS_UNSUCCESSFUL
;
175 vfatReleaseFCB(DeviceExt
, rcFcb
);
176 ExFreePool(PathNameBuffer
);
181 /* FsRtlIsNameInExpression need the searched string to be upcase,
182 * even if IgnoreCase is specified */
183 Status
= RtlUpcaseUnicodeString(&FileToFindUpcase
, FileToFindU
, TRUE
);
184 if (!NT_SUCCESS(Status
))
186 ExFreePool(PathNameBuffer
);
192 Status
= VfatGetNextDirEntry(DeviceExt
, &Context
, &Page
, Parent
, DirContext
, First
);
194 if (Status
== STATUS_NO_MORE_ENTRIES
)
198 if (ENTRY_VOLUME(IsFatX
, &DirContext
->DirEntry
))
200 DirContext
->DirIndex
++;
203 if (DirContext
->LongNameU
.Length
== 0 ||
204 DirContext
->ShortNameU
.Length
== 0)
206 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
207 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
209 ASSERT(DirContext
->LongNameU
.Length
!= 0 &&
210 DirContext
->ShortNameU
.Length
!= 0);
212 DirContext
->DirIndex
++;
217 Found
= FsRtlIsNameInExpression(&FileToFindUpcase
, &DirContext
->LongNameU
, TRUE
, NULL
) ||
218 FsRtlIsNameInExpression(&FileToFindUpcase
, &DirContext
->ShortNameU
, TRUE
, NULL
);
222 Found
= FsRtlAreNamesEqual(&DirContext
->LongNameU
, FileToFindU
, TRUE
, NULL
) ||
223 FsRtlAreNamesEqual(&DirContext
->ShortNameU
, FileToFindU
, TRUE
, NULL
);
230 RtlCopyUnicodeString(&PathNameU
, &Parent
->PathNameU
);
231 if (!vfatFCBIsRoot(Parent
))
233 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = L
'\\';
234 PathNameU
.Length
+= sizeof(WCHAR
);
236 RtlAppendUnicodeStringToString(&PathNameU
, &DirContext
->LongNameU
);
237 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = 0;
238 rcFcb
= vfatGrabFCBFromTable(DeviceExt
, &PathNameU
);
241 RtlCopyMemory(&DirContext
->DirEntry
, &rcFcb
->entry
, sizeof(DIR_ENTRY
));
242 vfatReleaseFCB(DeviceExt
, rcFcb
);
245 DPRINT("%u\n", DirContext
->LongNameU
.Length
);
246 DPRINT("FindFile: new Name %wZ, DirIndex %u\n",
247 &DirContext
->LongNameU
, DirContext
->DirIndex
);
251 CcUnpinData(Context
);
253 RtlFreeUnicodeString(&FileToFindUpcase
);
254 ExFreePool(PathNameBuffer
);
255 return STATUS_SUCCESS
;
257 DirContext
->DirIndex
++;
262 CcUnpinData(Context
);
265 RtlFreeUnicodeString(&FileToFindUpcase
);
266 ExFreePool(PathNameBuffer
);
271 * FUNCTION: Opens a file
276 PDEVICE_EXTENSION DeviceExt
,
277 PUNICODE_STRING PathNameU
,
278 PFILE_OBJECT FileObject
,
279 ULONG RequestedDisposition
,
280 ULONG RequestedOptions
,
286 DPRINT("VfatOpenFile(%p, '%wZ', %p, %p)\n", DeviceExt
, PathNameU
, FileObject
, ParentFcb
);
288 if (FileObject
->RelatedFileObject
)
290 DPRINT("'%wZ'\n", &FileObject
->RelatedFileObject
->FileName
);
292 *ParentFcb
= FileObject
->RelatedFileObject
->FsContext
;
299 if (!DeviceExt
->FatInfo
.FixedMedia
)
301 Status
= VfatBlockDeviceIoControl(DeviceExt
->StorageDevice
,
302 IOCTL_DISK_CHECK_VERIFY
,
308 if (!NT_SUCCESS(Status
))
310 DPRINT("Status %lx\n", Status
);
318 vfatGrabFCB(DeviceExt
, *ParentFcb
);
321 /* try first to find an existing FCB in memory */
322 DPRINT("Checking for existing FCB in memory\n");
324 Status
= vfatGetFCBForFile(DeviceExt
, ParentFcb
, &Fcb
, PathNameU
);
325 if (!NT_SUCCESS(Status
))
327 DPRINT ("Could not make a new FCB, status: %x\n", Status
);
331 /* Fail, if we try to overwrite an existing directory */
332 if ((!BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) && vfatFCBIsDirectory(Fcb
)) &&
333 (RequestedDisposition
== FILE_OVERWRITE
||
334 RequestedDisposition
== FILE_OVERWRITE_IF
||
335 RequestedDisposition
== FILE_SUPERSEDE
))
337 vfatReleaseFCB(DeviceExt
, Fcb
);
338 return STATUS_OBJECT_NAME_COLLISION
;
341 if (BooleanFlagOn(Fcb
->Flags
, FCB_DELETE_PENDING
))
343 vfatReleaseFCB(DeviceExt
, Fcb
);
344 return STATUS_DELETE_PENDING
;
347 /* Fail, if we try to overwrite a read-only file */
348 if (vfatFCBIsReadOnly(Fcb
) &&
349 (RequestedDisposition
== FILE_OVERWRITE
||
350 RequestedDisposition
== FILE_OVERWRITE_IF
))
352 vfatReleaseFCB(DeviceExt
, Fcb
);
353 return STATUS_ACCESS_DENIED
;
356 if (vfatFCBIsReadOnly(Fcb
) &&
357 (RequestedOptions
& FILE_DELETE_ON_CLOSE
))
359 vfatReleaseFCB(DeviceExt
, Fcb
);
360 return STATUS_CANNOT_DELETE
;
363 if ((vfatFCBIsRoot(Fcb
) ||
364 (Fcb
->LongNameU
.Length
== sizeof(WCHAR
) && Fcb
->LongNameU
.Buffer
[0] == L
'.') ||
365 (Fcb
->LongNameU
.Length
== 2 * sizeof(WCHAR
) && Fcb
->LongNameU
.Buffer
[0] == L
'.' && Fcb
->LongNameU
.Buffer
[1] == L
'.')) &&
366 BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
368 // we cannot delete a '.', '..' or the root directory
369 vfatReleaseFCB(DeviceExt
, Fcb
);
370 return STATUS_CANNOT_DELETE
;
373 DPRINT("Attaching FCB to fileObject\n");
374 Status
= vfatAttachFCBToFileObject(DeviceExt
, Fcb
, FileObject
);
375 if (!NT_SUCCESS(Status
))
377 vfatReleaseFCB(DeviceExt
, Fcb
);
383 * FUNCTION: Create or open a file
387 PDEVICE_OBJECT DeviceObject
,
390 PIO_STACK_LOCATION Stack
;
391 PFILE_OBJECT FileObject
;
392 NTSTATUS Status
= STATUS_SUCCESS
;
393 PDEVICE_EXTENSION DeviceExt
;
394 ULONG RequestedDisposition
, RequestedOptions
;
395 PVFATFCB pFcb
= NULL
;
396 PVFATFCB ParentFcb
= NULL
;
397 PVFATCCB pCcb
= NULL
;
399 BOOLEAN PagingFileCreate
;
401 BOOLEAN OpenTargetDir
;
402 BOOLEAN TrailingBackslash
;
403 UNICODE_STRING FileNameU
;
404 UNICODE_STRING PathNameU
;
407 /* Unpack the various parameters. */
408 Stack
= IoGetCurrentIrpStackLocation(Irp
);
409 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
410 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
411 PagingFileCreate
= BooleanFlagOn(Stack
->Flags
, SL_OPEN_PAGING_FILE
);
412 OpenTargetDir
= BooleanFlagOn(Stack
->Flags
, SL_OPEN_TARGET_DIRECTORY
);
414 FileObject
= Stack
->FileObject
;
415 DeviceExt
= DeviceObject
->DeviceExtension
;
417 if (BooleanFlagOn(Stack
->Parameters
.Create
.Options
, FILE_OPEN_BY_FILE_ID
))
419 return STATUS_NOT_IMPLEMENTED
;
422 /* Check their validity. */
423 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
424 RequestedDisposition
== FILE_SUPERSEDE
)
426 return STATUS_INVALID_PARAMETER
;
429 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
430 BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
))
432 return STATUS_INVALID_PARAMETER
;
435 /* Deny create if the volume is locked */
436 if (BooleanFlagOn(DeviceExt
->Flags
, VCB_VOLUME_LOCKED
))
438 return STATUS_ACCESS_DENIED
;
441 /* This a open operation for the volume itself */
442 if (FileObject
->FileName
.Length
== 0 &&
443 (FileObject
->RelatedFileObject
== NULL
||
444 FileObject
->RelatedFileObject
->FsContext2
!= NULL
||
445 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
447 DPRINT("Volume opening\n");
449 if (RequestedDisposition
!= FILE_OPEN
&&
450 RequestedDisposition
!= FILE_OPEN_IF
)
452 return STATUS_ACCESS_DENIED
;
455 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
456 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
== NULL
|| FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
458 return STATUS_NOT_A_DIRECTORY
;
463 return STATUS_INVALID_PARAMETER
;
466 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
468 return STATUS_CANNOT_DELETE
;
471 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
473 pFcb
= DeviceExt
->VolumeFcb
;
475 if (pFcb
->OpenHandleCount
== 0)
477 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
478 Stack
->Parameters
.Create
.ShareAccess
,
480 &pFcb
->FCBShareAccess
);
484 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
485 Stack
->Parameters
.Create
.ShareAccess
,
487 &pFcb
->FCBShareAccess
,
489 if (!NT_SUCCESS(Status
))
491 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
496 vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
497 DeviceExt
->OpenHandleCount
++;
498 pFcb
->OpenHandleCount
++;
499 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
501 Irp
->IoStatus
.Information
= FILE_OPENED
;
502 return STATUS_SUCCESS
;
505 if (FileObject
->RelatedFileObject
!= NULL
&&
506 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
)
508 ASSERT(FileObject
->FileName
.Length
!= 0);
509 return STATUS_OBJECT_PATH_NOT_FOUND
;
512 /* Check for illegal characters and illegal dot sequences in the file name */
513 PathNameU
= FileObject
->FileName
;
514 c
= PathNameU
.Buffer
+ PathNameU
.Length
/ sizeof(WCHAR
);
518 while (c
-- > PathNameU
.Buffer
)
520 if (*c
== L
'\\' || c
== PathNameU
.Buffer
)
522 if (Dots
&& last
> c
)
524 return STATUS_OBJECT_NAME_INVALID
;
526 if (*c
== L
'\\' && (c
- 1) > PathNameU
.Buffer
&&
529 return STATUS_OBJECT_NAME_INVALID
;
540 if (*c
!= '\\' && vfatIsLongIllegal(*c
))
542 return STATUS_OBJECT_NAME_INVALID
;
546 /* Check if we try to open target directory of root dir */
547 if (OpenTargetDir
&& FileObject
->RelatedFileObject
== NULL
&& PathNameU
.Length
== sizeof(WCHAR
) &&
548 PathNameU
.Buffer
[0] == L
'\\')
550 return STATUS_INVALID_PARAMETER
;
553 if (FileObject
->RelatedFileObject
&& PathNameU
.Length
>= sizeof(WCHAR
) && PathNameU
.Buffer
[0] == L
'\\')
555 return STATUS_OBJECT_NAME_INVALID
;
558 TrailingBackslash
= FALSE
;
559 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
561 PathNameU
.Length
-= sizeof(WCHAR
);
562 TrailingBackslash
= TRUE
;
565 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
567 return STATUS_OBJECT_NAME_INVALID
;
570 /* Try opening the file. */
573 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
575 Status
= VfatOpenFile(DeviceExt
, &PathNameU
, FileObject
, RequestedDisposition
, RequestedOptions
, &ParentFcb
);
580 LONG idx
, FileNameLen
;
582 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
584 ParentFcb
= (FileObject
->RelatedFileObject
!= NULL
) ? FileObject
->RelatedFileObject
->FsContext
: NULL
;
587 vfatGrabFCB(DeviceExt
, ParentFcb
);
590 Status
= vfatGetFCBForFile(DeviceExt
, &ParentFcb
, &TargetFcb
, &PathNameU
);
591 if (NT_SUCCESS(Status
))
593 vfatReleaseFCB(DeviceExt
, TargetFcb
);
594 Irp
->IoStatus
.Information
= FILE_EXISTS
;
598 Irp
->IoStatus
.Information
= FILE_DOES_NOT_EXIST
;
601 idx
= FileObject
->FileName
.Length
/ sizeof(WCHAR
) - 1;
603 /* Skip trailing \ - if any */
604 if (PathNameU
.Buffer
[idx
] == L
'\\')
607 PathNameU
.Length
-= sizeof(WCHAR
);
611 while (idx
>= 0 && PathNameU
.Buffer
[idx
] != L
'\\')
616 if (idx
> 0 || PathNameU
.Buffer
[0] == L
'\\')
618 /* We don't want to include / in the name */
619 FileNameLen
= PathNameU
.Length
- ((idx
+ 1) * sizeof(WCHAR
));
621 /* Update FO just to keep file name */
622 /* Skip first slash */
624 FileObject
->FileName
.Length
= FileNameLen
;
625 RtlMoveMemory(&PathNameU
.Buffer
[0], &PathNameU
.Buffer
[idx
], FileObject
->FileName
.Length
);
627 /* Terminate the string at the last backslash */
628 PathNameU
.Buffer
[idx
+ 1] = UNICODE_NULL
;
629 PathNameU
.Length
= (idx
+ 1) * sizeof(WCHAR
);
630 PathNameU
.MaximumLength
= PathNameU
.Length
+ sizeof(WCHAR
);
632 /* Update the file object as well */
633 FileObject
->FileName
.Length
= PathNameU
.Length
;
634 FileObject
->FileName
.MaximumLength
= PathNameU
.MaximumLength
;
639 /* This is a relative open and we have only the filename, so open the parent directory
640 * It is in RelatedFileObject
642 ASSERT(FileObject
->RelatedFileObject
!= NULL
);
644 /* No need to modify the FO, it already has the name */
647 /* We're done with opening! */
648 if (ParentFcb
!= NULL
)
650 Status
= vfatAttachFCBToFileObject(DeviceExt
, ParentFcb
, FileObject
);
653 if (NT_SUCCESS(Status
))
655 pFcb
= FileObject
->FsContext
;
656 ASSERT(pFcb
== ParentFcb
);
658 if (pFcb
->OpenHandleCount
== 0)
660 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
661 Stack
->Parameters
.Create
.ShareAccess
,
663 &pFcb
->FCBShareAccess
);
667 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
668 Stack
->Parameters
.Create
.ShareAccess
,
670 &pFcb
->FCBShareAccess
,
672 if (!NT_SUCCESS(Status
))
674 VfatCloseFile(DeviceExt
, FileObject
);
675 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
680 pCcb
= FileObject
->FsContext2
;
681 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
683 pCcb
->Flags
|= CCB_DELETE_ON_CLOSE
;
686 pFcb
->OpenHandleCount
++;
687 DeviceExt
->OpenHandleCount
++;
689 else if (ParentFcb
!= NULL
)
691 vfatReleaseFCB(DeviceExt
, ParentFcb
);
694 if (NT_SUCCESS(Status
))
696 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
700 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
707 * If the directory containing the file to open doesn't exist then
710 if (Status
== STATUS_OBJECT_PATH_NOT_FOUND
||
711 Status
== STATUS_INVALID_PARAMETER
||
712 Status
== STATUS_DELETE_PENDING
||
713 Status
== STATUS_ACCESS_DENIED
||
714 Status
== STATUS_OBJECT_NAME_COLLISION
)
718 vfatReleaseFCB(DeviceExt
, ParentFcb
);
720 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
724 if (!NT_SUCCESS(Status
) && ParentFcb
== NULL
)
726 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU
, Status
);
727 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
731 Attributes
= (Stack
->Parameters
.Create
.FileAttributes
& (FILE_ATTRIBUTE_ARCHIVE
|
732 FILE_ATTRIBUTE_SYSTEM
|
733 FILE_ATTRIBUTE_HIDDEN
|
734 FILE_ATTRIBUTE_DIRECTORY
|
735 FILE_ATTRIBUTE_READONLY
));
737 /* If the file open failed then create the required file */
738 if (!NT_SUCCESS (Status
))
740 if (RequestedDisposition
== FILE_CREATE
||
741 RequestedDisposition
== FILE_OPEN_IF
||
742 RequestedDisposition
== FILE_OVERWRITE_IF
||
743 RequestedDisposition
== FILE_SUPERSEDE
)
745 if (!BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
))
747 if (TrailingBackslash
)
749 vfatReleaseFCB(DeviceExt
, ParentFcb
);
750 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
751 return STATUS_OBJECT_NAME_INVALID
;
753 Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
755 vfatSplitPathName(&PathNameU
, NULL
, &FileNameU
);
756 Status
= VfatAddEntry(DeviceExt
, &FileNameU
, &pFcb
, ParentFcb
, RequestedOptions
,
758 vfatReleaseFCB(DeviceExt
, ParentFcb
);
759 if (NT_SUCCESS(Status
))
761 Status
= vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
762 if (!NT_SUCCESS(Status
))
764 vfatReleaseFCB(DeviceExt
, pFcb
);
765 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
769 Irp
->IoStatus
.Information
= FILE_CREATED
;
770 VfatSetAllocationSizeInformation(FileObject
,
773 &Irp
->Overlay
.AllocationSize
);
774 VfatSetExtendedAttributes(FileObject
,
775 Irp
->AssociatedIrp
.SystemBuffer
,
776 Stack
->Parameters
.Create
.EaLength
);
778 if (PagingFileCreate
)
780 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
781 SetFlag(DeviceExt
->Flags
, VCB_IS_SYS_OR_HAS_PAGE
);
786 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
792 vfatReleaseFCB(DeviceExt
, ParentFcb
);
793 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
801 vfatReleaseFCB(DeviceExt
, ParentFcb
);
804 pFcb
= FileObject
->FsContext
;
806 /* Otherwise fail if the caller wanted to create a new file */
807 if (RequestedDisposition
== FILE_CREATE
)
809 VfatCloseFile(DeviceExt
, FileObject
);
810 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
812 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
813 return STATUS_OBJECT_NAME_INVALID
;
815 Irp
->IoStatus
.Information
= FILE_EXISTS
;
816 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
817 return STATUS_OBJECT_NAME_COLLISION
;
820 if (pFcb
->OpenHandleCount
!= 0)
822 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
823 Stack
->Parameters
.Create
.ShareAccess
,
825 &pFcb
->FCBShareAccess
,
827 if (!NT_SUCCESS(Status
))
829 VfatCloseFile(DeviceExt
, FileObject
);
830 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
836 * Check the file has the requested attributes
838 if (BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
) &&
839 vfatFCBIsDirectory(pFcb
))
841 VfatCloseFile (DeviceExt
, FileObject
);
842 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
843 return STATUS_FILE_IS_A_DIRECTORY
;
845 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
846 !vfatFCBIsDirectory(pFcb
))
848 VfatCloseFile (DeviceExt
, FileObject
);
849 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
850 return STATUS_NOT_A_DIRECTORY
;
852 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
854 VfatCloseFile (DeviceExt
, FileObject
);
855 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
856 return STATUS_OBJECT_NAME_INVALID
;
858 #ifndef USE_ROS_CC_AND_FS
859 if (!vfatFCBIsDirectory(pFcb
))
861 if (BooleanFlagOn(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
, FILE_WRITE_DATA
) ||
862 RequestedDisposition
== FILE_OVERWRITE
||
863 RequestedDisposition
== FILE_OVERWRITE_IF
||
864 (RequestedOptions
& FILE_DELETE_ON_CLOSE
))
866 if (!MmFlushImageSection(&pFcb
->SectionObjectPointers
, MmFlushForWrite
))
868 DPRINT1("%wZ\n", &pFcb
->PathNameU
);
869 DPRINT1("%d %d %d\n", Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
& FILE_WRITE_DATA
,
870 RequestedDisposition
== FILE_OVERWRITE
, RequestedDisposition
== FILE_OVERWRITE_IF
);
871 VfatCloseFile (DeviceExt
, FileObject
);
872 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
873 return (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
)) ? STATUS_CANNOT_DELETE
874 : STATUS_SHARING_VIOLATION
;
879 if (PagingFileCreate
)
882 * Do more checking for page files. It is possible,
883 * that the file was opened and closed previously
884 * as a normal cached file. In this case, the cache
885 * manager has referenced the fileobject and the fcb
886 * is held in memory. Try to remove the fileobject
887 * from cache manager and use the fcb.
889 if (pFcb
->RefCount
> 1)
891 if(!BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
893 VfatCloseFile(DeviceExt
, FileObject
);
894 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
895 return STATUS_INVALID_PARAMETER
;
900 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
901 SetFlag(DeviceExt
->Flags
, VCB_IS_SYS_OR_HAS_PAGE
);
906 if (BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
908 VfatCloseFile(DeviceExt
, FileObject
);
909 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
910 return STATUS_INVALID_PARAMETER
;
914 if (RequestedDisposition
== FILE_OVERWRITE
||
915 RequestedDisposition
== FILE_OVERWRITE_IF
||
916 RequestedDisposition
== FILE_SUPERSEDE
)
918 if ((BooleanFlagOn(*pFcb
->Attributes
, FILE_ATTRIBUTE_HIDDEN
) && !BooleanFlagOn(Attributes
, FILE_ATTRIBUTE_HIDDEN
)) ||
919 (BooleanFlagOn(*pFcb
->Attributes
, FILE_ATTRIBUTE_SYSTEM
) && !BooleanFlagOn(Attributes
, FILE_ATTRIBUTE_SYSTEM
)))
921 VfatCloseFile(DeviceExt
, FileObject
);
922 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
923 return STATUS_ACCESS_DENIED
;
926 if (!vfatFCBIsDirectory(pFcb
))
928 LARGE_INTEGER SystemTime
;
930 if (RequestedDisposition
== FILE_SUPERSEDE
)
932 *pFcb
->Attributes
= Attributes
;
936 *pFcb
->Attributes
|= Attributes
;
938 *pFcb
->Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
940 KeQuerySystemTime(&SystemTime
);
941 if (vfatVolumeIsFatX(DeviceExt
))
943 FsdSystemTimeToDosDateTime(DeviceExt
,
944 &SystemTime
, &pFcb
->entry
.FatX
.UpdateDate
,
945 &pFcb
->entry
.FatX
.UpdateTime
);
949 FsdSystemTimeToDosDateTime(DeviceExt
,
950 &SystemTime
, &pFcb
->entry
.Fat
.UpdateDate
,
951 &pFcb
->entry
.Fat
.UpdateTime
);
954 VfatUpdateEntry(DeviceExt
, pFcb
);
957 ExAcquireResourceExclusiveLite(&(pFcb
->MainResource
), TRUE
);
958 Status
= VfatSetAllocationSizeInformation(FileObject
,
961 &Irp
->Overlay
.AllocationSize
);
962 ExReleaseResourceLite(&(pFcb
->MainResource
));
963 if (!NT_SUCCESS (Status
))
965 VfatCloseFile(DeviceExt
, FileObject
);
966 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
971 if (RequestedDisposition
== FILE_SUPERSEDE
)
973 Irp
->IoStatus
.Information
= FILE_SUPERSEDED
;
975 else if (RequestedDisposition
== FILE_OVERWRITE
||
976 RequestedDisposition
== FILE_OVERWRITE_IF
)
978 Irp
->IoStatus
.Information
= FILE_OVERWRITTEN
;
982 Irp
->IoStatus
.Information
= FILE_OPENED
;
986 if (pFcb
->OpenHandleCount
== 0)
988 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
989 Stack
->Parameters
.Create
.ShareAccess
,
991 &pFcb
->FCBShareAccess
);
995 IoUpdateShareAccess(FileObject
,
996 &pFcb
->FCBShareAccess
);
999 pCcb
= FileObject
->FsContext2
;
1000 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
1002 pCcb
->Flags
|= CCB_DELETE_ON_CLOSE
;
1005 if (Irp
->IoStatus
.Information
== FILE_CREATED
)
1007 vfatReportChange(DeviceExt
,
1009 (vfatFCBIsDirectory(pFcb
) ?
1010 FILE_NOTIFY_CHANGE_DIR_NAME
: FILE_NOTIFY_CHANGE_FILE_NAME
),
1013 else if (Irp
->IoStatus
.Information
== FILE_OVERWRITTEN
||
1014 Irp
->IoStatus
.Information
== FILE_SUPERSEDED
)
1016 vfatReportChange(DeviceExt
,
1018 FILE_NOTIFY_CHANGE_LAST_WRITE
| FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SIZE
,
1019 FILE_ACTION_MODIFIED
);
1022 pFcb
->OpenHandleCount
++;
1023 DeviceExt
->OpenHandleCount
++;
1025 /* FIXME : test write access if requested */
1027 /* FIXME: That is broken, we cannot reach this code path with failure */
1028 ASSERT(NT_SUCCESS(Status
));
1029 if (NT_SUCCESS(Status
))
1031 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
1035 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
1042 * FUNCTION: Create or open a file
1046 PVFAT_IRP_CONTEXT IrpContext
)
1052 if (IrpContext
->DeviceObject
== VfatGlobalData
->DeviceObject
)
1054 /* DeviceObject represents FileSystem instead of logical volume */
1055 DPRINT ("FsdCreate called with file system\n");
1056 IrpContext
->Irp
->IoStatus
.Information
= FILE_OPENED
;
1057 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;
1059 return STATUS_SUCCESS
;
1062 if (!BooleanFlagOn(IrpContext
->Flags
, IRPCONTEXT_CANWAIT
))
1064 return VfatMarkIrpContextForQueue(IrpContext
);
1067 IrpContext
->Irp
->IoStatus
.Information
= 0;
1068 ExAcquireResourceExclusiveLite(&IrpContext
->DeviceExt
->DirResource
, TRUE
);
1069 Status
= VfatCreateFile(IrpContext
->DeviceObject
, IrpContext
->Irp
);
1070 ExReleaseResourceLite(&IrpContext
->DeviceExt
->DirResource
);
1072 if (NT_SUCCESS(Status
))
1073 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;