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");
58 if( chroot == NULL ) {
62 chrootLen = strlen(chroot);
65 // Check if the path is already absolute
67 ret = malloc(chrootLen + pathLen + 1);
69 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
72 strcpy(ret + chrootLen, Path);
82 // Prepend the current directory
83 ret = malloc(chrootLen + cwdLen + 1 + pathLen + 1 );
84 strcpy(ret+chrootLen, cwd);
86 strcpy(ret+chrootLen+cwdLen+1, Path);
87 //Log("ret = '%s'", ret);
91 pathComps[iPos++] = tmpStr = ret+chrootLen+1;
96 pathComps[iPos++] = tmpStr;
97 if(iPos == MAX_PATH_SLASHES) {
98 LOG("Path '%s' has too many elements", Path);
105 pathComps[iPos] = NULL;
109 while(pathComps[iPos])
111 tmpStr = pathComps[iPos];
112 // Always Increment iPos
115 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
122 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
127 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
133 pathComps[iPos2] = tmpStr;
136 pathComps[iPos2] = NULL;
139 iPos2 = chrootLen + 1; iPos = 0;
141 while(pathComps[iPos])
143 tmpStr = pathComps[iPos];
144 while(*tmpStr && *tmpStr != '/')
146 ret[iPos2++] = *tmpStr;
157 // Prepend the chroot
158 memcpy( ret, chroot, chrootLen );
161 // Log_Debug("VFS", "VFS_GetAbsPath: RETURN '%s'", ret);
166 * \fn char *VFS_ParsePath(const char *Path, char **TruePath)
167 * \brief Parses a path, resolving sysmlinks and applying permissions
169 tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath, tVFS_Mount **MountPoint)
171 tVFS_Mount *mnt, *longestMount;
172 int cmp, retLength = 0;
174 int iNestedLinks = 0;
175 tVFS_Node *curNode, *tmpNode;
177 char path_buffer[MAX_PATH_LEN+1];
179 ENTER("sPath pTruePath", Path, TruePath);
184 *TruePath = malloc(strlen(Path)+1);
185 strcpy(*TruePath, Path);
187 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
196 // For root we always fast return
197 if(Path[0] == '/' && Path[1] == '\0') {
199 *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
200 strcpy(*TruePath, gVFS_RootMount->MountPoint);
202 if(MountPoint) *MountPoint = gVFS_RootMount;
203 LEAVE('p', gVFS_RootMount->RootNode);
204 return gVFS_RootMount->RootNode;
207 // Check if there is anything mounted
209 Log_Error("VFS", "VFS_ParsePath - No filesystems mounted");
214 longestMount = gVFS_RootMount;
215 for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
218 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
220 // Length Check - If the length is smaller than the longest match sofar
221 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
223 cmp = strcmp(Path, mnt->MountPoint);
226 // Fast Break - Request Mount Root
229 *TruePath = malloc( mnt->MountPointLen+1 );
230 strcpy(*TruePath, mnt->MountPoint);
234 LEAVE('p', mnt->RootNode);
235 return mnt->RootNode;
238 // Not a match, continue
239 if(cmp != '/') continue;
243 // Save to shorter variable
246 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
251 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
252 *TruePath = malloc( strlen(Path) + 1 );
253 strcpy(*TruePath, mnt->MountPoint);
254 retLength = mnt->MountPointLen;
257 curNode = mnt->RootNode;
258 curNode->ReferenceCount ++;
260 ofs = mnt->MountPointLen+1;
261 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
263 char pathEle[nextSlash+1];
266 if(nextSlash == 0) continue;
268 memcpy(pathEle, &Path[ofs], nextSlash);
269 pathEle[nextSlash] = 0;
271 // Check permissions on root of filesystem
272 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
273 if(curNode->Close) curNode->Close( curNode );
278 //Log("Permissions fail on '%s'", Path);
283 // Check if the node has a FindDir method
284 if( !curNode->FindDir )
286 if(curNode->Close) curNode->Close(curNode);
291 //Log("FindDir fail on '%s'", Path);
295 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
297 tmpNode = curNode->FindDir(curNode, pathEle);
298 LOG("tmpNode = %p", tmpNode);
300 //LOG2("curNode->Close = %p", curNode->Close);
301 curNode->Close(curNode);
307 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
312 //Log("Child fail on '%s' ('%s)", Path, pathEle);
317 // Handle Symbolic Links
318 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
324 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
326 if(curNode->Close) curNode->Close(curNode);
327 // No need to free *TruePath, it should already be NULL
332 if(iNestedLinks > MAX_NESTED_LINKS) {
333 if(curNode->Close) curNode->Close(curNode);
338 // Parse Symlink Path
339 // - Just update the path variable and restart the function
340 // > Count nested symlinks and limit to some value (counteracts loops)
342 int remlen = strlen(Path) - (ofs + nextSlash);
343 if( curNode->Size + remlen > MAX_PATH_LEN ) {
344 if(curNode->Close) curNode->Close(curNode);
345 Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
349 curNode->Read( curNode, 0, curNode->Size, path_buffer );
350 path_buffer[ curNode->Size ] = '\0';
351 strcat(path_buffer, Path + ofs+nextSlash);
354 // Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
362 // Handle Non-Directories
363 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
365 Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
366 if(TruePath) free(*TruePath);
371 // Check if path needs extending
372 if(!TruePath) continue;
374 // Increase buffer space
375 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
376 // Check if allocation succeeded
378 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
381 if(curNode->Close) curNode->Close(curNode);
387 (*TruePath)[retLength] = '/';
388 strcpy(*TruePath+retLength+1, pathEle);
390 LOG("*TruePath = '%s'", *TruePath);
393 retLength += nextSlash + 1;
396 if( !curNode->FindDir ) {
397 if(curNode->Close) curNode->Close(curNode);
402 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
408 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
409 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
410 LOG("tmpNode = %p", tmpNode);
411 if(curNode->Close) curNode->Close(curNode);
412 // Check if file was found
414 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
415 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
416 if(TruePath) free(*TruePath);
417 if(curNode->Close) curNode->Close(curNode);
424 // Increase buffer space
425 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
426 // Check if allocation succeeded
428 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
430 if(tmpNode->Close) tmpNode->Close(curNode);
436 (*TruePath)[retLength] = '/';
437 strcpy(*TruePath + retLength + 1, &Path[ofs]);
439 //retLength += strlen(tmpNode->Name) + 1;
451 * \brief Create and return a handle number for the given node and mode
453 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode )
457 ENTER("pNode pMount iMode", Node, Mount, Mode);
460 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
461 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
462 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
467 if( !VFS_CheckACL(Node, i) ) {
468 if(Node->Close) Node->Close( Node );
469 Log_Log("VFS", "VFS_int_CreateHandle: Permissions Failed");
474 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), Node, Mode );
476 Log_Notice("VFS", "VFS_int_CreateHandle: Out of handles");
481 VFS_GetHandle(i)->Mount = Mount;
487 * \fn int VFS_Open(const char *Path, Uint Mode)
490 int VFS_Open(const char *Path, Uint Mode)
496 ENTER("sPath xMode", Path, Mode);
499 absPath = VFS_GetAbsPath(Path);
500 if(absPath == NULL) {
501 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
504 LOG("absPath = \"%s\"", absPath);
505 // Parse path and get mount point
506 node = VFS_ParsePath(absPath, NULL, &mnt);
507 // Free generated path
511 LOG("Cannot find node");
516 // Check for symlinks
517 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
519 char tmppath[node->Size+1];
520 if( node->Size > MAX_PATH_LEN ) {
521 Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
525 Log_Warning("VFS", "VFS_Open - No read method on symlink");
528 // Read symlink's path
529 node->Read( node, 0, node->Size, tmppath );
530 tmppath[ node->Size ] = '\0';
531 if(node->Close) node->Close( node );
533 node = VFS_ParsePath(tmppath, NULL, &mnt);
535 LOG("Cannot find symlink target node (%s)", tmppath);
541 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
546 * \brief Open a file from an open directory
548 int VFS_OpenChild(int FD, const char *Name, Uint Mode)
553 ENTER("xFD sName xMode", FD, Name, Mode);
556 h = VFS_GetHandle(FD);
558 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
563 // Check for directory
564 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
565 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
571 node = h->Node->FindDir(h->Node, Name);
577 LEAVE_RET('x', VFS_int_CreateHandle(node, h->Mount, Mode));
580 int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode)
585 ENTER("iMount XInode xMode", Mount, Inode, Mode);
588 mnt = VFS_GetMountByIdent(Mount);
590 LOG("Mount point ident invalid");
595 // Does the filesystem support this?
596 if( !mnt->Filesystem->GetNodeFromINode ) {
597 LOG("Filesystem does not support inode accesses");
603 node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode);
605 LOG("Unable to find inode");
610 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
614 * \fn void VFS_Close(int FD)
615 * \brief Closes an open file handle
617 void VFS_Close(int FD)
622 h = VFS_GetHandle(FD);
624 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
628 #if VALIDATE_VFS_FUNCTIPONS
629 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
630 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
631 h->Node, h->Node->Close);
637 h->Node->Close( h->Node );
643 * \brief Change current working directory
645 int VFS_ChDir(const char *Dest)
652 buf = VFS_GetAbsPath(Dest);
654 Log_Notice("VFS", "VFS_ChDir: Path expansion failed");
658 // Check if path exists
659 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
661 Log_Notice("VFS", "VFS_ChDir: Path is invalid");
665 // Get node so we can check for directory
666 h = VFS_GetHandle(fd);
667 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
668 Log("VFS_ChDir: Path is not a directory");
676 // Free old working directory
677 if( CFGPTR(CFG_VFS_CWD) )
678 free( CFGPTR(CFG_VFS_CWD) );
680 CFGPTR(CFG_VFS_CWD) = buf;
682 Log("Updated CWD to '%s'", buf);
688 * \fn int VFS_ChRoot(char *New)
689 * \brief Change current root directory
691 int VFS_ChRoot(const char *New)
697 if(New[0] == '/' && New[1] == '\0')
698 return 1; // What a useless thing to ask!
701 buf = VFS_GetAbsPath(New);
703 LOG("Path expansion failed");
707 // Check if path exists
708 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
710 LOG("Path is invalid");
714 // Get node so we can check for directory
715 h = VFS_GetHandle(fd);
716 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
717 LOG("Path is not a directory");
725 // Free old working directory
726 if( CFGPTR(CFG_VFS_CHROOT) )
727 free( CFGPTR(CFG_VFS_CHROOT) );
729 CFGPTR(CFG_VFS_CHROOT) = buf;
731 LOG("Updated Root to '%s'", buf);