FAT - Removed unneeded malloc() calls
[tpg/acess2.git] / Modules / Filesystems / FAT / fat.c
index 25b9cb0..80bf391 100644 (file)
@@ -3,10 +3,15 @@
  * FAT12/16/32 Driver Version (Incl LFN)\r
  * \r
  * NOTE: This driver will only support _reading_ long file names, not\r
- * writing. I don't even know why i'm adding write-support. FAT sucks.\r
+ * writing. I don't even know why I'm adding write-support. FAT sucks.\r
  * \r
  * Known Bugs:\r
  * - LFN Is buggy in FAT_ReadDir\r
+ * \r
+ * Notes:\r
+ * - There's hard-coded 512 byte sectors everywhere, that needs to be\r
+ *   cleaned.\r
+ * - Thread safety is out the window with the write and LFN code\r
  */\r
 /**\r
  * \todo Implement changing of the parent directory when a file is written to\r
@@ -15,9 +20,9 @@
 #define DEBUG  0\r
 #define VERBOSE        1\r
 \r
-#define CACHE_FAT      1       //!< Caches the FAT in memory\r
+#define CACHE_FAT      0       //!< Caches the FAT in memory\r
 #define USE_LFN                1       //!< Enables the use of Long File Names\r
-#define        SUPPORT_WRITE   0\r
+#define        SUPPORT_WRITE   0       //!< Enables write support\r
 \r
 #include <acess.h>\r
 #include <modules.h>\r
 \r
 // === TYPES ===\r
 #if USE_LFN\r
-typedef struct s_lfncache\r
+/**\r
+ * \brief Long-Filename cache entry\r
+ */\r
+typedef struct sFAT_LFNCacheEnt\r
 {\r
-       Uint    Inode;\r
-       tFAT_VolInfo    *Disk;\r
-        int    id;\r
-       char    Name[256];\r
-       struct s_lfncache       *Next;\r
-}      t_lfncache;\r
+        int    ID;\r
+       // TODO: Handle UTF16 names correctly\r
+       char    Data[256];\r
+}      tFAT_LFNCacheEnt;\r
+/**\r
+ * \brief Long-Filename cache\r
+ */\r
+typedef struct sFAT_LFNCache\r
+{\r
+        int    NumEntries;\r
+       tFAT_LFNCacheEnt        Entries[];\r
+}      tFAT_LFNCache;\r
 #endif\r
 \r
 // === 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
-\r
+// --- Helpers\r
+ int   FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Cluster);\r
 Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 Cluster);\r
+#if SUPPORT_WRITE\r
 Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous);\r
-\r
+Uint32 FAT_int_FreeCluster(tFAT_VolInfo *Disk, Uint32 Cluster);\r
+#endif\r
 void   FAT_int_ReadCluster(tFAT_VolInfo *Disk, Uint32 Cluster, int Length, void *Buffer);\r
+// --- File IO\r
 Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);\r
 #if SUPPORT_WRITE\r
 void   FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer);\r
 Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);\r
 #endif\r
-\r
+// --- Directory IO\r
 char   *FAT_ReadDir(tVFS_Node *Node, int ID);\r
-tVFS_Node      *FAT_FindDir(tVFS_Node *Node, char *Name);\r
- int   FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags);\r
- int   FAT_Relink(tVFS_Node *node, char *OldName, char *NewName);\r
+tVFS_Node      *FAT_FindDir(tVFS_Node *Node, const char *Name);\r
+#if SUPPORT_WRITE\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
 // === Options ===\r
@@ -67,15 +88,12 @@ void        FAT_CloseFile(tVFS_Node *node);
 MODULE_DEFINE(0, (0<<8)|50 /*v0.50*/, VFAT, FAT_Install, NULL, NULL);\r
 tFAT_VolInfo   gFAT_Disks[8];\r
  int   giFAT_PartCount = 0;\r
-#if USE_LFN\r
-t_lfncache     *fat_lfncache;\r
-#endif\r
 tVFS_Driver    gFAT_FSInfo = {"fat", 0, FAT_InitDevice, FAT_Unmount, NULL};\r
 \r
 // === CODE ===\r
 /**\r
  * \fn int FAT_Install(char **Arguments)\r
- * \brief \r
+ * \brief Install the FAT Driver\r
  */\r
 int FAT_Install(char **Arguments)\r
 {\r
@@ -84,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
@@ -98,7 +115,7 @@ tVFS_Node *FAT_InitDevice(char *Device, char **Options)
        // Temporary Pointer\r
        bs = &diskInfo->bootsect;\r
        \r
-       //Open device and read boot sector\r
+       // Open device and read boot sector\r
        diskInfo->fileHandle = VFS_Open(Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);\r
        if(diskInfo->fileHandle == -1) {\r
                Log_Notice("FAT", "Unable to open device '%s'", Device);\r
@@ -238,7 +255,8 @@ tVFS_Node *FAT_InitDevice(char *Device, char **Options)
        \r
        // == VFS Interface\r
        node = &diskInfo->rootNode;\r
-       node->Size = bs->files_in_root;\r
+       //node->Size = bs->files_in_root;\r
+       node->Size = -1;\r
        node->Inode = diskInfo->rootOffset;     // 0:31 - Cluster, 32:63 - Parent Directory Cluster\r
        node->ImplPtr = diskInfo;       // Disk info pointer\r
        node->ImplInt = 0;      // 0:15 - Directory Index, 16: Dirty Flag, 17: Deletion Flag\r
@@ -322,10 +340,16 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu
                        if(Cluster)     *Cluster = cluster;\r
                        cluster = FAT_int_GetFatValue(disk, cluster);\r
                        // Check for end of cluster chain\r
-                       if(cluster == -1) {     LEAVE('i', 1);  return 1;}\r
+                       if(cluster == 0xFFFFFFFF) {     LEAVE('i', 1);  return 1;}\r
                }\r
                if(Cluster)     *Cluster = cluster;\r
        }\r
+       else {\r
+               // Increment by clusters in offset\r
+               cluster += Offset / disk->BytesPerCluster;\r
+       }\r
+       \r
+       LOG("cluster = %08x", cluster);\r
        \r
        // Bounds Checking (Used to spot corruption)\r
        if(cluster > disk->ClusterCount + 2)\r
@@ -346,6 +370,7 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu
                addr = disk->firstDataSect * disk->bootsect.bps;\r
                addr += (cluster - 2) * disk->BytesPerCluster;\r
        }\r
+       // In-cluster offset\r
        addr += Offset % disk->BytesPerCluster;\r
        \r
        LOG("addr = 0x%08x", addr);\r
@@ -368,7 +393,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
@@ -382,8 +407,8 @@ Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster)
        #endif\r
                ofs = Disk->bootsect.resvSectCount*512;\r
                if(Disk->type == FAT12) {\r
-                       VFS_ReadAt(Disk->fileHandle, ofs+(cluster>>1)*3, 3, &val);\r
-                       val = (cluster&1 ? val&0xFFF : val>>12);\r
+                       VFS_ReadAt(Disk->fileHandle, ofs+(cluster/2)*3, 3, &val);\r
+                       val = (cluster & 1 ? val>>12 : val & 0xFFF);\r
                        if(val == EOC_FAT12)    val = -1;\r
                } else if(Disk->type == FAT16) {\r
                        VFS_ReadAt(Disk->fileHandle, ofs+cluster*2, 2, &val);\r
@@ -395,7 +420,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
@@ -556,11 +581,13 @@ Uint32 FAT_int_FreeCluster(tFAT_VolInfo *Disk, Uint32 Cluster)
  */\r
 /**\r
  * \brief Read a cluster\r
+ * \param Disk Disk (Volume) to read from\r
+ * \param Length       Length to read\r
+ * \param Buffer       Destination for read data\r
  */\r
 void FAT_int_ReadCluster(tFAT_VolInfo *Disk, Uint32 Cluster, int Length, void *Buffer)\r
 {\r
        ENTER("pDisk xCluster iLength pBuffer", Disk, Cluster, Length, Buffer);\r
-       //Log("Cluster = %i (0x%x)", Cluster, Cluster);\r
        VFS_ReadAt(\r
                Disk->fileHandle,\r
                (Disk->firstDataSect + (Cluster-2)*Disk->bootsect.spc )\r
@@ -579,60 +606,49 @@ void FAT_int_ReadCluster(tFAT_VolInfo *Disk, Uint32 Cluster, int Length, void *B
  * \fn Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)\r
  * \brief Reads data from a specified file\r
  */\r
-Uint64 FAT_Read(tVFS_Node *Node, Uint64 offset, Uint64 length, void *buffer)\r
+Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)\r
 {\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
-       // 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 Inode Field\r
-       cluster = Node->Inode & 0xFFFFFFFF;\r
+       ENTER("pNode Xoffset Xlength pbuffer", Node, Offset, Length, Buffer);\r
        \r
        // Sanity Check offset\r
-       if(offset > Node->Size) {\r
-               LOG("Reading past EOF (%i > %i)", offset, node->Size);\r
+       if(Offset > Node->Size) {\r
+               LOG("Reading past EOF (%i > %i)", Offset, Node->Size);\r
                LEAVE('i', 0);\r
                return 0;\r
        }\r
+       \r
+       // Cluster is stored in the low 32-bits of the Inode field\r
+       cluster = Node->Inode & 0xFFFFFFFF;\r
+       \r
        // Clamp Size\r
-       if(offset + length > Node->Size) {\r
+       if(Offset >= Node->Size || Offset + Length > Node->Size) {\r
                LOG("Reading past EOF (%lli + %lli > %lli), clamped to %lli",\r
-                       offset, length, node->Size, node->Size - offset);\r
-               length = Node->Size - offset;\r
+                       Offset, Length, Node->Size, Node->Size - Offset);\r
+               Length = Node->Size - Offset;\r
        }\r
        \r
-       // Single Cluster including offset\r
-       if(length + offset < bpc)\r
+       // Reading from within the first cluster only?\r
+       if((int)Offset + (int)Length < bpc)\r
        {\r
+               LOG("First cluster only");\r
                FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf);\r
-               memcpy( buffer, (void*)( tmpBuf + offset%bpc ), length );\r
-               free(tmpBuf);\r
+               memcpy( Buffer, (void*)( tmpBuf + Offset%bpc ), Length );\r
+               #if DEBUG\r
+               //Debug_HexDump("FAT_Read", Buffer, Length);\r
+               #endif\r
                LEAVE('i', 1);\r
-               return length;\r
-       }\r
-       \r
-       #if 0\r
-       if( FAT_int_GetAddress(Node, offset, &addr) )\r
-       {\r
-               Log_Warning("FAT", "Offset is past end of cluster chain mark");\r
-               LEAVE('i', 0);\r
-               return 0;\r
+               return Length;\r
        }\r
-       #endif\r
-       \r
-       preSkip = offset / bpc;\r
        \r
-       //Skip previous clusters\r
-       for(i=preSkip;i--;)     {\r
+       // Skip previous clusters\r
+       preSkip = Offset / bpc;\r
+       for(i = preSkip; i--; ) {\r
                cluster = FAT_int_GetFatValue(disk, cluster);\r
                if(cluster == -1) {\r
                        Log_Warning("FAT", "Offset is past end of cluster chain mark");\r
@@ -642,58 +658,78 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 offset, Uint64 length, void *buffer)
        }\r
        \r
        // Get Count of Clusters to read\r
-       count = ((offset%bpc+length) / bpc) + 1;\r
+       count = ((Offset%bpc + Length) / bpc) + 1;\r
        \r
        // Get buffer Position after 1st cluster\r
-       pos = bpc - offset%bpc;\r
-       \r
-       // Read 1st Cluster\r
-       FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf);\r
-       memcpy(\r
-               buffer,\r
-               (void*)( tmpBuf + (bpc-pos) ),\r
-               (pos < length ? pos : length)\r
-               );\r
+       pos = bpc - Offset%bpc;\r
        \r
-       if (count == 1) {\r
-               free(tmpBuf);\r
-               LEAVE('i', 1);\r
-               return length;\r
+       // Read 1st Cluster (performs alignment for us)\r
+       if( pos == bpc && (int)Length >= bpc ) {\r
+               FAT_int_ReadCluster(disk, cluster, bpc, Buffer);\r
+       }\r
+       else {\r
+               FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf);\r
+               memcpy(\r
+                       Buffer,\r
+                       (void*)( tmpBuf + (bpc-pos) ),\r
+                       (pos < (int)Length ? (Uint)pos : Length)\r
+                       );\r
        }\r
        \r
-       cluster = FAT_int_GetFatValue(disk, cluster);\r
+       // Simple return\r
+       if( count == 1 ) {\r
+               #if DEBUG\r
+               //Debug_HexDump("FAT_Read", Buffer, Length);\r
+               #endif\r
+               LEAVE('i', 1);\r
+               return Length;\r
+       }\r
        \r
        #if DEBUG\r
        LOG("pos = %i", pos);\r
        LOG("Reading the rest of the clusters");\r
        #endif\r
        \r
-       \r
        // Read the rest of the cluster data\r
        for( i = 1; i < count-1; i++ )\r
        {\r
-               FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf);\r
-               memcpy((void*)(buffer+pos), tmpBuf, bpc);\r
-               pos += bpc;\r
+               // 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
+               // Read cluster\r
+               FAT_int_ReadCluster(disk, cluster, bpc, (void*)(Buffer+pos));\r
+               pos += bpc;\r
        }\r
        \r
-       FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf);\r
-       memcpy((void*)(buffer+pos), tmpBuf, length-pos);\r
+       // Get next cluster in the chain\r
+       cluster = FAT_int_GetFatValue(disk, cluster);\r
+       if(cluster == -1) {\r
+               Log_Warning("FAT", "FAT_Read: Read past End of Cluster Chain");\r
+               LEAVE('i', 0);\r
+               return 0;\r
+       }\r
+       \r
+       // Read final cluster\r
+       if( (int)Length - pos == bpc )\r
+       {\r
+               FAT_int_ReadCluster( disk, cluster, bpc, (void*)(Buffer+pos) );\r
+       }\r
+       else {\r
+               FAT_int_ReadCluster( disk, cluster, bpc, tmpBuf );\r
+               memcpy( (void*)(Buffer+pos), tmpBuf, Length-pos );\r
+       }\r
        \r
        #if DEBUG\r
        LOG("Free tmpBuf(0x%x) and Return", tmpBuf);\r
+       //Debug_HexDump("FAT_Read", Buffer, Length);\r
        #endif\r
        \r
-       free(tmpBuf);\r
-       LEAVE('X', length);\r
-       return length;\r
+       LEAVE('X', Length);\r
+       return Length;\r
 }\r
 \r
 #if SUPPORT_WRITE\r
@@ -723,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
@@ -751,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
@@ -824,52 +855,60 @@ Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
  * ====================\r
  */\r
 /**\r
- * \fn void FAT_int_ProperFilename(char *dest, char *src)\r
  * \brief Converts a FAT directory entry name into a proper filename\r
+ * \param dest Destination array (must be at least 13 bytes in size)\r
+ * \param src  8.3 filename (concatenated, e.g 'FILE1   TXT')\r
  */\r
-void FAT_int_ProperFilename(char *dest, char *src)\r
+void FAT_int_ProperFilename(char *dest, const char *src)\r
 {\r
-        int    a, b;\r
+        int    inpos, outpos;\r
        \r
-       for( a = 0; a < 8; a++) {\r
-               if(src[a] == ' ')       break;\r
-               dest[a] = src[a];\r
+       // Name\r
+       outpos = 0;\r
+       for( inpos = 0; inpos < 8; inpos++ ) {\r
+               if(src[inpos] == ' ')   break;\r
+               dest[outpos++] = src[inpos];\r
        }\r
-       b = a;\r
-       a = 8;\r
+       inpos = 8;\r
+       // Check for empty extensions\r
        if(src[8] != ' ')\r
-               dest[b++] = '.';\r
-       for( ; a < 11; a++, b++)        {\r
-               if(src[a] == ' ')       break;\r
-               dest[b] = src[a];\r
+       {\r
+               dest[outpos++] = '.';\r
+               for( ; inpos < 11; inpos++)     {\r
+                       if(src[inpos] == ' ')   break;\r
+                       dest[outpos++] = src[inpos];\r
+               }\r
        }\r
-       dest[b] = '\0';\r
-       #if DEBUG\r
+       dest[outpos++] = '\0';\r
+       \r
        //LOG("dest='%s'", dest);\r
-       #endif\r
 }\r
 \r
 /**\r
  * \fn char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)\r
  * \brief Converts either a LFN or a 8.3 Name into a proper name\r
+ * \param ft   Pointer to the file's entry in the parent directory\r
+ * \param LongFileName Long file name pointer\r
+ * \return Filename as a heap string\r
  */\r
 char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)\r
 {\r
        char    *ret;\r
-        int    len;\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
-               len = strlen(LongFileName);\r
-               ret = malloc(len+1);\r
-               strcpy(ret, LongFileName);\r
+               ret = strdup(LongFileName);\r
        }\r
        else\r
        {\r
        #endif\r
                ret = (char*) malloc(13);\r
-               memset(ret, 13, '\0');\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
@@ -879,24 +918,33 @@ char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName)
 }\r
 \r
 /**\r
- * \fn tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft)\r
  * \brief Creates a tVFS_Node structure for a given file entry\r
+ * \param Parent       Parent directory VFS node\r
+ * \param Entry        File table entry for the new node\r
+ * \param Pos  Position in the parent of the new node\r
  */\r
 tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos)\r
 {\r
-       tVFS_Node       node = {0};\r
+       tVFS_Node       node;\r
        tVFS_Node       *ret;\r
        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
        // Set Other Data\r
+       // 0-31: Cluster, 32-63: Parent Cluster\r
        node.Inode = Entry->cluster | (Entry->clusterHi<<16) | (Parent->Inode << 32);\r
        LOG("node.Inode = %llx", node.Inode);\r
+       // Position in parent directory\r
        node.ImplInt = Pos & 0xFFFF;\r
+       // Disk Pointer\r
        node.ImplPtr = disk;\r
        node.Size = Entry->size;\r
        LOG("Entry->size = %i", Entry->size);\r
+       // root:root\r
        node.UID = 0;   node.GID = 0;\r
        node.NumACLs = 1;\r
        \r
@@ -910,6 +958,7 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos)
                node.ACLs = &gVFS_ACL_EveryoneRWX;      // RWXRWXRWX\r
        }\r
        \r
+       // Create timestamps\r
        node.ATime = timestamp(0,0,0,\r
                        ((Entry->adate&0x1F) - 1),      // Days\r
                        ((Entry->adate&0x1E0) - 1),     // Months\r
@@ -935,6 +984,7 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos)
                        1980+((Entry->mdate&0xFF00)>>8) // Years\r
                        );\r
        \r
+       // Set pointers\r
        if(node.Flags & VFS_FFLAG_DIRECTORY) {\r
                //Log_Debug("FAT", "Directory %08x has size 0x%x", node.Inode, node.Size);\r
                node.ReadDir = FAT_ReadDir;\r
@@ -944,7 +994,8 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos)
                node.Relink = FAT_Relink;\r
                #endif\r
                node.Size = -1;\r
-       } else {\r
+       }\r
+       else {\r
                node.Read = FAT_Read;\r
                #if SUPPORT_WRITE\r
                node.Write = FAT_Write;\r
@@ -957,13 +1008,17 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos)
        return ret;\r
 }\r
 \r
-/* ====================\r
+/* \r
+ * ====================\r
  *     Directory IO\r
  * ====================\r
  */\r
 \r
 /**\r
  * \brief Reads a sector from the disk\r
+ * \param Node Directory node to read\r
+ * \param Sector       Sector number in the directory to read\r
+ * \param Buffer       Destination buffer for the read data\r
  */\r
 int FAT_int_ReadDirSector(tVFS_Node *Node, int Sector, fat_filetable *Buffer)\r
 {\r
@@ -972,12 +1027,14 @@ int FAT_int_ReadDirSector(tVFS_Node *Node, int Sector, fat_filetable *Buffer)
        \r
        ENTER("pNode iSector pEntry", Node, Sector, Buffer);\r
        \r
+       // Parse address\r
        if(FAT_int_GetAddress(Node, Sector * 512, &addr, NULL))\r
        {\r
                LEAVE('i', 1);\r
                return 1;\r
        }\r
        \r
+       LOG("addr = 0x%llx", addr);\r
        // Read Sector\r
        if(VFS_ReadAt(disk->fileHandle, addr, 512, Buffer) != 512)\r
        {\r
@@ -993,6 +1050,9 @@ int FAT_int_ReadDirSector(tVFS_Node *Node, int Sector, fat_filetable *Buffer)
 /**\r
  * \brief Writes an entry to the disk\r
  * \todo Support expanding a directory\r
+ * \param Node Directory node\r
+ * \param ID   ID of entry to update\r
+ * \param Entry        Entry data\r
  * \return Zero on success, non-zero on error\r
  */\r
 int FAT_int_WriteDirEntry(tVFS_Node *Node, int ID, fat_filetable *Entry)\r
@@ -1028,65 +1088,96 @@ 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
+ * \param Node Directory node\r
+ * \param ID   ID of the short name\r
+ * \return Pointer to the LFN cache entry\r
  */\r
-char *FAT_int_GetLFN(tVFS_Node *node)\r
+char *FAT_int_GetLFN(tVFS_Node *Node, int ID)\r
 {\r
-       t_lfncache      *tmp;\r
-       tmp = fat_lfncache;\r
-       while(tmp)\r
+       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
+       // Create a cache if it isn't there\r
+       if(!cache) {\r
+               cache = Node->Data = malloc( sizeof(tFAT_LFNCache) + sizeof(tFAT_LFNCacheEnt) );\r
+               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 this entry\r
+       firstFree = -1;\r
+       for( i = 0; i < cache->NumEntries; i++ )\r
        {\r
-               if(tmp->Inode == node->Inode && tmp->Disk == node->ImplPtr)\r
-                       return tmp->Name;\r
-               tmp = tmp->Next;\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
-       tmp = malloc(sizeof(t_lfncache));\r
-       tmp->Inode = node->Inode;\r
-       tmp->Disk = node->ImplPtr;\r
-       memset(tmp->Name, 0, 256);\r
        \r
-       tmp->Next = fat_lfncache;\r
-       fat_lfncache = tmp;\r
+       if(firstFree == -1) {\r
+               // Use `i` for temp length\r
+               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", "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
+       }\r
+       else {\r
+               i = firstFree;\r
+       }\r
+       \r
+       // Create new entry\r
+       cache->Entries[ i ].ID = ID;\r
+       cache->Entries[ i ].Data[0] = '\0';\r
        \r
-       return tmp->Name;\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
 /**\r
  * \fn void FAT_int_DelLFN(tVFS_Node *node)\r
  * \brief Delete a LFN cache entry\r
+ * \param Node Directory node\r
+ * \param ID   File Entry ID\r
  */\r
-void FAT_int_DelLFN(tVFS_Node *node)\r
+void FAT_int_DelLFN(tVFS_Node *Node, int ID)\r
 {\r
-       t_lfncache      *tmp;\r
+       tFAT_LFNCache   *cache = Node->Data;\r
+        int    i;\r
        \r
-       if(!fat_lfncache)       return;\r
+       // Fast return\r
+       if(!cache)      return;\r
        \r
-       if(!fat_lfncache->Next)\r
+       // Scan for a current entry\r
+       for( i = 0; i < cache->NumEntries; i++ )\r
        {\r
-               tmp = fat_lfncache;\r
-               fat_lfncache = tmp->Next;\r
-               free(tmp);\r
-               return;\r
-       }\r
-       tmp = fat_lfncache;\r
-       while(tmp && tmp->Next)\r
-       {\r
-               if(tmp->Inode == node->Inode && tmp->Disk == node->ImplPtr)\r
-               {\r
-                       free(tmp->Next);\r
-                       tmp->Next = tmp->Next->Next;\r
-                       return;\r
-               }\r
-               tmp = tmp->Next;\r
+               if( cache->Entries[i].ID == ID )\r
+                       cache->Entries[i].ID = -1;\r
        }\r
+       return ;\r
 }\r
 #endif\r
 \r
@@ -1094,11 +1185,12 @@ void FAT_int_DelLFN(tVFS_Node *node)
  * \fn char *FAT_ReadDir(tVFS_Node *Node, int ID)\r
  * \param Node Node structure of directory\r
  * \param ID   Directory position\r
+ * \return Filename as a heap string, NULL or VFS_SKIP\r
  */\r
 char *FAT_ReadDir(tVFS_Node *Node, int ID)\r
 {\r
-       fat_filetable   fileinfo[16];   //Sizeof=32, 16 per sector\r
-        int    a=0;\r
+       fat_filetable   fileinfo[16];   // sizeof(fat_filetable)=32, so 16 per sector\r
+        int    a = 0;\r
        char    *ret;\r
        #if USE_LFN\r
        char    *lfn = NULL;\r
@@ -1108,6 +1200,7 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
        \r
        if(FAT_int_ReadDirSector(Node, ID/16, fileinfo))\r
        {\r
+               LOG("End of chain, end of dir");\r
                LEAVE('n');\r
                return NULL;\r
        }\r
@@ -1115,9 +1208,7 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
        // Offset in sector\r
        a = ID % 16;\r
 \r
-       LOG("a = %i", a);\r
-       \r
-       LOG("name[0] = 0x%x", (Uint8)fileinfo[a].name[0]);\r
+       LOG("fileinfo[%i].name[0] = 0x%x", a, (Uint8)fileinfo[a].name[0]);\r
        \r
        // Check if this is the last entry\r
        if( fileinfo[a].name[0] == '\0' ) {\r
@@ -1130,52 +1221,66 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
        // Check for empty entry\r
        if( (Uint8)fileinfo[a].name[0] == 0xE5 ) {\r
                LOG("Empty Entry");\r
+               #if 0   // Stop on empty entry?\r
+               LEAVE('n');\r
+               return NULL;    // Stop\r
+               #else\r
                LEAVE('p', VFS_SKIP);\r
                return VFS_SKIP;        // Skip\r
+               #endif\r
        }\r
        \r
        #if USE_LFN\r
        // Get Long File Name Cache\r
-       lfn = FAT_int_GetLFN(Node);\r
        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
-               // Get the current length\r
-               len = strlen(lfn);\r
                \r
-               // Sanity Check (FAT implementations should not allow >255 bytes)\r
-               if(len + 13 > 255)      return VFS_SKIP;\r
-               // Rebase all bytes\r
-               for(a=len+1;a--;)       lfn[a+13] = lfn[a];\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(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
        #endif\r
        \r
-       //Check if it is a volume entry\r
+       // Check if it is a volume entry\r
        if(fileinfo[a].attrib & 0x08) {\r
                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
@@ -1183,8 +1288,9 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID)
                fileinfo[a].name[8], fileinfo[a].name[9], fileinfo[a].name[10] );\r
        \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
-       lfn[0] = '\0';\r
        #else\r
        ret = FAT_int_CreateName(&fileinfo[a], NULL);\r
        #endif\r
@@ -1197,7 +1303,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
@@ -1236,7 +1342,7 @@ tVFS_Node *FAT_FindDir(tVFS_Node *Node, char *Name)
                \r
                #if USE_LFN\r
                // Long File Name Entry\r
-               if(fileinfo[i&0xF].attrib == ATTR_LFN)\r
+               if(fileinfo[i & 0xF].attrib == ATTR_LFN)\r
                {\r
                        lfnInfo = (fat_longfilename *) &fileinfo[i&0xF];\r
                        if(lfnInfo->id & 0x40) {\r
@@ -1260,6 +1366,8 @@ tVFS_Node *FAT_FindDir(tVFS_Node *Node, char *Name)
                {\r
                        // Remove LFN if it does not apply\r
                        if(lfnId != i)  lfn[0] = '\0';\r
+               #else\r
+               if(fileinfo[i&0xF].attrib == ATTR_LFN)  continue;\r
                #endif\r
                        // Get Real Filename\r
                        FAT_int_ProperFilename(tmpName, fileinfo[i&0xF].name);\r
@@ -1351,7 +1459,7 @@ void FAT_CloseFile(tVFS_Node *Node)
        #if SUPPORT_WRITE\r
        // Update the node if it's dirty (don't bother if it's marked for\r
        // deletion)\r
-       if( Node->ImplInt & FAT_FLAG_DIRTY && !(Node->ImplInt & FAT_FLAG_DELETE) )\r
+       if( (Node->ImplInt & FAT_FLAG_DIRTY) && !(Node->ImplInt & FAT_FLAG_DELETE) )\r
        {\r
                tFAT_VolInfo    buf[16];\r
                tFAT_VolInfo    *ft = &buf[ (Node->ImplInt & 0xFFFF) % 16 ];\r
@@ -1368,13 +1476,7 @@ void FAT_CloseFile(tVFS_Node *Node)
        // 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
-               // Delete LFN Cache\r
-               #if USE_LFN\r
-               if(     Node->Flags & VFS_FFLAG_DIRECTORY)\r
-                       FAT_int_DelLFN(Node);\r
-               #endif\r
-               \r
+       {               \r
                #if SUPPORT_WRITE\r
                // Delete File\r
                if( Node->ImplInt & FAT_FLAG_DELETE ) {\r

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