3 * - Open, Close and ChDir
13 #define OPEN_MOUNT_ROOT 1
14 #define MAX_PATH_SLASHES 256
15 #define MAX_NESTED_LINKS 4
16 #define MAX_PATH_LEN 255
19 extern tVFS_Mount *gVFS_RootMount;
20 extern int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode);
21 extern tVFS_Node *VFS_MemFile_Create(const char *Path);
24 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode );
28 * \fn char *VFS_GetAbsPath(const char *Path)
29 * \brief Create an absolute path from a relative one
31 char *VFS_GetAbsPath(const char *Path)
34 int pathLen = strlen(Path);
35 char *pathComps[MAX_PATH_SLASHES];
39 const char *chroot = *Threads_GetChroot();
41 const char *cwd = *Threads_GetCWD();
48 ret = malloc(strlen(Path)+1);
50 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
61 chrootLen = strlen(chroot);
63 // Check if the path is already absolute
65 ret = malloc(chrootLen + pathLen + 1);
67 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
70 strcpy(ret + chrootLen, Path);
80 // Prepend the current directory
81 ret = malloc(chrootLen + cwdLen + 1 + pathLen + 1 );
82 strcpy(ret+chrootLen, cwd);
84 strcpy(ret+chrootLen+cwdLen+1, Path);
85 //Log("ret = '%s'", ret);
89 pathComps[iPos++] = tmpStr = ret+chrootLen+1;
94 pathComps[iPos++] = tmpStr;
95 if(iPos == MAX_PATH_SLASHES) {
96 LOG("Path '%s' has too many elements", Path);
103 pathComps[iPos] = NULL;
107 while(pathComps[iPos])
109 tmpStr = pathComps[iPos];
110 // Always Increment iPos
113 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
120 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
125 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
131 pathComps[iPos2] = tmpStr;
134 pathComps[iPos2] = NULL;
137 iPos2 = chrootLen + 1; iPos = 0;
139 while(pathComps[iPos])
141 tmpStr = pathComps[iPos];
142 while(*tmpStr && *tmpStr != '/')
144 ret[iPos2++] = *tmpStr;
155 // Prepend the chroot
157 memcpy( ret, chroot, chrootLen );
160 // Log_Debug("VFS", "VFS_GetAbsPath: RETURN '%s'", ret);
165 * \fn char *VFS_ParsePath(const char *Path, char **TruePath)
166 * \brief Parses a path, resolving sysmlinks and applying permissions
168 tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath, tVFS_Mount **MountPoint)
170 tVFS_Mount *mnt, *longestMount;
171 int cmp, retLength = 0;
173 int iNestedLinks = 0;
174 tVFS_Node *curNode, *tmpNode;
176 char path_buffer[MAX_PATH_LEN+1];
178 ENTER("sPath pTruePath", Path, TruePath);
181 if(Threads_GetUID() == 0 && Path[0] == '$') {
183 *TruePath = malloc(strlen(Path)+1);
184 strcpy(*TruePath, Path);
186 curNode = VFS_MemFile_Create(Path);
195 // For root we always fast return
196 if(Path[0] == '/' && Path[1] == '\0') {
198 *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
199 strcpy(*TruePath, gVFS_RootMount->MountPoint);
201 if(MountPoint) *MountPoint = gVFS_RootMount;
202 LEAVE('p', gVFS_RootMount->RootNode);
203 return gVFS_RootMount->RootNode;
206 // Check if there is anything mounted
208 Log_Error("VFS", "VFS_ParsePath - No filesystems mounted");
213 longestMount = gVFS_RootMount;
214 for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
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);
233 LEAVE('p', mnt->RootNode);
234 return mnt->RootNode;
237 // Not a match, continue
238 if(cmp != '/') continue;
242 // Save to shorter variable
245 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
250 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
251 *TruePath = malloc( strlen(Path) + 1 );
252 strcpy(*TruePath, mnt->MountPoint);
253 retLength = mnt->MountPointLen;
256 curNode = mnt->RootNode;
257 curNode->ReferenceCount ++;
259 ofs = mnt->MountPointLen+1;
260 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
262 char pathEle[nextSlash+1];
265 if(nextSlash == 0) continue;
267 memcpy(pathEle, &Path[ofs], nextSlash);
268 pathEle[nextSlash] = 0;
270 // Check permissions on root of filesystem
271 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
272 //Log("Permissions fail on '%s'", Path);
276 // Check if the node has a FindDir method
277 if( !curNode->Type->FindDir )
279 //Log("FindDir fail on '%s'", Path);
282 LOG("FindDir{=%p}(%p, '%s')", curNode->Type->FindDir, curNode, pathEle);
284 tmpNode = curNode->Type->FindDir(curNode, pathEle);
285 LOG("tmpNode = %p", tmpNode);
286 _CloseNode( curNode );
291 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
295 // Handle Symbolic Links
296 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
301 if(!curNode->Type || !curNode->Type->Read) {
302 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
307 if(iNestedLinks > MAX_NESTED_LINKS) {
308 Log_Notice("VFS", "VFS_ParsePath - Nested link limit exceeded");
312 // Parse Symlink Path
313 // - Just update the path variable and restart the function
314 // > Count nested symlinks and limit to some value (counteracts loops)
316 int remlen = strlen(Path) - (ofs + nextSlash);
317 if( curNode->Size + remlen > MAX_PATH_LEN ) {
318 Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
321 curNode->Type->Read( curNode, 0, curNode->Size, path_buffer );
322 path_buffer[ curNode->Size ] = '\0';
323 LOG("path_buffer = '%s'", path_buffer);
324 strcat(path_buffer, Path + ofs+nextSlash);
327 // Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
332 LOG("Symlink -> '%s', restart", Path);
336 // Handle Non-Directories
337 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
339 Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
343 // Check if path needs extending
344 if(!TruePath) continue;
346 // Increase buffer space
347 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
348 // Check if allocation succeeded
350 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
355 (*TruePath)[retLength] = '/';
356 strcpy(*TruePath+retLength+1, pathEle);
358 LOG("*TruePath = '%s'", *TruePath);
361 retLength += nextSlash + 1;
364 // Check final finddir call
365 if( !curNode->Type || !curNode->Type->FindDir ) {
366 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
371 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
372 tmpNode = curNode->Type->FindDir(curNode, &Path[ofs]);
373 LOG("tmpNode = %p", tmpNode);
374 // Check if file was found
376 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
379 _CloseNode( curNode );
383 // Increase buffer space
384 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
385 // Check if allocation succeeded
387 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
392 (*TruePath)[retLength] = '/';
393 strcpy(*TruePath + retLength + 1, &Path[ofs]);
395 //retLength += strlen(tmpNode->Name) + 1;
406 _CloseNode( curNode );
408 if(TruePath && *TruePath) {
417 * \brief Create and return a handle number for the given node and mode
419 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode )
423 ENTER("pNode pMount xMode", Node, Mount, Mode);
426 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
427 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
428 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
433 if( !VFS_CheckACL(Node, i) ) {
435 Log_Log("VFS", "VFS_int_CreateHandle: Permissions Failed");
440 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), Node, Mode );
442 Log_Notice("VFS", "VFS_int_CreateHandle: Out of handles");
447 VFS_GetHandle(i)->Mount = Mount;
453 * \fn int VFS_Open(const char *Path, Uint Mode)
456 int VFS_Open(const char *Path, Uint Mode)
462 ENTER("sPath xMode", Path, Mode);
465 absPath = VFS_GetAbsPath(Path);
466 if(absPath == NULL) {
467 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
470 LOG("absPath = \"%s\"", absPath);
471 // Parse path and get mount point
472 node = VFS_ParsePath(absPath, NULL, &mnt);
473 // Free generated path
477 LOG("Cannot find node");
482 // Check for symlinks
483 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
485 char tmppath[node->Size+1];
486 if( node->Size > MAX_PATH_LEN ) {
487 Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
490 if( !node->Type || !node->Type->Read ) {
491 Log_Warning("VFS", "VFS_Open - No read method on symlink");
494 // Read symlink's path
495 node->Type->Read( node, 0, node->Size, tmppath );
496 tmppath[ node->Size ] = '\0';
499 node = VFS_ParsePath(tmppath, NULL, &mnt);
501 LOG("Cannot find symlink target node (%s)", tmppath);
507 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
512 * \brief Open a file from an open directory
514 int VFS_OpenChild(int FD, const char *Name, Uint Mode)
519 ENTER("xFD sName xMode", FD, Name, Mode);
522 h = VFS_GetHandle(FD);
524 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
529 // Check for directory
530 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
531 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory");
537 if( !h->Node->Type || !h->Node->Type->FindDir ) {
538 Log_Error("VFS", "VFS_OpenChild - Node does not have a type/is missing FindDir");
544 node = h->Node->Type->FindDir(h->Node, Name);
550 LEAVE_RET('x', VFS_int_CreateHandle(node, h->Mount, Mode));
553 int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode)
558 ENTER("iMount XInode xMode", Mount, Inode, Mode);
561 mnt = VFS_GetMountByIdent(Mount);
563 LOG("Mount point ident invalid");
568 // Does the filesystem support this?
569 if( !mnt->Filesystem->GetNodeFromINode ) {
570 LOG("Filesystem does not support inode accesses");
576 node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode);
578 LOG("Unable to find inode");
583 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
587 * \fn void VFS_Close(int FD)
588 * \brief Closes an open file handle
590 void VFS_Close(int FD)
595 h = VFS_GetHandle(FD);
597 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
601 #if VALIDATE_VFS_FUNCTIPONS
602 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
603 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
604 h->Node, h->Node->Close);
615 * \brief Change current working directory
617 int VFS_ChDir(const char *Dest)
624 buf = VFS_GetAbsPath(Dest);
626 Log_Notice("VFS", "VFS_ChDir: Path expansion failed");
630 // Check if path exists
631 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
633 Log_Notice("VFS", "VFS_ChDir: Path is invalid");
637 // Get node so we can check for directory
638 h = VFS_GetHandle(fd);
639 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
640 Log("VFS_ChDir: Path is not a directory");
649 char **cwdptr = Threads_GetCWD();
650 // Free old working directory
651 if( *cwdptr ) free( *cwdptr );
656 Log("Updated CWD to '%s'", buf);
662 * \fn int VFS_ChRoot(char *New)
663 * \brief Change current root directory
665 int VFS_ChRoot(const char *New)
671 if(New[0] == '/' && New[1] == '\0')
672 return 1; // What a useless thing to ask!
675 buf = VFS_GetAbsPath(New);
677 LOG("Path expansion failed");
681 // Check if path exists
682 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
684 LOG("Path is invalid");
688 // Get node so we can check for directory
689 h = VFS_GetHandle(fd);
690 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
691 LOG("Path is not a directory");
701 char **chroot_ptr = Threads_GetChroot();
702 if( *chroot_ptr ) free( *chroot_ptr );
706 LOG("Updated Root to '%s'", buf);