Kernel/VFS - Fixed bug with setting flags on FD0
[tpg/acess2.git] / KernelLand / Kernel / vfs / handle.c
index 88877ab..6173c29 100644 (file)
 #define MAX_KERNEL_FILES       128
 
 // === PROTOTYPES ===
-#if 0
-tVFS_Handle    *VFS_GetHandle(int FD);
-#endif
- int   VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
 
 // === GLOBALS ===
 tVFS_Handle    *gaUserHandles = (void*)MM_PPD_HANDLES;
 tVFS_Handle    *gaKernelHandles = (void*)MM_KERNEL_VFS;
 
 // === CODE ===
-inline void _ReferenceNode(tVFS_Node *Node)
-{
-       if( !MM_GetPhysAddr(Node->Type) ) {
-               Log_Error("VFS", "Node %p's type is invalid (%p bad pointer) - %P corrupted",
-                       Node, Node->Type, MM_GetPhysAddr(&Node->Type));
-               return ;
-       }
-       if( Node->Type && Node->Type->Reference )
-               Node->Type->Reference( Node );
-       else
-               Node->ReferenceCount ++;
-}
-
 /**
  * \fn tVFS_Handle *VFS_GetHandle(int FD)
  * \brief Gets a pointer to the handle information structure
@@ -65,10 +48,27 @@ tVFS_Handle *VFS_GetHandle(int FD)
        return h;
 }
 
-int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
+int VFS_SetHandle(int FD, tVFS_Node *Node, int Mode)
 {
-        int    i;
+       tVFS_Handle     *h;
+       if(FD < 0)      return -1;
        
+       if( FD & VFS_KERNEL_FLAG ) {
+               FD &= (VFS_KERNEL_FLAG -1);
+               if( FD >= MAX_KERNEL_FILES )    return -1;
+               h = &gaKernelHandles[FD];
+       }
+       else {
+               if( FD >= *Threads_GetMaxFD())  return -1;
+               h = &gaUserHandles[FD];
+       }
+       h->Node = Node;
+       h->Mode = Mode;
+       return FD;
+}
+
+int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
+{
        // Check for a user open
        if(bIsUser)
        {
@@ -89,7 +89,7 @@ int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
                        memset( gaUserHandles, 0, size );
                }
                // Get a handle
-               for( i = 0; i < max_handles; i ++ )
+               for( int i = 0; i < max_handles; i ++ )
                {
                        if(gaUserHandles[i].Node)       continue;
                        gaUserHandles[i].Node = Node;
@@ -116,7 +116,7 @@ int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
                        memset( gaKernelHandles, 0, size );
                }
                // Get a handle
-               for(i=0;i<MAX_KERNEL_FILES;i++)
+               for(int i=0;i<MAX_KERNEL_FILES;i++)
                {
                        if(gaKernelHandles[i].Node)     continue;
                        gaKernelHandles[i].Node = Node;
@@ -195,7 +195,14 @@ void *VFS_SaveHandles(int NumFDs, int *FDs)
                tVFS_Handle     *h;
                if( FDs == NULL )
                        h = &gaUserHandles[i];
-               else {
+               else if( FDs[i] == -1 )
+               {
+                       Log_Warning("VFS", "VFS_SaveHandles - Slot %i error FD (-1), ignorning", i);
+                       memset(&ret[i], 0, sizeof(tVFS_Handle));
+                       continue ;
+               }
+               else
+               {
                        h = VFS_GetHandle(FDs[i] & (VFS_KERNEL_FLAG - 1));
                        if(!h) {
                                Log_Warning("VFS", "VFS_SaveHandles - Invalid FD %i",

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