X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Modules%2FFilesystems%2FFAT%2Ffat.c;h=80bf391b9768eb85fbcaf8b6a20c311e4925ad89;hb=37de13c0c8c88fa6edba679c20becec15547533e;hp=26b1961396ae65aab08e66e8eeff28bc791291e4;hpb=18a6f0a17a50c25007a8f9b910af02bdf6fbcb78;p=tpg%2Facess2.git diff --git a/Modules/Filesystems/FAT/fat.c b/Modules/Filesystems/FAT/fat.c index 26b19613..80bf391b 100644 --- a/Modules/Filesystems/FAT/fat.c +++ b/Modules/Filesystems/FAT/fat.c @@ -56,7 +56,7 @@ typedef struct sFAT_LFNCache // === PROTOTYPES === // --- Driver Core int FAT_Install(char **Arguments); -tVFS_Node *FAT_InitDevice(char *device, char **options); +tVFS_Node *FAT_InitDevice(const char *device, const char **options); void FAT_Unmount(tVFS_Node *Node); // --- Helpers int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Cluster); @@ -102,10 +102,9 @@ int FAT_Install(char **Arguments) } /** - * \fn tVFS_Node *FAT_InitDevice(char *Device, char **Options) * \brief Reads the boot sector of a disk and prepares the structures for it */ -tVFS_Node *FAT_InitDevice(char *Device, char **Options) +tVFS_Node *FAT_InitDevice(const char *Device, const char **Options) { fat_bootsect *bs; int i; @@ -611,9 +610,9 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) { int preSkip, count; int i, cluster, pos; - int bpc; - void *tmpBuf; tFAT_VolInfo *disk = Node->ImplPtr; + char tmpBuf[disk->BytesPerCluster]; + int bpc = disk->BytesPerCluster; ENTER("pNode Xoffset Xlength pbuffer", Node, Offset, Length, Buffer); @@ -624,11 +623,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) return 0; } - // Calculate and Allocate Bytes Per Cluster - bpc = disk->BytesPerCluster; - tmpBuf = (void*) malloc(bpc); - if( !tmpBuf ) return 0; - // Cluster is stored in the low 32-bits of the Inode field cluster = Node->Inode & 0xFFFFFFFF; @@ -645,7 +639,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) LOG("First cluster only"); FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf); memcpy( Buffer, (void*)( tmpBuf + Offset%bpc ), Length ); - free(tmpBuf); #if DEBUG //Debug_HexDump("FAT_Read", Buffer, Length); #endif @@ -659,7 +652,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) cluster = FAT_int_GetFatValue(disk, cluster); if(cluster == -1) { Log_Warning("FAT", "Offset is past end of cluster chain mark"); - free(tmpBuf); LEAVE('i', 0); return 0; } @@ -689,7 +681,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) #if DEBUG //Debug_HexDump("FAT_Read", Buffer, Length); #endif - free(tmpBuf); LEAVE('i', 1); return Length; } @@ -705,8 +696,7 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) // Get next cluster in the chain cluster = FAT_int_GetFatValue(disk, cluster); if(cluster == -1) { - Warning("FAT_Read - Read past End of Cluster Chain"); - free(tmpBuf); + Log_Warning("FAT", "FAT_Read: Read past End of Cluster Chain"); LEAVE('i', 0); return 0; } @@ -718,8 +708,7 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) // Get next cluster in the chain cluster = FAT_int_GetFatValue(disk, cluster); if(cluster == -1) { - Warning("FAT_Read - Read past End of Cluster Chain"); - free(tmpBuf); + Log_Warning("FAT", "FAT_Read: Read past End of Cluster Chain"); LEAVE('i', 0); return 0; } @@ -739,7 +728,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) //Debug_HexDump("FAT_Read", Buffer, Length); #endif - free(tmpBuf); LEAVE('X', Length); return Length; } @@ -771,7 +759,7 @@ void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer) Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) { tFAT_VolInfo *disk = Node->ImplPtr; - void *tmpBuf; + char tmpBuf[disk->BytesPerCluster]; int remLength = Length; Uint32 cluster, tmpCluster; int bNewCluster = 0; @@ -799,29 +787,24 @@ Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) if( Offset + Length < disk->BytesPerCluster ) { - tmpBuf = malloc( disk->BytesPerCluster ); + char tmpBuf[disk->BytesPerCluster]; // Read-Modify-Write FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf ); memcpy( tmpBuf + Offset, Buffer, Length ); FAT_int_WriteCluster( disk, cluster, tmpBuf ); - free(tmpBuf); return Length; } // Clean up changes within a cluster if( Offset ) - { - tmpBuf = malloc( disk->BytesPerCluster ); - + { // Read-Modify-Write FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf ); memcpy( tmpBuf + Offset, Buffer, disk->BytesPerCluster - Offset ); FAT_int_WriteCluster( disk, cluster, tmpBuf ); - free(tmpBuf); - remLength -= disk->BytesPerCluster - Offset; Buffer += disk->BytesPerCluster - Offset; @@ -912,6 +895,7 @@ char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName) { char *ret; ENTER("pft sLongFileName", ft, LongFileName); + //Log_Debug("FAT", "FAT_int_CreateName(ft=%p, LongFileName=%p'%s')", ft, LongFileName); #if USE_LFN if(LongFileName && LongFileName[0] != '\0') { @@ -1117,6 +1101,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) tFAT_LFNCache *cache; int i, firstFree; + Mutex_Acquire( &Node->Lock ); + // TODO: Thread Safety (Lock things) cache = Node->Data; @@ -1126,15 +1112,20 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) cache->NumEntries = 1; cache->Entries[0].ID = ID; cache->Entries[0].Data[0] = '\0'; + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data); return cache->Entries[0].Data; } - // Scan for a current entry + // Scan for this entry firstFree = -1; for( i = 0; i < cache->NumEntries; i++ ) { - if( cache->Entries[i].ID == ID ) + if( cache->Entries[i].ID == ID ) { + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data); return cache->Entries[i].Data; + } if( cache->Entries[i].ID == -1 && firstFree == -1 ) firstFree = i; } @@ -1144,9 +1135,11 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt); Node->Data = realloc( Node->Data, i ); if( !Node->Data ) { - Log_Error("FAT", "malloc() fail, unable to allocate %i for LFN cache", i); + Log_Error("FAT", "realloc() fail, unable to allocate %i for LFN cache", i); + Mutex_Release( &Node->Lock ); return NULL; } + //Log_Debug("FAT", "Realloc (%i)\n", i); cache = Node->Data; i = cache->NumEntries; cache->NumEntries ++; @@ -1159,7 +1152,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) cache->Entries[ i ].ID = ID; cache->Entries[ i ].Data[0] = '\0'; - //TODO: Unlock + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i); return cache->Entries[ i ].Data; } @@ -1251,7 +1245,8 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) // Bit 6 indicates the start of an entry if(lfnInfo->id & 0x40) memset(lfn, 0, 256); - a = (lfnInfo->id & 0x3F) * 13; + a = ((lfnInfo->id & 0x3F) - 1) * 13; + //Log_Debug("FAT", "ID = 0x%02x, a = %i", lfnInfo->id, a); // Sanity Check (FAT implementations should not allow >255 character names) if(a > 255) return VFS_SKIP; @@ -1265,6 +1260,7 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) lfn[a+ 9] = lfnInfo->name2[4]; lfn[a+10] = lfnInfo->name2[5]; lfn[a+11] = lfnInfo->name3[0]; lfn[a+12] = lfnInfo->name3[1]; LOG("lfn = '%s'", lfn); + //Log_Debug("FAT", "lfn = '%s'", lfn); LEAVE('p', VFS_SKIP); return VFS_SKIP; } @@ -1275,11 +1271,16 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) LEAVE('p', VFS_SKIP); return VFS_SKIP; } - // Ignore . and .. - if(fileinfo[a].name[0] == '.') { + // Ignore . + if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == ' ') { + LEAVE('p', VFS_SKIP); + return VFS_SKIP; + } + // and .. + if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == '.' && fileinfo[a].name[2] == ' ') { LEAVE('p', VFS_SKIP); return VFS_SKIP; - } + } LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'", fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3], @@ -1288,6 +1289,7 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) #if USE_LFN lfn = FAT_int_GetLFN(Node, ID); + //Log_Debug("FAT", "lfn = %p'%s'", lfn, lfn); ret = FAT_int_CreateName(&fileinfo[a], lfn); #else ret = FAT_int_CreateName(&fileinfo[a], NULL);