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(const 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);
195 // 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 an`ything 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) {
323 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
325 if(curNode->Close) curNode->Close(curNode);
326 // No need to free *TruePath, see above
331 tmp = malloc( curNode->Size + 1 );
333 Log_Warning("VFS", "VFS_ParsePath - Malloc failure");
334 // No need to free *TruePath, see above
338 curNode->Read( curNode, 0, curNode->Size, tmp );
339 tmp[ curNode->Size ] = '\0';
341 // Parse Symlink Path
342 curNode = VFS_ParsePath(tmp, TruePath);
344 LOG("VFS", "*TruePath='%s'", *TruePath);
348 Log_Debug("VFS", "Symlink fail '%s'", tmp);
349 free(tmp); // Free temp string
350 if(TruePath) free(TruePath);
360 retLength = strlen(*TruePath);
366 // Handle Non-Directories
367 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
369 Warning("VFS_ParsePath - File in directory context");
370 if(TruePath) free(*TruePath);
375 // Check if path needs extending
376 if(!TruePath) continue;
378 // Increase buffer space
379 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
380 // Check if allocation succeeded
382 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
385 if(curNode->Close) curNode->Close(curNode);
391 (*TruePath)[retLength] = '/';
392 strcpy(*TruePath+retLength+1, pathEle);
394 LOG("*TruePath = '%s'", *TruePath);
397 retLength += nextSlash + 1;
401 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
402 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
403 LOG("tmpNode = %p", tmpNode);
404 if(curNode->Close) curNode->Close(curNode);
405 // Check if file was found
407 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
408 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
409 if(TruePath) free(*TruePath);
410 if(curNode->Close) curNode->Close(curNode);
417 // Increase buffer space
418 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
419 // Check if allocation succeeded
421 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
423 if(tmpNode->Close) tmpNode->Close(curNode);
429 (*TruePath)[retLength] = '/';
430 strcpy(*TruePath + retLength + 1, &Path[ofs]);
432 //retLength += strlen(tmpNode->Name) + 1;
440 * \fn int VFS_Open(char *Path, Uint Mode)
443 int VFS_Open(char *Path, Uint Mode)
449 ENTER("sPath xMode", Path, Mode);
452 absPath = VFS_GetAbsPath(Path);
453 LOG("absPath = \"%s\"", absPath);
454 // Parse path and get mount point
455 node = VFS_ParsePath(absPath, NULL);
456 // Free generated path
460 LOG("Cannot find node");
465 // Check for symlinks
466 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
469 Warning("No read method on symlink");
473 absPath = malloc(node->Size+1); // Allocate Buffer
474 node->Read( node, 0, node->Size, absPath ); // Read Path
476 absPath[ node->Size ] = '\0'; // End String
477 if(node->Close) node->Close( node ); // Close old node
478 node = VFS_ParsePath(absPath, NULL); // Get new node
479 free( absPath ); // Free allocated path
483 LOG("Cannot find node");
489 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
490 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
491 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
496 if( !VFS_CheckACL(node, i) ) {
497 if(node->Close) node->Close( node );
498 Log("VFS_Open: Permissions Failed");
503 // Check for a user open
504 if(Mode & VFS_OPENFLAG_USER)
507 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
510 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
511 for(addr = 0; addr < size; addr += 0x1000)
512 MM_Allocate( (Uint)gaUserHandles + addr );
513 memset( gaUserHandles, 0, size );
516 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
518 if(gaUserHandles[i].Node) continue;
519 gaUserHandles[i].Node = node;
520 gaUserHandles[i].Position = 0;
521 gaUserHandles[i].Mode = Mode;
528 // Allocate space if not already
529 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
532 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
533 for(addr = 0; addr < size; addr += 0x1000)
534 MM_Allocate( (Uint)gaKernelHandles + addr );
535 memset( gaKernelHandles, 0, size );
538 for(i=0;i<MAX_KERNEL_FILES;i++)
540 if(gaKernelHandles[i].Node) continue;
541 gaKernelHandles[i].Node = node;
542 gaKernelHandles[i].Position = 0;
543 gaKernelHandles[i].Mode = Mode;
544 LEAVE('x', i|VFS_KERNEL_FLAG);
545 return i|VFS_KERNEL_FLAG;
549 Log("VFS_Open: Out of handles");
556 * \brief Open a file from an open directory
558 int VFS_OpenChild(Uint *Errno, int FD, char *Name, Uint Mode)
565 h = VFS_GetHandle(FD);
567 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
568 if(Errno) *Errno = EINVAL;
573 // Check for directory
574 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
575 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
576 if(Errno) *Errno = ENOTDIR;
582 node = h->Node->FindDir(h->Node, Name);
584 if(Errno) *Errno = ENOENT;
590 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
591 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
592 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
595 if( !VFS_CheckACL(node, i) ) {
596 if(node->Close) node->Close( node );
597 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
598 if(Errno) *Errno = EACCES;
603 // Check for a user open
604 if(Mode & VFS_OPENFLAG_USER)
607 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
610 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
611 for(addr = 0; addr < size; addr += 0x1000)
612 MM_Allocate( (Uint)gaUserHandles + addr );
613 memset( gaUserHandles, 0, size );
616 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
618 if(gaUserHandles[i].Node) continue;
619 gaUserHandles[i].Node = node;
620 gaUserHandles[i].Position = 0;
621 gaUserHandles[i].Mode = Mode;
628 // Allocate space if not already
629 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
632 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
633 for(addr = 0; addr < size; addr += 0x1000)
634 MM_Allocate( (Uint)gaKernelHandles + addr );
635 memset( gaKernelHandles, 0, size );
638 for(i=0;i<MAX_KERNEL_FILES;i++)
640 if(gaKernelHandles[i].Node) continue;
641 gaKernelHandles[i].Node = node;
642 gaKernelHandles[i].Position = 0;
643 gaKernelHandles[i].Mode = Mode;
644 LEAVE('x', i|VFS_KERNEL_FLAG);
645 return i|VFS_KERNEL_FLAG;
649 Log_Error("VFS", "VFS_OpenChild - Out of handles");
650 if(Errno) *Errno = ENFILE;
656 * \fn void VFS_Close(int FD)
657 * \brief Closes an open file handle
659 void VFS_Close(int FD)
664 h = VFS_GetHandle(FD);
666 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
670 #if VALIDATE_VFS_FUNCTIPONS
671 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
672 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
673 h->Node, h->Node->Close);
679 h->Node->Close( h->Node );
685 * \brief Change current working directory
687 int VFS_ChDir(char *Dest)
694 buf = VFS_GetAbsPath(Dest);
696 Log("VFS_ChDir: Path expansion failed");
700 // Check if path exists
701 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
703 Log("VFS_ChDir: Path is invalid");
707 // Get node so we can check for directory
708 h = VFS_GetHandle(fd);
709 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
710 Log("VFS_ChDir: Path is not a directory");
718 // Free old working directory
719 if( CFGPTR(CFG_VFS_CWD) )
720 free( CFGPTR(CFG_VFS_CWD) );
722 CFGPTR(CFG_VFS_CWD) = buf;
724 Log("Updated CWD to '%s'", buf);
730 * \fn int VFS_ChRoot(char *New)
731 * \brief Change current root directory
733 int VFS_ChRoot(char *New)
739 if(New[0] == '/' && New[1] == '\0')
740 return 1; // What a useless thing to ask!
743 buf = VFS_GetAbsPath(New);
745 LOG("Path expansion failed");
749 // Check if path exists
750 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
752 LOG("Path is invalid");
756 // Get node so we can check for directory
757 h = VFS_GetHandle(fd);
758 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
759 LOG("Path is not a directory");
767 // Free old working directory
768 if( CFGPTR(CFG_VFS_CHROOT) )
769 free( CFGPTR(CFG_VFS_CHROOT) );
771 CFGPTR(CFG_VFS_CHROOT) = buf;
773 LOG("Updated Root to '%s'", buf);
779 * \fn tVFS_Handle *VFS_GetHandle(int FD)
780 * \brief Gets a pointer to the handle information structure
782 tVFS_Handle *VFS_GetHandle(int FD)
786 //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
788 if(FD < 0) return NULL;
790 if(FD & VFS_KERNEL_FLAG) {
791 FD &= (VFS_KERNEL_FLAG - 1);
792 if(FD >= MAX_KERNEL_FILES) return NULL;
793 h = &gaKernelHandles[ FD ];
795 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
796 h = &gaUserHandles[ FD ];
799 if(h->Node == NULL) return NULL;
800 //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);