+tUserHandles *VFS_int_GetUserHandles(int PID, int bCreate)
+{
+ tUserHandles *ent, *prev = NULL;
+ for( ent = gpUserHandles; ent; prev = ent, ent = ent->Next ) {
+ if( ent->PID == PID ) {
+ //if( bCreate )
+ // Log_Warning("VFS", "Process %i already has a handle list", PID);
+ LOG("Found list for %i", PID);
+ return ent;
+ }
+ if( ent->PID > PID ) break;
+ }
+
+ if(!bCreate) {
+ LOG("Not creating for %i", PID);
+ return NULL;
+ }
+
+ ent = calloc( 1, sizeof(tUserHandles) );
+ ent->PID = PID;
+ if( prev ) {
+ ent->Next = prev->Next;
+ prev->Next = ent;
+ }
+ else {
+ ent->Next = gpUserHandles;
+ gpUserHandles = ent;
+ }
+ LOG("Created handle list for process %i", PID);
+ return ent;
+}
+
+/**
+ * \brief Clone the handle list of the current process into another
+ */
+void VFS_CloneHandleList(int PID)
+{
+ tUserHandles *ent;
+ const tUserHandles *cur;
+ int maxhandles;
+
+ cur = VFS_int_GetUserHandles(Threads_GetPID(), 0);
+ if(!cur) return ; // Don't need to do anything if the current list is empty
+
+ ent = VFS_int_GetUserHandles(PID, 1);
+
+ maxhandles = *Threads_GetMaxFD(NULL);
+ memcpy(ent->Handles, cur->Handles, maxhandles*sizeof(tVFS_Handle));
+
+ // Reference all
+ for( int i = 0; i < maxhandles; i ++ )
+ {
+ if(!ent->Handles[i].Node) continue;
+
+ if(ent->Handles[i].Node->Type->Reference)
+ ent->Handles[i].Node->Type->Reference(ent->Handles[i].Node);
+ }
+}
+
+void VFS_CloneHandlesFromList(int PID, int nFD, int FDs[])
+{
+ tUserHandles *ent;
+ const tUserHandles *cur;
+ int maxhandles;
+
+ cur = VFS_int_GetUserHandles(Threads_GetPID(), 0);
+ if(!cur) return ; // Don't need to do anything if the current list is empty
+
+ ent = VFS_int_GetUserHandles(PID, 1);
+
+ LOG("Copying %i FDs from %i", nFD, PID);
+
+ maxhandles = *Threads_GetMaxFD(NULL);
+ if( nFD > maxhandles )
+ nFD = maxhandles;
+ for( int i = 0; i < nFD; i ++ )
+ {
+ int fd = FDs[i];
+ if( fd >= maxhandles ) {
+ ent->Handles[i].Node = NULL;
+ continue ;
+ }
+ memcpy(&ent->Handles[i], &cur->Handles[fd], sizeof(tVFS_Handle));
+ }
+ for( int i = nFD; i < maxhandles; i ++ )
+ ent->Handles[i].Node = NULL;
+
+ // Reference
+ for( int i = 0; i < maxhandles; i ++ )
+ {
+ if(!ent->Handles[i].Node) continue;
+
+ if(ent->Handles[i].Node->Type->Reference)
+ ent->Handles[i].Node->Type->Reference(ent->Handles[i].Node);
+ }
+}
+