3 * - Open, Close and ChDir
12 #define OPEN_MOUNT_ROOT 1
13 #define MAX_PATH_SLASHES 256
14 #define MAX_NESTED_LINKS 4
15 #define MAX_PATH_LEN 255
18 extern tVFS_Node gVFS_MemRoot;
19 extern tVFS_Mount *gVFS_RootMount;
20 extern int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode);
23 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode );
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 const char *chroot = CFGPTR(CFG_VFS_CHROOT);
40 const char *cwd = CFGPTR(CFG_VFS_CWD);
47 ret = malloc(strlen(Path)+1);
49 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
60 chrootLen = strlen(chroot);
62 // Check if the path is already absolute
64 ret = malloc(chrootLen + pathLen + 1);
66 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
69 strcpy(ret + chrootLen, Path);
79 // Prepend the current directory
80 ret = malloc(chrootLen + cwdLen + 1 + pathLen + 1 );
81 strcpy(ret+chrootLen, cwd);
83 strcpy(ret+chrootLen+cwdLen+1, Path);
84 //Log("ret = '%s'", ret);
88 pathComps[iPos++] = tmpStr = ret+chrootLen+1;
93 pathComps[iPos++] = tmpStr;
94 if(iPos == MAX_PATH_SLASHES) {
95 LOG("Path '%s' has too many elements", Path);
102 pathComps[iPos] = NULL;
106 while(pathComps[iPos])
108 tmpStr = pathComps[iPos];
109 // Always Increment iPos
112 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
119 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
124 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
130 pathComps[iPos2] = tmpStr;
133 pathComps[iPos2] = NULL;
136 iPos2 = chrootLen + 1; iPos = 0;
138 while(pathComps[iPos])
140 tmpStr = pathComps[iPos];
141 while(*tmpStr && *tmpStr != '/')
143 ret[iPos2++] = *tmpStr;
154 // Prepend the chroot
156 memcpy( ret, chroot, chrootLen );
159 // Log_Debug("VFS", "VFS_GetAbsPath: RETURN '%s'", ret);
164 * \fn char *VFS_ParsePath(const char *Path, char **TruePath)
165 * \brief Parses a path, resolving sysmlinks and applying permissions
167 tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath, tVFS_Mount **MountPoint)
169 tVFS_Mount *mnt, *longestMount;
170 int cmp, retLength = 0;
172 int iNestedLinks = 0;
173 tVFS_Node *curNode, *tmpNode;
175 char path_buffer[MAX_PATH_LEN+1];
177 ENTER("sPath pTruePath", Path, TruePath);
182 *TruePath = malloc(strlen(Path)+1);
183 strcpy(*TruePath, Path);
185 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
194 // 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 if(MountPoint) *MountPoint = gVFS_RootMount;
201 LEAVE('p', gVFS_RootMount->RootNode);
202 return gVFS_RootMount->RootNode;
205 // Check if there is anything mounted
207 Log_Error("VFS", "VFS_ParsePath - No filesystems mounted");
212 longestMount = gVFS_RootMount;
213 for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
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);
232 LEAVE('p', mnt->RootNode);
233 return mnt->RootNode;
236 // Not a match, continue
237 if(cmp != '/') continue;
241 // Save to shorter variable
244 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
249 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
250 *TruePath = malloc( strlen(Path) + 1 );
251 strcpy(*TruePath, mnt->MountPoint);
252 retLength = mnt->MountPointLen;
255 curNode = mnt->RootNode;
256 curNode->ReferenceCount ++;
258 ofs = mnt->MountPointLen+1;
259 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
261 char pathEle[nextSlash+1];
264 if(nextSlash == 0) continue;
266 memcpy(pathEle, &Path[ofs], nextSlash);
267 pathEle[nextSlash] = 0;
269 // Check permissions on root of filesystem
270 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
271 if(curNode->Close) curNode->Close( curNode );
276 //Log("Permissions fail on '%s'", Path);
281 // Check if the node has a FindDir method
282 if( !curNode->FindDir )
284 if(curNode->Close) curNode->Close(curNode);
289 //Log("FindDir fail on '%s'", Path);
293 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
295 tmpNode = curNode->FindDir(curNode, pathEle);
296 LOG("tmpNode = %p", tmpNode);
298 //LOG2("curNode->Close = %p", curNode->Close);
299 curNode->Close(curNode);
305 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
310 //Log("Child fail on '%s' ('%s)", Path, pathEle);
315 // Handle Symbolic Links
316 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
322 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
324 if(curNode->Close) curNode->Close(curNode);
325 // No need to free *TruePath, it should already be NULL
330 if(iNestedLinks > MAX_NESTED_LINKS) {
331 if(curNode->Close) curNode->Close(curNode);
336 // Parse Symlink Path
337 // - Just update the path variable and restart the function
338 // > Count nested symlinks and limit to some value (counteracts loops)
340 int remlen = strlen(Path) - (ofs + nextSlash);
341 if( curNode->Size + remlen > MAX_PATH_LEN ) {
342 if(curNode->Close) curNode->Close(curNode);
343 Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
347 curNode->Read( curNode, 0, curNode->Size, path_buffer );
348 path_buffer[ curNode->Size ] = '\0';
349 strcat(path_buffer, Path + ofs+nextSlash);
352 // Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
360 // Handle Non-Directories
361 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
363 Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
364 if(TruePath) free(*TruePath);
369 // Check if path needs extending
370 if(!TruePath) continue;
372 // Increase buffer space
373 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
374 // Check if allocation succeeded
376 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
379 if(curNode->Close) curNode->Close(curNode);
385 (*TruePath)[retLength] = '/';
386 strcpy(*TruePath+retLength+1, pathEle);
388 LOG("*TruePath = '%s'", *TruePath);
391 retLength += nextSlash + 1;
394 if( !curNode->FindDir ) {
395 if(curNode->Close) curNode->Close(curNode);
400 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
406 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
407 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
408 LOG("tmpNode = %p", tmpNode);
409 if(curNode->Close) curNode->Close(curNode);
410 // Check if file was found
412 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
413 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
414 if(TruePath) free(*TruePath);
415 if(curNode->Close) curNode->Close(curNode);
422 // Increase buffer space
423 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
424 // Check if allocation succeeded
426 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
428 if(tmpNode->Close) tmpNode->Close(curNode);
434 (*TruePath)[retLength] = '/';
435 strcpy(*TruePath + retLength + 1, &Path[ofs]);
437 //retLength += strlen(tmpNode->Name) + 1;
449 * \brief Create and return a handle number for the given node and mode
451 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode )
455 ENTER("pNode pMount iMode", Node, Mount, Mode);
458 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
459 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
460 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
465 if( !VFS_CheckACL(Node, i) ) {
466 if(Node->Close) Node->Close( Node );
467 Log_Log("VFS", "VFS_int_CreateHandle: Permissions Failed");
472 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), Node, Mode );
474 Log_Notice("VFS", "VFS_int_CreateHandle: Out of handles");
479 VFS_GetHandle(i)->Mount = Mount;
485 * \fn int VFS_Open(const char *Path, Uint Mode)
488 int VFS_Open(const char *Path, Uint Mode)
494 ENTER("sPath xMode", Path, Mode);
497 absPath = VFS_GetAbsPath(Path);
498 if(absPath == NULL) {
499 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
502 LOG("absPath = \"%s\"", absPath);
503 // Parse path and get mount point
504 node = VFS_ParsePath(absPath, NULL, &mnt);
505 // Free generated path
509 LOG("Cannot find node");
514 // Check for symlinks
515 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
517 char tmppath[node->Size+1];
518 if( node->Size > MAX_PATH_LEN ) {
519 Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
523 Log_Warning("VFS", "VFS_Open - No read method on symlink");
526 // Read symlink's path
527 node->Read( node, 0, node->Size, tmppath );
528 tmppath[ node->Size ] = '\0';
529 if(node->Close) node->Close( node );
531 node = VFS_ParsePath(tmppath, NULL, &mnt);
533 LOG("Cannot find symlink target node (%s)", tmppath);
539 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
544 * \brief Open a file from an open directory
546 int VFS_OpenChild(int FD, const char *Name, Uint Mode)
551 ENTER("xFD sName xMode", FD, Name, Mode);
554 h = VFS_GetHandle(FD);
556 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
561 // Check for directory
562 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
563 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
569 node = h->Node->FindDir(h->Node, Name);
575 LEAVE_RET('x', VFS_int_CreateHandle(node, h->Mount, Mode));
578 int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode)
583 ENTER("iMount XInode xMode", Mount, Inode, Mode);
586 mnt = VFS_GetMountByIdent(Mount);
588 LOG("Mount point ident invalid");
593 // Does the filesystem support this?
594 if( !mnt->Filesystem->GetNodeFromINode ) {
595 LOG("Filesystem does not support inode accesses");
601 node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode);
603 LOG("Unable to find inode");
608 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
612 * \fn void VFS_Close(int FD)
613 * \brief Closes an open file handle
615 void VFS_Close(int FD)
620 h = VFS_GetHandle(FD);
622 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
626 #if VALIDATE_VFS_FUNCTIPONS
627 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
628 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
629 h->Node, h->Node->Close);
635 h->Node->Close( h->Node );
641 * \brief Change current working directory
643 int VFS_ChDir(const char *Dest)
650 buf = VFS_GetAbsPath(Dest);
652 Log_Notice("VFS", "VFS_ChDir: Path expansion failed");
656 // Check if path exists
657 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
659 Log_Notice("VFS", "VFS_ChDir: Path is invalid");
663 // Get node so we can check for directory
664 h = VFS_GetHandle(fd);
665 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
666 Log("VFS_ChDir: Path is not a directory");
674 // Free old working directory
675 if( CFGPTR(CFG_VFS_CWD) )
676 free( CFGPTR(CFG_VFS_CWD) );
678 CFGPTR(CFG_VFS_CWD) = buf;
680 Log("Updated CWD to '%s'", buf);
686 * \fn int VFS_ChRoot(char *New)
687 * \brief Change current root directory
689 int VFS_ChRoot(const char *New)
695 if(New[0] == '/' && New[1] == '\0')
696 return 1; // What a useless thing to ask!
699 buf = VFS_GetAbsPath(New);
701 LOG("Path expansion failed");
705 // Check if path exists
706 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
708 LOG("Path is invalid");
712 // Get node so we can check for directory
713 h = VFS_GetHandle(fd);
714 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
715 LOG("Path is not a directory");
723 // Free old working directory
724 if( CFGPTR(CFG_VFS_CHROOT) )
725 free( CFGPTR(CFG_VFS_CHROOT) );
727 CFGPTR(CFG_VFS_CHROOT) = buf;
729 LOG("Updated Root to '%s'", buf);