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;
240 // Save to shorter variable
243 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
248 *TruePath = malloc( mnt->MountPointLen+1 );
249 strcpy(*TruePath, mnt->MountPoint);
250 retLength = mnt->MountPointLen;
253 curNode = mnt->RootNode;
254 curNode->ReferenceCount ++;
256 ofs = mnt->MountPointLen+1;
257 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
259 char pathEle[nextSlash+1];
262 if(nextSlash == 0) continue;
264 memcpy(pathEle, &Path[ofs], nextSlash);
265 pathEle[nextSlash] = 0;
267 // Check permissions on root of filesystem
268 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
269 if(curNode->Close) curNode->Close( curNode );
274 //Log("Permissions fail on '%s'", Path);
279 // Check if the node has a FindDir method
280 if( !curNode->FindDir )
282 if(curNode->Close) curNode->Close(curNode);
287 //Log("FindDir fail on '%s'", Path);
291 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
293 tmpNode = curNode->FindDir(curNode, pathEle);
294 LOG("tmpNode = %p", tmpNode);
296 //LOG2("curNode->Close = %p", curNode->Close);
297 curNode->Close(curNode);
303 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
308 //Log("Child fail on '%s' ('%s)", Path, pathEle);
313 // Handle Symbolic Links
314 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
320 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
322 if(curNode->Close) curNode->Close(curNode);
323 // No need to free *TruePath, see above
328 tmp = malloc( curNode->Size + 1 );
330 Log_Warning("VFS", "VFS_ParsePath - Malloc failure");
331 // No need to free *TruePath, see above
335 curNode->Read( curNode, 0, curNode->Size, tmp );
336 tmp[ curNode->Size ] = '\0';
338 // Parse Symlink Path
339 curNode = VFS_ParsePath(tmp, TruePath);
341 LOG("VFS", "*TruePath='%s'", *TruePath);
345 Log_Debug("VFS", "Symlink fail '%s'", tmp);
346 free(tmp); // Free temp string
347 if(TruePath) free(TruePath);
357 retLength = strlen(*TruePath);
363 // Handle Non-Directories
364 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
366 Warning("VFS_ParsePath - File in directory context");
367 if(TruePath) free(*TruePath);
372 // Check if path needs extending
373 if(!TruePath) continue;
375 // Increase buffer space
376 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
377 // Check if allocation succeeded
379 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
382 if(curNode->Close) curNode->Close(curNode);
388 (*TruePath)[retLength] = '/';
389 strcpy(*TruePath+retLength+1, pathEle);
391 LOG("*TruePath = '%s'", *TruePath);
394 retLength += nextSlash + 1;
397 if( !curNode->FindDir ) {
398 if(curNode->Close) curNode->Close(curNode);
403 Log("FindDir fail on '%s'", Path);
409 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
410 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
411 LOG("tmpNode = %p", tmpNode);
412 if(curNode->Close) curNode->Close(curNode);
413 // Check if file was found
415 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
416 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
417 if(TruePath) free(*TruePath);
418 if(curNode->Close) curNode->Close(curNode);
425 // Increase buffer space
426 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
427 // Check if allocation succeeded
429 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
431 if(tmpNode->Close) tmpNode->Close(curNode);
437 (*TruePath)[retLength] = '/';
438 strcpy(*TruePath + retLength + 1, &Path[ofs]);
440 //retLength += strlen(tmpNode->Name) + 1;
448 * \fn int VFS_Open(char *Path, Uint Mode)
451 int VFS_Open(char *Path, Uint Mode)
457 ENTER("sPath xMode", Path, Mode);
460 absPath = VFS_GetAbsPath(Path);
461 LOG("absPath = \"%s\"", absPath);
462 // Parse path and get mount point
463 node = VFS_ParsePath(absPath, NULL);
464 // Free generated path
468 LOG("Cannot find node");
473 // Check for symlinks
474 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
477 Warning("No read method on symlink");
481 absPath = malloc(node->Size+1); // Allocate Buffer
482 node->Read( node, 0, node->Size, absPath ); // Read Path
484 absPath[ node->Size ] = '\0'; // End String
485 if(node->Close) node->Close( node ); // Close old node
486 node = VFS_ParsePath(absPath, NULL); // Get new node
487 free( absPath ); // Free allocated path
491 LOG("Cannot find node");
497 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
498 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
499 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
504 if( !VFS_CheckACL(node, i) ) {
505 if(node->Close) node->Close( node );
506 Log("VFS_Open: Permissions Failed");
511 // Check for a user open
512 if(Mode & VFS_OPENFLAG_USER)
515 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
518 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
519 for(addr = 0; addr < size; addr += 0x1000)
520 MM_Allocate( (Uint)gaUserHandles + addr );
521 memset( gaUserHandles, 0, size );
524 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
526 if(gaUserHandles[i].Node) continue;
527 gaUserHandles[i].Node = node;
528 gaUserHandles[i].Position = 0;
529 gaUserHandles[i].Mode = Mode;
536 // Allocate space if not already
537 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
540 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
541 for(addr = 0; addr < size; addr += 0x1000)
542 MM_Allocate( (Uint)gaKernelHandles + addr );
543 memset( gaKernelHandles, 0, size );
546 for(i=0;i<MAX_KERNEL_FILES;i++)
548 if(gaKernelHandles[i].Node) continue;
549 gaKernelHandles[i].Node = node;
550 gaKernelHandles[i].Position = 0;
551 gaKernelHandles[i].Mode = Mode;
552 LEAVE('x', i|VFS_KERNEL_FLAG);
553 return i|VFS_KERNEL_FLAG;
557 Log("VFS_Open: Out of handles");
564 * \brief Open a file from an open directory
566 int VFS_OpenChild(Uint *Errno, int FD, char *Name, Uint Mode)
573 h = VFS_GetHandle(FD);
575 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
576 if(Errno) *Errno = EINVAL;
581 // Check for directory
582 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
583 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
584 if(Errno) *Errno = ENOTDIR;
590 node = h->Node->FindDir(h->Node, Name);
592 if(Errno) *Errno = ENOENT;
598 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
599 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
600 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
603 if( !VFS_CheckACL(node, i) ) {
604 if(node->Close) node->Close( node );
605 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
606 if(Errno) *Errno = EACCES;
611 // Check for a user open
612 if(Mode & VFS_OPENFLAG_USER)
615 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
618 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
619 for(addr = 0; addr < size; addr += 0x1000)
620 MM_Allocate( (Uint)gaUserHandles + addr );
621 memset( gaUserHandles, 0, size );
624 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
626 if(gaUserHandles[i].Node) continue;
627 gaUserHandles[i].Node = node;
628 gaUserHandles[i].Position = 0;
629 gaUserHandles[i].Mode = Mode;
636 // Allocate space if not already
637 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
640 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
641 for(addr = 0; addr < size; addr += 0x1000)
642 MM_Allocate( (Uint)gaKernelHandles + addr );
643 memset( gaKernelHandles, 0, size );
646 for(i=0;i<MAX_KERNEL_FILES;i++)
648 if(gaKernelHandles[i].Node) continue;
649 gaKernelHandles[i].Node = node;
650 gaKernelHandles[i].Position = 0;
651 gaKernelHandles[i].Mode = Mode;
652 LEAVE('x', i|VFS_KERNEL_FLAG);
653 return i|VFS_KERNEL_FLAG;
657 Log_Error("VFS", "VFS_OpenChild - Out of handles");
658 if(Errno) *Errno = ENFILE;
664 * \fn void VFS_Close(int FD)
665 * \brief Closes an open file handle
667 void VFS_Close(int FD)
672 h = VFS_GetHandle(FD);
674 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
678 #if VALIDATE_VFS_FUNCTIPONS
679 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
680 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
681 h->Node, h->Node->Close);
687 h->Node->Close( h->Node );
693 * \brief Change current working directory
695 int VFS_ChDir(char *Dest)
702 buf = VFS_GetAbsPath(Dest);
704 Log("VFS_ChDir: Path expansion failed");
708 // Check if path exists
709 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
711 Log("VFS_ChDir: Path is invalid");
715 // Get node so we can check for directory
716 h = VFS_GetHandle(fd);
717 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
718 Log("VFS_ChDir: Path is not a directory");
726 // Free old working directory
727 if( CFGPTR(CFG_VFS_CWD) )
728 free( CFGPTR(CFG_VFS_CWD) );
730 CFGPTR(CFG_VFS_CWD) = buf;
732 Log("Updated CWD to '%s'", buf);
738 * \fn int VFS_ChRoot(char *New)
739 * \brief Change current root directory
741 int VFS_ChRoot(char *New)
747 if(New[0] == '/' && New[1] == '\0')
748 return 1; // What a useless thing to ask!
751 buf = VFS_GetAbsPath(New);
753 LOG("Path expansion failed");
757 // Check if path exists
758 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
760 LOG("Path is invalid");
764 // Get node so we can check for directory
765 h = VFS_GetHandle(fd);
766 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
767 LOG("Path is not a directory");
775 // Free old working directory
776 if( CFGPTR(CFG_VFS_CHROOT) )
777 free( CFGPTR(CFG_VFS_CHROOT) );
779 CFGPTR(CFG_VFS_CHROOT) = buf;
781 LOG("Updated Root to '%s'", buf);
787 * \fn tVFS_Handle *VFS_GetHandle(int FD)
788 * \brief Gets a pointer to the handle information structure
790 tVFS_Handle *VFS_GetHandle(int FD)
794 //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
796 if(FD < 0) return NULL;
798 if(FD & VFS_KERNEL_FLAG) {
799 FD &= (VFS_KERNEL_FLAG - 1);
800 if(FD >= MAX_KERNEL_FILES) return NULL;
801 h = &gaKernelHandles[ FD ];
803 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
804 h = &gaUserHandles[ FD ];
807 if(h->Node == NULL) return NULL;
808 //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);