FAT - Removed unneeded malloc() calls
[tpg/acess2.git] / Modules / Filesystems / FAT / fat.c
index 7eab093..80bf391 100644 (file)
@@ -40,6 +40,7 @@
 typedef struct sFAT_LFNCacheEnt\r
 {\r
         int    ID;\r
+       // TODO: Handle UTF16 names correctly\r
        char    Data[256];\r
 }      tFAT_LFNCacheEnt;\r
 /**\r
@@ -55,7 +56,7 @@ typedef struct sFAT_LFNCache
 // === PROTOTYPES ===\r
 // --- Driver Core\r
  int   FAT_Install(char **Arguments);\r
-tVFS_Node      *FAT_InitDevice(char *device, char **options);\r
+tVFS_Node      *FAT_InitDevice(const char *device, const char **options);\r
 void   FAT_Unmount(tVFS_Node *Node);\r
 // --- Helpers\r
  int   FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Cluster);\r
@@ -101,10 +102,9 @@ int FAT_Install(char **Arguments)
 }\r
 \r
 /**\r
- * \fn tVFS_Node *FAT_InitDevice(char *Device, char **Options)\r
  * \brief Reads the boot sector of a disk and prepares the structures for it\r
  */\r
-tVFS_Node *FAT_InitDevice(char *Device, char **Options)\r
+tVFS_Node *FAT_InitDevice(const char *Device, const char **Options)\r
 {\r
        fat_bootsect *bs;\r
         int    i;\r
@@ -610,9 +610,9 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
 {\r
         int    preSkip, count;\r
         int    i, cluster, pos;\r
-        int    bpc;\r
-       void    *tmpBuf;\r
        tFAT_VolInfo    *disk = Node->ImplPtr;\r
+       char    tmpBuf[disk->BytesPerCluster];\r
+        int    bpc = disk->BytesPerCluster;\r
        \r
        ENTER("pNode Xoffset Xlength pbuffer", Node, Offset, Length, Buffer);\r
        \r
@@ -623,11 +623,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                return 0;\r
        }\r
        \r
-       // Calculate and Allocate Bytes Per Cluster\r
-       bpc = disk->BytesPerCluster;\r
-       tmpBuf = (void*) malloc(bpc);\r
-       if( !tmpBuf )   return 0;\r
-       \r
        // Cluster is stored in the low 32-bits of the Inode field\r
        cluster = Node->Inode & 0xFFFFFFFF;\r
        \r
@@ -644,7 +639,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                LOG("First cluster only");\r
                FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf);\r
                memcpy( Buffer, (void*)( tmpBuf + Offset%bpc ), Length );\r
-               free(tmpBuf);\r
                #if DEBUG\r
                //Debug_HexDump("FAT_Read", Buffer, Length);\r
                #endif\r
@@ -658,7 +652,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                cluster = FAT_int_GetFatValue(disk, cluster);\r
                if(cluster == -1) {\r
                        Log_Warning("FAT", "Offset is past end of cluster chain mark");\r
-                       free(tmpBuf);\r
                        LEAVE('i', 0);\r
                        return 0;\r
                }\r
@@ -688,7 +681,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                #if DEBUG\r
                //Debug_HexDump("FAT_Read", Buffer, Length);\r
                #endif\r
-               free(tmpBuf);\r
                LEAVE('i', 1);\r
                return Length;\r
        }\r
@@ -704,8 +696,7 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
                // Get next cluster in the chain\r
                cluster = FAT_int_GetFatValue(disk, cluster);\r
                if(cluster == -1) {\r
-                       Warning("FAT_Read - Read past End of Cluster Chain");\r
-                       free(tmpBuf);\r
+                       Log_Warning("FAT", "FAT_Read: Read past End of Cluster Chain");\r
                        LEAVE('i', 0);\r
                        return 0;\r
                }\r
@@ -717,8 +708,7 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
        // Get next cluster in the chain\r
        cluster = FAT_int_GetFatValue(disk, cluster);\r
        if(cluster == -1) {\r
-               Warning("FAT_Read - Read past End of Cluster Chain");\r
-               free(tmpBuf);\r
+               Log_Warning("FAT", "FAT_Read: Read past End of Cluster Chain");\r
                LEAVE('i', 0);\r
                return 0;\r
        }\r
@@ -738,7 +728,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
        //Debug_HexDump("FAT_Read", Buffer, Length);\r
        #endif\r
        \r
-       free(tmpBuf);\r
        LEAVE('X', Length);\r
        return Length;\r
 }\r
@@ -770,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)\r
 {\r
        tFAT_VolInfo    *disk = Node->ImplPtr;\r
-       void    *tmpBuf;\r
+       char    tmpBuf[disk->BytesPerCluster];\r
         int    remLength = Length;\r
        Uint32  cluster, tmpCluster;\r
         int    bNewCluster = 0;\r
@@ -798,29 +787,24 @@ Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
        \r
        if( Offset + Length < disk->BytesPerCluster )\r
        {\r
-               tmpBuf = malloc( disk->BytesPerCluster );\r
+               char    tmpBuf[disk->BytesPerCluster];\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
-               free(tmpBuf);\r
                return Length;\r
        }\r
        \r
        // Clean up changes within a cluster\r
        if( Offset )\r
-       {\r
-               tmpBuf = malloc( disk->BytesPerCluster );\r
-               \r
+       {       \r
                // Read-Modify-Write\r
                FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
                memcpy( tmpBuf + Offset, Buffer, disk->BytesPerCluster - Offset );\r
                FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
                \r
-               free(tmpBuf);\r
-               \r
                remLength -= disk->BytesPerCluster - Offset;\r
                Buffer += disk->BytesPerCluster - Offset;\r
                \r
@@ -911,6 +895,7 @@ char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)
 {\r
        char    *ret;\r
        ENTER("pft sLongFileName", ft, LongFileName);\r
+       //Log_Debug("FAT", "FAT_int_CreateName(ft=%p, LongFileName=%p'%s')", ft, LongFileName);\r
        #if USE_LFN\r
        if(LongFileName && LongFileName[0] != '\0')\r
        {       \r
@@ -920,6 +905,10 @@ char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)
        {\r
        #endif\r
                ret = (char*) malloc(13);\r
+               if( !ret ) {\r
+                       Log_Warning("FAT", "FAT_int_CreateName: malloc(13) failed");\r
+                       return NULL;\r
+               }\r
                FAT_int_ProperFilename(ret, ft->name);\r
        #if USE_LFN\r
        }\r
@@ -1099,12 +1088,7 @@ int FAT_int_WriteDirEntry(tVFS_Node *Node, int ID, fat_filetable *Entry)
 }\r
 #endif\r
 \r
-#if USE_LFN\r
-// I should probably more tightly associate the LFN cache with the node\r
-// somehow, maybe by adding a field to tVFS_Node before locking it\r
-// Maybe .Cache or something like that (something that is free'd by the\r
-// Inode_UncacheNode function)\r
-       \r
+#if USE_LFN    \r
 /**\r
  * \fn char *FAT_int_GetLFN(tVFS_Node *node)\r
  * \brief Return pointer to LFN cache entry\r
@@ -1117,6 +1101,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
        tFAT_LFNCache   *cache;\r
         int    i, firstFree;\r
        \r
+       Mutex_Acquire( &Node->Lock );\r
+       \r
        // TODO: Thread Safety (Lock things)\r
        cache = Node->Data;\r
        \r
@@ -1126,15 +1112,20 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
                cache->NumEntries = 1;\r
                cache->Entries[0].ID = ID;\r
                cache->Entries[0].Data[0] = '\0';\r
+               Mutex_Release( &Node->Lock );\r
+               //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data);\r
                return cache->Entries[0].Data;\r
        }\r
        \r
-       // Scan for a current entry\r
+       // Scan for this entry\r
        firstFree = -1;\r
        for( i = 0; i < cache->NumEntries; i++ )\r
        {\r
-               if( cache->Entries[i].ID == ID )\r
+               if( cache->Entries[i].ID == ID ) {\r
+                       Mutex_Release( &Node->Lock );\r
+                       //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data);\r
                        return cache->Entries[i].Data;\r
+               }\r
                if( cache->Entries[i].ID == -1 && firstFree == -1 )\r
                        firstFree = i;\r
        }\r
@@ -1144,9 +1135,11 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
                i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt);\r
                Node->Data = realloc( Node->Data, i );\r
                if( !Node->Data ) {\r
-                       Log_Error("FAT", "malloc() fail, unable to allocate %i for LFN cache", i);\r
+                       Log_Error("FAT", "realloc() fail, unable to allocate %i for LFN cache", i);\r
+                       Mutex_Release( &Node->Lock );\r
                        return NULL;\r
                }\r
+               //Log_Debug("FAT", "Realloc (%i)\n", i);\r
                cache = Node->Data;\r
                i = cache->NumEntries;\r
                cache->NumEntries ++;\r
@@ -1159,7 +1152,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID)
        cache->Entries[ i ].ID = ID;\r
        cache->Entries[ i ].Data[0] = '\0';\r
        \r
-       //TODO: Unlock\r
+       Mutex_Release( &Node->Lock );\r
+       //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i);\r
        return cache->Entries[ i ].Data;\r
 }\r
 \r
@@ -1241,33 +1235,32 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
        if(fileinfo[a].attrib == ATTR_LFN)\r
        {\r
                fat_longfilename        *lfnInfo;\r
-                int    len;\r
                \r
                lfnInfo = (fat_longfilename *) &fileinfo[a];\r
                \r
                // Get cache for corresponding file\r
+               // > ID + Index gets the corresponding short node\r
                lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) );\r
                \r
                // Bit 6 indicates the start of an entry\r
                if(lfnInfo->id & 0x40)  memset(lfn, 0, 256);\r
                \r
-               // Get the current length\r
-               len = strlen(lfn);\r
+               a = ((lfnInfo->id & 0x3F) - 1) * 13;\r
+               //Log_Debug("FAT", "ID = 0x%02x, a = %i", lfnInfo->id, a);\r
                \r
                // Sanity Check (FAT implementations should not allow >255 character names)\r
-               if(len + 13 > 255)      return VFS_SKIP;\r
-               // Rebase all bytes\r
-               for(a=len+1;a--;)       lfn[a+13] = lfn[a];\r
+               if(a > 255)     return VFS_SKIP;\r
                \r
                // Append new bytes\r
-               lfn[ 0] = lfnInfo->name1[0];    lfn[ 1] = lfnInfo->name1[1];\r
-               lfn[ 2] = lfnInfo->name1[2];    lfn[ 3] = lfnInfo->name1[3];\r
-               lfn[ 4] = lfnInfo->name1[4];    \r
-               lfn[ 5] = lfnInfo->name2[0];    lfn[ 6] = lfnInfo->name2[1];\r
-               lfn[ 7] = lfnInfo->name2[2];    lfn[ 8] = lfnInfo->name2[3];\r
-               lfn[ 9] = lfnInfo->name2[4];    lfn[10] = lfnInfo->name2[5];\r
-               lfn[11] = lfnInfo->name3[0];    lfn[12] = lfnInfo->name3[1];\r
+               lfn[a+ 0] = lfnInfo->name1[0];  lfn[a+ 1] = lfnInfo->name1[1];\r
+               lfn[a+ 2] = lfnInfo->name1[2];  lfn[a+ 3] = lfnInfo->name1[3];\r
+               lfn[a+ 4] = lfnInfo->name1[4];  \r
+               lfn[a+ 5] = lfnInfo->name2[0];  lfn[a+ 6] = lfnInfo->name2[1];\r
+               lfn[a+ 7] = lfnInfo->name2[2];  lfn[a+ 8] = lfnInfo->name2[3];\r
+               lfn[a+ 9] = lfnInfo->name2[4];  lfn[a+10] = lfnInfo->name2[5];\r
+               lfn[a+11] = lfnInfo->name3[0];  lfn[a+12] = lfnInfo->name3[1];\r
                LOG("lfn = '%s'", lfn);\r
+               //Log_Debug("FAT", "lfn = '%s'", lfn);\r
                LEAVE('p', VFS_SKIP);\r
                return VFS_SKIP;\r
        }\r
@@ -1278,11 +1271,16 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
                LEAVE('p', VFS_SKIP);\r
                return VFS_SKIP;\r
        }\r
-       // Ignore . and ..\r
-       if(fileinfo[a].name[0] == '.') {\r
+       // Ignore .\r
+       if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == ' ') {\r
                LEAVE('p', VFS_SKIP);\r
                return VFS_SKIP;\r
-       }       \r
+       }\r
+       // and ..\r
+       if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == '.' && fileinfo[a].name[2] == ' ') {\r
+               LEAVE('p', VFS_SKIP);\r
+               return VFS_SKIP;\r
+       }\r
        \r
        LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'",\r
                fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3],\r
@@ -1291,6 +1289,7 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
        \r
        #if USE_LFN\r
        lfn = FAT_int_GetLFN(Node, ID);\r
+       //Log_Debug("FAT", "lfn = %p'%s'", lfn, lfn);\r
        ret = FAT_int_CreateName(&fileinfo[a], lfn);\r
        #else\r
        ret = FAT_int_CreateName(&fileinfo[a], NULL);\r

UCC git Repository :: git.ucc.asn.au