[FASTFAT]
[reactos.git] / reactos / drivers / filesystems / fastfat / dirwr.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/filesystems/fastfat/dirwr.c
5 * PURPOSE: VFAT Filesystem : write in directory
6 * PROGRAMMER: Rex Jolliff (rex@lvcablemodem.com)
7 * Herve Poussineau (reactos@poussine.freesurf.fr)
8 * Pierre Schweitzer (pierre@reactos.org)
9 *
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include "vfat.h"
15
16 #define NDEBUG
17 #include <debug.h>
18
19 /*
20 * update an existing FAT entry
21 */
22 NTSTATUS
23 VfatUpdateEntry(
24 IN PVFATFCB pFcb)
25 {
26 PVOID Context;
27 PDIR_ENTRY PinEntry;
28 LARGE_INTEGER Offset;
29 ULONG SizeDirEntry;
30 ULONG dirIndex;
31
32 ASSERT(pFcb);
33
34 if (pFcb->Flags & FCB_IS_FATX_ENTRY)
35 {
36 SizeDirEntry = sizeof(FATX_DIR_ENTRY);
37 dirIndex = pFcb->startIndex;
38 }
39 else
40 {
41 SizeDirEntry = sizeof(FAT_DIR_ENTRY);
42 dirIndex = pFcb->dirIndex;
43 }
44
45 DPRINT("updEntry dirIndex %u, PathName \'%wZ\'\n", dirIndex, &pFcb->PathNameU);
46
47 if (vfatFCBIsRoot(pFcb) || (pFcb->Flags & (FCB_IS_FAT|FCB_IS_VOLUME)))
48 {
49 return STATUS_SUCCESS;
50 }
51
52 ASSERT(pFcb->parentFcb);
53
54 Offset.u.HighPart = 0;
55 Offset.u.LowPart = dirIndex * SizeDirEntry;
56 _SEH2_TRY
57 {
58 CcPinRead(pFcb->parentFcb->FileObject, &Offset, SizeDirEntry, PIN_WAIT, &Context, (PVOID*)&PinEntry);
59 }
60 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
61 {
62 DPRINT1("Failed write to \'%wZ\'.\n", &pFcb->parentFcb->PathNameU);
63 _SEH2_YIELD(return _SEH2_GetExceptionCode());
64 }
65 _SEH2_END;
66
67 pFcb->Flags &= ~FCB_IS_DIRTY;
68 RtlCopyMemory(PinEntry, &pFcb->entry, SizeDirEntry);
69 CcSetDirtyPinnedData(Context, NULL);
70 CcUnpinData(Context);
71 return STATUS_SUCCESS;
72 }
73
74 /*
75 * rename an existing FAT entry
76 */
77 NTSTATUS
78 vfatRenameEntry(
79 IN PDEVICE_EXTENSION DeviceExt,
80 IN PVFATFCB pFcb,
81 IN PUNICODE_STRING FileName,
82 IN BOOLEAN CaseChangeOnly)
83 {
84 OEM_STRING NameA;
85 ULONG StartIndex;
86 PVOID Context = NULL;
87 LARGE_INTEGER Offset;
88 PFATX_DIR_ENTRY pDirEntry;
89 NTSTATUS Status;
90
91 DPRINT("vfatRenameEntry(%p, %p, %wZ, %d)\n", DeviceExt, pFcb, FileName, CaseChangeOnly);
92
93 if (pFcb->Flags & FCB_IS_FATX_ENTRY)
94 {
95 VFAT_DIRENTRY_CONTEXT DirContext;
96
97 /* Open associated dir entry */
98 StartIndex = pFcb->startIndex;
99 Offset.u.HighPart = 0;
100 Offset.u.LowPart = (StartIndex * sizeof(FATX_DIR_ENTRY) / PAGE_SIZE) * PAGE_SIZE;
101 _SEH2_TRY
102 {
103 CcPinRead(pFcb->parentFcb->FileObject, &Offset, PAGE_SIZE, PIN_WAIT, &Context, (PVOID*)&pDirEntry);
104 }
105 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
106 {
107 DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset.u.HighPart, Offset.u.LowPart, PAGE_SIZE);
108 _SEH2_YIELD(return _SEH2_GetExceptionCode());
109 }
110 _SEH2_END;
111
112 pDirEntry = &pDirEntry[StartIndex % (PAGE_SIZE / sizeof(FATX_DIR_ENTRY))];
113
114 /* Set file name */
115 NameA.Buffer = (PCHAR)pDirEntry->Filename;
116 NameA.Length = 0;
117 NameA.MaximumLength = 42;
118 RtlUnicodeStringToOemString(&NameA, FileName, FALSE);
119 pDirEntry->FilenameLength = (unsigned char)NameA.Length;
120
121 /* Update FCB */
122 DirContext.ShortNameU.Length = 0;
123 DirContext.ShortNameU.MaximumLength = 0;
124 DirContext.ShortNameU.Buffer = NULL;
125 DirContext.LongNameU = *FileName;
126 DirContext.DirEntry.FatX = *pDirEntry;
127
128 CcSetDirtyPinnedData(Context, NULL);
129 CcUnpinData(Context);
130
131 Status = vfatUpdateFCB(DeviceExt, pFcb, &DirContext, pFcb->parentFcb);
132 if (NT_SUCCESS(Status))
133 {
134 CcPurgeCacheSection(&pFcb->parentFcb->SectionObjectPointers, NULL, 0, FALSE);
135 }
136
137 return Status;
138 }
139 else
140 {
141 /* This we cannot handle properly, move file - would likely need love */
142 return VfatMoveEntry(DeviceExt, pFcb, FileName, pFcb->parentFcb);
143 }
144 }
145
146 /*
147 * try to find contiguous entries frees in directory,
148 * extend a directory if is neccesary
149 */
150 BOOLEAN
151 vfatFindDirSpace(
152 IN PDEVICE_EXTENSION DeviceExt,
153 IN PVFATFCB pDirFcb,
154 IN ULONG nbSlots,
155 OUT PULONG start)
156 {
157 LARGE_INTEGER FileOffset;
158 ULONG i, count, size, nbFree = 0;
159 PDIR_ENTRY pFatEntry = NULL;
160 PVOID Context = NULL;
161 NTSTATUS Status;
162 ULONG SizeDirEntry;
163 FileOffset.QuadPart = 0;
164
165 if (DeviceExt->Flags & VCB_IS_FATX)
166 SizeDirEntry = sizeof(FATX_DIR_ENTRY);
167 else
168 SizeDirEntry = sizeof(FAT_DIR_ENTRY);
169
170 count = pDirFcb->RFCB.FileSize.u.LowPart / SizeDirEntry;
171 size = DeviceExt->FatInfo.BytesPerCluster / SizeDirEntry;
172 for (i = 0; i < count; i++, pFatEntry = (PDIR_ENTRY)((ULONG_PTR)pFatEntry + SizeDirEntry))
173 {
174 if (Context == NULL || (i % size) == 0)
175 {
176 if (Context)
177 {
178 CcUnpinData(Context);
179 }
180 _SEH2_TRY
181 {
182 CcPinRead(pDirFcb->FileObject, &FileOffset, DeviceExt->FatInfo.BytesPerCluster, PIN_WAIT, &Context, (PVOID*)&pFatEntry);
183 }
184 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
185 {
186 _SEH2_YIELD(return FALSE);
187 }
188 _SEH2_END;
189
190 FileOffset.u.LowPart += DeviceExt->FatInfo.BytesPerCluster;
191 }
192 if (ENTRY_END(DeviceExt, pFatEntry))
193 {
194 break;
195 }
196 if (ENTRY_DELETED(DeviceExt, pFatEntry))
197 {
198 nbFree++;
199 }
200 else
201 {
202 nbFree = 0;
203 }
204 if (nbFree == nbSlots)
205 {
206 break;
207 }
208 }
209 if (Context)
210 {
211 CcUnpinData(Context);
212 Context = NULL;
213 }
214 if (nbFree == nbSlots)
215 {
216 /* found enough contiguous free slots */
217 *start = i - nbSlots + 1;
218 }
219 else
220 {
221 *start = i - nbFree;
222 if (*start + nbSlots > count)
223 {
224 LARGE_INTEGER AllocationSize;
225 /* extend the directory */
226 if (vfatFCBIsRoot(pDirFcb) && DeviceExt->FatInfo.FatType != FAT32)
227 {
228 /* We can't extend a root directory on a FAT12/FAT16/FATX partition */
229 return FALSE;
230 }
231 AllocationSize.QuadPart = pDirFcb->RFCB.FileSize.u.LowPart + DeviceExt->FatInfo.BytesPerCluster;
232 Status = VfatSetAllocationSizeInformation(pDirFcb->FileObject, pDirFcb,
233 DeviceExt, &AllocationSize);
234 if (!NT_SUCCESS(Status))
235 {
236 return FALSE;
237 }
238 /* clear the new dir cluster */
239 FileOffset.u.LowPart = (ULONG)(pDirFcb->RFCB.FileSize.QuadPart -
240 DeviceExt->FatInfo.BytesPerCluster);
241 _SEH2_TRY
242 {
243 CcPinRead(pDirFcb->FileObject, &FileOffset, DeviceExt->FatInfo.BytesPerCluster, PIN_WAIT, &Context, (PVOID*)&pFatEntry);
244 }
245 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
246 {
247 _SEH2_YIELD(return FALSE);
248 }
249 _SEH2_END;
250
251 if (DeviceExt->Flags & VCB_IS_FATX)
252 memset(pFatEntry, 0xff, DeviceExt->FatInfo.BytesPerCluster);
253 else
254 RtlZeroMemory(pFatEntry, DeviceExt->FatInfo.BytesPerCluster);
255 }
256 else if (*start + nbSlots < count)
257 {
258 /* clear the entry after the last new entry */
259 FileOffset.u.LowPart = (*start + nbSlots) * SizeDirEntry;
260 _SEH2_TRY
261 {
262 CcPinRead(pDirFcb->FileObject, &FileOffset, SizeDirEntry, PIN_WAIT, &Context, (PVOID*)&pFatEntry);
263 }
264 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
265 {
266 _SEH2_YIELD(return FALSE);
267 }
268 _SEH2_END;
269
270 if (DeviceExt->Flags & VCB_IS_FATX)
271 memset(pFatEntry, 0xff, SizeDirEntry);
272 else
273 RtlZeroMemory(pFatEntry, SizeDirEntry);
274 }
275 if (Context)
276 {
277 CcSetDirtyPinnedData(Context, NULL);
278 CcUnpinData(Context);
279 }
280 }
281 DPRINT("nbSlots %u nbFree %u, entry number %u\n", nbSlots, nbFree, *start);
282 return TRUE;
283 }
284
285 /*
286 create a new FAT entry
287 */
288 static NTSTATUS
289 FATAddEntry(
290 IN PDEVICE_EXTENSION DeviceExt,
291 IN PUNICODE_STRING NameU,
292 IN PVFATFCB* Fcb,
293 IN PVFATFCB ParentFcb,
294 IN ULONG RequestedOptions,
295 IN UCHAR ReqAttr,
296 IN PVFAT_MOVE_CONTEXT MoveContext)
297 {
298 PVOID Context = NULL;
299 PFAT_DIR_ENTRY pFatEntry;
300 slot *pSlots;
301 USHORT nbSlots = 0, j, posCar;
302 PUCHAR Buffer;
303 BOOLEAN needTilde = FALSE, needLong = FALSE;
304 BOOLEAN lCaseBase = FALSE, uCaseBase, lCaseExt = FALSE, uCaseExt;
305 ULONG CurrentCluster;
306 LARGE_INTEGER SystemTime, FileOffset;
307 NTSTATUS Status = STATUS_SUCCESS;
308 ULONG size;
309 long i;
310
311 OEM_STRING NameA;
312 CHAR aName[13];
313 BOOLEAN IsNameLegal;
314 BOOLEAN SpacesFound;
315
316 VFAT_DIRENTRY_CONTEXT DirContext;
317 WCHAR LongNameBuffer[LONGNAME_MAX_LENGTH + 1];
318 WCHAR ShortNameBuffer[13];
319
320 DPRINT("addEntry: Name='%wZ', Dir='%wZ'\n", NameU, &ParentFcb->PathNameU);
321
322 DirContext.LongNameU = *NameU;
323
324 /* nb of entry needed for long name+normal entry */
325 nbSlots = (DirContext.LongNameU.Length / sizeof(WCHAR) + 12) / 13 + 1;
326 DPRINT("NameLen= %u, nbSlots =%u\n", DirContext.LongNameU.Length / sizeof(WCHAR), nbSlots);
327 Buffer = ExAllocatePoolWithTag(NonPagedPool, (nbSlots - 1) * sizeof(FAT_DIR_ENTRY), TAG_VFAT);
328 if (Buffer == NULL)
329 {
330 return STATUS_INSUFFICIENT_RESOURCES;
331 }
332 RtlZeroMemory(Buffer, (nbSlots - 1) * sizeof(FAT_DIR_ENTRY));
333 pSlots = (slot *) Buffer;
334
335 NameA.Buffer = aName;
336 NameA.Length = 0;
337 NameA.MaximumLength = sizeof(aName);
338
339 DirContext.ShortNameU.Buffer = ShortNameBuffer;
340 DirContext.ShortNameU.Length = 0;
341 DirContext.ShortNameU.MaximumLength = sizeof(ShortNameBuffer);
342
343 RtlZeroMemory(&DirContext.DirEntry.Fat, sizeof(FAT_DIR_ENTRY));
344
345 IsNameLegal = RtlIsNameLegalDOS8Dot3(&DirContext.LongNameU, &NameA, &SpacesFound);
346
347 if (!IsNameLegal || SpacesFound)
348 {
349 GENERATE_NAME_CONTEXT NameContext;
350 VFAT_DIRENTRY_CONTEXT SearchContext;
351 WCHAR ShortSearchName[13];
352 needTilde = TRUE;
353 needLong = TRUE;
354 RtlZeroMemory(&NameContext, sizeof(GENERATE_NAME_CONTEXT));
355 SearchContext.LongNameU.Buffer = LongNameBuffer;
356 SearchContext.LongNameU.MaximumLength = sizeof(LongNameBuffer);
357 SearchContext.ShortNameU.Buffer = ShortSearchName;
358 SearchContext.ShortNameU.MaximumLength = sizeof(ShortSearchName);
359
360 for (i = 0; i < 100; i++)
361 {
362 RtlGenerate8dot3Name(&DirContext.LongNameU, FALSE, &NameContext, &DirContext.ShortNameU);
363 DirContext.ShortNameU.Buffer[DirContext.ShortNameU.Length / sizeof(WCHAR)] = 0;
364 SearchContext.DirIndex = 0;
365 Status = FindFile(DeviceExt, ParentFcb, &DirContext.ShortNameU, &SearchContext, TRUE);
366 if (!NT_SUCCESS(Status))
367 {
368 break;
369 }
370 else if (MoveContext)
371 {
372 ASSERT(*Fcb);
373 if (strncmp((char *)SearchContext.DirEntry.Fat.ShortName, (char *)(*Fcb)->entry.Fat.ShortName, 11) == 0)
374 {
375 if (MoveContext->InPlace)
376 {
377 ASSERT(SearchContext.DirEntry.Fat.FileSize == MoveContext->FileSize);
378 break;
379 }
380 }
381 }
382 }
383 if (i == 100) /* FIXME : what to do after this ? */
384 {
385 ExFreePoolWithTag(Buffer, TAG_VFAT);
386 return STATUS_UNSUCCESSFUL;
387 }
388 IsNameLegal = RtlIsNameLegalDOS8Dot3(&DirContext.ShortNameU, &NameA, &SpacesFound);
389 aName[NameA.Length]=0;
390 }
391 else
392 {
393 aName[NameA.Length] = 0;
394 for (posCar = 0; posCar < DirContext.LongNameU.Length / sizeof(WCHAR); posCar++)
395 {
396 if (DirContext.LongNameU.Buffer[posCar] == L'.')
397 {
398 break;
399 }
400 }
401 /* check if the name and the extension contains upper case characters */
402 RtlDowncaseUnicodeString(&DirContext.ShortNameU, &DirContext.LongNameU, FALSE);
403 DirContext.ShortNameU.Buffer[DirContext.ShortNameU.Length / sizeof(WCHAR)] = 0;
404 uCaseBase = wcsncmp(DirContext.LongNameU.Buffer,
405 DirContext.ShortNameU.Buffer, posCar) ? TRUE : FALSE;
406 if (posCar < DirContext.LongNameU.Length/sizeof(WCHAR))
407 {
408 i = DirContext.LongNameU.Length / sizeof(WCHAR) - posCar;
409 uCaseExt = wcsncmp(DirContext.LongNameU.Buffer + posCar,
410 DirContext.ShortNameU.Buffer + posCar, i) ? TRUE : FALSE;
411 }
412 else
413 {
414 uCaseExt = FALSE;
415 }
416 /* check if the name and the extension contains lower case characters */
417 RtlUpcaseUnicodeString(&DirContext.ShortNameU, &DirContext.LongNameU, FALSE);
418 DirContext.ShortNameU.Buffer[DirContext.ShortNameU.Length / sizeof(WCHAR)] = 0;
419 lCaseBase = wcsncmp(DirContext.LongNameU.Buffer,
420 DirContext.ShortNameU.Buffer, posCar) ? TRUE : FALSE;
421 if (posCar < DirContext.LongNameU.Length / sizeof(WCHAR))
422 {
423 i = DirContext.LongNameU.Length / sizeof(WCHAR) - posCar;
424 lCaseExt = wcsncmp(DirContext.LongNameU.Buffer + posCar,
425 DirContext.ShortNameU.Buffer + posCar, i) ? TRUE : FALSE;
426 }
427 else
428 {
429 lCaseExt = FALSE;
430 }
431 if ((lCaseBase && uCaseBase) || (lCaseExt && uCaseExt))
432 {
433 needLong = TRUE;
434 }
435 }
436 DPRINT("'%s', '%wZ', needTilde=%u, needLong=%u\n",
437 aName, &DirContext.LongNameU, needTilde, needLong);
438 memset(DirContext.DirEntry.Fat.ShortName, ' ', 11);
439 for (i = 0; i < 8 && aName[i] && aName[i] != '.'; i++)
440 {
441 DirContext.DirEntry.Fat.Filename[i] = aName[i];
442 }
443 if (aName[i] == '.')
444 {
445 i++;
446 for (j = 0; j < 3 && aName[i]; j++, i++)
447 {
448 DirContext.DirEntry.Fat.Ext[j] = aName[i];
449 }
450 }
451 if (DirContext.DirEntry.Fat.Filename[0] == 0xe5)
452 {
453 DirContext.DirEntry.Fat.Filename[0] = 0x05;
454 }
455
456 if (needLong)
457 {
458 RtlCopyMemory(LongNameBuffer, DirContext.LongNameU.Buffer, DirContext.LongNameU.Length);
459 DirContext.LongNameU.Buffer = LongNameBuffer;
460 DirContext.LongNameU.MaximumLength = sizeof(LongNameBuffer);
461 DirContext.LongNameU.Buffer[DirContext.LongNameU.Length / sizeof(WCHAR)] = 0;
462 memset(DirContext.LongNameU.Buffer + DirContext.LongNameU.Length / sizeof(WCHAR) + 1, 0xff,
463 DirContext.LongNameU.MaximumLength - DirContext.LongNameU.Length - sizeof(WCHAR));
464 }
465 else
466 {
467 nbSlots = 1;
468 if (lCaseBase)
469 {
470 DirContext.DirEntry.Fat.lCase |= VFAT_CASE_LOWER_BASE;
471 }
472 if (lCaseExt)
473 {
474 DirContext.DirEntry.Fat.lCase |= VFAT_CASE_LOWER_EXT;
475 }
476 }
477
478 DPRINT ("dos name=%11.11s\n", DirContext.DirEntry.Fat.Filename);
479
480 /* set attributes */
481 DirContext.DirEntry.Fat.Attrib = ReqAttr;
482 if (RequestedOptions & FILE_DIRECTORY_FILE)
483 {
484 DirContext.DirEntry.Fat.Attrib |= FILE_ATTRIBUTE_DIRECTORY;
485 }
486 /* set dates and times */
487 KeQuerySystemTime(&SystemTime);
488 FsdSystemTimeToDosDateTime(DeviceExt, &SystemTime, &DirContext.DirEntry.Fat.CreationDate,
489 &DirContext.DirEntry.Fat.CreationTime);
490 DirContext.DirEntry.Fat.UpdateDate = DirContext.DirEntry.Fat.CreationDate;
491 DirContext.DirEntry.Fat.UpdateTime = DirContext.DirEntry.Fat.CreationTime;
492 DirContext.DirEntry.Fat.AccessDate = DirContext.DirEntry.Fat.CreationDate;
493 /* If it's moving, preserve creation time and file size */
494 if (MoveContext != NULL)
495 {
496 DirContext.DirEntry.Fat.CreationDate = MoveContext->CreationDate;
497 DirContext.DirEntry.Fat.CreationTime = MoveContext->CreationTime;
498 DirContext.DirEntry.Fat.FileSize = MoveContext->FileSize;
499 }
500
501 if (needLong)
502 {
503 /* calculate checksum for 8.3 name */
504 for (pSlots[0].alias_checksum = 0, i = 0; i < 11; i++)
505 {
506 pSlots[0].alias_checksum = (((pSlots[0].alias_checksum & 1) << 7
507 | ((pSlots[0].alias_checksum & 0xfe) >> 1))
508 + DirContext.DirEntry.Fat.ShortName[i]);
509 }
510 /* construct slots and entry */
511 for (i = nbSlots - 2; i >= 0; i--)
512 {
513 DPRINT("construct slot %d\n", i);
514 pSlots[i].attr = 0xf;
515 if (i)
516 {
517 pSlots[i].id = (unsigned char)(nbSlots - i - 1);
518 }
519 else
520 {
521 pSlots[i].id = (unsigned char)(nbSlots - i - 1 + 0x40);
522 }
523 pSlots[i].alias_checksum = pSlots[0].alias_checksum;
524 RtlCopyMemory(pSlots[i].name0_4, DirContext.LongNameU.Buffer + (nbSlots - i - 2) * 13, 10);
525 RtlCopyMemory(pSlots[i].name5_10, DirContext.LongNameU.Buffer + (nbSlots - i - 2) * 13 + 5, 12);
526 RtlCopyMemory(pSlots[i].name11_12, DirContext.LongNameU.Buffer + (nbSlots - i - 2) * 13 + 11, 4);
527 }
528 }
529 /* try to find nbSlots contiguous entries frees in directory */
530 if (!vfatFindDirSpace(DeviceExt, ParentFcb, nbSlots, &DirContext.StartIndex))
531 {
532 ExFreePoolWithTag(Buffer, TAG_VFAT);
533 return STATUS_DISK_FULL;
534 }
535 DirContext.DirIndex = DirContext.StartIndex + nbSlots - 1;
536 if (RequestedOptions & FILE_DIRECTORY_FILE)
537 {
538 /* If we aren't moving, use next */
539 if (MoveContext == NULL)
540 {
541 CurrentCluster = 0;
542 Status = NextCluster(DeviceExt, 0, &CurrentCluster, TRUE);
543 if (CurrentCluster == 0xffffffff || !NT_SUCCESS(Status))
544 {
545 ExFreePoolWithTag(Buffer, TAG_VFAT);
546 if (!NT_SUCCESS(Status))
547 {
548 return Status;
549 }
550 return STATUS_DISK_FULL;
551 }
552 }
553 else
554 {
555 CurrentCluster = MoveContext->FirstCluster;
556 }
557
558 if (DeviceExt->FatInfo.FatType == FAT32)
559 {
560 DirContext.DirEntry.Fat.FirstClusterHigh = (unsigned short)(CurrentCluster >> 16);
561 }
562 DirContext.DirEntry.Fat.FirstCluster = (unsigned short)CurrentCluster;
563 }
564 else if (MoveContext != NULL)
565 {
566 CurrentCluster = MoveContext->FirstCluster;
567
568 if (DeviceExt->FatInfo.FatType == FAT32)
569 {
570 DirContext.DirEntry.Fat.FirstClusterHigh = (unsigned short)(CurrentCluster >> 16);
571 }
572 DirContext.DirEntry.Fat.FirstCluster = (unsigned short)CurrentCluster;
573 }
574
575 i = DeviceExt->FatInfo.BytesPerCluster / sizeof(FAT_DIR_ENTRY);
576 FileOffset.u.HighPart = 0;
577 FileOffset.u.LowPart = DirContext.StartIndex * sizeof(FAT_DIR_ENTRY);
578 if (DirContext.StartIndex / i == DirContext.DirIndex / i)
579 {
580 /* one cluster */
581 _SEH2_TRY
582 {
583 CcPinRead(ParentFcb->FileObject, &FileOffset, nbSlots * sizeof(FAT_DIR_ENTRY), PIN_WAIT, &Context, (PVOID*)&pFatEntry);
584 }
585 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
586 {
587 ExFreePoolWithTag(Buffer, TAG_VFAT);
588 _SEH2_YIELD(return _SEH2_GetExceptionCode());
589 }
590 _SEH2_END;
591
592 if (nbSlots > 1)
593 {
594 RtlCopyMemory(pFatEntry, Buffer, (nbSlots - 1) * sizeof(FAT_DIR_ENTRY));
595 }
596 RtlCopyMemory(pFatEntry + (nbSlots - 1), &DirContext.DirEntry.Fat, sizeof(FAT_DIR_ENTRY));
597 }
598 else
599 {
600 /* two clusters */
601 size = DeviceExt->FatInfo.BytesPerCluster -
602 (DirContext.StartIndex * sizeof(FAT_DIR_ENTRY)) % DeviceExt->FatInfo.BytesPerCluster;
603 i = size / sizeof(FAT_DIR_ENTRY);
604 _SEH2_TRY
605 {
606 CcPinRead(ParentFcb->FileObject, &FileOffset, size, PIN_WAIT, &Context, (PVOID*)&pFatEntry);
607 }
608 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
609 {
610 ExFreePoolWithTag(Buffer, TAG_VFAT);
611 _SEH2_YIELD(return _SEH2_GetExceptionCode());
612 }
613 _SEH2_END;
614 RtlCopyMemory(pFatEntry, Buffer, size);
615 CcSetDirtyPinnedData(Context, NULL);
616 CcUnpinData(Context);
617 FileOffset.u.LowPart += size;
618 _SEH2_TRY
619 {
620 CcPinRead(ParentFcb->FileObject, &FileOffset, nbSlots * sizeof(FAT_DIR_ENTRY) - size, PIN_WAIT, &Context, (PVOID*)&pFatEntry);
621 }
622 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
623 {
624 ExFreePoolWithTag(Buffer, TAG_VFAT);
625 _SEH2_YIELD(return _SEH2_GetExceptionCode());
626 }
627 _SEH2_END;
628 if (nbSlots - 1 > i)
629 {
630 RtlCopyMemory(pFatEntry, (PVOID)(Buffer + size), (nbSlots - 1 - i) * sizeof(FAT_DIR_ENTRY));
631 }
632 RtlCopyMemory(pFatEntry + nbSlots - 1 - i, &DirContext.DirEntry.Fat, sizeof(FAT_DIR_ENTRY));
633 }
634 CcSetDirtyPinnedData(Context, NULL);
635 CcUnpinData(Context);
636
637 if (MoveContext != NULL)
638 {
639 /* We're modifying an existing FCB - likely rename/move */
640 Status = vfatUpdateFCB(DeviceExt, *Fcb, &DirContext, ParentFcb);
641 }
642 else
643 {
644 Status = vfatMakeFCBFromDirEntry(DeviceExt, ParentFcb, &DirContext, Fcb);
645 }
646 if (!NT_SUCCESS(Status))
647 {
648 ExFreePoolWithTag(Buffer, TAG_VFAT);
649 return Status;
650 }
651
652 DPRINT("new : entry=%11.11s\n", (*Fcb)->entry.Fat.Filename);
653 DPRINT("new : entry=%11.11s\n", DirContext.DirEntry.Fat.Filename);
654
655 if (RequestedOptions & FILE_DIRECTORY_FILE)
656 {
657 FileOffset.QuadPart = 0;
658 _SEH2_TRY
659 {
660 CcPinRead((*Fcb)->FileObject, &FileOffset, DeviceExt->FatInfo.BytesPerCluster, PIN_WAIT, &Context, (PVOID*)&pFatEntry);
661 }
662 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
663 {
664 ExFreePoolWithTag(Buffer, TAG_VFAT);
665 _SEH2_YIELD(return _SEH2_GetExceptionCode());
666 }
667 _SEH2_END;
668 /* clear the new directory cluster if not moving */
669 if (MoveContext == NULL)
670 {
671 RtlZeroMemory(pFatEntry, DeviceExt->FatInfo.BytesPerCluster);
672 /* create '.' and '..' */
673 RtlCopyMemory(&pFatEntry[0].Attrib, &DirContext.DirEntry.Fat.Attrib, sizeof(FAT_DIR_ENTRY) - 11);
674 RtlCopyMemory(pFatEntry[0].ShortName, ". ", 11);
675 RtlCopyMemory(&pFatEntry[1].Attrib, &DirContext.DirEntry.Fat.Attrib, sizeof(FAT_DIR_ENTRY) - 11);
676 RtlCopyMemory(pFatEntry[1].ShortName, ".. ", 11);
677 }
678
679 pFatEntry[1].FirstCluster = ParentFcb->entry.Fat.FirstCluster;
680 pFatEntry[1].FirstClusterHigh = ParentFcb->entry.Fat.FirstClusterHigh;
681 if (vfatFCBIsRoot(ParentFcb))
682 {
683 pFatEntry[1].FirstCluster = 0;
684 pFatEntry[1].FirstClusterHigh = 0;
685 }
686 CcSetDirtyPinnedData(Context, NULL);
687 CcUnpinData(Context);
688 }
689 ExFreePoolWithTag(Buffer, TAG_VFAT);
690 DPRINT("addentry ok\n");
691 return STATUS_SUCCESS;
692 }
693
694 /*
695 create a new FAT entry
696 */
697 static NTSTATUS
698 FATXAddEntry(
699 IN PDEVICE_EXTENSION DeviceExt,
700 IN PUNICODE_STRING NameU,
701 IN PVFATFCB* Fcb,
702 IN PVFATFCB ParentFcb,
703 IN ULONG RequestedOptions,
704 IN UCHAR ReqAttr,
705 IN PVFAT_MOVE_CONTEXT MoveContext)
706 {
707 PVOID Context = NULL;
708 LARGE_INTEGER SystemTime, FileOffset;
709 OEM_STRING NameA;
710 VFAT_DIRENTRY_CONTEXT DirContext;
711 PFATX_DIR_ENTRY pFatXDirEntry;
712 ULONG Index;
713
714 DPRINT("addEntry: Name='%wZ', Dir='%wZ'\n", NameU, &ParentFcb->PathNameU);
715
716 DirContext.LongNameU = *NameU;
717
718 if (DirContext.LongNameU.Length / sizeof(WCHAR) > 42)
719 {
720 /* name too long */
721 return STATUS_NAME_TOO_LONG;
722 }
723
724 /* try to find 1 entry free in directory */
725 if (!vfatFindDirSpace(DeviceExt, ParentFcb, 1, &DirContext.StartIndex))
726 {
727 return STATUS_DISK_FULL;
728 }
729 Index = DirContext.DirIndex = DirContext.StartIndex;
730 if (!vfatFCBIsRoot(ParentFcb))
731 {
732 DirContext.DirIndex += 2;
733 DirContext.StartIndex += 2;
734 }
735
736 DirContext.ShortNameU.Buffer = 0;
737 DirContext.ShortNameU.Length = 0;
738 DirContext.ShortNameU.MaximumLength = 0;
739 RtlZeroMemory(&DirContext.DirEntry.FatX, sizeof(FATX_DIR_ENTRY));
740 memset(DirContext.DirEntry.FatX.Filename, 0xff, 42);
741 /* Use cluster, if moving */
742 if (MoveContext != NULL)
743 {
744 DirContext.DirEntry.FatX.FirstCluster = MoveContext->FirstCluster;
745 }
746 else
747 {
748 DirContext.DirEntry.FatX.FirstCluster = 0;
749 }
750 DirContext.DirEntry.FatX.FileSize = 0;
751
752 /* set file name */
753 NameA.Buffer = (PCHAR)DirContext.DirEntry.FatX.Filename;
754 NameA.Length = 0;
755 NameA.MaximumLength = 42;
756 RtlUnicodeStringToOemString(&NameA, &DirContext.LongNameU, FALSE);
757 DirContext.DirEntry.FatX.FilenameLength = (unsigned char)NameA.Length;
758
759 /* set attributes */
760 DirContext.DirEntry.FatX.Attrib = ReqAttr;
761 if (RequestedOptions & FILE_DIRECTORY_FILE)
762 {
763 DirContext.DirEntry.FatX.Attrib |= FILE_ATTRIBUTE_DIRECTORY;
764 }
765
766 /* set dates and times */
767 KeQuerySystemTime(&SystemTime);
768 FsdSystemTimeToDosDateTime(DeviceExt, &SystemTime, &DirContext.DirEntry.FatX.CreationDate,
769 &DirContext.DirEntry.FatX.CreationTime);
770 DirContext.DirEntry.FatX.UpdateDate = DirContext.DirEntry.FatX.CreationDate;
771 DirContext.DirEntry.FatX.UpdateTime = DirContext.DirEntry.FatX.CreationTime;
772 DirContext.DirEntry.FatX.AccessDate = DirContext.DirEntry.FatX.CreationDate;
773 DirContext.DirEntry.FatX.AccessTime = DirContext.DirEntry.FatX.CreationTime;
774 /* If it's moving, preserve creation time and file size */
775 if (MoveContext != NULL)
776 {
777 DirContext.DirEntry.FatX.CreationDate = MoveContext->CreationDate;
778 DirContext.DirEntry.FatX.CreationTime = MoveContext->CreationTime;
779 DirContext.DirEntry.FatX.FileSize = MoveContext->FileSize;
780 }
781
782 /* add entry into parent directory */
783 FileOffset.u.HighPart = 0;
784 FileOffset.u.LowPart = Index * sizeof(FATX_DIR_ENTRY);
785 _SEH2_TRY
786 {
787 CcPinRead(ParentFcb->FileObject, &FileOffset, sizeof(FATX_DIR_ENTRY), PIN_WAIT, &Context, (PVOID*)&pFatXDirEntry);
788 }
789 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
790 {
791 _SEH2_YIELD(return _SEH2_GetExceptionCode());
792 }
793 _SEH2_END;
794 RtlCopyMemory(pFatXDirEntry, &DirContext.DirEntry.FatX, sizeof(FATX_DIR_ENTRY));
795 CcSetDirtyPinnedData(Context, NULL);
796 CcUnpinData(Context);
797
798 if (MoveContext != NULL)
799 {
800 /* We're modifying an existing FCB - likely rename/move */
801 /* FIXME: check status */
802 vfatUpdateFCB(DeviceExt, *Fcb, &DirContext, ParentFcb);
803 }
804 else
805 {
806 /* FIXME: check status */
807 vfatMakeFCBFromDirEntry(DeviceExt, ParentFcb, &DirContext, Fcb);
808 }
809
810 DPRINT("addentry ok\n");
811 return STATUS_SUCCESS;
812 }
813
814 NTSTATUS
815 VfatAddEntry(
816 IN PDEVICE_EXTENSION DeviceExt,
817 IN PUNICODE_STRING NameU,
818 IN PVFATFCB *Fcb,
819 IN PVFATFCB ParentFcb,
820 IN ULONG RequestedOptions,
821 IN UCHAR ReqAttr,
822 IN PVFAT_MOVE_CONTEXT MoveContext)
823 {
824 if (DeviceExt->Flags & VCB_IS_FATX)
825 return FATXAddEntry(DeviceExt, NameU, Fcb, ParentFcb, RequestedOptions, ReqAttr, MoveContext);
826 else
827 return FATAddEntry(DeviceExt, NameU, Fcb, ParentFcb, RequestedOptions, ReqAttr, MoveContext);
828 }
829
830 /*
831 * deleting an existing FAT entry
832 */
833 static NTSTATUS
834 FATDelEntry(
835 IN PDEVICE_EXTENSION DeviceExt,
836 IN PVFATFCB pFcb,
837 OUT PVFAT_MOVE_CONTEXT MoveContext)
838 {
839 ULONG CurrentCluster = 0, NextCluster, i;
840 PVOID Context = NULL;
841 LARGE_INTEGER Offset;
842 PFAT_DIR_ENTRY pDirEntry = NULL;
843
844 ASSERT(pFcb);
845 ASSERT(pFcb->parentFcb);
846
847 DPRINT("delEntry PathName \'%wZ\'\n", &pFcb->PathNameU);
848 DPRINT("delete entry: %u to %u\n", pFcb->startIndex, pFcb->dirIndex);
849 Offset.u.HighPart = 0;
850 for (i = pFcb->startIndex; i <= pFcb->dirIndex; i++)
851 {
852 if (Context == NULL || ((i * sizeof(FAT_DIR_ENTRY)) % PAGE_SIZE) == 0)
853 {
854 if (Context)
855 {
856 CcSetDirtyPinnedData(Context, NULL);
857 CcUnpinData(Context);
858 }
859 Offset.u.LowPart = (i * sizeof(FAT_DIR_ENTRY) / PAGE_SIZE) * PAGE_SIZE;
860 _SEH2_TRY
861 {
862 CcPinRead(pFcb->parentFcb->FileObject, &Offset, PAGE_SIZE, PIN_WAIT, &Context, (PVOID*)&pDirEntry);
863 }
864 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
865 {
866 _SEH2_YIELD(return _SEH2_GetExceptionCode());
867 }
868 _SEH2_END;
869 }
870 pDirEntry[i % (PAGE_SIZE / sizeof(FAT_DIR_ENTRY))].Filename[0] = 0xe5;
871 if (i == pFcb->dirIndex)
872 {
873 CurrentCluster =
874 vfatDirEntryGetFirstCluster(DeviceExt,
875 (PDIR_ENTRY)&pDirEntry[i % (PAGE_SIZE / sizeof(FAT_DIR_ENTRY))]);
876 }
877 }
878
879 /* In case of moving, save properties */
880 if (MoveContext != NULL)
881 {
882 pDirEntry = &pDirEntry[pFcb->dirIndex % (PAGE_SIZE / sizeof(FAT_DIR_ENTRY))];
883 MoveContext->FirstCluster = CurrentCluster;
884 MoveContext->FileSize = pDirEntry->FileSize;
885 MoveContext->CreationTime = pDirEntry->CreationTime;
886 MoveContext->CreationDate = pDirEntry->CreationDate;
887 }
888
889 if (Context)
890 {
891 CcSetDirtyPinnedData(Context, NULL);
892 CcUnpinData(Context);
893 }
894
895 /* In case of moving, don't delete data */
896 if (MoveContext == NULL)
897 {
898 while (CurrentCluster && CurrentCluster != 0xffffffff)
899 {
900 GetNextCluster(DeviceExt, CurrentCluster, &NextCluster);
901 /* FIXME: check status */
902 WriteCluster(DeviceExt, CurrentCluster, 0);
903 CurrentCluster = NextCluster;
904 }
905 }
906
907 return STATUS_SUCCESS;
908 }
909
910 /*
911 * deleting an existing FAT entry
912 */
913 static NTSTATUS
914 FATXDelEntry(
915 IN PDEVICE_EXTENSION DeviceExt,
916 IN PVFATFCB pFcb,
917 OUT PVFAT_MOVE_CONTEXT MoveContext)
918 {
919 ULONG CurrentCluster = 0, NextCluster;
920 PVOID Context = NULL;
921 LARGE_INTEGER Offset;
922 PFATX_DIR_ENTRY pDirEntry;
923 ULONG StartIndex;
924
925 ASSERT(pFcb);
926 ASSERT(pFcb->parentFcb);
927 ASSERT(pFcb->Flags & FCB_IS_FATX_ENTRY);
928
929 StartIndex = pFcb->startIndex;
930
931 DPRINT("delEntry PathName \'%wZ\'\n", &pFcb->PathNameU);
932 DPRINT("delete entry: %u\n", StartIndex);
933 Offset.u.HighPart = 0;
934 Offset.u.LowPart = (StartIndex * sizeof(FATX_DIR_ENTRY) / PAGE_SIZE) * PAGE_SIZE;
935 _SEH2_TRY
936 {
937 CcPinRead(pFcb->parentFcb->FileObject, &Offset, PAGE_SIZE, PIN_WAIT, &Context, (PVOID*)&pDirEntry);
938 }
939 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
940 {
941 DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset.u.HighPart, Offset.u.LowPart, PAGE_SIZE);
942 _SEH2_YIELD(return _SEH2_GetExceptionCode());
943 }
944 _SEH2_END;
945 pDirEntry = &pDirEntry[StartIndex % (PAGE_SIZE / sizeof(FATX_DIR_ENTRY))];
946 pDirEntry->FilenameLength = 0xe5;
947 CurrentCluster = vfatDirEntryGetFirstCluster(DeviceExt,
948 (PDIR_ENTRY)pDirEntry);
949
950 /* In case of moving, save properties */
951 if (MoveContext != NULL)
952 {
953 MoveContext->FirstCluster = CurrentCluster;
954 MoveContext->FileSize = pDirEntry->FileSize;
955 MoveContext->CreationTime = pDirEntry->CreationTime;
956 MoveContext->CreationDate = pDirEntry->CreationDate;
957 }
958
959 CcSetDirtyPinnedData(Context, NULL);
960 CcUnpinData(Context);
961
962 /* In case of moving, don't delete data */
963 if (MoveContext == NULL)
964 {
965 while (CurrentCluster && CurrentCluster != 0xffffffff)
966 {
967 GetNextCluster(DeviceExt, CurrentCluster, &NextCluster);
968 /* FIXME: check status */
969 WriteCluster(DeviceExt, CurrentCluster, 0);
970 CurrentCluster = NextCluster;
971 }
972 }
973
974 return STATUS_SUCCESS;
975 }
976
977 NTSTATUS
978 VfatDelEntry(
979 IN PDEVICE_EXTENSION DeviceExt,
980 IN PVFATFCB pFcb,
981 OUT PVFAT_MOVE_CONTEXT MoveContext)
982 {
983 if (DeviceExt->Flags & VCB_IS_FATX)
984 return FATXDelEntry(DeviceExt, pFcb, MoveContext);
985 else
986 return FATDelEntry(DeviceExt, pFcb, MoveContext);
987 }
988
989 /*
990 * move an existing FAT entry
991 */
992 NTSTATUS
993 VfatMoveEntry(
994 IN PDEVICE_EXTENSION DeviceExt,
995 IN PVFATFCB pFcb,
996 IN PUNICODE_STRING FileName,
997 IN PVFATFCB ParentFcb)
998 {
999 NTSTATUS Status;
1000 PVFATFCB OldParent;
1001 VFAT_MOVE_CONTEXT MoveContext;
1002
1003 DPRINT("VfatMoveEntry(%p, %p, %wZ, %p)\n", DeviceExt, pFcb, FileName, ParentFcb);
1004
1005 /* Delete old entry while keeping data */
1006 Status = VfatDelEntry(DeviceExt, pFcb, &MoveContext);
1007 if (!NT_SUCCESS(Status))
1008 {
1009 return Status;
1010 }
1011
1012 OldParent = pFcb->parentFcb;
1013 CcPurgeCacheSection(&OldParent->SectionObjectPointers, NULL, 0, FALSE);
1014 MoveContext.InPlace = (OldParent == ParentFcb);
1015
1016 /* Add our new entry with our cluster */
1017 Status = VfatAddEntry(DeviceExt,
1018 FileName,
1019 &pFcb,
1020 ParentFcb,
1021 (vfatFCBIsDirectory(pFcb) ? FILE_DIRECTORY_FILE : 0),
1022 *pFcb->Attributes,
1023 &MoveContext);
1024
1025 CcPurgeCacheSection(&pFcb->parentFcb->SectionObjectPointers, NULL, 0, FALSE);
1026
1027 return Status;
1028 }
1029
1030 /* EOF */