3 * - Open, Close and ChDir
13 #define OPEN_MOUNT_ROOT 1
14 #define MAX_KERNEL_FILES 128
15 #define MAX_PATH_SLASHES 256
18 extern tVFS_Node gVFS_MemRoot;
19 extern tVFS_Mount *gVFS_RootMount;
22 tVFS_Handle *gaUserHandles = (void*)MM_PPD_VFS;
23 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
27 * \fn char *VFS_GetAbsPath(const char *Path)
28 * \brief Create an absolute path from a relative one
30 char *VFS_GetAbsPath(const char *Path)
33 int pathLen = strlen(Path);
34 char *pathComps[MAX_PATH_SLASHES];
38 char *chroot = CFGPTR(CFG_VFS_CHROOT);
40 char *cwd = CFGPTR(CFG_VFS_CWD);
47 ret = malloc(strlen(Path)+1);
49 Warning("VFS_GetAbsPath - malloc() returned NULL");
58 if( chroot == NULL ) {
62 chrootLen = strlen(chroot);
65 // Check if the path is already absolute
67 ret = malloc(pathLen + 1);
69 Warning("VFS_GetAbsPath - malloc() returned NULL");
81 // Prepend the current directory
82 ret = malloc( cwdLen + 1 + pathLen + 1 );
85 strcpy(&ret[cwdLen+1], Path);
86 //Log("ret = '%s'\n", ret);
90 pathComps[iPos++] = tmpStr = ret+1;
95 pathComps[iPos++] = tmpStr;
96 if(iPos == MAX_PATH_SLASHES) {
97 LOG("Path '%s' has too many elements", Path);
104 pathComps[iPos] = NULL;
108 while(pathComps[iPos])
110 tmpStr = pathComps[iPos];
111 // Always Increment iPos
114 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
121 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
126 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
132 pathComps[iPos2] = tmpStr;
135 pathComps[iPos2] = NULL;
140 while(pathComps[iPos])
142 tmpStr = pathComps[iPos];
143 while(*tmpStr && *tmpStr != '/')
145 ret[iPos2++] = *tmpStr;
157 // Prepend the chroot
158 tmpStr = malloc(chrootLen + strlen(ret) + 1);
159 strcpy( tmpStr, chroot );
160 strcpy( tmpStr+chrootLen, ret );
165 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
170 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
171 * \brief Parses a path, resolving sysmlinks and applying permissions
173 tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath)
176 tVFS_Mount *longestMount = gVFS_RootMount; // Root is first
177 int cmp, retLength = 0;
179 tVFS_Node *curNode, *tmpNode;
182 ENTER("sPath pTruePath", Path, TruePath);
187 *TruePath = malloc(strlen(Path)+1);
188 strcpy(*TruePath, Path);
190 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
194 // For root we always fast return
196 if(Path[0] == '/' && Path[1] == '\0') {
198 *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
199 strcpy(*TruePath, gVFS_RootMount->MountPoint);
201 LEAVE('p', gVFS_RootMount->RootNode);
202 return gVFS_RootMount->RootNode;
205 // Check if there is anything mounted
207 Warning("WTF! There's nothing mounted?");
212 for(mnt = gVFS_Mounts;
217 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
219 // Length Check - If the length is smaller than the longest match sofar
220 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
222 cmp = strcmp(Path, mnt->MountPoint);
225 // Fast Break - Request Mount Root
228 *TruePath = malloc( mnt->MountPointLen+1 );
229 strcpy(*TruePath, mnt->MountPoint);
231 LEAVE('p', mnt->RootNode);
232 return mnt->RootNode;
235 // Not a match, continue
236 if(cmp != '/') continue;
241 /*if(!longestMount) {
242 Log("VFS_ParsePath - ERROR: No Root Node\n");
246 // Save to shorter variable
249 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
254 *TruePath = malloc( mnt->MountPointLen+1 );
255 strcpy(*TruePath, mnt->MountPoint);
256 retLength = mnt->MountPointLen;
259 curNode = mnt->RootNode;
260 curNode->ReferenceCount ++;
262 ofs = mnt->MountPointLen+1;
263 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs = nextSlash + 1)
265 char pathEle[nextSlash+1];
268 if(nextSlash == 0) continue;
270 memcpy(pathEle, &Path[ofs], nextSlash);
271 pathEle[nextSlash] = 0;
273 // Check permissions on root of filesystem
274 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
275 if(curNode->Close) curNode->Close( curNode );
280 //Log("Permissions fail on '%s'", Path);
285 // Check if the node has a FindDir method
286 if( !curNode->FindDir )
288 if(curNode->Close) curNode->Close(curNode);
293 //Log("FindDir fail on '%s'", Path);
297 LOG("FindDir(%p, '%s')", curNode, pathEle);
299 tmpNode = curNode->FindDir(curNode, pathEle);
300 LOG("tmpNode = %p", tmpNode);
301 if(curNode->Close) curNode->Close(curNode);
306 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
311 //Log("Child fail on '%s' ('%s)", Path, pathEle);
316 // Handle Symbolic Links
317 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
322 tmp = malloc( curNode->Size + 1 );
324 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
326 if(curNode->Close) curNode->Close(curNode);
330 curNode->Read( curNode, 0, curNode->Size, tmp );
331 tmp[ curNode->Size ] = '\0';
333 // Parse Symlink Path
334 curNode = VFS_ParsePath(tmp, TruePath);
336 Log_Debug("VFS", "*TruePath='%s'", *TruePath);
340 Log("Symlink fail '%s'", tmp);
341 free(tmp); // Free temp string
351 retLength = strlen(*TruePath);
357 // Handle Non-Directories
358 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
360 Warning("VFS_ParsePath - File in directory context");
361 if(TruePath) free(*TruePath);
366 // Check if path needs extending
367 if(!TruePath) continue;
369 // Increase buffer space
370 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
371 // Check if allocation succeeded
373 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
375 if(curNode->Close) curNode->Close(curNode);
381 (*TruePath)[retLength] = '/';
382 strcpy(*TruePath+retLength+1, pathEle);
384 retLength += nextSlash;
388 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
389 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
390 LOG("tmpNode = %p", tmpNode);
391 if(curNode->Close) curNode->Close(curNode);
392 // Check if file was found
394 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
395 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
396 if(TruePath) free(*TruePath);
397 if(curNode->Close) curNode->Close(curNode);
404 // Increase buffer space
405 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
406 // Check if allocation succeeded
408 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
410 if(tmpNode->Close) tmpNode->Close(curNode);
416 (*TruePath)[retLength] = '/';
417 strcpy(*TruePath + retLength + 1, &Path[ofs]);
419 //retLength += strlen(tmpNode->Name) + 1;
427 * \fn int VFS_Open(char *Path, Uint Mode)
430 int VFS_Open(char *Path, Uint Mode)
436 ENTER("sPath xMode", Path, Mode);
439 absPath = VFS_GetAbsPath(Path);
440 LOG("absPath = \"%s\"", absPath);
441 // Parse path and get mount point
442 node = VFS_ParsePath(absPath, NULL);
443 // Free generated path
447 LOG("Cannot find node");
452 // Check for symlinks
453 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
456 Warning("No read method on symlink");
460 absPath = malloc(node->Size+1); // Allocate Buffer
461 node->Read( node, 0, node->Size, absPath ); // Read Path
463 absPath[ node->Size ] = '\0'; // End String
464 if(node->Close) node->Close( node ); // Close old node
465 node = VFS_ParsePath(absPath, NULL); // Get new node
466 free( absPath ); // Free allocated path
470 LOG("Cannot find node");
476 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
477 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
478 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
483 if( !VFS_CheckACL(node, i) ) {
484 if(node->Close) node->Close( node );
485 Log("VFS_Open: Permissions Failed");
490 // Check for a user open
491 if(Mode & VFS_OPENFLAG_USER)
494 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
497 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
498 for(addr = 0; addr < size; addr += 0x1000)
499 MM_Allocate( (Uint)gaUserHandles + addr );
500 memset( gaUserHandles, 0, size );
503 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
505 if(gaUserHandles[i].Node) continue;
506 gaUserHandles[i].Node = node;
507 gaUserHandles[i].Position = 0;
508 gaUserHandles[i].Mode = Mode;
515 // Allocate space if not already
516 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
519 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
520 for(addr = 0; addr < size; addr += 0x1000)
521 MM_Allocate( (Uint)gaKernelHandles + addr );
522 memset( gaKernelHandles, 0, size );
525 for(i=0;i<MAX_KERNEL_FILES;i++)
527 if(gaKernelHandles[i].Node) continue;
528 gaKernelHandles[i].Node = node;
529 gaKernelHandles[i].Position = 0;
530 gaKernelHandles[i].Mode = Mode;
531 LEAVE('x', i|VFS_KERNEL_FLAG);
532 return i|VFS_KERNEL_FLAG;
536 Log("VFS_Open: Out of handles");
543 * \brief Open a file from an open directory
545 int VFS_OpenChild(Uint *Errno, int FD, char *Name, Uint Mode)
552 h = VFS_GetHandle(FD);
554 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
555 if(Errno) *Errno = EINVAL;
560 // Check for directory
561 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
562 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
563 if(Errno) *Errno = ENOTDIR;
569 node = h->Node->FindDir(h->Node, Name);
571 if(Errno) *Errno = ENOENT;
577 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
578 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
579 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
582 if( !VFS_CheckACL(node, i) ) {
583 if(node->Close) node->Close( node );
584 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
585 if(Errno) *Errno = EACCES;
590 // Check for a user open
591 if(Mode & VFS_OPENFLAG_USER)
594 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
597 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
598 for(addr = 0; addr < size; addr += 0x1000)
599 MM_Allocate( (Uint)gaUserHandles + addr );
600 memset( gaUserHandles, 0, size );
603 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
605 if(gaUserHandles[i].Node) continue;
606 gaUserHandles[i].Node = node;
607 gaUserHandles[i].Position = 0;
608 gaUserHandles[i].Mode = Mode;
615 // Allocate space if not already
616 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
619 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
620 for(addr = 0; addr < size; addr += 0x1000)
621 MM_Allocate( (Uint)gaKernelHandles + addr );
622 memset( gaKernelHandles, 0, size );
625 for(i=0;i<MAX_KERNEL_FILES;i++)
627 if(gaKernelHandles[i].Node) continue;
628 gaKernelHandles[i].Node = node;
629 gaKernelHandles[i].Position = 0;
630 gaKernelHandles[i].Mode = Mode;
631 LEAVE('x', i|VFS_KERNEL_FLAG);
632 return i|VFS_KERNEL_FLAG;
636 Log_Error("VFS", "VFS_OpenChild - Out of handles");
637 if(Errno) *Errno = ENFILE;
643 * \fn void VFS_Close(int FD)
644 * \brief Closes an open file handle
646 void VFS_Close(int FD)
651 h = VFS_GetHandle(FD);
653 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
657 #if VALIDATE_VFS_FUNCTIPONS
658 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
659 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
660 h->Node, h->Node->Close);
666 h->Node->Close( h->Node );
672 * \brief Change current working directory
674 int VFS_ChDir(char *Dest)
681 buf = VFS_GetAbsPath(Dest);
683 Log("VFS_ChDir: Path expansion failed");
687 // Check if path exists
688 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
690 Log("VFS_ChDir: Path is invalid");
694 // Get node so we can check for directory
695 h = VFS_GetHandle(fd);
696 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
697 Log("VFS_ChDir: Path is not a directory");
705 // Free old working directory
706 if( CFGPTR(CFG_VFS_CWD) )
707 free( CFGPTR(CFG_VFS_CWD) );
709 CFGPTR(CFG_VFS_CWD) = buf;
711 Log("Updated CWD to '%s'", buf);
717 * \fn int VFS_ChRoot(char *New)
718 * \brief Change current root directory
720 int VFS_ChRoot(char *New)
726 if(New[0] == '/' && New[1] == '\0')
727 return 1; // What a useless thing to ask!
730 buf = VFS_GetAbsPath(New);
732 LOG("Path expansion failed");
736 // Check if path exists
737 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
739 LOG("Path is invalid");
743 // Get node so we can check for directory
744 h = VFS_GetHandle(fd);
745 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
746 LOG("Path is not a directory");
754 // Free old working directory
755 if( CFGPTR(CFG_VFS_CHROOT) )
756 free( CFGPTR(CFG_VFS_CHROOT) );
758 CFGPTR(CFG_VFS_CHROOT) = buf;
760 LOG("Updated Root to '%s'", buf);
766 * \fn tVFS_Handle *VFS_GetHandle(int FD)
767 * \brief Gets a pointer to the handle information structure
769 tVFS_Handle *VFS_GetHandle(int FD)
773 //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
775 if(FD < 0) return NULL;
777 if(FD & VFS_KERNEL_FLAG) {
778 FD &= (VFS_KERNEL_FLAG - 1);
779 if(FD >= MAX_KERNEL_FILES) return NULL;
780 h = &gaKernelHandles[ FD ];
782 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
783 h = &gaUserHandles[ FD ];
786 if(h->Node == NULL) return NULL;
787 //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);