4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS kernel
6 * FILE: services/fs/vfat/dir.c
7 * PURPOSE: VFAT Filesystem : directory control
13 #include <ddk/ntddk.h>
22 // function like DosDateTimeToFileTime
23 BOOL FsdDosDateTimeToFileTime (WORD wDosDate, WORD wDosTime, TIME * FileTime)
25 PDOSTIME pdtime = (PDOSTIME) & wDosTime;
26 PDOSDATE pddate = (PDOSDATE) & wDosDate;
27 TIME_FIELDS TimeFields;
32 TimeFields.Milliseconds = 0;
33 TimeFields.Second = pdtime->Second * 2;
34 TimeFields.Minute = pdtime->Minute;
35 TimeFields.Hour = pdtime->Hour;
37 TimeFields.Day = pddate->Day;
38 TimeFields.Month = pddate->Month;
39 TimeFields.Year = 1980 + pddate->Year;
41 RtlTimeFieldsToTime (&TimeFields, (PLARGE_INTEGER) FileTime);
47 // function like FileTimeToDosDateTime
49 FsdFileTimeToDosDateTime (TIME * FileTime, WORD * pwDosDate, WORD * pwDosTime)
51 PDOSTIME pdtime = (PDOSTIME) pwDosTime;
52 PDOSDATE pddate = (PDOSDATE) pwDosDate;
53 TIME_FIELDS TimeFields;
58 RtlTimeToTimeFields ((PLARGE_INTEGER) FileTime, &TimeFields);
62 pdtime->Second = TimeFields.Second / 2;
63 pdtime->Minute = TimeFields.Minute;
64 pdtime->Hour = TimeFields.Hour;
69 pddate->Day = TimeFields.Day;
70 pddate->Month = TimeFields.Month;
71 pddate->Year = TimeFields.Year - 1980;
80 vfat_wstrlen (PWSTR s)
96 #define DWORD_ROUND_UP(x) ( (((ULONG)(x))%32) ? ((((ULONG)x)&(~0x1f))+0x20) : ((ULONG)x) )
99 VfatGetFileNameInformation (PVFATFCB pFcb,
100 PFILE_NAMES_INFORMATION pInfo, ULONG BufferLength)
103 Length = vfat_wstrlen (pFcb->ObjectName) * sizeof(WCHAR);
104 if ((sizeof (FILE_DIRECTORY_INFORMATION) + Length) > BufferLength)
105 return STATUS_BUFFER_OVERFLOW;
106 pInfo->FileNameLength = Length;
107 pInfo->NextEntryOffset =
108 DWORD_ROUND_UP (sizeof (FILE_DIRECTORY_INFORMATION) + Length);
109 memcpy (pInfo->FileName, pFcb->ObjectName, Length);
110 return STATUS_SUCCESS;
114 VfatGetFileDirectoryInformation (PVFATFCB pFcb,
115 PDEVICE_EXTENSION DeviceExt,
116 PFILE_DIRECTORY_INFORMATION pInfo,
119 unsigned long long AllocSize;
121 Length = vfat_wstrlen (pFcb->ObjectName) * sizeof(WCHAR);
122 if ((sizeof (FILE_DIRECTORY_INFORMATION) + Length) > BufferLength)
123 return STATUS_BUFFER_OVERFLOW;
124 pInfo->FileNameLength = Length;
125 pInfo->NextEntryOffset =
126 DWORD_ROUND_UP (sizeof (FILE_DIRECTORY_INFORMATION) + Length);
127 memcpy (pInfo->FileName, pFcb->ObjectName, Length);
128 // pInfo->FileIndex=;
129 FsdDosDateTimeToFileTime (pFcb->entry.CreationDate,
130 pFcb->entry.CreationTime, &pInfo->CreationTime);
131 FsdDosDateTimeToFileTime (pFcb->entry.AccessDate, 0,
132 &pInfo->LastAccessTime);
133 FsdDosDateTimeToFileTime (pFcb->entry.UpdateDate, pFcb->entry.UpdateTime,
134 &pInfo->LastWriteTime);
135 FsdDosDateTimeToFileTime (pFcb->entry.UpdateDate, pFcb->entry.UpdateTime,
137 pInfo->EndOfFile = RtlConvertUlongToLargeInteger (pFcb->entry.FileSize);
138 /* Make allocsize a rounded up multiple of BytesPerCluster */
139 AllocSize = ((pFcb->entry.FileSize + DeviceExt->FatInfo.BytesPerCluster - 1) /
140 DeviceExt->FatInfo.BytesPerCluster) * DeviceExt->FatInfo.BytesPerCluster;
141 pInfo->AllocationSize.QuadPart = AllocSize;
142 pInfo->FileAttributes = pFcb->entry.Attrib;
144 return STATUS_SUCCESS;
148 VfatGetFileFullDirectoryInformation (PVFATFCB pFcb,
149 PDEVICE_EXTENSION DeviceExt,
150 PFILE_FULL_DIRECTORY_INFORMATION pInfo,
153 unsigned long long AllocSize;
155 Length = vfat_wstrlen (pFcb->ObjectName) * sizeof(WCHAR);
156 if ((sizeof (FILE_FULL_DIRECTORY_INFORMATION) + Length) > BufferLength)
157 return STATUS_BUFFER_OVERFLOW;
158 pInfo->FileNameLength = Length;
159 pInfo->NextEntryOffset =
160 DWORD_ROUND_UP (sizeof (FILE_FULL_DIRECTORY_INFORMATION) + Length);
161 memcpy (pInfo->FileName, pFcb->ObjectName, Length);
162 // pInfo->FileIndex=;
163 FsdDosDateTimeToFileTime (pFcb->entry.CreationDate,
164 pFcb->entry.CreationTime, &pInfo->CreationTime);
165 FsdDosDateTimeToFileTime (pFcb->entry.AccessDate, 0,
166 &pInfo->LastAccessTime);
167 FsdDosDateTimeToFileTime (pFcb->entry.UpdateDate, pFcb->entry.UpdateTime,
168 &pInfo->LastWriteTime);
169 FsdDosDateTimeToFileTime (pFcb->entry.UpdateDate, pFcb->entry.UpdateTime,
171 pInfo->EndOfFile = RtlConvertUlongToLargeInteger (pFcb->entry.FileSize);
172 /* Make allocsize a rounded up multiple of BytesPerCluster */
173 AllocSize = ((pFcb->entry.FileSize + DeviceExt->FatInfo.BytesPerCluster - 1) /
174 DeviceExt->FatInfo.BytesPerCluster) * DeviceExt->FatInfo.BytesPerCluster;
175 pInfo->AllocationSize.QuadPart = AllocSize;
176 pInfo->FileAttributes = pFcb->entry.Attrib;
178 return STATUS_SUCCESS;
182 VfatGetFileBothInformation (PVFATFCB pFcb,
183 PDEVICE_EXTENSION DeviceExt,
184 PFILE_BOTH_DIRECTORY_INFORMATION pInfo,
188 unsigned long long AllocSize;
190 Length = vfat_wstrlen (pFcb->ObjectName) * sizeof(WCHAR);
191 if ((sizeof (FILE_BOTH_DIRECTORY_INFORMATION) + Length) > BufferLength)
192 return STATUS_BUFFER_OVERFLOW;
193 pInfo->FileNameLength = Length;
194 pInfo->NextEntryOffset =
195 DWORD_ROUND_UP (sizeof (FILE_BOTH_DIRECTORY_INFORMATION) + Length);
196 memcpy (pInfo->FileName, pFcb->ObjectName, Length);
197 // pInfo->FileIndex=;
198 FsdDosDateTimeToFileTime (pFcb->entry.CreationDate,
199 pFcb->entry.CreationTime, &pInfo->CreationTime);
200 FsdDosDateTimeToFileTime (pFcb->entry.AccessDate, 0,
201 &pInfo->LastAccessTime);
202 FsdDosDateTimeToFileTime (pFcb->entry.UpdateDate, pFcb->entry.UpdateTime,
203 &pInfo->LastWriteTime);
204 FsdDosDateTimeToFileTime (pFcb->entry.UpdateDate, pFcb->entry.UpdateTime,
206 pInfo->EndOfFile = RtlConvertUlongToLargeInteger (pFcb->entry.FileSize);
207 /* Make allocsize a rounded up multiple of BytesPerCluster */
208 AllocSize = ((pFcb->entry.FileSize + DeviceExt->FatInfo.BytesPerCluster - 1) /
209 DeviceExt->FatInfo.BytesPerCluster) * DeviceExt->FatInfo.BytesPerCluster;
210 pInfo->AllocationSize.QuadPart = AllocSize;
211 pInfo->FileAttributes = pFcb->entry.Attrib;
213 for (i = 0; i < 8 && (pFcb->entry.Filename[i] != ' '); i++)
214 pInfo->ShortName[i] = pFcb->entry.Filename[i];
215 pInfo->ShortNameLength = i;
216 pInfo->ShortName[i] = '.';
217 for (i = 0; i < 3 && (pFcb->entry.Ext[i] != ' '); i++)
218 pInfo->ShortName[i + 1 + pInfo->ShortNameLength] = pFcb->entry.Ext[i];
220 pInfo->ShortNameLength += (i + 1);
221 pInfo->ShortNameLength *= sizeof(WCHAR);
222 return STATUS_SUCCESS;
225 NTSTATUS DoQuery (PVFAT_IRP_CONTEXT IrpContext)
227 NTSTATUS RC = STATUS_SUCCESS;
228 long BufferLength = 0;
229 PUNICODE_STRING pSearchPattern = NULL;
230 FILE_INFORMATION_CLASS FileInformationClass;
231 unsigned long FileIndex = 0;
232 unsigned char *Buffer = NULL;
233 PFILE_NAMES_INFORMATION Buffer0 = NULL;
237 BOOLEAN First = FALSE;
239 pCcb = (PVFATCCB) IrpContext->FileObject->FsContext2;
242 if (!ExAcquireResourceSharedLite(&pFcb->MainResource, IrpContext->Flags & IRPCONTEXT_CANWAIT))
244 return STATUS_PENDING;
247 // Obtain the callers parameters
248 BufferLength = IrpContext->Stack->Parameters.QueryDirectory.Length;
249 pSearchPattern = IrpContext->Stack->Parameters.QueryDirectory.FileName;
250 FileInformationClass =
251 IrpContext->Stack->Parameters.QueryDirectory.FileInformationClass;
252 FileIndex = IrpContext->Stack->Parameters.QueryDirectory.FileIndex;
255 if (!pCcb->DirectorySearchPattern)
258 pCcb->DirectorySearchPattern =
259 ExAllocatePool(NonPagedPool, pSearchPattern->Length + sizeof(WCHAR));
260 if (!pCcb->DirectorySearchPattern)
262 ExReleaseResourceLite(&pFcb->MainResource);
263 return STATUS_INSUFFICIENT_RESOURCES;
265 memcpy(pCcb->DirectorySearchPattern, pSearchPattern->Buffer,
266 pSearchPattern->Length);
267 pCcb->DirectorySearchPattern[pSearchPattern->Length / sizeof(WCHAR)] = 0;
270 else if (!pCcb->DirectorySearchPattern)
273 pCcb->DirectorySearchPattern = ExAllocatePool(NonPagedPool, 2 * sizeof(WCHAR));
274 if (!pCcb->DirectorySearchPattern)
276 ExReleaseResourceLite(&pFcb->MainResource);
277 return STATUS_INSUFFICIENT_RESOURCES;
279 pCcb->DirectorySearchPattern[0] = L'*';
280 pCcb->DirectorySearchPattern[1] = 0;
283 if (IrpContext->Stack->Flags & SL_INDEX_SPECIFIED)
285 pCcb->Entry = pCcb->CurrentByteOffset.u.LowPart;
287 else if (First || (IrpContext->Stack->Flags & SL_RESTART_SCAN))
291 // determine Buffer for result :
292 if (IrpContext->Irp->MdlAddress)
294 Buffer = MmGetSystemAddressForMdl (IrpContext->Irp->MdlAddress);
298 Buffer = IrpContext->Irp->UserBuffer;
300 DPRINT ("Buffer=%x tofind=%S\n", Buffer, pCcb->DirectorySearchPattern);
302 tmpFcb.ObjectName = tmpFcb.PathName;
303 while (RC == STATUS_SUCCESS && BufferLength > 0)
305 RC = FindFile (IrpContext->DeviceExt, &tmpFcb, pFcb,
306 pCcb->DirectorySearchPattern, &pCcb->Entry, NULL);
307 DPRINT ("Found %S, RC=%x, entry %x\n", tmpFcb.ObjectName, RC, pCcb->Entry);
310 switch (FileInformationClass)
312 case FileNameInformation:
313 RC = VfatGetFileNameInformation (&tmpFcb,
314 (PFILE_NAMES_INFORMATION) Buffer, BufferLength);
316 case FileDirectoryInformation:
317 RC = VfatGetFileDirectoryInformation (&tmpFcb, IrpContext->DeviceExt,
318 (PFILE_DIRECTORY_INFORMATION) Buffer, BufferLength);
320 case FileFullDirectoryInformation:
321 RC = VfatGetFileFullDirectoryInformation (&tmpFcb, IrpContext->DeviceExt,
322 (PFILE_FULL_DIRECTORY_INFORMATION) Buffer, BufferLength);
324 case FileBothDirectoryInformation:
325 RC = VfatGetFileBothInformation (&tmpFcb, IrpContext->DeviceExt,
326 (PFILE_BOTH_DIRECTORY_INFORMATION) Buffer, BufferLength);
329 RC = STATUS_INVALID_INFO_CLASS;
331 if (RC == STATUS_BUFFER_OVERFLOW)
335 Buffer0->NextEntryOffset = 0;
344 Buffer0->NextEntryOffset = 0;
348 RC = STATUS_NO_SUCH_FILE;
352 RC = STATUS_NO_MORE_FILES;
356 Buffer0 = (PFILE_NAMES_INFORMATION) Buffer;
357 Buffer0->FileIndex = FileIndex++;
359 if (IrpContext->Stack->Flags & SL_RETURN_SINGLE_ENTRY)
363 BufferLength -= Buffer0->NextEntryOffset;
364 Buffer += Buffer0->NextEntryOffset;
368 Buffer0->NextEntryOffset = 0;
374 ExReleaseResourceLite(&pFcb->MainResource);
379 NTSTATUS VfatDirectoryControl (PVFAT_IRP_CONTEXT IrpContext)
381 * FUNCTION: directory control : read/write directory informations
384 NTSTATUS RC = STATUS_SUCCESS;
386 switch (IrpContext->MinorFunction)
388 case IRP_MN_QUERY_DIRECTORY:
389 RC = DoQuery (IrpContext);
391 case IRP_MN_NOTIFY_CHANGE_DIRECTORY:
392 DPRINT (" vfat, dir : change\n");
393 RC = STATUS_NOT_IMPLEMENTED;
397 DbgPrint ("unexpected minor function %x in VFAT driver\n",
398 IrpContext->MinorFunction);
399 RC = STATUS_INVALID_DEVICE_REQUEST;
402 if (RC == STATUS_PENDING)
404 RC = VfatQueueRequest(IrpContext);
408 IrpContext->Irp->IoStatus.Status = RC;
409 IrpContext->Irp->IoStatus.Information = 0;
410 IoCompleteRequest (IrpContext->Irp, IO_NO_INCREMENT);
411 VfatFreeIrpContext(IrpContext);