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 Flags)
458 return VFS_OpenEx(Path, Flags, 0);
461 int VFS_OpenEx(const char *Path, Uint Flags, Uint Mode)
467 ENTER("sPath xFlags oMode", Path, Flags);
470 absPath = VFS_GetAbsPath(Path);
471 if(absPath == NULL) {
472 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
475 LOG("absPath = \"%s\"", absPath);
477 // Parse path and get mount point
478 node = VFS_ParsePath(absPath, NULL, &mnt);
480 // Create file if requested and it doesn't exist
481 if( !node && (Flags & VFS_OPENFLAG_CREATE) )
483 // TODO: Translate `Mode` into ACL and node flags
484 // Get parent, create node
485 VFS_MkNod(absPath, 0);
486 node = VFS_ParsePath(absPath, NULL, &mnt);
489 // Free generated path
495 LOG("Cannot find node");
500 // Check for symlinks
501 if( !(Flags & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
503 char tmppath[node->Size+1];
504 if( node->Size > MAX_PATH_LEN ) {
505 Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
508 if( !node->Type || !node->Type->Read ) {
509 Log_Warning("VFS", "VFS_Open - No read method on symlink");
512 // Read symlink's path
513 node->Type->Read( node, 0, node->Size, tmppath );
514 tmppath[ node->Size ] = '\0';
517 node = VFS_ParsePath(tmppath, NULL, &mnt);
519 LOG("Cannot find symlink target node (%s)", tmppath);
525 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Flags));
530 * \brief Open a file from an open directory
532 int VFS_OpenChild(int FD, const char *Name, Uint Mode)
537 ENTER("xFD sName xMode", FD, Name, Mode);
540 h = VFS_GetHandle(FD);
542 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
547 // Check for directory
548 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
549 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory");
555 if( !h->Node->Type || !h->Node->Type->FindDir ) {
556 Log_Error("VFS", "VFS_OpenChild - Node does not have a type/is missing FindDir");
562 node = h->Node->Type->FindDir(h->Node, Name);
568 LEAVE_RET('x', VFS_int_CreateHandle(node, h->Mount, Mode));
571 int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode)
576 ENTER("iMount XInode xMode", Mount, Inode, Mode);
579 mnt = VFS_GetMountByIdent(Mount);
581 LOG("Mount point ident invalid");
586 // Does the filesystem support this?
587 if( !mnt->Filesystem->GetNodeFromINode ) {
588 LOG("Filesystem does not support inode accesses");
594 node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode);
596 LOG("Unable to find inode");
601 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
605 * \fn void VFS_Close(int FD)
606 * \brief Closes an open file handle
608 void VFS_Close(int FD)
613 h = VFS_GetHandle(FD);
615 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
619 #if VALIDATE_VFS_FUNCTIPONS
620 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
621 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
622 h->Node, h->Node->Close);
633 * \brief Change current working directory
635 int VFS_ChDir(const char *Dest)
642 buf = VFS_GetAbsPath(Dest);
644 Log_Notice("VFS", "VFS_ChDir: Path expansion failed");
648 // Check if path exists
649 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
651 Log_Notice("VFS", "VFS_ChDir: Path is invalid");
655 // Get node so we can check for directory
656 h = VFS_GetHandle(fd);
657 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
658 Log("VFS_ChDir: Path is not a directory");
667 char **cwdptr = Threads_GetCWD();
668 // Free old working directory
669 if( *cwdptr ) free( *cwdptr );
674 Log("Updated CWD to '%s'", buf);
680 * \fn int VFS_ChRoot(char *New)
681 * \brief Change current root directory
683 int VFS_ChRoot(const char *New)
689 if(New[0] == '/' && New[1] == '\0')
690 return 1; // What a useless thing to ask!
693 buf = VFS_GetAbsPath(New);
695 LOG("Path expansion failed");
699 // Check if path exists
700 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
702 LOG("Path is invalid");
706 // Get node so we can check for directory
707 h = VFS_GetHandle(fd);
708 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
709 LOG("Path is not a directory");
719 char **chroot_ptr = Threads_GetChroot();
720 if( *chroot_ptr ) free( *chroot_ptr );
724 LOG("Updated Root to '%s'", buf);