3 * - Open, Close and ChDir
23 #define OPEN_MOUNT_ROOT 1
24 #define MAX_KERNEL_FILES 128
27 extern tVFS_Node gVFS_MemRoot;
28 extern tVFS_Mount *gRootMount;
31 tVFS_Handle *gaUserHandles = (void*)MM_PPD_VFS;
32 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
36 * \fn char *VFS_GetAbsPath(char *Path)
37 * \brief Create an absolute path from a relative one
39 char *VFS_GetAbsPath(char *Path)
42 int pathLen = strlen(Path);
44 int pos, slashNum=0, baseLen;
45 Uint slashOffsets[256];
46 char *cwd = CFGPTR(CFG_VFS_CWD);
53 ret = malloc(strlen(Path)+1);
59 // Check if the path is already absolute
61 ret = malloc(pathLen + 1);
66 // Prepend the current directory
67 ret = malloc(cwdLen+pathLen+1);
69 strcpy(&ret[cwdLen], Path);
71 // Pre-fill the slash positions
73 while( (pos = strpos( &ret[pos+1], '/' )) != -1 )
74 slashOffsets[slashNum++] = pos;
80 read = write = baseLen; // Cwd has already been parsed
81 for(; read < baseLen+pathLen; read = pos+1)
83 pos = strpos( &ret[read], '/' );
84 // If we are in the last section, force a break at the end of the itteration
85 if(pos == -1) pos = baseLen+pathLen;
86 else pos += read; // Else, Adjust to absolute
92 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
94 if(strncmp(&ret[read], "..", pos-read) == 0)
96 // If there is no higher, silently ignore
97 if(!slashNum) continue;
98 // Reverse write pointer
99 write = slashOffsets[ slashNum-- ];
105 // Only copy if the positions differ
107 memcpy( &ret[write], &ret[read], pos-read+1 );
111 slashOffsets[ slashNum++ ] = pos;
113 LOG("Path '%s' has too many elements", Path);
120 // `ret` should now be the absolute path
126 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
127 * \brief Parses a path, resolving sysmlinks and applying permissions
129 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
132 tVFS_Mount *longestMount = gRootMount; // Root is first
133 int cmp, retLength = 0;
135 tVFS_Node *curNode, *tmpNode;
138 ENTER("sPath pTruePath", Path, TruePath);
143 *TruePath = malloc(strlen(Path)+1);
144 strcpy(*TruePath, Path);
146 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
150 // For root we always fast return
152 if(Path[0] == '/' && Path[1] == '\0') {
154 *TruePath = malloc( gRootMount->MountPointLen+1 );
155 strcpy(*TruePath, gRootMount->MountPoint);
157 LEAVE('p', gRootMount->RootNode);
158 return gRootMount->RootNode;
161 // Check if there is anything mounted
162 if(!gMounts) return NULL;
170 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
172 // Length Check - If the length is smaller than the longest match sofar
173 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
175 cmp = strcmp(Path, mnt->MountPoint);
178 // Fast Break - Request Mount Root
181 *TruePath = malloc( mnt->MountPointLen+1 );
182 strcpy(*TruePath, mnt->MountPoint);
184 LEAVE('p', mnt->RootNode);
185 return mnt->RootNode;
188 // Not a match, continue
189 if(cmp != '/') continue;
194 /*if(!longestMount) {
195 Log("VFS_GetTruePath - ERROR: No Root Node\n");
199 // Save to shorter variable
202 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
207 *TruePath = malloc( mnt->MountPointLen+1 );
208 strcpy(*TruePath, mnt->MountPoint);
209 retLength = mnt->MountPointLen;
212 curNode = mnt->RootNode;
213 curNode->ReferenceCount ++;
215 ofs = mnt->MountPointLen+1;
216 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
219 Path[nextSlash] = '\0';
221 // Check for empty string
222 if( Path[ofs] == '\0' ) continue;
224 // Check permissions on root of filesystem
225 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
226 curNode->Close( curNode );
227 if(TruePath) free(*TruePath);
232 // Check if the node has a FindDir method
233 if(!curNode->FindDir) {
234 if(curNode->Close) curNode->Close(curNode);
235 if(TruePath) free(*TruePath);
236 Path[nextSlash] = '/';
240 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
242 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
243 LOG("tmpNode = %p", tmpNode);
245 curNode->Close(curNode);
250 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
253 Path[nextSlash] = '/';
258 // Handle Symbolic Links
259 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
262 tmp = malloc( curNode->Size + 1 );
263 curNode->Read( curNode, 0, curNode->Size, tmp );
264 tmp[ curNode->Size ] = '\0';
266 // Parse Symlink Path
267 curNode = VFS_ParsePath(tmp, TruePath);
268 free(tmp); // Free temp string
279 retLength = strlen(tmp);
285 // Handle Non-Directories
286 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
288 Warning("VFS_ParsePath - File in directory context");
289 if(TruePath) free(*TruePath);
294 // Check if path needs extending
295 if(!TruePath) continue;
297 // Increase buffer space
298 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
299 // Check if allocation succeeded
301 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
303 if(curNode->Close) curNode->Close(curNode);
309 (*TruePath)[retLength] = '/';
310 strcpy(*TruePath+retLength+1, &Path[ofs]);
312 retLength += strlen(&Path[ofs])+1;
316 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
317 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
318 LOG("tmpNode = %p", tmpNode);
319 if(curNode->Close) curNode->Close(curNode);
320 // Check if file was found
322 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
323 if(TruePath) free(*TruePath);
324 if(curNode->Close) curNode->Close(curNode);
331 // Increase buffer space
332 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
333 // Check if allocation succeeded
335 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
337 if(tmpNode->Close) tmpNode->Close(curNode);
343 (*TruePath)[retLength] = '/';
344 strcpy(*TruePath + retLength + 1, &Path[ofs]);
346 //retLength += strlen(tmpNode->Name) + 1;
354 * \fn int VFS_Open(char *Path, Uint Mode)
357 int VFS_Open(char *Path, Uint Mode)
363 ENTER("sPath xMode", Path, Mode);
366 absPath = VFS_GetAbsPath(Path);
367 LOG("absPath = \"%s\"", absPath);
368 // Parse path and get mount point
369 node = VFS_ParsePath(absPath, NULL);
370 // Free generated path
374 LOG("Cannot find node");
379 // Check for symlinks
380 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
383 LOG("No read method on symlink");
387 absPath = malloc(node->Size+1); // Allocate Buffer
388 node->Read( node, 0, node->Size, absPath ); // Read Path
390 absPath[ node->Size ] = '\0'; // End String
391 if(node->Close) node->Close( node ); // Close old node
392 node = VFS_ParsePath(absPath, NULL); // Get new node
393 free( absPath ); // Free allocated path
402 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
403 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
404 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
409 if( !VFS_CheckACL(node, i) ) {
415 // Check for a user open
416 if(Mode & VFS_OPENFLAG_USER)
419 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
422 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
423 for(addr = 0; addr < size; addr += 0x1000)
424 MM_Allocate( (Uint)gaUserHandles + addr );
425 memset( gaUserHandles, 0, size );
428 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
430 if(gaUserHandles[i].Node) continue;
431 gaUserHandles[i].Node = node;
432 gaUserHandles[i].Position = 0;
433 gaUserHandles[i].Mode = Mode;
440 // Allocate space if not already
441 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
444 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
445 for(addr = 0; addr < size; addr += 0x1000)
446 MM_Allocate( (Uint)gaKernelHandles + addr );
447 memset( gaKernelHandles, 0, size );
450 for(i=0;i<MAX_KERNEL_FILES;i++)
452 if(gaKernelHandles[i].Node) continue;
453 gaKernelHandles[i].Node = node;
454 gaKernelHandles[i].Position = 0;
455 gaKernelHandles[i].Mode = Mode;
456 LEAVE('x', i|VFS_KERNEL_FLAG);
457 return i|VFS_KERNEL_FLAG;
466 * \fn void VFS_Close(int FD)
467 * \brief Closes an open file handle
469 void VFS_Close(int FD)
474 h = VFS_GetHandle(FD);
476 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
481 h->Node->Close( h->Node );
487 * \fn tVFS_Handle *VFS_GetHandle(int FD)
488 * \brief Gets a pointer to the handle information structure
490 tVFS_Handle *VFS_GetHandle(int FD)
492 if(FD < 0) return NULL;
494 if(FD & VFS_KERNEL_FLAG) {
495 FD &= (VFS_KERNEL_FLAG - 1);
496 if(FD >= MAX_KERNEL_FILES) return NULL;
497 return &gaKernelHandles[ FD ];
499 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
500 return &gaUserHandles[ FD ];