3 * - Open, Close and ChDir
12 #define OPEN_MOUNT_ROOT 1
13 #define MAX_KERNEL_FILES 128
14 #define MAX_PATH_SLASHES 256
17 extern tVFS_Node gVFS_MemRoot;
18 extern tVFS_Mount *gVFS_RootMount;
21 tVFS_Handle *gaUserHandles = (void*)MM_PPD_VFS;
22 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
26 * \fn char *VFS_GetAbsPath(char *Path)
27 * \brief Create an absolute path from a relative one
29 char *VFS_GetAbsPath(char *Path)
32 int pathLen = strlen(Path);
33 char *pathComps[MAX_PATH_SLASHES];
37 char *chroot = CFGPTR(CFG_VFS_CHROOT);
39 char *cwd = CFGPTR(CFG_VFS_CWD);
46 ret = malloc(strlen(Path)+1);
48 Warning("VFS_GetAbsPath - malloc() returned NULL");
57 if( chroot == NULL ) {
61 chrootLen = strlen(chroot);
64 // Check if the path is already absolute
66 ret = malloc(pathLen + 1);
68 Warning("VFS_GetAbsPath - malloc() returned NULL");
80 // Prepend the current directory
81 ret = malloc( cwdLen + 1 + pathLen + 1 );
84 strcpy(&ret[cwdLen+1], Path);
85 //Log("ret = '%s'\n", ret);
89 pathComps[iPos++] = tmpStr = ret+1;
94 pathComps[iPos++] = tmpStr;
95 if(iPos == MAX_PATH_SLASHES) {
96 LOG("Path '%s' has too many elements", Path);
103 pathComps[iPos] = NULL;
107 while(pathComps[iPos])
109 tmpStr = pathComps[iPos];
110 // Always Increment iPos
113 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
120 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
125 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
131 pathComps[iPos2] = tmpStr;
134 pathComps[iPos2] = NULL;
139 while(pathComps[iPos])
141 tmpStr = pathComps[iPos];
142 while(*tmpStr && *tmpStr != '/')
144 ret[iPos2++] = *tmpStr;
156 // Prepend the chroot
157 tmpStr = malloc(chrootLen + strlen(ret) + 1);
158 strcpy( tmpStr, chroot );
159 strcpy( tmpStr+chrootLen, ret );
164 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
169 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
170 * \brief Parses a path, resolving sysmlinks and applying permissions
172 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
175 tVFS_Mount *longestMount = gVFS_RootMount; // Root is first
176 int cmp, retLength = 0;
178 tVFS_Node *curNode, *tmpNode;
181 ENTER("sPath pTruePath", Path, TruePath);
186 *TruePath = malloc(strlen(Path)+1);
187 strcpy(*TruePath, Path);
189 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
193 // For root we always fast return
195 if(Path[0] == '/' && Path[1] == '\0') {
197 *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
198 strcpy(*TruePath, gVFS_RootMount->MountPoint);
200 LEAVE('p', gVFS_RootMount->RootNode);
201 return gVFS_RootMount->RootNode;
204 // Check if there is anything mounted
206 Warning("WTF! There's nothing mounted?");
211 for(mnt = gVFS_Mounts;
216 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
218 // Length Check - If the length is smaller than the longest match sofar
219 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
221 cmp = strcmp(Path, mnt->MountPoint);
224 // Fast Break - Request Mount Root
227 *TruePath = malloc( mnt->MountPointLen+1 );
228 strcpy(*TruePath, mnt->MountPoint);
230 LEAVE('p', mnt->RootNode);
231 return mnt->RootNode;
234 // Not a match, continue
235 if(cmp != '/') continue;
240 /*if(!longestMount) {
241 Log("VFS_ParsePath - ERROR: No Root Node\n");
245 // Save to shorter variable
248 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
253 *TruePath = malloc( mnt->MountPointLen+1 );
254 strcpy(*TruePath, mnt->MountPoint);
255 retLength = mnt->MountPointLen;
258 curNode = mnt->RootNode;
259 curNode->ReferenceCount ++;
261 ofs = mnt->MountPointLen+1;
262 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
265 Path[nextSlash] = '\0';
267 // Check for empty string
268 if( Path[ofs] == '\0' ) continue;
270 // Check permissions on root of filesystem
271 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
272 curNode->Close( curNode );
277 //Log("Permissions fail on '%s'", Path);
282 // Check if the node has a FindDir method
283 if(!curNode->FindDir) {
284 if(curNode->Close) curNode->Close(curNode);
289 Path[nextSlash] = '/';
290 //Log("FindDir fail on '%s'", Path);
294 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
296 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
297 LOG("tmpNode = %p", tmpNode);
299 curNode->Close(curNode);
304 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
309 //Log("Child fail on '%s' ('%s)", Path, &Path[ofs]);
310 Path[nextSlash] = '/';
315 // Handle Symbolic Links
316 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
321 tmp = malloc( curNode->Size + 1 );
322 curNode->Read( curNode, 0, curNode->Size, tmp );
323 tmp[ curNode->Size ] = '\0';
325 // Parse Symlink Path
326 curNode = VFS_ParsePath(tmp, TruePath);
330 Log("Symlink fail '%s'", tmp);
331 free(tmp); // Free temp string
339 retLength = strlen(tmp);
341 free(tmp); // Free temp string
347 // Handle Non-Directories
348 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
350 Warning("VFS_ParsePath - File in directory context");
351 if(TruePath) free(*TruePath);
356 // Check if path needs extending
357 if(!TruePath) continue;
359 // Increase buffer space
360 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
361 // Check if allocation succeeded
363 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
365 if(curNode->Close) curNode->Close(curNode);
371 (*TruePath)[retLength] = '/';
372 strcpy(*TruePath+retLength+1, &Path[ofs]);
374 retLength += strlen(&Path[ofs])+1;
378 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
379 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
380 LOG("tmpNode = %p", tmpNode);
381 if(curNode->Close) curNode->Close(curNode);
382 // Check if file was found
384 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
385 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
386 if(TruePath) free(*TruePath);
387 if(curNode->Close) curNode->Close(curNode);
394 // Increase buffer space
395 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
396 // Check if allocation succeeded
398 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
400 if(tmpNode->Close) tmpNode->Close(curNode);
406 (*TruePath)[retLength] = '/';
407 strcpy(*TruePath + retLength + 1, &Path[ofs]);
409 //retLength += strlen(tmpNode->Name) + 1;
417 * \fn int VFS_Open(char *Path, Uint Mode)
420 int VFS_Open(char *Path, Uint Mode)
426 ENTER("sPath xMode", Path, Mode);
429 absPath = VFS_GetAbsPath(Path);
430 LOG("absPath = \"%s\"", absPath);
431 // Parse path and get mount point
432 node = VFS_ParsePath(absPath, NULL);
433 // Free generated path
437 LOG("Cannot find node");
442 // Check for symlinks
443 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
446 Warning("No read method on symlink");
450 absPath = malloc(node->Size+1); // Allocate Buffer
451 node->Read( node, 0, node->Size, absPath ); // Read Path
453 absPath[ node->Size ] = '\0'; // End String
454 if(node->Close) node->Close( node ); // Close old node
455 node = VFS_ParsePath(absPath, NULL); // Get new node
456 free( absPath ); // Free allocated path
460 LOG("Cannot find node");
466 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
467 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
468 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
473 if( !VFS_CheckACL(node, i) ) {
475 Log("VFS_Open: Permissions Failed");
480 // Check for a user open
481 if(Mode & VFS_OPENFLAG_USER)
484 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
487 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
488 for(addr = 0; addr < size; addr += 0x1000)
489 MM_Allocate( (Uint)gaUserHandles + addr );
490 memset( gaUserHandles, 0, size );
493 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
495 if(gaUserHandles[i].Node) continue;
496 gaUserHandles[i].Node = node;
497 gaUserHandles[i].Position = 0;
498 gaUserHandles[i].Mode = Mode;
505 // Allocate space if not already
506 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
509 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
510 for(addr = 0; addr < size; addr += 0x1000)
511 MM_Allocate( (Uint)gaKernelHandles + addr );
512 memset( gaKernelHandles, 0, size );
515 for(i=0;i<MAX_KERNEL_FILES;i++)
517 if(gaKernelHandles[i].Node) continue;
518 gaKernelHandles[i].Node = node;
519 gaKernelHandles[i].Position = 0;
520 gaKernelHandles[i].Mode = Mode;
521 LEAVE('x', i|VFS_KERNEL_FLAG);
522 return i|VFS_KERNEL_FLAG;
526 Log("VFS_Open: Out of handles");
532 * \fn void VFS_Close(int FD)
533 * \brief Closes an open file handle
535 void VFS_Close(int FD)
540 h = VFS_GetHandle(FD);
542 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
547 h->Node->Close( h->Node );
553 * \brief Change current working directory
555 int VFS_ChDir(char *Dest)
562 buf = VFS_GetAbsPath(Dest);
564 Log("VFS_ChDir: Path expansion failed");
568 // Check if path exists
569 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
571 Log("VFS_ChDir: Path is invalid");
575 // Get node so we can check for directory
576 h = VFS_GetHandle(fd);
577 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
578 Log("VFS_ChDir: Path is not a directory");
586 // Free old working directory
587 if( CFGPTR(CFG_VFS_CWD) )
588 free( CFGPTR(CFG_VFS_CWD) );
590 CFGPTR(CFG_VFS_CWD) = buf;
592 Log("Updated CWD to '%s'", buf);
598 * \fn int VFS_ChRoot(char *New)
599 * \brief Change current root directory
601 int VFS_ChRoot(char *New)
607 if(New[0] == '/' && New[1] == '\0')
608 return 1; // What a useless thing to ask!
611 buf = VFS_GetAbsPath(New);
613 LOG("Path expansion failed");
617 // Check if path exists
618 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
620 LOG("Path is invalid");
624 // Get node so we can check for directory
625 h = VFS_GetHandle(fd);
626 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
627 LOG("Path is not a directory");
635 // Free old working directory
636 if( CFGPTR(CFG_VFS_CHROOT) )
637 free( CFGPTR(CFG_VFS_CHROOT) );
639 CFGPTR(CFG_VFS_CHROOT) = buf;
641 LOG("Updated Root to '%s'", buf);
647 * \fn tVFS_Handle *VFS_GetHandle(int FD)
648 * \brief Gets a pointer to the handle information structure
650 tVFS_Handle *VFS_GetHandle(int FD)
654 if(FD < 0) return NULL;
656 if(FD & VFS_KERNEL_FLAG) {
657 FD &= (VFS_KERNEL_FLAG - 1);
658 if(FD >= MAX_KERNEL_FILES) return NULL;
659 h = &gaKernelHandles[ FD ];
661 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
662 h = &gaUserHandles[ FD ];
665 if(h->Node == NULL) return NULL;