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);
55 Warning("VFS_GetAbsPath - malloc() returned NULL");
63 // Check if the path is already absolute
65 ret = malloc(pathLen + 1);
67 Warning("VFS_GetAbsPath - malloc() returned NULL");
74 // Prepend the current directory
75 ret = malloc(cwdLen+pathLen+1);
77 strcpy(&ret[cwdLen], Path);
79 // Pre-fill the slash positions
81 while( (pos = strpos( &ret[pos+1], '/' )) != -1 )
82 slashOffsets[slashNum++] = pos;
88 read = write = baseLen; // Cwd has already been parsed
89 for(; read < baseLen+pathLen; read = pos+1)
91 pos = strpos( &ret[read], '/' );
92 // If we are in the last section, force a break at the end of the itteration
93 if(pos == -1) pos = baseLen+pathLen;
94 else pos += read; // Else, Adjust to absolute
100 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
102 if(strncmp(&ret[read], "..", pos-read) == 0)
104 // If there is no higher, silently ignore
105 if(!slashNum) continue;
106 // Reverse write pointer
107 write = slashOffsets[ slashNum-- ];
113 // Only copy if the positions differ
115 memcpy( &ret[write], &ret[read], pos-read+1 );
119 slashOffsets[ slashNum++ ] = pos;
121 LOG("Path '%s' has too many elements", Path);
128 // `ret` should now be the absolute path
130 Log("VFS_GetAbsPath: RETURN '%s'", ret);
135 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
136 * \brief Parses a path, resolving sysmlinks and applying permissions
138 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
141 tVFS_Mount *longestMount = gRootMount; // Root is first
142 int cmp, retLength = 0;
144 tVFS_Node *curNode, *tmpNode;
147 ENTER("sPath pTruePath", Path, TruePath);
152 *TruePath = malloc(strlen(Path)+1);
153 strcpy(*TruePath, Path);
155 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
159 // For root we always fast return
161 if(Path[0] == '/' && Path[1] == '\0') {
163 *TruePath = malloc( gRootMount->MountPointLen+1 );
164 strcpy(*TruePath, gRootMount->MountPoint);
166 LEAVE('p', gRootMount->RootNode);
167 return gRootMount->RootNode;
170 // Check if there is anything mounted
171 if(!gMounts) return NULL;
179 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
181 // Length Check - If the length is smaller than the longest match sofar
182 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
184 cmp = strcmp(Path, mnt->MountPoint);
187 // Fast Break - Request Mount Root
190 *TruePath = malloc( mnt->MountPointLen+1 );
191 strcpy(*TruePath, mnt->MountPoint);
193 LEAVE('p', mnt->RootNode);
194 return mnt->RootNode;
197 // Not a match, continue
198 if(cmp != '/') continue;
203 /*if(!longestMount) {
204 Log("VFS_GetTruePath - ERROR: No Root Node\n");
208 // Save to shorter variable
211 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
216 *TruePath = malloc( mnt->MountPointLen+1 );
217 strcpy(*TruePath, mnt->MountPoint);
218 retLength = mnt->MountPointLen;
221 curNode = mnt->RootNode;
222 curNode->ReferenceCount ++;
224 ofs = mnt->MountPointLen+1;
225 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
228 Path[nextSlash] = '\0';
230 // Check for empty string
231 if( Path[ofs] == '\0' ) continue;
233 // Check permissions on root of filesystem
234 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
235 curNode->Close( curNode );
244 // Check if the node has a FindDir method
245 if(!curNode->FindDir) {
246 if(curNode->Close) curNode->Close(curNode);
251 Path[nextSlash] = '/';
255 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
257 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
258 LOG("tmpNode = %p", tmpNode);
260 curNode->Close(curNode);
265 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
270 Path[nextSlash] = '/';
275 // Handle Symbolic Links
276 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
281 tmp = malloc( curNode->Size + 1 );
282 curNode->Read( curNode, 0, curNode->Size, tmp );
283 tmp[ curNode->Size ] = '\0';
285 // Parse Symlink Path
286 curNode = VFS_ParsePath(tmp, TruePath);
290 free(tmp); // Free temp string
298 retLength = strlen(tmp);
300 free(tmp); // Free temp string
306 // Handle Non-Directories
307 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
309 Warning("VFS_ParsePath - File in directory context");
310 if(TruePath) free(*TruePath);
315 // Check if path needs extending
316 if(!TruePath) continue;
318 // Increase buffer space
319 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
320 // Check if allocation succeeded
322 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
324 if(curNode->Close) curNode->Close(curNode);
330 (*TruePath)[retLength] = '/';
331 strcpy(*TruePath+retLength+1, &Path[ofs]);
333 retLength += strlen(&Path[ofs])+1;
337 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
338 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
339 LOG("tmpNode = %p", tmpNode);
340 if(curNode->Close) curNode->Close(curNode);
341 // Check if file was found
343 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
344 if(TruePath) free(*TruePath);
345 if(curNode->Close) curNode->Close(curNode);
352 // Increase buffer space
353 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
354 // Check if allocation succeeded
356 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
358 if(tmpNode->Close) tmpNode->Close(curNode);
364 (*TruePath)[retLength] = '/';
365 strcpy(*TruePath + retLength + 1, &Path[ofs]);
367 //retLength += strlen(tmpNode->Name) + 1;
375 * \fn int VFS_Open(char *Path, Uint Mode)
378 int VFS_Open(char *Path, Uint Mode)
384 ENTER("sPath xMode", Path, Mode);
387 absPath = VFS_GetAbsPath(Path);
388 LOG("absPath = \"%s\"", absPath);
389 // Parse path and get mount point
390 node = VFS_ParsePath(absPath, NULL);
391 // Free generated path
395 LOG("Cannot find node");
400 // Check for symlinks
401 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
404 Warning("No read method on symlink");
408 absPath = malloc(node->Size+1); // Allocate Buffer
409 node->Read( node, 0, node->Size, absPath ); // Read Path
411 absPath[ node->Size ] = '\0'; // End String
412 if(node->Close) node->Close( node ); // Close old node
413 node = VFS_ParsePath(absPath, NULL); // Get new node
414 free( absPath ); // Free allocated path
423 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
424 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
425 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
430 if( !VFS_CheckACL(node, i) ) {
436 // Check for a user open
437 if(Mode & VFS_OPENFLAG_USER)
440 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
443 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
444 for(addr = 0; addr < size; addr += 0x1000)
445 MM_Allocate( (Uint)gaUserHandles + addr );
446 memset( gaUserHandles, 0, size );
449 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
451 if(gaUserHandles[i].Node) continue;
452 gaUserHandles[i].Node = node;
453 gaUserHandles[i].Position = 0;
454 gaUserHandles[i].Mode = Mode;
461 // Allocate space if not already
462 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
465 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
466 for(addr = 0; addr < size; addr += 0x1000)
467 MM_Allocate( (Uint)gaKernelHandles + addr );
468 memset( gaKernelHandles, 0, size );
471 for(i=0;i<MAX_KERNEL_FILES;i++)
473 if(gaKernelHandles[i].Node) continue;
474 gaKernelHandles[i].Node = node;
475 gaKernelHandles[i].Position = 0;
476 gaKernelHandles[i].Mode = Mode;
477 LEAVE('x', i|VFS_KERNEL_FLAG);
478 return i|VFS_KERNEL_FLAG;
487 * \fn void VFS_Close(int FD)
488 * \brief Closes an open file handle
490 void VFS_Close(int FD)
495 h = VFS_GetHandle(FD);
497 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
502 h->Node->Close( h->Node );
508 * \fn int VFS_ChDir(char *New)
509 * \brief Change current working directory
511 int VFS_ChDir(char *New)
518 buf = VFS_GetAbsPath(New);
520 Log("VFS_ChDir: Path expansion failed");
524 // Check if path exists
525 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
527 Log("VFS_ChDir: Path is invalid");
531 // Get node so we can check for directory
532 h = VFS_GetHandle(fd);
533 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
534 Log("VFS_ChDir: Path is not a directory");
542 // Free old working directory
543 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
545 CFGPTR(CFG_VFS_CWD) = buf;
547 Log("Updated CWD to '%s'", buf);
553 * \fn tVFS_Handle *VFS_GetHandle(int FD)
554 * \brief Gets a pointer to the handle information structure
556 tVFS_Handle *VFS_GetHandle(int FD)
560 if(FD < 0) return NULL;
562 if(FD & VFS_KERNEL_FLAG) {
563 FD &= (VFS_KERNEL_FLAG - 1);
564 if(FD >= MAX_KERNEL_FILES) return NULL;
565 h = &gaKernelHandles[ FD ];
567 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
568 h = &gaUserHandles[ FD ];
571 if(h->Node == NULL) return NULL;