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(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
155 memcpy( ret, chroot, chrootLen );
158 // Log_Debug("VFS", "VFS_GetAbsPath: RETURN '%s'", ret);
163 * \fn char *VFS_ParsePath(const char *Path, char **TruePath)
164 * \brief Parses a path, resolving sysmlinks and applying permissions
166 tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath)
168 tVFS_Mount *mnt, *longestMount;
169 int cmp, retLength = 0;
171 int iNestedLinks = 0;
172 tVFS_Node *curNode, *tmpNode;
174 char path_buffer[MAX_PATH_LEN+1];
176 ENTER("sPath pTruePath", Path, TruePath);
181 *TruePath = malloc(strlen(Path)+1);
182 strcpy(*TruePath, Path);
184 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
190 // For root we always fast return
191 if(Path[0] == '/' && Path[1] == '\0') {
193 *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
194 strcpy(*TruePath, gVFS_RootMount->MountPoint);
196 LEAVE('p', gVFS_RootMount->RootNode);
197 return gVFS_RootMount->RootNode;
200 // Check if there is anything mounted
202 Log_Error("VFS", "VFS_ParsePath - No filesystems mounted");
207 longestMount = gVFS_RootMount;
208 for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
211 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
213 // Length Check - If the length is smaller than the longest match sofar
214 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
216 cmp = strcmp(Path, mnt->MountPoint);
219 // Fast Break - Request Mount Root
222 *TruePath = malloc( mnt->MountPointLen+1 );
223 strcpy(*TruePath, mnt->MountPoint);
225 LEAVE('p', mnt->RootNode);
226 return mnt->RootNode;
229 // Not a match, continue
230 if(cmp != '/') continue;
234 // Save to shorter variable
237 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
242 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
243 *TruePath = malloc( strlen(Path) + 1 );
244 strcpy(*TruePath, mnt->MountPoint);
245 retLength = mnt->MountPointLen;
248 curNode = mnt->RootNode;
249 curNode->ReferenceCount ++;
251 ofs = mnt->MountPointLen+1;
252 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
254 char pathEle[nextSlash+1];
257 if(nextSlash == 0) continue;
259 memcpy(pathEle, &Path[ofs], nextSlash);
260 pathEle[nextSlash] = 0;
262 // Check permissions on root of filesystem
263 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
264 if(curNode->Close) curNode->Close( curNode );
269 //Log("Permissions fail on '%s'", Path);
274 // Check if the node has a FindDir method
275 if( !curNode->FindDir )
277 if(curNode->Close) curNode->Close(curNode);
282 //Log("FindDir fail on '%s'", Path);
286 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
288 tmpNode = curNode->FindDir(curNode, pathEle);
289 LOG("tmpNode = %p", tmpNode);
291 //LOG2("curNode->Close = %p", curNode->Close);
292 curNode->Close(curNode);
298 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
303 //Log("Child fail on '%s' ('%s)", Path, pathEle);
308 // Handle Symbolic Links
309 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
315 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
317 if(curNode->Close) curNode->Close(curNode);
318 // No need to free *TruePath, it should already be NULL
323 if(iNestedLinks > MAX_NESTED_LINKS) {
324 if(curNode->Close) curNode->Close(curNode);
329 // Parse Symlink Path
330 // - Just update the path variable and restart the function
331 // > Count nested symlinks and limit to some value (counteracts loops)
333 int remlen = strlen(Path) - (ofs + nextSlash);
334 if( curNode->Size + remlen > MAX_PATH_LEN ) {
335 if(curNode->Close) curNode->Close(curNode);
336 Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
340 curNode->Read( curNode, 0, curNode->Size, path_buffer );
341 path_buffer[ curNode->Size ] = '\0';
342 strcat(path_buffer, Path + ofs+nextSlash);
345 // Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
353 // Handle Non-Directories
354 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
356 Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
357 if(TruePath) free(*TruePath);
362 // Check if path needs extending
363 if(!TruePath) continue;
365 // Increase buffer space
366 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
367 // Check if allocation succeeded
369 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
372 if(curNode->Close) curNode->Close(curNode);
378 (*TruePath)[retLength] = '/';
379 strcpy(*TruePath+retLength+1, pathEle);
381 LOG("*TruePath = '%s'", *TruePath);
384 retLength += nextSlash + 1;
387 if( !curNode->FindDir ) {
388 if(curNode->Close) curNode->Close(curNode);
393 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
399 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
400 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
401 LOG("tmpNode = %p", tmpNode);
402 if(curNode->Close) curNode->Close(curNode);
403 // Check if file was found
405 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
406 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
407 if(TruePath) free(*TruePath);
408 if(curNode->Close) curNode->Close(curNode);
415 // Increase buffer space
416 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
417 // Check if allocation succeeded
419 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
421 if(tmpNode->Close) tmpNode->Close(curNode);
427 (*TruePath)[retLength] = '/';
428 strcpy(*TruePath + retLength + 1, &Path[ofs]);
430 //retLength += strlen(tmpNode->Name) + 1;
438 * \fn int VFS_Open(const char *Path, Uint Mode)
441 int VFS_Open(const char *Path, Uint Mode)
447 ENTER("sPath xMode", Path, Mode);
450 absPath = VFS_GetAbsPath(Path);
451 if(absPath == NULL) {
452 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
455 LOG("absPath = \"%s\"", absPath);
456 // Parse path and get mount point
457 node = VFS_ParsePath(absPath, NULL);
458 // Free generated path
462 LOG("Cannot find node");
466 // Check for symlinks
467 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
469 char tmppath[node->Size+1];
470 if( node->Size > MAX_PATH_LEN ) {
471 Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
475 Log_Warning("VFS", "VFS_Open - No read method on symlink");
478 // Read symlink's path
479 node->Read( node, 0, node->Size, tmppath );
480 tmppath[ node->Size ] = '\0';
481 if(node->Close) node->Close( node );
483 node = VFS_ParsePath(tmppath, NULL);
485 LOG("Cannot find symlink target node (%s)", tmppath);
491 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
492 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
493 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
498 if( !VFS_CheckACL(node, i) ) {
499 if(node->Close) node->Close( node );
500 Log_Log("VFS", "VFS_Open: Permissions Failed");
504 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
506 Log_Notice("VFS", "VFS_Open: Out of handles");
515 * \brief Open a file from an open directory
517 int VFS_OpenChild(Uint *Errno, int FD, const char *Name, Uint Mode)
524 h = VFS_GetHandle(FD);
526 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
527 if(Errno) *Errno = EINVAL;
531 // Check for directory
532 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
533 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
534 if(Errno) *Errno = ENOTDIR;
539 node = h->Node->FindDir(h->Node, Name);
541 if(Errno) *Errno = ENOENT;
546 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
547 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
548 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
551 if( !VFS_CheckACL(node, i) ) {
552 if(node->Close) node->Close( node );
553 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
554 if(Errno) *Errno = EACCES;
558 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
563 Log_Error("VFS", "VFS_OpenChild - Out of handles");
564 if(Errno) *Errno = ENFILE;
569 * \fn void VFS_Close(int FD)
570 * \brief Closes an open file handle
572 void VFS_Close(int FD)
577 h = VFS_GetHandle(FD);
579 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
583 #if VALIDATE_VFS_FUNCTIPONS
584 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
585 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
586 h->Node, h->Node->Close);
592 h->Node->Close( h->Node );
598 * \brief Change current working directory
600 int VFS_ChDir(const char *Dest)
607 buf = VFS_GetAbsPath(Dest);
609 Log("VFS_ChDir: Path expansion failed");
613 // Check if path exists
614 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
616 Log("VFS_ChDir: Path is invalid");
620 // Get node so we can check for directory
621 h = VFS_GetHandle(fd);
622 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
623 Log("VFS_ChDir: Path is not a directory");
631 // Free old working directory
632 if( CFGPTR(CFG_VFS_CWD) )
633 free( CFGPTR(CFG_VFS_CWD) );
635 CFGPTR(CFG_VFS_CWD) = buf;
637 Log("Updated CWD to '%s'", buf);
643 * \fn int VFS_ChRoot(char *New)
644 * \brief Change current root directory
646 int VFS_ChRoot(const char *New)
652 if(New[0] == '/' && New[1] == '\0')
653 return 1; // What a useless thing to ask!
656 buf = VFS_GetAbsPath(New);
658 LOG("Path expansion failed");
662 // Check if path exists
663 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
665 LOG("Path is invalid");
669 // Get node so we can check for directory
670 h = VFS_GetHandle(fd);
671 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
672 LOG("Path is not a directory");
680 // Free old working directory
681 if( CFGPTR(CFG_VFS_CHROOT) )
682 free( CFGPTR(CFG_VFS_CHROOT) );
684 CFGPTR(CFG_VFS_CHROOT) = buf;
686 LOG("Updated Root to '%s'", buf);