* \todo Implement changing of the parent directory when a file is written to\r
* \todo Implement file creation / deletion\r
*/\r
-#define DEBUG 1\r
+#define DEBUG 0\r
#define VERBOSE 1\r
\r
#include <acess.h>\r
// === PROTOTYPES ===\r
// --- Driver Core\r
int FAT_Install(char **Arguments);\r
+ int FAT_Detect(int FD);\r
tVFS_Node *FAT_InitDevice(const char *device, const char **options);\r
void FAT_Unmount(tVFS_Node *Node);\r
// --- Helpers\r
int giFAT_MaxCachedClusters = 1024*512/4;\r
\r
// === SEMI-GLOBALS ===\r
-MODULE_DEFINE(0, VER2(1,1) /*v1.01*/, VFAT, FAT_Install, NULL, NULL);\r
+MODULE_DEFINE(0, VER2(0,80) /*v0.80*/, VFAT, FAT_Install, NULL, NULL);\r
tFAT_VolInfo gFAT_Disks[8];\r
int giFAT_PartCount = 0;\r
-tVFS_Driver gFAT_FSInfo = {"fat", 0, FAT_InitDevice, FAT_Unmount, FAT_GetNodeFromINode, NULL};\r
+tVFS_Driver gFAT_FSInfo = {\r
+ .Name = "fat",\r
+ .Detect = FAT_Detect,\r
+ .InitDevice = FAT_InitDevice,\r
+ .Unmount = FAT_Unmount,\r
+ .GetNodeFromINode = FAT_GetNodeFromINode\r
+};\r
tVFS_NodeType gFAT_DirType = {\r
.TypeName = "FAT-Dir",\r
.ReadDir = FAT_ReadDir,\r
return MODULE_ERR_OK;\r
}\r
\r
+/**\r
+ * \brief Detect if a file is a FAT device\r
+ */\r
+int FAT_Detect(int FD)\r
+{\r
+ fat_bootsect bs;\r
+ \r
+ if( VFS_ReadAt(FD, 0, 512, &bs) != 512) {\r
+ return 0;\r
+ }\r
+\r
+ if(bs.bps == 0 || bs.spc == 0)\r
+ return 0;\r
+\r
+ Log_Debug("FAT", "_Detect: Media type = %02x", bs.mediaDesc);\r
+ if( bs.mediaDesc < 0xF0 )\r
+ return 0;\r
+\r
+ return 1;\r
+}\r
/**\r
* \brief Reads the boot sector of a disk and prepares the structures for it\r
*/\r
\r
if(bs->bps == 0 || bs->spc == 0) {\r
Log_Notice("FAT", "Error in FAT Boot Sector (zero BPS/SPC)");\r
+ VFS_Close(diskInfo->fileHandle);\r
return NULL;\r
}\r
\r
diskInfo->FATCache = (Uint32*)malloc(sizeof(Uint32)*diskInfo->ClusterCount);\r
if(diskInfo->FATCache == NULL) {\r
Log_Warning("FAT", "Heap Exhausted");\r
+ VFS_Cose(diskInfo->fileHandle);\r
return NULL;\r
}\r
Ofs = bs->resvSectCount*512;\r
ENTER("pNode XOffset", Node, Offset);\r
\r
cluster = base_cluster = Node->Inode & 0xFFFFFFF; // Cluster ID\r
- LOG("base cluster = 0x%07x", cluster);\r
+// LOG("base cluster = 0x%07x", cluster);\r
\r
// Do Cluster Skip\r
// - Pre FAT32 had a reserved area for the root.\r
if(Cluster) *Cluster = cluster;\r
cluster = FAT_int_GetFatValue(disk, cluster);\r
// Check for end of cluster chain\r
- if(cluster == 0xFFFFFFFF) { LEAVE('i', 1); return 1;}\r
+ if(cluster == GETFATVALUE_EOC) { LEAVE('i', 1); return 1; }\r
}\r
if(Cluster) *Cluster = cluster;\r
}\r
else {\r
// TODO: Bounds checking on root\r
- LOG("Root cluster count %i", disk->bootsect.files_in_root*32/disk->BytesPerCluster);\r
+// LOG("Root cluster count %i", disk->bootsect.files_in_root*32/disk->BytesPerCluster);\r
// Increment by clusters in offset\r
cluster += Offset / disk->BytesPerCluster;\r
}\r
\r
- LOG("cluster = 0x%07x", cluster);\r
+// LOG("cluster = 0x%07x", cluster);\r
\r
// Bounds Checking (Used to spot corruption)\r
if(cluster > disk->ClusterCount + 2)\r
for(i = preSkip; i--; )\r
{\r
cluster = FAT_int_GetFatValue(disk, cluster);\r
- if(cluster == -1) {\r
+ if(cluster == GETFATVALUE_EOC) {\r
Log_Warning("FAT", "Offset is past end of cluster chain mark");\r
LEAVE('i', 0);\r
return 0;\r
LOG("pos = %i, Reading the rest of the clusters");\r
// Get next cluster in the chain\r
cluster = FAT_int_GetFatValue(disk, cluster);\r
- if(cluster == -1) {\r
+ if(cluster == GETFATVALUE_EOC) {\r
Log_Warning("FAT", "Read past End of Cluster Chain (Align)");\r
LEAVE('X', pos);\r
return pos;\r
// Read the rest of the cluster data\r
for( ; count; count -- )\r
{\r
- if(cluster == -1) {\r
+ if(cluster == GETFATVALUE_EOC) {\r
Log_Warning("FAT", "Read past End of Cluster Chain (Bulk)");\r
LEAVE('X', pos);\r
return pos;\r
int remLength = Length;\r
Uint32 cluster, tmpCluster;\r
int bNewCluster = 0;\r
+ off_t original_offset = Offset;\r
\r
if(Offset > Node->Size) return 0;\r
\r
+ ENTER("pNode Xoffset xlength pbuffer", Node, Offset, Length, Buffer);\r
+ \r
// Seek Clusters\r
cluster = Node->Inode & 0xFFFFFFFF;\r
while( Offset > disk->BytesPerCluster )\r
{\r
cluster = FAT_int_GetFatValue( disk, cluster );\r
- if(cluster == -1) {\r
+ if(cluster == GETFATVALUE_EOC) {\r
Log_Warning("FAT", "EOC Unexpectedly Reached");\r
+ LEAVE('i', 0);\r
return 0;\r
}\r
Offset -= disk->BytesPerCluster;\r
if( Offset == disk->BytesPerCluster )\r
{\r
Uint32 tmp = FAT_int_AllocateCluster(disk, cluster);\r
- if(!tmp) return 0;\r
+ if(!tmp) {\r
+ LEAVE('i', 0);\r
+ return 0;\r
+ }\r
cluster = tmp;\r
Offset -= disk->BytesPerCluster;\r
}\r
{\r
char tmpBuf[disk->BytesPerCluster];\r
\r
+ LOG("Read-Modify-Write single");\r
+ \r
// Read-Modify-Write\r
FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
memcpy( tmpBuf + Offset, Buffer, Length );\r
FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
- \r
- return Length;\r
+ goto return_full;\r
}\r
\r
// Clean up changes within a cluster\r
if( Offset )\r
{ \r
+ LOG("Read-Modify-Write first");\r
+\r
// Read-Modify-Write\r
FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
memcpy( tmpBuf + Offset, Buffer, disk->BytesPerCluster - Offset );\r
\r
// Get next cluster (allocating if needed)\r
tmpCluster = FAT_int_GetFatValue(disk, cluster);\r
- if(tmpCluster == -1) {\r
+ if(tmpCluster == GETFATVALUE_EOC) {\r
tmpCluster = FAT_int_AllocateCluster(disk, cluster);\r
- if( tmpCluster == 0 ) {\r
- return Length - remLength;\r
- }\r
+ if( tmpCluster == 0 )\r
+ goto ret_incomplete;\r
}\r
cluster = tmpCluster;\r
}\r
{\r
FAT_int_WriteCluster( disk, cluster, Buffer );\r
Buffer += disk->BytesPerCluster;\r
+ remLength -= disk->BytesPerCluster;\r
\r
// Get next cluster (allocating if needed)\r
tmpCluster = FAT_int_GetFatValue(disk, cluster);\r
- if(tmpCluster == -1) {\r
+ if(tmpCluster == GETFATVALUE_EOC) {\r
bNewCluster = 1;\r
tmpCluster = FAT_int_AllocateCluster(disk, cluster);\r
- if( tmpCluster == 0 ) {\r
- return Length - remLength;\r
- }\r
+ if( tmpCluster == 0 )\r
+ goto ret_incomplete;\r
}\r
cluster = tmpCluster;\r
}\r
\r
// Finish off\r
- if( bNewCluster )\r
- memset(tmpBuf, 0, disk->BytesPerCluster);\r
- else\r
- FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
- memcpy( tmpBuf, Buffer, remLength );\r
- FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
- free( tmpBuf );\r
- \r
+ if( remLength )\r
+ {\r
+ if( bNewCluster )\r
+ memset(tmpBuf, 0, disk->BytesPerCluster);\r
+ else\r
+ FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
+ memcpy( tmpBuf, Buffer, remLength );\r
+ FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
+ }\r
+\r
+return_full:\r
+ if( original_offset + Length > Node->Size ) {\r
+ Node->Size = original_offset + Length;\r
+ LOG("Updated size to %x", Node->Size);\r
+ Node->ImplInt |= FAT_FLAG_DIRTY;\r
+ }\r
+\r
+ LEAVE('i', Length);\r
+ return Length;\r
+ret_incomplete:\r
+ LOG("Write incomplete");\r
+ Length -= remLength;\r
+ if( original_offset + Length > Node->Size ) {\r
+ Node->Size = original_offset + Length; \r
+ Node->ImplInt |= FAT_FLAG_DIRTY;\r
+ }\r
+ LEAVE('i', Length);\r
return Length;\r
}\r
#endif\r
{\r
tFAT_VolInfo *disk = Node->ImplPtr;\r
if(Node == NULL) return ;\r
- \r
+\r
+ ENTER("pNode", Node); \r
+\r
#if SUPPORT_WRITE\r
// Update the node if it's dirty (don't bother if it's marked for\r
// deletion)\r
dirnode = FAT_int_CreateIncompleteDirNode(disk, Node->Inode >> 32);\r
if( !dirnode ) {\r
Log_Error("FAT", "Can't get node for directory cluster #0x%x", Node->Inode>>32);\r
+ LEAVE('-');\r
return ;\r
}\r
\r
Node->ImplInt &= ~FAT_FLAG_DIRTY;\r
}\r
#endif\r
+\r
+ Uint32 cluster = Node->Inode;\r
+ Uint32 implint = Node->ImplInt;\r
\r
- // TODO: Make this more thread safe somehow, probably by moving the\r
- // Inode_UncacheNode higher up and saving the cluster value somewhere\r
- if( Node->ReferenceCount == 1 )\r
- { \r
- #if SUPPORT_WRITE\r
+ #if SUPPORT_WRITE\r
+ if( FAT_int_DerefNode(Node) == 1 )\r
+ {\r
+ LOG("implint = %x", implint);\r
// Delete File\r
- if( Node->ImplInt & FAT_FLAG_DELETE ) {\r
+ if( implint & FAT_FLAG_DELETE ) {\r
+ Log_Debug("FAT", "Deallocating chain stating at 0x%07x", cluster);\r
// Since the node is marked, we only need to remove it's data\r
- Uint32 cluster = Node->Inode & 0xFFFFFFFF;\r
while( cluster != -1 )\r
- cluster = FAT_int_FreeCluster(Node->ImplPtr, cluster);\r
+ cluster = FAT_int_FreeCluster(disk, cluster);\r
}\r
- #endif\r
}\r
- \r
- FAT_int_DerefNode(Node);\r
- return ;\r
+ #endif\r
+ LEAVE('-');\r
}\r