Cut down on debug, fixed tabs, made process tree be killed when root is killed
[tpg/acess2.git] / Kernel / vfs / fs / fat.c
index c4f6671..bfae405 100644 (file)
@@ -39,7 +39,7 @@ typedef struct s_lfncache {
 // === PROTOTYPES ===\r
  int   FAT_Install(char **Arguments);\r
 tVFS_Node      *FAT_InitDevice(char *device, char *options);\r
-void   FAT_CloseDevice(tVFS_Node *node);\r
+void   FAT_Unmount(tVFS_Node *Node);\r
 Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer);\r
 Uint64 FAT_Write(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer);\r
 char   *FAT_ReadDir(tVFS_Node *dirNode, int dirpos);\r
@@ -59,7 +59,7 @@ Uint32        *fat_cache[8];
 t_lfncache     *fat_lfncache;\r
 #endif\r
 tVFS_Driver    gFAT_FSInfo = {\r
-       "fat", 0, FAT_InitDevice, NULL\r
+       "fat", 0, FAT_InitDevice, FAT_Unmount, NULL\r
        };\r
 \r
 // === CODE ===\r
@@ -73,7 +73,9 @@ int FAT_Install(char **Arguments)
        return 0;\r
 }\r
 \r
-/* Reads the boot sector of a disk and prepares the structures for it\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
 {\r
@@ -146,7 +148,6 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options)
        #endif\r
        \r
        //Get Name\r
-       //puts(" Name: ");\r
        if(diskInfo->type == FAT32) {\r
                for(i=0;i<11;i++)\r
                        diskInfo->name[i] = (bs->spec.fat32.label[i] == ' ' ? '\0' : bs->spec.fat32.label[i]);\r
@@ -156,7 +157,6 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options)
                        diskInfo->name[i] = (bs->spec.fat16.label[i] == ' ' ? '\0' : bs->spec.fat16.label[i]);\r
        }\r
        diskInfo->name[11] = '\0';\r
-       //puts(diskInfo->name); putch('\n');\r
        \r
        //Compute Root directory offset\r
        if(diskInfo->type == FAT32)\r
@@ -224,16 +224,15 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options)
                Log(" FAT_InitDisk: Inode Cache handle is %i\n", gFAT_Disks[giFAT_PartCount].inodeHandle);\r
        #endif\r
        \r
-       //== VFS Interface\r
+       // == VFS Interface\r
        node = &gFAT_Disks[giFAT_PartCount].rootNode;\r
-       //node->Name = gFAT_Disks[giFAT_PartCount].name;\r
        node->Inode = diskInfo->rootOffset;\r
-       node->Size = bs->files_in_root; //Unknown - To be set on readdir\r
+       node->Size = bs->files_in_root; // Unknown - To be set on readdir\r
        node->ImplInt = giFAT_PartCount;\r
        \r
        node->ReferenceCount = 1;\r
        \r
-       node->UID = 0;  node->GID= 0;\r
+       node->UID = 0;  node->GID = 0;\r
        node->NumACLs = 1;\r
        node->ACLs = &gVFS_ACL_EveryoneRWX;\r
        node->Flags = VFS_FFLAG_DIRECTORY;\r
@@ -244,26 +243,24 @@ tVFS_Node *FAT_InitDevice(char *Device, char *options)
        node->FindDir = FAT_FindDir;\r
        node->Relink = FAT_Relink;\r
        node->MkNod = FAT_Mknod;\r
-       node->Close = FAT_CloseDevice;\r
+       //node->Close = FAT_CloseDevice;\r
        \r
        giFAT_PartCount ++;\r
        return node;\r
 }\r
 \r
 /**\r
- * \fn void FAT_CloseDevice(tVFS_Node *node)\r
+ * \fn void FAT_Unmount(tVFS_Node *Node)\r
  * \brief Closes a mount and marks it as free\r
  */\r
-void FAT_CloseDevice(tVFS_Node *node)\r
+void FAT_Unmount(tVFS_Node *Node)\r
 {\r
-       node->ReferenceCount --;\r
-       \r
-       if(node->ReferenceCount > 0)    return;\r
-       \r
        // Close Disk Handle\r
-       VFS_Close( gFAT_Disks[node->ImplInt].fileHandle );\r
-       Inode_ClearCache(gFAT_Disks[node->ImplInt].inodeHandle);\r
-       gFAT_Disks[node->ImplInt].fileHandle = -2;\r
+       VFS_Close( gFAT_Disks[Node->ImplInt].fileHandle );\r
+       // Clear Node Cache\r
+       Inode_ClearCache(gFAT_Disks[Node->ImplInt].inodeHandle);\r
+       // Mark as unused\r
+       gFAT_Disks[Node->ImplInt].fileHandle = -2;\r
        return;\r
 }\r
 \r
@@ -345,6 +342,18 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
                return 0;\r
        }\r
        \r
+       // Sanity Check offset\r
+       if(offset > node->Size) {\r
+               //Log("FAT_Read: Reading past EOF (%i > %i)", offset, node->Size);\r
+               return 0;\r
+       }\r
+       // Clamp Size\r
+       if(offset + length > node->Size) {\r
+               //Log("FAT_Read: Reading past EOF (%lli + %lli > %lli), clamped to %lli",\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
        {\r
@@ -352,7 +361,7 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
                memcpy( buffer, (void*)( tmpBuf + offset%bpc ), length );\r
                free(tmpBuf);\r
                LEAVE('i', 1);\r
-               return 1;\r
+               return length;\r
        }\r
        \r
        preSkip = offset / bpc;\r
@@ -382,7 +391,7 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
        if (count == 1) {\r
                free(tmpBuf);\r
                LEAVE('i', 1);\r
-               return 1;\r
+               return length;\r
        }\r
        \r
        cluster = FAT_int_GetFatValue(handle, cluster);\r
@@ -492,8 +501,6 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft, char *LongFi
        \r
        ENTER("pParent pFT sLongFileName", parent, ft, LongFileName);\r
        \r
-       // Get Name\r
-       //node.Name = FAT_int_CreateName(parent, ft, LongFileName);\r
        // Set Other Data\r
        node.Inode = ft->cluster | (ft->clusterHi<<16);\r
        node.Size = ft->size;\r
@@ -532,6 +539,7 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *parent, fat_filetable *ft, char *LongFi
                node.ReadDir = FAT_ReadDir;\r
                node.FindDir = FAT_FindDir;\r
                node.MkNod = FAT_Mknod;\r
+               node.Size = -1;\r
        } else {\r
                node.Read = FAT_Read;\r
                node.Write = FAT_Write;\r
@@ -669,16 +677,16 @@ char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos)
        // Offset in sector\r
        a = dirpos & 0xF;\r
 \r
-       LOG("offset=%i, a=%i\n", (Uint)offset, a);\r
+       LOG("offset=%i, a=%i", (Uint)offset, a);\r
        \r
        // Read Sector\r
        VFS_ReadAt(disk->fileHandle, offset*512, 512, fileinfo);        // Read Dir Data\r
        \r
-       LOG("name[0] = 0x%x\n", (Uint8)fileinfo[a].name[0]);\r
+       LOG("name[0] = 0x%x", (Uint8)fileinfo[a].name[0]);\r
        //Check if this is the last entry\r
        if(fileinfo[a].name[0] == '\0') {\r
                dirNode->Size = dirpos;\r
-               LOG("End of list\n");\r
+               LOG("End of list");\r
                LEAVE('n');\r
                return NULL;    // break\r
        }\r
@@ -703,7 +711,7 @@ char *FAT_ReadDir(tVFS_Node *dirNode, int dirpos)
                // Get the current length\r
                len = strlen(lfn);\r
                \r
-               // Sanity Check (FAT implementations do not allow >255 bytes)\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

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