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);
24 * \fn char *VFS_GetAbsPath(const char *Path)
25 * \brief Create an absolute path from a relative one
27 char *VFS_GetAbsPath(const char *Path)
30 int pathLen = strlen(Path);
31 char *pathComps[MAX_PATH_SLASHES];
35 const char *chroot = CFGPTR(CFG_VFS_CHROOT);
37 const char *cwd = CFGPTR(CFG_VFS_CWD);
44 ret = malloc(strlen(Path)+1);
46 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
55 if( chroot == NULL ) {
59 chrootLen = strlen(chroot);
62 // Check if the path is already absolute
64 ret = malloc(pathLen + 1);
66 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
78 // Prepend the current directory
79 ret = malloc( cwdLen + 1 + pathLen + 1 );
82 strcpy(&ret[cwdLen+1], Path);
83 //Log("ret = '%s'", ret);
87 pathComps[iPos++] = tmpStr = ret+1;
92 pathComps[iPos++] = tmpStr;
93 if(iPos == MAX_PATH_SLASHES) {
94 LOG("Path '%s' has too many elements", Path);
101 pathComps[iPos] = NULL;
105 while(pathComps[iPos])
107 tmpStr = pathComps[iPos];
108 // Always Increment iPos
111 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
118 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
123 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
129 pathComps[iPos2] = tmpStr;
132 pathComps[iPos2] = NULL;
137 while(pathComps[iPos])
139 tmpStr = pathComps[iPos];
140 while(*tmpStr && *tmpStr != '/')
142 ret[iPos2++] = *tmpStr;
154 // Prepend the chroot
155 tmpStr = malloc(chrootLen + strlen(ret) + 1);
156 strcpy( tmpStr, chroot );
157 strcpy( tmpStr+chrootLen, ret );
162 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
167 * \fn char *VFS_ParsePath(const char *Path, char **TruePath)
168 * \brief Parses a path, resolving sysmlinks and applying permissions
170 tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath)
172 tVFS_Mount *mnt, *longestMount;
173 int cmp, retLength = 0;
175 int iNestedLinks = 0;
176 tVFS_Node *curNode, *tmpNode;
178 char path_buffer[MAX_PATH_LEN+1];
180 ENTER("sPath pTruePath", Path, TruePath);
185 *TruePath = malloc(strlen(Path)+1);
186 strcpy(*TruePath, Path);
188 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 LEAVE('p', gVFS_RootMount->RootNode);
201 return gVFS_RootMount->RootNode;
204 // Check if there is anything mounted
206 Warning("WTF! There's nothing mounted?");
211 longestMount = gVFS_RootMount;
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 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
249 *TruePath = malloc( strlen(Path) + 1 );
250 strcpy(*TruePath, mnt->MountPoint);
251 retLength = mnt->MountPointLen;
254 curNode = mnt->RootNode;
255 curNode->ReferenceCount ++;
257 ofs = mnt->MountPointLen+1;
258 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
260 char pathEle[nextSlash+1];
263 if(nextSlash == 0) continue;
265 memcpy(pathEle, &Path[ofs], nextSlash);
266 pathEle[nextSlash] = 0;
268 // Check permissions on root of filesystem
269 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
270 if(curNode->Close) curNode->Close( curNode );
275 //Log("Permissions fail on '%s'", Path);
280 // Check if the node has a FindDir method
281 if( !curNode->FindDir )
283 if(curNode->Close) curNode->Close(curNode);
288 //Log("FindDir fail on '%s'", Path);
292 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
294 tmpNode = curNode->FindDir(curNode, pathEle);
295 LOG("tmpNode = %p", tmpNode);
297 //LOG2("curNode->Close = %p", curNode->Close);
298 curNode->Close(curNode);
304 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
309 //Log("Child fail on '%s' ('%s)", Path, pathEle);
314 // Handle Symbolic Links
315 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
321 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
323 if(curNode->Close) curNode->Close(curNode);
324 // No need to free *TruePath, it should already be NULL
329 if(iNestedLinks > MAX_NESTED_LINKS) {
330 if(curNode->Close) curNode->Close(curNode);
335 // Parse Symlink Path
336 // - Just update the path variable and restart the function
337 // > Count nested symlinks and limit to some value (counteracts loops)
339 int remlen = strlen(Path) - (ofs + nextSlash);
340 if( curNode->Size + remlen > MAX_PATH_LEN ) {
341 if(curNode->Close) curNode->Close(curNode);
342 Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
346 curNode->Read( curNode, 0, curNode->Size, path_buffer );
347 path_buffer[ curNode->Size ] = '\0';
348 strcat(path_buffer, &Path[ofs+nextSlash]);
359 // Handle Non-Directories
360 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
362 Warning("VFS_ParsePath - File in directory context");
363 if(TruePath) free(*TruePath);
368 // Check if path needs extending
369 if(!TruePath) continue;
371 // Increase buffer space
372 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
373 // Check if allocation succeeded
375 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
378 if(curNode->Close) curNode->Close(curNode);
384 (*TruePath)[retLength] = '/';
385 strcpy(*TruePath+retLength+1, pathEle);
387 LOG("*TruePath = '%s'", *TruePath);
390 retLength += nextSlash + 1;
393 if( !curNode->FindDir ) {
394 if(curNode->Close) curNode->Close(curNode);
399 Log("FindDir fail on '%s'", Path);
405 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
406 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
407 LOG("tmpNode = %p", tmpNode);
408 if(curNode->Close) curNode->Close(curNode);
409 // Check if file was found
411 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
412 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
413 if(TruePath) free(*TruePath);
414 if(curNode->Close) curNode->Close(curNode);
421 // Increase buffer space
422 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
423 // Check if allocation succeeded
425 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
427 if(tmpNode->Close) tmpNode->Close(curNode);
433 (*TruePath)[retLength] = '/';
434 strcpy(*TruePath + retLength + 1, &Path[ofs]);
436 //retLength += strlen(tmpNode->Name) + 1;
444 * \fn int VFS_Open(const char *Path, Uint Mode)
447 int VFS_Open(const char *Path, Uint Mode)
453 ENTER("sPath xMode", Path, Mode);
456 absPath = VFS_GetAbsPath(Path);
457 if(absPath == NULL) {
458 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", 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 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
517 Log("VFS_Open: Out of handles");
524 * \brief Open a file from an open directory
526 int VFS_OpenChild(Uint *Errno, int FD, const char *Name, Uint Mode)
533 h = VFS_GetHandle(FD);
535 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
536 if(Errno) *Errno = EINVAL;
541 // Check for directory
542 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
543 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
544 if(Errno) *Errno = ENOTDIR;
550 node = h->Node->FindDir(h->Node, Name);
552 if(Errno) *Errno = ENOENT;
558 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
559 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
560 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
563 if( !VFS_CheckACL(node, i) ) {
564 if(node->Close) node->Close( node );
565 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
566 if(Errno) *Errno = EACCES;
571 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
577 Log_Error("VFS", "VFS_OpenChild - Out of handles");
578 if(Errno) *Errno = ENFILE;
584 * \fn void VFS_Close(int FD)
585 * \brief Closes an open file handle
587 void VFS_Close(int FD)
592 h = VFS_GetHandle(FD);
594 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
598 #if VALIDATE_VFS_FUNCTIPONS
599 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
600 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
601 h->Node, h->Node->Close);
607 h->Node->Close( h->Node );
613 * \brief Change current working directory
615 int VFS_ChDir(const char *Dest)
622 buf = VFS_GetAbsPath(Dest);
624 Log("VFS_ChDir: Path expansion failed");
628 // Check if path exists
629 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
631 Log("VFS_ChDir: Path is invalid");
635 // Get node so we can check for directory
636 h = VFS_GetHandle(fd);
637 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
638 Log("VFS_ChDir: Path is not a directory");
646 // Free old working directory
647 if( CFGPTR(CFG_VFS_CWD) )
648 free( CFGPTR(CFG_VFS_CWD) );
650 CFGPTR(CFG_VFS_CWD) = buf;
652 Log("Updated CWD to '%s'", buf);
658 * \fn int VFS_ChRoot(char *New)
659 * \brief Change current root directory
661 int VFS_ChRoot(const char *New)
667 if(New[0] == '/' && New[1] == '\0')
668 return 1; // What a useless thing to ask!
671 buf = VFS_GetAbsPath(New);
673 LOG("Path expansion failed");
677 // Check if path exists
678 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
680 LOG("Path is invalid");
684 // Get node so we can check for directory
685 h = VFS_GetHandle(fd);
686 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
687 LOG("Path is not a directory");
695 // Free old working directory
696 if( CFGPTR(CFG_VFS_CHROOT) )
697 free( CFGPTR(CFG_VFS_CHROOT) );
699 CFGPTR(CFG_VFS_CHROOT) = buf;
701 LOG("Updated Root to '%s'", buf);