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_Mount *gVFS_RootMount;
19 extern int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode);
20 extern tVFS_Node *VFS_MemFile_Create(const char *Path);
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);
180 if(Threads_GetUID() == 0 && Path[0] == '$') {
182 *TruePath = malloc(strlen(Path)+1);
183 strcpy(*TruePath, Path);
185 curNode = VFS_MemFile_Create(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 //Log("Permissions fail on '%s'", Path);
275 // Check if the node has a FindDir method
276 if( !curNode->Type->FindDir )
278 //Log("FindDir fail on '%s'", Path);
281 LOG("FindDir{=%p}(%p, '%s')", curNode->Type->FindDir, curNode, pathEle);
283 tmpNode = curNode->Type->FindDir(curNode, pathEle);
284 LOG("tmpNode = %p", tmpNode);
285 _CloseNode( curNode );
290 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
294 // Handle Symbolic Links
295 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
300 if(!curNode->Type || !curNode->Type->Read) {
301 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
306 if(iNestedLinks > MAX_NESTED_LINKS) {
307 Log_Notice("VFS", "VFS_ParsePath - Nested link limit exceeded");
311 // Parse Symlink Path
312 // - Just update the path variable and restart the function
313 // > Count nested symlinks and limit to some value (counteracts loops)
315 int remlen = strlen(Path) - (ofs + nextSlash);
316 if( curNode->Size + remlen > MAX_PATH_LEN ) {
317 Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
320 curNode->Type->Read( curNode, 0, curNode->Size, path_buffer );
321 path_buffer[ curNode->Size ] = '\0';
322 LOG("path_buffer = '%s'", path_buffer);
323 strcat(path_buffer, Path + ofs+nextSlash);
326 // Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
331 LOG("Symlink -> '%s', restart", Path);
335 // Handle Non-Directories
336 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
338 Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
342 // Check if path needs extending
343 if(!TruePath) continue;
345 // Increase buffer space
346 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
347 // Check if allocation succeeded
349 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
354 (*TruePath)[retLength] = '/';
355 strcpy(*TruePath+retLength+1, pathEle);
357 LOG("*TruePath = '%s'", *TruePath);
360 retLength += nextSlash + 1;
363 // Check final finddir call
364 if( !curNode->Type || !curNode->Type->FindDir ) {
365 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
370 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
371 tmpNode = curNode->Type->FindDir(curNode, &Path[ofs]);
372 LOG("tmpNode = %p", tmpNode);
373 // Check if file was found
375 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
378 _CloseNode( curNode );
382 // Increase buffer space
383 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
384 // Check if allocation succeeded
386 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
391 (*TruePath)[retLength] = '/';
392 strcpy(*TruePath + retLength + 1, &Path[ofs]);
394 //retLength += strlen(tmpNode->Name) + 1;
405 _CloseNode( curNode );
407 if(TruePath && *TruePath) {
416 * \brief Create and return a handle number for the given node and mode
418 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode )
422 ENTER("pNode pMount xMode", Node, Mount, Mode);
425 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
426 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
427 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
432 if( !VFS_CheckACL(Node, i) ) {
434 Log_Log("VFS", "VFS_int_CreateHandle: Permissions Failed");
439 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), Node, Mode );
441 Log_Notice("VFS", "VFS_int_CreateHandle: Out of handles");
446 VFS_GetHandle(i)->Mount = Mount;
452 * \fn int VFS_Open(const char *Path, Uint Mode)
455 int VFS_Open(const char *Path, Uint Mode)
461 ENTER("sPath xMode", Path, Mode);
464 absPath = VFS_GetAbsPath(Path);
465 if(absPath == NULL) {
466 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
469 LOG("absPath = \"%s\"", absPath);
470 // Parse path and get mount point
471 node = VFS_ParsePath(absPath, NULL, &mnt);
472 // Free generated path
476 LOG("Cannot find node");
481 // Check for symlinks
482 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
484 char tmppath[node->Size+1];
485 if( node->Size > MAX_PATH_LEN ) {
486 Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
489 if( !node->Type || !node->Type->Read ) {
490 Log_Warning("VFS", "VFS_Open - No read method on symlink");
493 // Read symlink's path
494 node->Type->Read( node, 0, node->Size, tmppath );
495 tmppath[ node->Size ] = '\0';
498 node = VFS_ParsePath(tmppath, NULL, &mnt);
500 LOG("Cannot find symlink target node (%s)", tmppath);
506 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
511 * \brief Open a file from an open directory
513 int VFS_OpenChild(int FD, const char *Name, Uint Mode)
518 ENTER("xFD sName xMode", FD, Name, Mode);
521 h = VFS_GetHandle(FD);
523 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
528 // Check for directory
529 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
530 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory");
536 if( !h->Node->Type || !h->Node->Type->FindDir ) {
537 Log_Error("VFS", "VFS_OpenChild - Node does not have a type/is missing FindDir");
543 node = h->Node->Type->FindDir(h->Node, Name);
549 LEAVE_RET('x', VFS_int_CreateHandle(node, h->Mount, Mode));
552 int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode)
557 ENTER("iMount XInode xMode", Mount, Inode, Mode);
560 mnt = VFS_GetMountByIdent(Mount);
562 LOG("Mount point ident invalid");
567 // Does the filesystem support this?
568 if( !mnt->Filesystem->GetNodeFromINode ) {
569 LOG("Filesystem does not support inode accesses");
575 node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode);
577 LOG("Unable to find inode");
582 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
586 * \fn void VFS_Close(int FD)
587 * \brief Closes an open file handle
589 void VFS_Close(int FD)
594 h = VFS_GetHandle(FD);
596 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
600 #if VALIDATE_VFS_FUNCTIPONS
601 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
602 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
603 h->Node, h->Node->Close);
614 * \brief Change current working directory
616 int VFS_ChDir(const char *Dest)
623 buf = VFS_GetAbsPath(Dest);
625 Log_Notice("VFS", "VFS_ChDir: Path expansion failed");
629 // Check if path exists
630 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
632 Log_Notice("VFS", "VFS_ChDir: Path is invalid");
636 // Get node so we can check for directory
637 h = VFS_GetHandle(fd);
638 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
639 Log("VFS_ChDir: Path is not a directory");
647 // Free old working directory
648 if( CFGPTR(CFG_VFS_CWD) )
649 free( CFGPTR(CFG_VFS_CWD) );
651 CFGPTR(CFG_VFS_CWD) = buf;
653 Log("Updated CWD to '%s'", buf);
659 * \fn int VFS_ChRoot(char *New)
660 * \brief Change current root directory
662 int VFS_ChRoot(const char *New)
668 if(New[0] == '/' && New[1] == '\0')
669 return 1; // What a useless thing to ask!
672 buf = VFS_GetAbsPath(New);
674 LOG("Path expansion failed");
678 // Check if path exists
679 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
681 LOG("Path is invalid");
685 // Get node so we can check for directory
686 h = VFS_GetHandle(fd);
687 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
688 LOG("Path is not a directory");
696 // Free old working directory
697 if( CFGPTR(CFG_VFS_CHROOT) )
698 free( CFGPTR(CFG_VFS_CHROOT) );
700 CFGPTR(CFG_VFS_CHROOT) = buf;
702 LOG("Updated Root to '%s'", buf);