Attempting to fix heap corruption
[tpg/acess2.git] / Modules / Filesystems / FAT / fat.c
index e3a681a..26b1961 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
@@ -73,10 +74,10 @@ Uint64      FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
 #endif\r
 // --- Directory IO\r
 char   *FAT_ReadDir(tVFS_Node *Node, int ID);\r
-tVFS_Node      *FAT_FindDir(tVFS_Node *Node, char *Name);\r
+tVFS_Node      *FAT_FindDir(tVFS_Node *Node, const char *Name);\r
 #if SUPPORT_WRITE\r
- int   FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags);\r
- int   FAT_Relink(tVFS_Node *node, char *OldName, char *NewName);\r
+ int   FAT_Mknod(tVFS_Node *Node, const char *Name, Uint Flags);\r
+ int   FAT_Relink(tVFS_Node *node, const char *OldName, const char *NewName);\r
 #endif\r
 void   FAT_CloseFile(tVFS_Node *node);\r
 \r
@@ -393,7 +394,7 @@ Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster)
        Uint32  val = 0;\r
        Uint32  ofs;\r
        ENTER("pDisk xCluster", Disk, cluster);\r
-       LOCK( &Disk->lFAT );\r
+       Mutex_Acquire( &Disk->lFAT );\r
        #if CACHE_FAT\r
        if( Disk->ClusterCount <= giFAT_MaxCachedClusters )\r
        {\r
@@ -420,7 +421,7 @@ Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster)
        #if CACHE_FAT\r
        }\r
        #endif /*CACHE_FAT*/\r
-       RELEASE( &Disk->lFAT );\r
+       Mutex_Release( &Disk->lFAT );\r
        LEAVE('x', val);\r
        return val;\r
 }\r
@@ -920,6 +921,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
@@ -941,6 +946,7 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos)
        tFAT_VolInfo    *disk = Parent->ImplPtr;\r
        \r
        ENTER("pParent pFT", Parent, Entry);\r
+       LOG("disk = %p", disk);\r
        \r
        memset(&node, 0, sizeof(tVFS_Node));\r
        \r
@@ -1098,12 +1104,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
@@ -1240,32 +1241,29 @@ 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) * 13;\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
                LEAVE('p', VFS_SKIP);\r
                return VFS_SKIP;\r
@@ -1303,7 +1301,7 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
  * \fn tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name)\r
  * \brief Finds an entry in the current directory\r
  */\r
-tVFS_Node *FAT_FindDir(tVFS_Node *Node, char *Name)\r
+tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name)\r
 {\r
        fat_filetable   fileinfo[16];\r
        char    tmpName[13];\r

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