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 Log_Error("VFS", "VFS_ParsePath - No filesystems mounted");
211 longestMount = gVFS_RootMount;
212 for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
215 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
217 // Length Check - If the length is smaller than the longest match sofar
218 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
220 cmp = strcmp(Path, mnt->MountPoint);
223 // Fast Break - Request Mount Root
226 *TruePath = malloc( mnt->MountPointLen+1 );
227 strcpy(*TruePath, mnt->MountPoint);
229 LEAVE('p', mnt->RootNode);
230 return mnt->RootNode;
233 // Not a match, continue
234 if(cmp != '/') continue;
238 // Save to shorter variable
241 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
246 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
247 *TruePath = malloc( strlen(Path) + 1 );
248 strcpy(*TruePath, mnt->MountPoint);
249 retLength = mnt->MountPointLen;
252 curNode = mnt->RootNode;
253 curNode->ReferenceCount ++;
255 ofs = mnt->MountPointLen+1;
256 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
258 char pathEle[nextSlash+1];
261 if(nextSlash == 0) continue;
263 memcpy(pathEle, &Path[ofs], nextSlash);
264 pathEle[nextSlash] = 0;
266 // Check permissions on root of filesystem
267 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
268 if(curNode->Close) curNode->Close( curNode );
273 //Log("Permissions fail on '%s'", Path);
278 // Check if the node has a FindDir method
279 if( !curNode->FindDir )
281 if(curNode->Close) curNode->Close(curNode);
286 //Log("FindDir fail on '%s'", Path);
290 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
292 tmpNode = curNode->FindDir(curNode, pathEle);
293 LOG("tmpNode = %p", tmpNode);
295 //LOG2("curNode->Close = %p", curNode->Close);
296 curNode->Close(curNode);
302 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
307 //Log("Child fail on '%s' ('%s)", Path, pathEle);
312 // Handle Symbolic Links
313 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
319 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
321 if(curNode->Close) curNode->Close(curNode);
322 // No need to free *TruePath, it should already be NULL
327 if(iNestedLinks > MAX_NESTED_LINKS) {
328 if(curNode->Close) curNode->Close(curNode);
333 // Parse Symlink Path
334 // - Just update the path variable and restart the function
335 // > Count nested symlinks and limit to some value (counteracts loops)
337 int remlen = strlen(Path) - (ofs + nextSlash);
338 if( curNode->Size + remlen > MAX_PATH_LEN ) {
339 if(curNode->Close) curNode->Close(curNode);
340 Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
344 curNode->Read( curNode, 0, curNode->Size, path_buffer );
345 path_buffer[ curNode->Size ] = '\0';
346 strcat(path_buffer, Path + ofs+nextSlash);
349 // Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
357 // Handle Non-Directories
358 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
360 Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
361 if(TruePath) free(*TruePath);
366 // Check if path needs extending
367 if(!TruePath) continue;
369 // Increase buffer space
370 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
371 // Check if allocation succeeded
373 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
376 if(curNode->Close) curNode->Close(curNode);
382 (*TruePath)[retLength] = '/';
383 strcpy(*TruePath+retLength+1, pathEle);
385 LOG("*TruePath = '%s'", *TruePath);
388 retLength += nextSlash + 1;
391 if( !curNode->FindDir ) {
392 if(curNode->Close) curNode->Close(curNode);
397 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
403 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
404 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
405 LOG("tmpNode = %p", tmpNode);
406 if(curNode->Close) curNode->Close(curNode);
407 // Check if file was found
409 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
410 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
411 if(TruePath) free(*TruePath);
412 if(curNode->Close) curNode->Close(curNode);
419 // Increase buffer space
420 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
421 // Check if allocation succeeded
423 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
425 if(tmpNode->Close) tmpNode->Close(curNode);
431 (*TruePath)[retLength] = '/';
432 strcpy(*TruePath + retLength + 1, &Path[ofs]);
434 //retLength += strlen(tmpNode->Name) + 1;
442 * \fn int VFS_Open(const char *Path, Uint Mode)
445 int VFS_Open(const char *Path, Uint Mode)
451 ENTER("sPath xMode", Path, Mode);
454 absPath = VFS_GetAbsPath(Path);
455 if(absPath == NULL) {
456 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
459 LOG("absPath = \"%s\"", absPath);
460 // Parse path and get mount point
461 node = VFS_ParsePath(absPath, NULL);
462 // Free generated path
466 LOG("Cannot find node");
470 // Check for symlinks
471 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
473 char tmppath[node->Size+1];
474 if( node->Size > MAX_PATH_LEN ) {
475 Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
479 Log_Warning("VFS", "VFS_Open - No read method on symlink");
482 // Read symlink's path
483 node->Read( node, 0, node->Size, tmppath );
484 tmppath[ node->Size ] = '\0';
485 if(node->Close) node->Close( node );
487 node = VFS_ParsePath(tmppath, NULL);
489 LOG("Cannot find symlink target node (%s)", tmppath);
495 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
496 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
497 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
502 if( !VFS_CheckACL(node, i) ) {
503 if(node->Close) node->Close( node );
504 Log_Log("VFS", "VFS_Open: Permissions Failed");
508 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
510 Log_Notice("VFS", "VFS_Open: Out of handles");
519 * \brief Open a file from an open directory
521 int VFS_OpenChild(Uint *Errno, int FD, const char *Name, Uint Mode)
528 h = VFS_GetHandle(FD);
530 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
531 if(Errno) *Errno = EINVAL;
535 // Check for directory
536 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
537 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
538 if(Errno) *Errno = ENOTDIR;
543 node = h->Node->FindDir(h->Node, Name);
545 if(Errno) *Errno = ENOENT;
550 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
551 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
552 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
555 if( !VFS_CheckACL(node, i) ) {
556 if(node->Close) node->Close( node );
557 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
558 if(Errno) *Errno = EACCES;
562 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
567 Log_Error("VFS", "VFS_OpenChild - Out of handles");
568 if(Errno) *Errno = ENFILE;
573 * \fn void VFS_Close(int FD)
574 * \brief Closes an open file handle
576 void VFS_Close(int FD)
581 h = VFS_GetHandle(FD);
583 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
587 #if VALIDATE_VFS_FUNCTIPONS
588 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
589 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
590 h->Node, h->Node->Close);
596 h->Node->Close( h->Node );
602 * \brief Change current working directory
604 int VFS_ChDir(const char *Dest)
611 buf = VFS_GetAbsPath(Dest);
613 Log("VFS_ChDir: Path expansion failed");
617 // Check if path exists
618 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
620 Log("VFS_ChDir: Path is invalid");
624 // Get node so we can check for directory
625 h = VFS_GetHandle(fd);
626 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
627 Log("VFS_ChDir: Path is not a directory");
635 // Free old working directory
636 if( CFGPTR(CFG_VFS_CWD) )
637 free( CFGPTR(CFG_VFS_CWD) );
639 CFGPTR(CFG_VFS_CWD) = buf;
641 Log("Updated CWD to '%s'", buf);
647 * \fn int VFS_ChRoot(char *New)
648 * \brief Change current root directory
650 int VFS_ChRoot(const char *New)
656 if(New[0] == '/' && New[1] == '\0')
657 return 1; // What a useless thing to ask!
660 buf = VFS_GetAbsPath(New);
662 LOG("Path expansion failed");
666 // Check if path exists
667 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
669 LOG("Path is invalid");
673 // Get node so we can check for directory
674 h = VFS_GetHandle(fd);
675 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
676 LOG("Path is not a directory");
684 // Free old working directory
685 if( CFGPTR(CFG_VFS_CHROOT) )
686 free( CFGPTR(CFG_VFS_CHROOT) );
688 CFGPTR(CFG_VFS_CHROOT) = buf;
690 LOG("Updated Root to '%s'", buf);