3 * FAT12/16/32 Driver Version (Incl LFN)
\r
5 * NOTE: This driver will only support _reading_ long file names, not
\r
6 * writing. I don't even know why I'm adding write-support. FAT sucks.
\r
9 * - LFN Is buggy in FAT_ReadDir
\r
12 * - There's hard-coded 512 byte sectors everywhere, that needs to be
\r
14 * - Thread safety is out the window with the write and LFN code
\r
17 * \todo Implement changing of the parent directory when a file is written to
\r
18 * \todo Implement file creation / deletion
\r
23 #define CACHE_FAT 0 //!< Caches the FAT in memory
\r
24 #define USE_LFN 1 //!< Enables the use of Long File Names
\r
25 #define SUPPORT_WRITE 0 //!< Enables write support
\r
28 #include <modules.h>
\r
32 #define FAT_FLAG_DIRTY 0x10000
\r
33 #define FAT_FLAG_DELETE 0x20000
\r
38 * \brief Long-Filename cache entry
\r
40 typedef struct sFAT_LFNCacheEnt
\r
43 // TODO: Handle UTF16 names correctly
\r
47 * \brief Long-Filename cache
\r
49 typedef struct sFAT_LFNCache
\r
52 tFAT_LFNCacheEnt Entries[];
\r
56 // === PROTOTYPES ===
\r
58 int FAT_Install(char **Arguments);
\r
59 tVFS_Node *FAT_InitDevice(const char *device, const char **options);
\r
60 void FAT_Unmount(tVFS_Node *Node);
\r
62 int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Cluster);
\r
63 Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 Cluster);
\r
65 Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous);
\r
66 Uint32 FAT_int_FreeCluster(tFAT_VolInfo *Disk, Uint32 Cluster);
\r
68 void FAT_int_ReadCluster(tFAT_VolInfo *Disk, Uint32 Cluster, int Length, void *Buffer);
\r
70 size_t FAT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
\r
72 void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer);
\r
73 size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
\r
76 char *FAT_ReadDir(tVFS_Node *Node, int ID);
\r
77 tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name);
\r
78 tVFS_Node *FAT_GetNodeFromINode(tVFS_Node *Root, Uint64 Inode);
\r
80 int FAT_Mknod(tVFS_Node *Node, const char *Name, Uint Flags);
\r
81 int FAT_Relink(tVFS_Node *node, const char *OldName, const char *NewName);
\r
83 void FAT_CloseFile(tVFS_Node *node);
\r
86 int giFAT_MaxCachedClusters = 1024*512/4;
\r
88 // === SEMI-GLOBALS ===
\r
89 MODULE_DEFINE(0, (0<<8)|50 /*v0.50*/, VFAT, FAT_Install, NULL, NULL);
\r
90 tFAT_VolInfo gFAT_Disks[8];
\r
91 int giFAT_PartCount = 0;
\r
92 tVFS_Driver gFAT_FSInfo = {"fat", 0, FAT_InitDevice, FAT_Unmount, FAT_GetNodeFromINode, NULL};
\r
93 tVFS_NodeType gFAT_DirType = {
\r
94 .TypeName = "FAT-Dir",
\r
95 .ReadDir = FAT_ReadDir,
\r
96 .FindDir = FAT_FindDir,
\r
99 .Relink = FAT_Relink,
\r
101 .Close = FAT_CloseFile
\r
103 tVFS_NodeType gFAT_FileType = {
\r
104 .TypeName = "FAT-File",
\r
107 .Write = FAT_Write,
\r
109 .Close = FAT_CloseFile
\r
114 * \fn int FAT_Install(char **Arguments)
\r
115 * \brief Install the FAT Driver
\r
117 int FAT_Install(char **Arguments)
\r
119 VFS_AddDriver( &gFAT_FSInfo );
\r
120 return MODULE_ERR_OK;
\r
124 * \brief Reads the boot sector of a disk and prepares the structures for it
\r
126 tVFS_Node *FAT_InitDevice(const char *Device, const char **Options)
\r
130 Uint32 FATSz, RootDirSectors, TotSec;
\r
131 tVFS_Node *node = NULL;
\r
132 tFAT_VolInfo *diskInfo = &gFAT_Disks[giFAT_PartCount];
\r
134 // Temporary Pointer
\r
135 bs = &diskInfo->bootsect;
\r
137 // Open device and read boot sector
\r
138 diskInfo->fileHandle = VFS_Open(Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
\r
139 if(diskInfo->fileHandle == -1) {
\r
140 Log_Notice("FAT", "Unable to open device '%s'", Device);
\r
144 VFS_ReadAt(diskInfo->fileHandle, 0, 512, bs);
\r
146 if(bs->bps == 0 || bs->spc == 0) {
\r
147 Log_Notice("FAT", "Error in FAT Boot Sector (zero BPS/SPC)");
\r
151 // FAT Type Determining
\r
152 // - From Microsoft FAT Specifcation
\r
153 RootDirSectors = ((bs->files_in_root*32) + (bs->bps - 1)) / bs->bps;
\r
155 if(bs->fatSz16 != 0)
\r
156 FATSz = bs->fatSz16;
\r
158 FATSz = bs->spec.fat32.fatSz32;
\r
160 if(bs->totalSect16 != 0)
\r
161 TotSec = bs->totalSect16;
\r
163 TotSec = bs->totalSect32;
\r
165 diskInfo->ClusterCount = (TotSec - (bs->resvSectCount + (bs->fatCount * FATSz) + RootDirSectors)) / bs->spc;
\r
167 if(diskInfo->ClusterCount < 4085)
\r
168 diskInfo->type = FAT12;
\r
169 else if(diskInfo->ClusterCount < 65525)
\r
170 diskInfo->type = FAT16;
\r
172 diskInfo->type = FAT32;
\r
176 char *sFatType, *sSize;
\r
177 Uint iSize = diskInfo->ClusterCount * bs->spc * bs->bps / 1024;
\r
179 switch(diskInfo->type)
\r
181 case FAT12: sFatType = "FAT12"; break;
\r
182 case FAT16: sFatType = "FAT16"; break;
\r
183 case FAT32: sFatType = "FAT32"; break;
\r
184 default: sFatType = "UNKNOWN"; break;
\r
186 if(iSize <= 2*1024) {
\r
189 else if(iSize <= 2*1024*1024) {
\r
197 Log_Notice("FAT", "'%s' %s, %i %s", Device, sFatType, iSize, sSize);
\r
202 if(diskInfo->type == FAT32) {
\r
204 diskInfo->name[i] = (bs->spec.fat32.label[i] == ' ' ? '\0' : bs->spec.fat32.label[i]);
\r
208 diskInfo->name[i] = (bs->spec.fat16.label[i] == ' ' ? '\0' : bs->spec.fat16.label[i]);
\r
210 diskInfo->name[11] = '\0';
\r
212 // Compute Root directory offset
\r
213 if(diskInfo->type == FAT32)
\r
214 diskInfo->rootOffset = bs->spec.fat32.rootClust;
\r
216 diskInfo->rootOffset = (FATSz * bs->fatCount) / bs->spc;
\r
218 diskInfo->firstDataSect = bs->resvSectCount + (bs->fatCount * FATSz) + RootDirSectors;
\r
220 //Allow for Caching the FAT
\r
222 if( diskInfo->ClusterCount <= giFAT_MaxCachedClusters )
\r
225 diskInfo->FATCache = (Uint32*)malloc(sizeof(Uint32)*diskInfo->ClusterCount);
\r
226 if(diskInfo->FATCache == NULL) {
\r
227 Log_Warning("FAT", "Heap Exhausted");
\r
230 Ofs = bs->resvSectCount*512;
\r
231 if(diskInfo->type == FAT12)
\r
236 for(i = 0; i < diskInfo->ClusterCount/2; i++) {
\r
237 j = i & 511; //%512
\r
239 VFS_ReadAt(diskInfo->fileHandle, Ofs, 3*512, buf);
\r
242 val = *((int*)(buf+j*3));
\r
243 diskInfo->FATCache[i*2] = val & 0xFFF;
\r
244 diskInfo->FATCache[i*2+1] = (val>>12) & 0xFFF;
\r
247 else if(diskInfo->type == FAT16)
\r
250 for(i=0;i<diskInfo->ClusterCount;i++) {
\r
251 if( (i & 255) == 0 ) {
\r
252 VFS_ReadAt(diskInfo->fileHandle, Ofs, 512, buf);
\r
255 diskInfo->FATCache[i] = buf[i&255];
\r
258 else if(diskInfo->type == FAT32)
\r
261 for(i=0;i<diskInfo->ClusterCount;i++) {
\r
262 if( (i & 127) == 0 ) {
\r
263 VFS_ReadAt(diskInfo->fileHandle, Ofs, 512, buf);
\r
266 diskInfo->FATCache[i] = buf[i&127];
\r
269 LOG("FAT Fully Cached");
\r
271 #endif /*CACHE_FAT*/
\r
273 diskInfo->BytesPerCluster = bs->spc * bs->bps;
\r
275 // Initalise inode cache for filesystem
\r
276 diskInfo->inodeHandle = Inode_GetHandle();
\r
277 LOG("Inode Cache handle is %i", diskInfo->inodeHandle);
\r
279 // == VFS Interface
\r
280 node = &diskInfo->rootNode;
\r
281 //node->Size = bs->files_in_root;
\r
283 node->Inode = diskInfo->rootOffset; // 0:31 - Cluster, 32:63 - Parent Directory Cluster
\r
284 node->ImplPtr = diskInfo; // Disk info pointer
\r
285 node->ImplInt = 0; // 0:15 - Directory Index, 16: Dirty Flag, 17: Deletion Flag
\r
287 node->ReferenceCount = 1;
\r
289 node->UID = 0; node->GID = 0;
\r
291 node->ACLs = &gVFS_ACL_EveryoneRWX;
\r
292 node->Flags = VFS_FFLAG_DIRECTORY;
\r
293 node->CTime = node->MTime = node->ATime = now();
\r
295 node->Type = &gFAT_DirType;
\r
297 giFAT_PartCount ++;
\r
302 * \brief Closes a mount and marks it as free
\r
303 * \param Node Mount Root
\r
305 * \todo Remove FAT Cache
\r
306 * \todo Clear LFN Cache
\r
307 * \todo Check that all files are closed and flushed
\r
309 void FAT_Unmount(tVFS_Node *Node)
\r
311 tFAT_VolInfo *disk = Node->ImplPtr;
\r
313 // Close Disk Handle
\r
314 VFS_Close( disk->fileHandle );
\r
315 // Clear Node Cache
\r
316 Inode_ClearCache(disk->inodeHandle);
\r
318 disk->fileHandle = -2;
\r
323 * \brief Converts an offset in a file into a disk address
\r
324 * \param Node File (or directory) node
\r
325 * \param Offset Offset in the file
\r
326 * \param Addr Return Address
\r
327 * \param Cluster Set to the current cluster (or the last one if \a Offset
\r
328 * is past EOC) - Not touched if the node is the root
\r
330 * \return Zero on success, non-zero on error
\r
332 int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Cluster)
\r
337 tFAT_VolInfo *disk = Node->ImplPtr;
\r
339 ENTER("pNode XOffset", Node, Offset);
\r
341 cluster = Node->Inode & 0xFFFFFFF; // Cluster ID
\r
342 LOG("cluster = 0x%07x", cluster);
\r
345 // - Pre FAT32 had a reserved area for the root.
\r
346 if( disk->type == FAT32 || cluster != disk->rootOffset )
\r
348 skip = Offset / disk->BytesPerCluster;
\r
349 LOG("skip = %i", skip);
\r
350 // Skip previous clusters
\r
353 if(Cluster) *Cluster = cluster;
\r
354 cluster = FAT_int_GetFatValue(disk, cluster);
\r
355 // Check for end of cluster chain
\r
356 if(cluster == 0xFFFFFFFF) { LEAVE('i', 1); return 1;}
\r
358 if(Cluster) *Cluster = cluster;
\r
361 // Increment by clusters in offset
\r
362 cluster += Offset / disk->BytesPerCluster;
\r
365 LOG("cluster = %08x", cluster);
\r
367 // Bounds Checking (Used to spot corruption)
\r
368 if(cluster > disk->ClusterCount + 2)
\r
370 Log_Warning("FAT", "Cluster ID is over cluster count (0x%x>0x%x)",
\r
371 cluster, disk->ClusterCount+2);
\r
377 // - Pre FAT32 cluster base (in sectors)
\r
378 if( cluster == disk->rootOffset && disk->type != FAT32 ) {
\r
379 addr = disk->bootsect.resvSectCount * disk->bootsect.bps;
\r
380 addr += cluster * disk->BytesPerCluster;
\r
383 addr = disk->firstDataSect * disk->bootsect.bps;
\r
384 addr += (cluster - 2) * disk->BytesPerCluster;
\r
386 // In-cluster offset
\r
387 addr += Offset % disk->BytesPerCluster;
\r
389 LOG("addr = 0x%08x", addr);
\r
396 * ====================
\r
398 * ====================
\r
401 * \fn Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster)
\r
402 * \brief Fetches a value from the FAT
\r
404 Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster)
\r
408 ENTER("pDisk xCluster", Disk, cluster);
\r
409 Mutex_Acquire( &Disk->lFAT );
\r
411 if( Disk->ClusterCount <= giFAT_MaxCachedClusters )
\r
413 val = Disk->FATCache[cluster];
\r
414 if(Disk->type == FAT12 && val == EOC_FAT12) val = -1;
\r
415 if(Disk->type == FAT16 && val == EOC_FAT16) val = -1;
\r
416 if(Disk->type == FAT32 && val == EOC_FAT32) val = -1;
\r
421 ofs = Disk->bootsect.resvSectCount*512;
\r
422 if(Disk->type == FAT12) {
\r
423 VFS_ReadAt(Disk->fileHandle, ofs+(cluster/2)*3, 3, &val);
\r
424 val = (cluster & 1 ? val>>12 : val & 0xFFF);
\r
425 if(val == EOC_FAT12) val = -1;
\r
426 } else if(Disk->type == FAT16) {
\r
427 VFS_ReadAt(Disk->fileHandle, ofs+cluster*2, 2, &val);
\r
428 if(val == EOC_FAT16) val = -1;
\r
430 VFS_ReadAt(Disk->fileHandle, ofs+cluster*4, 4, &val);
\r
431 if(val == EOC_FAT32) val = -1;
\r
435 #endif /*CACHE_FAT*/
\r
436 Mutex_Release( &Disk->lFAT );
\r
443 * \brief Allocate a new cluster
\r
445 Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous)
\r
447 Uint32 ret = Previous;
\r
449 if( Disk->ClusterCount <= giFAT_MaxCachedClusters )
\r
454 for(ret = Previous; ret < Disk->ClusterCount; ret++)
\r
456 if(Disk->FATCache[ret] == 0)
\r
459 for(ret = 0; ret < Previous; ret++)
\r
461 if(Disk->FATCache[ret] == 0)
\r
465 RELEASE(Disk->lFAT);
\r
471 case FAT12: eoc = EOC_FAT12; break;
\r
472 case FAT16: eoc = EOC_FAT16; break;
\r
473 case FAT32: eoc = EOC_FAT32; break;
\r
477 Disk->FATCache[ret] = eoc;
\r
478 Disk->FATCache[Previous] = ret;
\r
480 RELEASE(Disk->lFAT);
\r
487 Uint32 ofs = Disk->bootsect.resvSectCount*512;
\r
488 Log_Warning("FAT", "TODO: Implement cluster allocation with non cached FAT");
\r
494 VFS_ReadAt(Disk->fileHandle, ofs+(Previous>>1)*3, 3, &val);
\r
495 if( Previous & 1 ) {
\r
503 VFS_WriteAt(Disk->fileHandle, ofs+(Previous>>1)*3, 3, &val);
\r
505 VFS_ReadAt(Disk->fileHandle, ofs+(ret>>1)*3, 3, &val);
\r
506 if( Cluster & 1 ) {
\r
514 VFS_WriteAt(Disk->fileHandle, ofs+(ret>>1)*3, 3, &val);
\r
517 VFS_ReadAt(Disk->fileHandle, ofs+Previous*2, 2, &ret);
\r
518 VFS_WriteAt(Disk->fileHandle, ofs+ret*2, 2, &eoc);
\r
521 VFS_ReadAt(Disk->fileHandle, ofs+Previous*4, 4, &ret);
\r
522 VFS_WriteAt(Disk->fileHandle, ofs+ret*4, 4, &eoc);
\r
532 * \brief Free's a cluster
\r
533 * \return The original contents of the cluster
\r
535 Uint32 FAT_int_FreeCluster(tFAT_VolInfo *Disk, Uint32 Cluster)
\r
539 if( Disk->ClusterCount <= giFAT_MaxCachedClusters )
\r
543 ret = Disk->FATCache[Cluster];
\r
544 Disk->FATCache[Cluster] = 0;
\r
546 RELEASE(Disk->lFAT);
\r
552 Uint32 ofs = Disk->bootsect.resvSectCount*512;
\r
557 VFS_ReadAt(Disk->fileHandle, ofs+(Cluster>>1)*3, 3, &val);
\r
558 if( Cluster & 1 ) {
\r
559 ret = val & 0xFFF0000;
\r
566 VFS_WriteAt(Disk->fileHandle, ofs+(Previous>>1)*3, 3, &val);
\r
569 VFS_ReadAt(Disk->fileHandle, ofs+Previous*2, 2, &ret);
\r
571 VFS_WriteAt(Disk->fileHandle, ofs+Cluster*2, 2, &val);
\r
574 VFS_ReadAt(Disk->fileHandle, ofs+Previous*4, 4, &ret);
\r
576 VFS_WriteAt(Disk->fileHandle, ofs+Cluster*2, 2, &val);
\r
579 RELEASE(Disk->lFAT);
\r
583 if(Disk->type == FAT12 && ret == EOC_FAT12) ret = -1;
\r
584 if(Disk->type == FAT16 && ret == EOC_FAT16) ret = -1;
\r
585 if(Disk->type == FAT32 && ret == EOC_FAT32) ret = -1;
\r
591 * ====================
\r
593 * ====================
\r
596 * \brief Read a cluster
\r
597 * \param Disk Disk (Volume) to read from
\r
598 * \param Length Length to read
\r
599 * \param Buffer Destination for read data
\r
601 void FAT_int_ReadCluster(tFAT_VolInfo *Disk, Uint32 Cluster, int Length, void *Buffer)
\r
603 ENTER("pDisk xCluster iLength pBuffer", Disk, Cluster, Length, Buffer);
\r
606 (Disk->firstDataSect + (Cluster-2)*Disk->bootsect.spc )
\r
607 * Disk->bootsect.bps,
\r
614 /* ====================
\r
616 * ====================
\r
619 * \brief Reads data from a specified file
\r
621 size_t FAT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
\r
623 int preSkip, count;
\r
624 Uint64 final_bytes;
\r
625 int i, cluster, pos;
\r
626 tFAT_VolInfo *disk = Node->ImplPtr;
\r
627 char tmpBuf[disk->BytesPerCluster];
\r
628 int bpc = disk->BytesPerCluster;
\r
630 ENTER("pNode Xoffset xlength pbuffer", Node, Offset, Length, Buffer);
\r
632 // Sanity Check offset
\r
633 if(Offset > Node->Size) {
\r
634 LOG("Seek past EOF (%i > %i)", Offset, Node->Size);
\r
639 // Cluster is stored in the low 32-bits of the Inode field
\r
640 cluster = Node->Inode & 0xFFFFFFFF;
\r
643 if(Offset + Length > Node->Size) {
\r
644 LOG("Reading past EOF (%lli + %lli > %lli), clamped to %lli",
\r
645 Offset, Length, Node->Size, Node->Size - Offset);
\r
646 Length = Node->Size - Offset;
\r
649 // Skip previous clusters
\r
650 preSkip = Offset / bpc;
\r
652 LOG("preSkip = %i, Offset = %i", preSkip, (int)Offset);
\r
653 for(i = preSkip; i--; )
\r
655 cluster = FAT_int_GetFatValue(disk, cluster);
\r
656 if(cluster == -1) {
\r
657 Log_Warning("FAT", "Offset is past end of cluster chain mark");
\r
663 // Reading from within one cluster
\r
664 if((int)Offset + (int)Length <= bpc)
\r
666 LOG("single cluster only");
\r
667 FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf);
\r
668 memcpy( Buffer, (void*)( tmpBuf + Offset%bpc ), Length );
\r
669 LEAVE('X', Length);
\r
673 // Align read to a cluster
\r
676 pos = bpc - Offset;
\r
677 FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf);
\r
678 memcpy( Buffer, (void*)( tmpBuf + Offset ), pos );
\r
679 LOG("pos = %i, Reading the rest of the clusters");
\r
680 // Get next cluster in the chain
\r
681 cluster = FAT_int_GetFatValue(disk, cluster);
\r
682 if(cluster == -1) {
\r
683 Log_Warning("FAT", "Read past End of Cluster Chain (Align)");
\r
691 // Get Count of Clusters to read
\r
692 // count = DivMod64U(Length - pos, bpc, &final_bytes);
\r
693 count = (Length - pos) / bpc;
\r
694 final_bytes = (Length - pos) % bpc;
\r
695 LOG("Offset = %i, Length = %i, count = %i, final_bytes = %i", (int)Offset, (int)Length, count, final_bytes);
\r
697 // Read the rest of the cluster data
\r
698 for( ; count; count -- )
\r
700 if(cluster == -1) {
\r
701 Log_Warning("FAT", "Read past End of Cluster Chain (Bulk)");
\r
706 FAT_int_ReadCluster(disk, cluster, bpc, (void*)(Buffer+pos));
\r
708 // Get next cluster in the chain
\r
709 cluster = FAT_int_GetFatValue(disk, cluster);
\r
712 if( final_bytes > 0 )
\r
714 if(cluster == -1) {
\r
715 Log_Warning("FAT", "Read past End of Cluster Chain (Final)");
\r
719 // Read final cluster
\r
720 FAT_int_ReadCluster( disk, cluster, bpc, tmpBuf );
\r
721 memcpy( (void*)(Buffer+pos), tmpBuf, Length-pos );
\r
725 //Debug_HexDump("FAT_Read", Buffer, Length);
\r
728 LEAVE('X', Length);
\r
734 * \brief Write a cluster to disk
\r
736 void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer)
\r
738 ENTER("pDisk xCluster pBuffer", Disk, Cluster, Buffer);
\r
741 (Disk->firstDataSect + (Cluster-2)*Disk->bootsect.spc )
\r
742 * Disk->bootsect.bps,
\r
743 Disk->BytesPerCluster,
\r
750 * \brief Write to a file
\r
751 * \param Node File Node
\r
752 * \param Offset Offset within file
\r
753 * \param Length Size of data to write
\r
754 * \param Buffer Data source
\r
756 size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
\r
758 tFAT_VolInfo *disk = Node->ImplPtr;
\r
759 char tmpBuf[disk->BytesPerCluster];
\r
760 int remLength = Length;
\r
761 Uint32 cluster, tmpCluster;
\r
762 int bNewCluster = 0;
\r
764 if(Offset > Node->Size) return 0;
\r
767 cluster = Node->Inode & 0xFFFFFFFF;
\r
768 while( Offset > disk->BytesPerCluster )
\r
770 cluster = FAT_int_GetFatValue( disk, cluster );
\r
771 if(cluster == -1) {
\r
772 Log_Warning("FAT", "EOC Unexpectedly Reached");
\r
775 Offset -= disk->BytesPerCluster;
\r
777 if( Offset == disk->BytesPerCluster )
\r
779 Uint32 tmp = FAT_int_AllocateCluster(disk, cluster);
\r
782 Offset -= disk->BytesPerCluster;
\r
785 if( Offset + Length < disk->BytesPerCluster )
\r
787 char tmpBuf[disk->BytesPerCluster];
\r
789 // Read-Modify-Write
\r
790 FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );
\r
791 memcpy( tmpBuf + Offset, Buffer, Length );
\r
792 FAT_int_WriteCluster( disk, cluster, tmpBuf );
\r
797 // Clean up changes within a cluster
\r
800 // Read-Modify-Write
\r
801 FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );
\r
802 memcpy( tmpBuf + Offset, Buffer, disk->BytesPerCluster - Offset );
\r
803 FAT_int_WriteCluster( disk, cluster, tmpBuf );
\r
805 remLength -= disk->BytesPerCluster - Offset;
\r
806 Buffer += disk->BytesPerCluster - Offset;
\r
808 // Get next cluster (allocating if needed)
\r
809 tmpCluster = FAT_int_GetFatValue(disk, cluster);
\r
810 if(tmpCluster == -1) {
\r
811 tmpCluster = FAT_int_AllocateCluster(disk, cluster);
\r
812 if( tmpCluster == 0 ) {
\r
813 return Length - remLength;
\r
816 cluster = tmpCluster;
\r
819 while( remLength > disk->BytesPerCluster )
\r
821 FAT_int_WriteCluster( disk, cluster, Buffer );
\r
822 Buffer += disk->BytesPerCluster;
\r
824 // Get next cluster (allocating if needed)
\r
825 tmpCluster = FAT_int_GetFatValue(disk, cluster);
\r
826 if(tmpCluster == -1) {
\r
828 tmpCluster = FAT_int_AllocateCluster(disk, cluster);
\r
829 if( tmpCluster == 0 ) {
\r
830 return Length - remLength;
\r
833 cluster = tmpCluster;
\r
837 tmpBuf = malloc( disk->BytesPerCluster );
\r
839 memset(tmpBuf, 0, disk->BytesPerCluster);
\r
841 FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );
\r
842 memcpy( tmpBuf, Buffer, remLength );
\r
843 FAT_int_WriteCluster( disk, cluster, tmpBuf );
\r
850 /* ====================
\r
851 * File Names & Nodes
\r
852 * ====================
\r
855 * \brief Converts a FAT directory entry name into a proper filename
\r
856 * \param dest Destination array (must be at least 13 bytes in size)
\r
857 * \param src 8.3 filename (concatenated, e.g 'FILE1 TXT')
\r
859 void FAT_int_ProperFilename(char *dest, const char *src)
\r
865 for( inpos = 0; inpos < 8; inpos++ ) {
\r
866 if(src[inpos] == ' ') break;
\r
867 dest[outpos++] = src[inpos];
\r
870 // Check for empty extensions
\r
873 dest[outpos++] = '.';
\r
874 for( ; inpos < 11; inpos++) {
\r
875 if(src[inpos] == ' ') break;
\r
876 dest[outpos++] = src[inpos];
\r
879 dest[outpos++] = '\0';
\r
881 //LOG("dest='%s'", dest);
\r
885 * \fn char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)
\r
886 * \brief Converts either a LFN or a 8.3 Name into a proper name
\r
887 * \param ft Pointer to the file's entry in the parent directory
\r
888 * \param LongFileName Long file name pointer
\r
889 * \return Filename as a heap string
\r
891 char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)
\r
894 ENTER("pft sLongFileName", ft, LongFileName);
\r
895 //Log_Debug("FAT", "FAT_int_CreateName(ft=%p, LongFileName=%p'%s')", ft, LongFileName);
\r
897 if(LongFileName && LongFileName[0] != '\0')
\r
899 ret = strdup(LongFileName);
\r
904 ret = (char*) malloc(13);
\r
906 Log_Warning("FAT", "FAT_int_CreateName: malloc(13) failed");
\r
909 FAT_int_ProperFilename(ret, ft->name);
\r
918 * \brief Creates a tVFS_Node structure for a given file entry
\r
919 * \param Parent Parent directory VFS node
\r
920 * \param Entry File table entry for the new node
\r
921 * \param Pos Position in the parent of the new node
\r
923 tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos)
\r
927 tFAT_VolInfo *disk = Parent->ImplPtr;
\r
929 ENTER("pParent pFT", Parent, Entry);
\r
930 LOG("disk = %p", disk);
\r
932 memset(&node, 0, sizeof(tVFS_Node));
\r
935 // 0-27: Cluster, 32-59: Parent Cluster
\r
936 node.Inode = Entry->cluster | (Entry->clusterHi<<16) | (Parent->Inode << 32);
\r
937 LOG("node.Inode = %llx", node.Inode);
\r
938 // Position in parent directory
\r
939 node.ImplInt = Pos & 0xFFFF;
\r
941 node.ImplPtr = disk;
\r
942 node.Size = Entry->size;
\r
943 LOG("Entry->size = %i", Entry->size);
\r
945 node.UID = 0; node.GID = 0;
\r
949 if(Entry->attrib & ATTR_DIRECTORY) node.Flags |= VFS_FFLAG_DIRECTORY;
\r
950 if(Entry->attrib & ATTR_READONLY) {
\r
951 node.Flags |= VFS_FFLAG_READONLY;
\r
952 node.ACLs = &gVFS_ACL_EveryoneRX; // R-XR-XR-X
\r
955 node.ACLs = &gVFS_ACL_EveryoneRWX; // RWXRWXRWX
\r
958 // Create timestamps
\r
959 node.ATime = timestamp(0,0,0,
\r
960 ((Entry->adate&0x1F) - 1), // Days
\r
961 ((Entry->adate&0x1E0) - 1), // Months
\r
962 1980+((Entry->adate&0xFF00)>>8) // Years
\r
965 node.CTime = Entry->ctimems * 10; // Miliseconds
\r
966 node.CTime += timestamp(
\r
967 ((Entry->ctime&0x1F)<<1), // Seconds
\r
968 ((Entry->ctime&0x3F0)>>5), // Minutes
\r
969 ((Entry->ctime&0xF800)>>11), // Hours
\r
970 ((Entry->cdate&0x1F)-1), // Days
\r
971 ((Entry->cdate&0x1E0)-1), // Months
\r
972 1980+((Entry->cdate&0xFF00)>>8) // Years
\r
975 node.MTime = timestamp(
\r
976 ((Entry->mtime&0x1F)<<1), // Seconds
\r
977 ((Entry->mtime&0x3F0)>>5), // Minutes
\r
978 ((Entry->mtime&0xF800)>>11), // Hours
\r
979 ((Entry->mdate&0x1F)-1), // Days
\r
980 ((Entry->mdate&0x1E0)-1), // Months
\r
981 1980+((Entry->mdate&0xFF00)>>8) // Years
\r
985 if(node.Flags & VFS_FFLAG_DIRECTORY) {
\r
986 //Log_Debug("FAT", "Directory %08x has size 0x%x", node.Inode, node.Size);
\r
987 node.Type = &gFAT_DirType;
\r
991 node.Type = &gFAT_FileType;
\r
994 ret = Inode_CacheNode(disk->inodeHandle, &node);
\r
1000 * ====================
\r
1002 * ====================
\r
1006 * \brief Reads a sector from the disk
\r
1007 * \param Node Directory node to read
\r
1008 * \param Sector Sector number in the directory to read
\r
1009 * \param Buffer Destination buffer for the read data
\r
1011 int FAT_int_ReadDirSector(tVFS_Node *Node, int Sector, fat_filetable *Buffer)
\r
1014 tFAT_VolInfo *disk = Node->ImplPtr;
\r
1016 ENTER("pNode iSector pEntry", Node, Sector, Buffer);
\r
1019 if(FAT_int_GetAddress(Node, Sector * 512, &addr, NULL))
\r
1025 LOG("addr = 0x%llx", addr);
\r
1027 if(VFS_ReadAt(disk->fileHandle, addr, 512, Buffer) != 512)
\r
1039 * \brief Writes an entry to the disk
\r
1040 * \todo Support expanding a directory
\r
1041 * \param Node Directory node
\r
1042 * \param ID ID of entry to update
\r
1043 * \param Entry Entry data
\r
1044 * \return Zero on success, non-zero on error
\r
1046 int FAT_int_WriteDirEntry(tVFS_Node *Node, int ID, fat_filetable *Entry)
\r
1050 Uint32 cluster = 0;
\r
1051 tFAT_VolInfo *disk = Node->ImplPtr;
\r
1053 ENTER("pNode iID pEntry", Node, ID, Entry);
\r
1055 tmp = FAT_int_GetAddress(Node, ID * sizeof(fat_filetable), &addr, &cluster);
\r
1058 //TODO: Allocate a cluster
\r
1059 cluster = FAT_int_AllocateCluster(Node->ImplPtr, cluster);
\r
1060 if(cluster == -1) {
\r
1061 Log_Warning("FAT", "Unable to allocate an other cluster for %p", Node);
\r
1065 FAT_int_GetAddress(Node, ID * sizeof(fat_filetable), &addr, &cluster);
\r
1069 LOG("addr = 0x%llx", addr);
\r
1072 VFS_WriteAt(disk->fileHandle, addr, sizeof(fat_filetable), Entry); // Read Dir Data
\r
1081 * \fn char *FAT_int_GetLFN(tVFS_Node *node)
\r
1082 * \brief Return pointer to LFN cache entry
\r
1083 * \param Node Directory node
\r
1084 * \param ID ID of the short name
\r
1085 * \return Pointer to the LFN cache entry
\r
1087 char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
\r
1089 tFAT_LFNCache *cache;
\r
1092 Mutex_Acquire( &Node->Lock );
\r
1094 // TODO: Thread Safety (Lock things)
\r
1095 cache = Node->Data;
\r
1097 // Create a cache if it isn't there
\r
1099 cache = Node->Data = malloc( sizeof(tFAT_LFNCache) + sizeof(tFAT_LFNCacheEnt) );
\r
1100 cache->NumEntries = 1;
\r
1101 cache->Entries[0].ID = ID;
\r
1102 cache->Entries[0].Data[0] = '\0';
\r
1103 Mutex_Release( &Node->Lock );
\r
1104 //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data);
\r
1105 return cache->Entries[0].Data;
\r
1108 // Scan for this entry
\r
1110 for( i = 0; i < cache->NumEntries; i++ )
\r
1112 if( cache->Entries[i].ID == ID ) {
\r
1113 Mutex_Release( &Node->Lock );
\r
1114 //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data);
\r
1115 return cache->Entries[i].Data;
\r
1117 if( cache->Entries[i].ID == -1 && firstFree == -1 )
\r
1121 if(firstFree == -1) {
\r
1122 // Use `i` for temp length
\r
1123 i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt);
\r
1124 Node->Data = realloc( Node->Data, i );
\r
1125 if( !Node->Data ) {
\r
1126 Log_Error("FAT", "realloc() fail, unable to allocate %i for LFN cache", i);
\r
1127 Mutex_Release( &Node->Lock );
\r
1130 //Log_Debug("FAT", "Realloc (%i)\n", i);
\r
1131 cache = Node->Data;
\r
1132 i = cache->NumEntries;
\r
1133 cache->NumEntries ++;
\r
1139 // Create new entry
\r
1140 cache->Entries[ i ].ID = ID;
\r
1141 cache->Entries[ i ].Data[0] = '\0';
\r
1143 Mutex_Release( &Node->Lock );
\r
1144 //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i);
\r
1145 return cache->Entries[ i ].Data;
\r
1149 * \fn void FAT_int_DelLFN(tVFS_Node *node)
\r
1150 * \brief Delete a LFN cache entry
\r
1151 * \param Node Directory node
\r
1152 * \param ID File Entry ID
\r
1154 void FAT_int_DelLFN(tVFS_Node *Node, int ID)
\r
1156 tFAT_LFNCache *cache = Node->Data;
\r
1160 if(!cache) return;
\r
1162 // Scan for a current entry
\r
1163 for( i = 0; i < cache->NumEntries; i++ )
\r
1165 if( cache->Entries[i].ID == ID )
\r
1166 cache->Entries[i].ID = -1;
\r
1173 * \fn char *FAT_ReadDir(tVFS_Node *Node, int ID)
\r
1174 * \param Node Node structure of directory
\r
1175 * \param ID Directory position
\r
1176 * \return Filename as a heap string, NULL or VFS_SKIP
\r
1178 char *FAT_ReadDir(tVFS_Node *Node, int ID)
\r
1180 fat_filetable fileinfo[16]; // sizeof(fat_filetable)=32, so 16 per sector
\r
1187 ENTER("pNode iID", Node, ID);
\r
1189 if(FAT_int_ReadDirSector(Node, ID/16, fileinfo))
\r
1191 LOG("End of chain, end of dir");
\r
1196 // Offset in sector
\r
1199 LOG("fileinfo[%i].name[0] = 0x%x", a, (Uint8)fileinfo[a].name[0]);
\r
1201 // Check if this is the last entry
\r
1202 if( fileinfo[a].name[0] == '\0' ) {
\r
1204 LOG("End of list");
\r
1206 return NULL; // break
\r
1209 // Check for empty entry
\r
1210 if( (Uint8)fileinfo[a].name[0] == 0xE5 ) {
\r
1211 LOG("Empty Entry");
\r
1212 #if 0 // Stop on empty entry?
\r
1214 return NULL; // Stop
\r
1216 LEAVE('p', VFS_SKIP);
\r
1217 return VFS_SKIP; // Skip
\r
1222 // Get Long File Name Cache
\r
1223 if(fileinfo[a].attrib == ATTR_LFN)
\r
1225 fat_longfilename *lfnInfo;
\r
1227 lfnInfo = (fat_longfilename *) &fileinfo[a];
\r
1229 // Get cache for corresponding file
\r
1230 // > ID + Index gets the corresponding short node
\r
1231 lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) );
\r
1233 // Bit 6 indicates the start of an entry
\r
1234 if(lfnInfo->id & 0x40) memset(lfn, 0, 256);
\r
1236 a = ((lfnInfo->id & 0x3F) - 1) * 13;
\r
1237 //Log_Debug("FAT", "ID = 0x%02x, a = %i", lfnInfo->id, a);
\r
1239 // Sanity Check (FAT implementations should not allow >255 character names)
\r
1240 if(a > 255) return VFS_SKIP;
\r
1242 // Append new bytes
\r
1243 lfn[a+ 0] = lfnInfo->name1[0]; lfn[a+ 1] = lfnInfo->name1[1];
\r
1244 lfn[a+ 2] = lfnInfo->name1[2]; lfn[a+ 3] = lfnInfo->name1[3];
\r
1245 lfn[a+ 4] = lfnInfo->name1[4];
\r
1246 lfn[a+ 5] = lfnInfo->name2[0]; lfn[a+ 6] = lfnInfo->name2[1];
\r
1247 lfn[a+ 7] = lfnInfo->name2[2]; lfn[a+ 8] = lfnInfo->name2[3];
\r
1248 lfn[a+ 9] = lfnInfo->name2[4]; lfn[a+10] = lfnInfo->name2[5];
\r
1249 lfn[a+11] = lfnInfo->name3[0]; lfn[a+12] = lfnInfo->name3[1];
\r
1250 LOG("lfn = '%s'", lfn);
\r
1251 //Log_Debug("FAT", "lfn = '%s'", lfn);
\r
1252 LEAVE('p', VFS_SKIP);
\r
1257 // Check if it is a volume entry
\r
1258 if(fileinfo[a].attrib & 0x08) {
\r
1259 LEAVE('p', VFS_SKIP);
\r
1263 if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == ' ') {
\r
1264 LEAVE('p', VFS_SKIP);
\r
1268 if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == '.' && fileinfo[a].name[2] == ' ') {
\r
1269 LEAVE('p', VFS_SKIP);
\r
1273 LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'",
\r
1274 fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3],
\r
1275 fileinfo[a].name[4], fileinfo[a].name[5], fileinfo[a].name[6], fileinfo[a].name[7],
\r
1276 fileinfo[a].name[8], fileinfo[a].name[9], fileinfo[a].name[10] );
\r
1279 lfn = FAT_int_GetLFN(Node, ID);
\r
1280 //Log_Debug("FAT", "lfn = %p'%s'", lfn, lfn);
\r
1281 ret = FAT_int_CreateName(&fileinfo[a], lfn);
\r
1283 ret = FAT_int_CreateName(&fileinfo[a], NULL);
\r
1291 * \fn tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name)
\r
1292 * \brief Finds an entry in the current directory
\r
1294 tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name)
\r
1296 fat_filetable fileinfo[16];
\r
1299 fat_longfilename *lfnInfo;
\r
1301 int lfnPos=255, lfnId = -1;
\r
1304 tVFS_Node *tmpNode;
\r
1305 tFAT_VolInfo *disk = Node->ImplPtr;
\r
1308 ENTER("pNode sname", Node, Name);
\r
1311 if(!Name || Name[0] == '\0') {
\r
1316 for( i = 0; ; i++ )
\r
1318 if((i & 0xF) == 0) {
\r
1319 if(FAT_int_ReadDirSector(Node, i/16, fileinfo))
\r
1326 //Check if the files are free
\r
1327 if(fileinfo[i&0xF].name[0] == '\0') break; // End of List marker
\r
1328 if(fileinfo[i&0xF].name[0] == '\xE5') continue; // Free entry
\r
1332 // Long File Name Entry
\r
1333 if(fileinfo[i & 0xF].attrib == ATTR_LFN)
\r
1335 lfnInfo = (fat_longfilename *) &fileinfo[i&0xF];
\r
1336 if(lfnInfo->id & 0x40) {
\r
1337 memset(lfn, 0, 256);
\r
1338 lfnPos = (lfnInfo->id & 0x3F) * 13 - 1;
\r
1340 // Sanity check the position so we don't overflow
\r
1343 lfn[lfnPos--] = lfnInfo->name3[1]; lfn[lfnPos--] = lfnInfo->name3[0];
\r
1344 lfn[lfnPos--] = lfnInfo->name2[5]; lfn[lfnPos--] = lfnInfo->name2[4];
\r
1345 lfn[lfnPos--] = lfnInfo->name2[3]; lfn[lfnPos--] = lfnInfo->name2[2];
\r
1346 lfn[lfnPos--] = lfnInfo->name2[1]; lfn[lfnPos--] = lfnInfo->name2[0];
\r
1347 lfn[lfnPos--] = lfnInfo->name1[4]; lfn[lfnPos--] = lfnInfo->name1[3];
\r
1348 lfn[lfnPos--] = lfnInfo->name1[2]; lfn[lfnPos--] = lfnInfo->name1[1];
\r
1349 lfn[lfnPos--] = lfnInfo->name1[0];
\r
1350 if((lfnInfo->id&0x3F) == 1)
\r
1357 // Remove LFN if it does not apply
\r
1358 if(lfnId != i) lfn[0] = '\0';
\r
1360 if(fileinfo[i&0xF].attrib == ATTR_LFN) continue;
\r
1362 // Get Real Filename
\r
1363 FAT_int_ProperFilename(tmpName, fileinfo[i&0xF].name);
\r
1364 LOG("tmpName = '%s'", tmpName);
\r
1366 // Only the long name is case sensitive, 8.3 is not
\r
1368 if(strucmp(tmpName, Name) == 0 || strcmp(lfn, Name) == 0)
\r
1370 if(strucmp(tmpName, Name) == 0)
\r
1373 cluster = fileinfo[i&0xF].cluster | (fileinfo[i&0xF].clusterHi << 16);
\r
1374 tmpNode = Inode_GetCache(disk->inodeHandle, cluster);
\r
1375 if(tmpNode == NULL) // Node is not cached
\r
1377 tmpNode = FAT_int_CreateNode(Node, &fileinfo[i&0xF], i);
\r
1379 LEAVE('p', tmpNode);
\r
1391 tVFS_Node *FAT_GetNodeFromINode(tVFS_Node *Root, Uint64 Inode)
\r
1393 tFAT_VolInfo *disk = Root->ImplPtr;
\r
1394 int ents_per_sector = 512 / sizeof(fat_filetable);
\r
1395 fat_filetable fileinfo[ents_per_sector];
\r
1396 int sector = 0, i;
\r
1397 tVFS_Node stub_node;
\r
1399 ENTER("pRoot XInode", Root, Inode);
\r
1401 stub_node.ImplPtr = disk;
\r
1402 stub_node.Size = -1;
\r
1403 stub_node.Inode = Inode >> 32;
\r
1405 for( i = 0; ; i ++ )
\r
1407 if( i == 0 || i == ents_per_sector )
\r
1409 if(FAT_int_ReadDirSector(&stub_node, sector, fileinfo))
\r
1411 LOG("ReadDirSector failed");
\r
1419 // Check for free/end of list
\r
1420 if(fileinfo[i].name[0] == '\0') break; // End of List marker
\r
1421 if(fileinfo[i].name[0] == '\xE5') continue; // Free entry
\r
1423 if(fileinfo[i].attrib == ATTR_LFN) continue;
\r
1425 LOG("fileinfo[i].cluster = %x %04x", fileinfo[i].clusterHi, fileinfo[i].cluster);
\r
1429 FAT_int_ProperFilename(tmpName, fileinfo[i].name);
\r
1430 LOG("tmpName = '%s'", tmpName);
\r
1435 if(fileinfo[i].cluster != (Inode & 0xFFFF)) continue;
\r
1436 if(fileinfo[i].clusterHi != ((Inode >> 16) & 0xFFFF)) continue;
\r
1438 LEAVE_RET('p', FAT_int_CreateNode(&stub_node, &fileinfo[i], sector*ents_per_sector+i));
\r
1440 LOG("sector = %i, i = %i", sector, i);
\r
1447 * \fn int FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags)
\r
1448 * \brief Create a new node
\r
1450 int FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags)
\r
1456 * \fn int FAT_Relink(tVFS_Node *Node, char *OldName, char *NewName)
\r
1457 * \brief Rename / Delete a file
\r
1459 int FAT_Relink(tVFS_Node *Node, char *OldName, char *NewName)
\r
1462 fat_filetable ft = {0};
\r
1465 child = FAT_FindDir(Node, OldName);
\r
1466 if(!child) return ENOTFOUND;
\r
1469 if( NewName == NULL )
\r
1471 child->ImplInt |= FAT_FLAG_DELETE; // Mark for deletion on close
\r
1473 // Delete from the directory
\r
1474 ft.name[0] = '\xE9';
\r
1475 FAT_int_WriteDirEntry(Node, child->ImplInt & 0xFFFF, &ft);
\r
1483 Log_Warning("FAT", "Renaming no yet supported %p ('%s' => '%s')",
\r
1484 Node, OldName, NewName);
\r
1489 child->Close( child );
\r
1495 * \fn void FAT_CloseFile(tVFS_Node *Node)
\r
1496 * \brief Close an open file
\r
1498 void FAT_CloseFile(tVFS_Node *Node)
\r
1500 tFAT_VolInfo *disk = Node->ImplPtr;
\r
1501 if(Node == NULL) return ;
\r
1504 // Update the node if it's dirty (don't bother if it's marked for
\r
1506 if( (Node->ImplInt & FAT_FLAG_DIRTY) && !(Node->ImplInt & FAT_FLAG_DELETE) )
\r
1508 tFAT_VolInfo buf[16];
\r
1509 tFAT_VolInfo *ft = &buf[ (Node->ImplInt & 0xFFFF) % 16 ];
\r
1511 FAT_int_ReadDirSector(Node, (Node->ImplInt & 0xFFFF)/16, buf);
\r
1512 ft->size = Node->Size;
\r
1513 // TODO: update adate, mtime, mdate
\r
1514 FAT_int_WriteDirEntry(Node, Node->ImplInt & 0xFFFF, ft);
\r
1516 Node->ImplInt &= ~FAT_FLAG_DIRTY;
\r
1520 // TODO: Make this more thread safe somehow, probably by moving the
\r
1521 // Inode_UncacheNode higher up and saving the cluster value somewhere
\r
1522 if( Node->ReferenceCount == 1 )
\r
1526 if( Node->ImplInt & FAT_FLAG_DELETE ) {
\r
1527 // Since the node is marked, we only need to remove it's data
\r
1528 Uint32 cluster = Node->Inode & 0xFFFFFFFF;
\r
1529 while( cluster != -1 )
\r
1530 cluster = FAT_int_FreeCluster(Node->ImplPtr, cluster);
\r
1535 Inode_UncacheNode(disk->inodeHandle, Node->Inode);
\r