AcessNative - Updates for recent changes
[tpg/acess2.git] / AcessNative / acesskernel_src / vfs_handle.c
1 /*
2  * Acess2 VFS
3  * - AllocHandle, GetHandle
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <vfs.h>
8 #include <vfs_int.h>
9 #include <vfs_ext.h>
10 #include <threads.h>
11
12 // === CONSTANTS ===
13 #define MAX_KERNEL_FILES        128
14 #define MAX_USER_FILES  64
15
16 // === IMPORTS ===
17 extern int      Server_GetClientID(void);
18
19 // === PROTOTYPES ===
20 tVFS_Handle     *VFS_GetHandle(int FD);
21  int    VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
22
23 // === Types ===
24 typedef struct sUserHandles
25 {
26         struct sUserHandles     *Next;
27          int    PID;
28         tVFS_Handle     Handles[MAX_USER_FILES];
29 }       tUserHandles;
30
31 // === GLOBALS ===
32 tUserHandles    *gpUserHandles = NULL;
33 tVFS_Handle     gaKernelHandles[MAX_KERNEL_FILES];
34
35 // === CODE ===
36 tUserHandles *VFS_int_GetUserHandles(int PID, int bCreate)
37 {
38         tUserHandles    *ent, *prev = NULL;
39         for( ent = gpUserHandles; ent; prev = ent, ent = ent->Next ) {
40                 if( ent->PID == PID ) {
41                         //if( bCreate )
42                         //      Log_Warning("VFS", "Process %i already has a handle list", PID);
43                         return ent;
44                 }
45                 if( ent->PID > PID )    break;
46         }
47         
48         if(!bCreate)
49                 return NULL;
50         
51         ent = calloc( 1, sizeof(tUserHandles) );
52         ent->PID = PID;
53         if( prev ) {
54                 ent->Next = prev->Next;
55                 prev->Next = ent;
56         }
57         else {
58                 ent->Next = gpUserHandles;
59                 gpUserHandles = ent;
60         }
61         Log_Notice("VFS", "Created handle list for process %i", PID);
62         return ent;
63 }
64
65 /**
66  * \brief Clone the handle list of the current process into another
67  */
68 void VFS_CloneHandleList(int PID)
69 {
70         tUserHandles    *ent;
71         tUserHandles    *cur;
72          int    i, maxhandles;
73         
74         cur = VFS_int_GetUserHandles(Threads_GetPID(), 0);
75         if(!cur)        return ;        // Don't need to do anything if the current list is empty
76         
77         ent = VFS_int_GetUserHandles(PID, 1);
78         
79         maxhandles = *Threads_GetMaxFD();
80         memcpy(ent->Handles, cur->Handles, maxhandles*sizeof(tVFS_Handle));
81         
82         for( i = 0; i < maxhandles; i ++ )
83         {
84                 if(!cur->Handles[i].Node)       continue;
85                 
86                 if(ent->Handles[i].Node->Type->Reference)
87                         ent->Handles[i].Node->Type->Reference(ent->Handles[i].Node);
88         }
89 }
90
91 void VFS_CloneHandlesFromList(int PID, int nFD, int FDs[])
92 {
93         tUserHandles    *ent;
94         tUserHandles    *cur;
95          int    i, maxhandles;
96         
97         cur = VFS_int_GetUserHandles(Threads_GetPID(), 0);
98         if(!cur)        return ;        // Don't need to do anything if the current list is empty
99         
100         ent = VFS_int_GetUserHandles(PID, 1);
101         
102         maxhandles = *Threads_GetMaxFD();
103         if( nFD > maxhandles )
104                 nFD = maxhandles;
105         for( i = 0; i < nFD; i ++ )
106         {
107                 if( FDs[i] >= maxhandles ) {
108                         ent->Handles[i].Node = NULL;
109                         continue ;
110                 }
111                 memcpy(&ent->Handles[i], &cur->Handles[ FDs[i] ], sizeof(tVFS_Handle));
112         }
113         for( ; i < maxhandles; i ++ )
114                 cur->Handles[i].Node = NULL;
115         
116         for( i = 0; i < maxhandles; i ++ )
117         {
118                 if(!cur->Handles[i].Node)       continue;
119                 
120                 if(ent->Handles[i].Node->Type->Reference)
121                         ent->Handles[i].Node->Type->Reference(ent->Handles[i].Node);
122         }
123 }
124
125 /**
126  * \fn tVFS_Handle *VFS_GetHandle(int FD)
127  * \brief Gets a pointer to the handle information structure
128  */
129 tVFS_Handle *VFS_GetHandle(int FD)
130 {
131         tVFS_Handle     *h;
132         
133         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
134         
135         if(FD < 0) {
136                 LOG("FD (%i) < 0, RETURN NULL", FD);
137                 return NULL;
138         }
139         
140         if(FD & VFS_KERNEL_FLAG) {
141                 FD &= (VFS_KERNEL_FLAG - 1);
142                 if(FD >= MAX_KERNEL_FILES) {
143                         LOG("FD (%i) > MAX_KERNEL_FILES (%i), RETURN NULL", FD, MAX_KERNEL_FILES);
144                         return NULL;
145                 }
146                 h = &gaKernelHandles[ FD ];
147         }
148         else
149         {
150                 tUserHandles    *ent;
151                  int    pid = Threads_GetPID();
152                  int    maxhandles = *Threads_GetMaxFD();
153                 
154                 ent = VFS_int_GetUserHandles(pid, 0);
155                 if(!ent) {
156                         Log_Error("VFS", "Client %i does not have a handle list (>)", pid);
157                         return NULL;
158                 }
159                 
160                 if(FD >= maxhandles) {
161                         LOG("FD (%i) > Limit (%i), RETURN NULL", FD, maxhandles);
162                         return NULL;
163                 }
164                 h = &ent->Handles[ FD ];
165                 LOG("FD (%i) -> %p (Mode:0x%x,Node:%p)", FD, h, h->Mode, h->Node);
166         }
167         
168         if(h->Node == NULL) {
169                 LOG("FD (%i) Unused", FD);
170                 return NULL;
171         }
172         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
173         return h;
174 }
175
176 int VFS_SetHandle(int FD, tVFS_Node *Node, int Mode)
177 {
178         tVFS_Handle     *h;
179         if(FD < 0)      return -1;
180         
181         if( FD & VFS_KERNEL_FLAG ) {
182                 FD &= (VFS_KERNEL_FLAG -1);
183                 if( FD >= MAX_KERNEL_FILES )    return -1;
184                 h = &gaKernelHandles[FD];
185         }
186         else {
187                 tUserHandles    *ent;
188                  int    pid = Threads_GetPID();
189                  int    maxhandles = *Threads_GetMaxFD();
190                 
191                 ent = VFS_int_GetUserHandles(pid, 0);
192                 if(!ent) {
193                         Log_Error("VFS", "Client %i does not have a handle list (>)", pid);
194                         return NULL;
195                 }
196                 
197                 if(FD >= maxhandles) {
198                         LOG("FD (%i) > Limit (%i), RETURN NULL", FD, maxhandles);
199                         return NULL;
200                 }
201                 h = &ent->Handles[ FD ];
202         }
203         h->Node = Node;
204         h->Mode = Mode;
205         return FD;
206 }
207
208
209 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
210 {
211          int    i;
212         
213         // Check for a user open
214         if(bIsUser)
215         {
216                 tUserHandles    *ent;
217                  int    maxhandles = *Threads_GetMaxFD();
218                 // Find the PID's handle list
219                 ent = VFS_int_GetUserHandles(Threads_GetPID(), 1);
220                 // Get a handle
221                 for( i = 0; i < maxhandles; i ++ )
222                 {
223                         if(ent->Handles[i].Node)        continue;
224                         ent->Handles[i].Node = Node;
225                         ent->Handles[i].Position = 0;
226                         ent->Handles[i].Mode = Mode;
227                         return i;
228                 }
229         }
230         else
231         {
232                 // Get a handle
233                 for(i=0;i<MAX_KERNEL_FILES;i++)
234                 {
235                         if(gaKernelHandles[i].Node)     continue;
236                         gaKernelHandles[i].Node = Node;
237                         gaKernelHandles[i].Position = 0;
238                         gaKernelHandles[i].Mode = Mode;
239                         return i|VFS_KERNEL_FLAG;
240                 }
241         }
242         
243         return -1;
244 }

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