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);
62 // Trim trailing slash off chroot
63 if( chrootLen && chroot[chrootLen - 1] == '/' )
66 // Check if the path is already absolute
68 ret = malloc(chrootLen + pathLen + 1);
70 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
73 strcpy(ret + chrootLen, Path);
83 // Prepend the current directory
84 ret = malloc(chrootLen + cwdLen + 1 + pathLen + 1 );
85 strcpy(ret+chrootLen, cwd);
87 strcpy(ret+chrootLen+cwdLen+1, Path);
88 //Log("ret = '%s'", ret);
92 pathComps[iPos++] = tmpStr = ret+chrootLen+1;
97 pathComps[iPos++] = tmpStr;
98 if(iPos == MAX_PATH_SLASHES) {
99 LOG("Path '%s' has too many elements", Path);
106 pathComps[iPos] = NULL;
110 while(pathComps[iPos])
112 tmpStr = pathComps[iPos];
113 // Always Increment iPos
116 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
123 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
128 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
134 pathComps[iPos2] = tmpStr;
137 pathComps[iPos2] = NULL;
140 iPos2 = chrootLen + 1; iPos = 0;
142 while(pathComps[iPos])
144 tmpStr = pathComps[iPos];
145 while(*tmpStr && *tmpStr != '/')
147 ret[iPos2++] = *tmpStr;
158 // Prepend the chroot
160 memcpy( ret, chroot, chrootLen );
163 // Log_Debug("VFS", "VFS_GetAbsPath: RETURN '%s'", ret);
168 * \fn char *VFS_ParsePath(const char *Path, char **TruePath)
169 * \brief Parses a path, resolving sysmlinks and applying permissions
171 tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath, tVFS_Mount **MountPoint)
173 tVFS_Mount *mnt, *longestMount;
174 int cmp, retLength = 0;
176 int iNestedLinks = 0;
177 tVFS_Node *curNode, *tmpNode;
179 char path_buffer[MAX_PATH_LEN+1];
181 ENTER("sPath pTruePath", Path, TruePath);
184 if(Threads_GetUID() == 0 && Path[0] == '$') {
186 *TruePath = malloc(strlen(Path)+1);
187 strcpy(*TruePath, Path);
189 curNode = VFS_MemFile_Create(Path);
198 // For root we always fast return
199 if(Path[0] == '/' && Path[1] == '\0') {
201 *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
202 strcpy(*TruePath, gVFS_RootMount->MountPoint);
204 gVFS_RootMount->OpenHandleCount ++;
205 if(MountPoint) *MountPoint = gVFS_RootMount;
206 LEAVE('p', gVFS_RootMount->RootNode);
207 return gVFS_RootMount->RootNode;
210 // Check if there is anything mounted
212 Log_Error("VFS", "VFS_ParsePath - No filesystems mounted");
217 longestMount = gVFS_RootMount;
218 RWLock_AcquireRead( &glVFS_MountList );
219 for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
222 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
224 // Length Check - If the length is smaller than the longest match sofar
225 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
227 cmp = strncmp(Path, mnt->MountPoint, mnt->MountPointLen);
228 // Not a match, continue
229 if(cmp != 0) continue;
232 // Fast Break - Request Mount Root
233 if(Path[mnt->MountPointLen] == '\0') {
235 *TruePath = malloc( mnt->MountPointLen+1 );
236 strcpy(*TruePath, mnt->MountPoint);
240 RWLock_Release( &glVFS_MountList );
241 LOG("Mount %p root", mnt);
242 LEAVE('p', mnt->RootNode);
243 return mnt->RootNode;
248 longestMount->OpenHandleCount ++; // Increment assuimg it worked
249 RWLock_Release( &glVFS_MountList );
251 // Save to shorter variable
254 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
259 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
260 *TruePath = malloc( strlen(Path) + 1 );
261 strcpy(*TruePath, mnt->MountPoint);
262 retLength = mnt->MountPointLen;
265 curNode = mnt->RootNode;
266 curNode->ReferenceCount ++;
268 ofs = mnt->MountPointLen+1;
269 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
271 char pathEle[nextSlash+1];
274 if(nextSlash == 0) continue;
276 memcpy(pathEle, &Path[ofs], nextSlash);
277 pathEle[nextSlash] = 0;
279 // Check permissions on root of filesystem
280 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
281 LOG("Permissions failure on '%s'", Path);
285 // Check if the node has a FindDir method
288 LOG("Finddir failure on '%s' - No type", Path);
289 Log_Error("VFS", "Node at '%s' has no type (mount %s:%s)",
290 Path, mnt->Filesystem->Name, mnt->MountPoint);
293 if( !curNode->Type->FindDir )
295 LOG("Finddir failure on '%s' - No FindDir method in %s", Path, curNode->Type->TypeName);
298 LOG("FindDir{=%p}(%p, '%s')", curNode->Type->FindDir, curNode, pathEle);
300 tmpNode = curNode->Type->FindDir(curNode, pathEle);
301 LOG("tmpNode = %p", tmpNode);
302 _CloseNode( curNode );
307 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
311 // Handle Symbolic Links
312 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
317 if(!curNode->Type || !curNode->Type->Read) {
318 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
323 if(iNestedLinks > MAX_NESTED_LINKS) {
324 Log_Notice("VFS", "VFS_ParsePath - Nested link limit exceeded");
328 // Parse Symlink Path
329 // - Just update the path variable and restart the function
330 // > Count nested symlinks and limit to some value (counteracts loops)
332 int remlen = strlen(Path) - (ofs + nextSlash);
333 if( curNode->Size + remlen > MAX_PATH_LEN ) {
334 Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
337 curNode->Type->Read( curNode, 0, curNode->Size, path_buffer );
338 path_buffer[ curNode->Size ] = '\0';
339 LOG("path_buffer = '%s'", path_buffer);
340 strcat(path_buffer, Path + ofs+nextSlash);
341 // TODO: Pass to VFS_GetAbsPath to handle ../. in the symlink
344 // Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
349 LOG("Symlink -> '%s', restart", Path);
350 mnt->OpenHandleCount --; // Not in this mountpoint
354 // Handle Non-Directories
355 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
357 Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
361 // Check if path needs extending
362 if(!TruePath) continue;
364 // Increase buffer space
365 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
366 // Check if allocation succeeded
368 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
373 (*TruePath)[retLength] = '/';
374 strcpy(*TruePath+retLength+1, pathEle);
376 LOG("*TruePath = '%s'", *TruePath);
379 retLength += nextSlash + 1;
382 // Check final finddir call
383 if( !curNode->Type || !curNode->Type->FindDir ) {
384 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
389 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
390 tmpNode = curNode->Type->FindDir(curNode, &Path[ofs]);
391 LOG("tmpNode = %p", tmpNode);
392 // Check if file was found
394 LOG("Node '%s' not found in dir '%.*s'", &Path[ofs], ofs, Path);
397 _CloseNode( curNode );
401 // Increase buffer space
402 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
403 // Check if allocation succeeded
405 Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
410 (*TruePath)[retLength] = '/';
411 strcpy(*TruePath + retLength + 1, &Path[ofs]);
413 //retLength += strlen(tmpNode->Name) + 1;
420 // Leave the mointpoint's count increased
426 _CloseNode( curNode );
428 if(TruePath && *TruePath) {
432 // Open failed, so decrement the open handle count
433 mnt->OpenHandleCount --;
440 * \brief Create and return a handle number for the given node and mode
442 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode )
446 ENTER("pNode pMount xMode", Node, Mount, Mode);
449 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
450 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
451 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
456 if( !VFS_CheckACL(Node, i) ) {
458 Log_Log("VFS", "VFS_int_CreateHandle: Permissions Failed");
463 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), Node, Mode );
465 Log_Notice("VFS", "VFS_int_CreateHandle: Out of handles");
470 VFS_GetHandle(i)->Mount = Mount;
476 * \fn int VFS_Open(const char *Path, Uint Mode)
479 int VFS_Open(const char *Path, Uint Flags)
481 return VFS_OpenEx(Path, Flags, 0);
484 int VFS_OpenEx(const char *Path, Uint Flags, Uint Mode)
490 ENTER("sPath xFlags oMode", Path, Flags);
493 absPath = VFS_GetAbsPath(Path);
494 if(absPath == NULL) {
495 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
498 LOG("absPath = \"%s\"", absPath);
500 // Parse path and get mount point
501 node = VFS_ParsePath(absPath, NULL, &mnt);
503 // Create file if requested and it doesn't exist
504 if( !node && (Flags & VFS_OPENFLAG_CREATE) )
506 // TODO: Translate `Mode` into ACL and node flags
507 // Get parent, create node
508 if( VFS_MkNod(absPath, 0) ) {
512 node = VFS_ParsePath(absPath, NULL, &mnt);
515 // Free generated path
521 LOG("Cannot find node");
526 // Check for symlinks
527 if( !(Flags & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
529 char tmppath[node->Size+1];
530 if( node->Size > MAX_PATH_LEN ) {
531 Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
534 if( !node->Type || !node->Type->Read ) {
535 Log_Warning("VFS", "VFS_Open - No read method on symlink");
538 // Read symlink's path
539 node->Type->Read( node, 0, node->Size, tmppath );
540 tmppath[ node->Size ] = '\0';
543 node = VFS_ParsePath(tmppath, NULL, &mnt);
545 LOG("Cannot find symlink target node (%s)", tmppath);
551 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Flags));
556 * \brief Open a file from an open directory
558 int VFS_OpenChild(int FD, const char *Name, Uint Mode)
563 ENTER("xFD sName xMode", FD, Name, Mode);
566 h = VFS_GetHandle(FD);
568 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
573 // Check for directory
574 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
575 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory");
581 if( !h->Node->Type || !h->Node->Type->FindDir ) {
582 Log_Error("VFS", "VFS_OpenChild - Node does not have a type/is missing FindDir");
588 node = h->Node->Type->FindDir(h->Node, Name);
594 // Increment open handle count, no problems with the mount going away as `h` is already open on it
595 h->Mount->OpenHandleCount ++;
597 LEAVE_RET('x', VFS_int_CreateHandle(node, h->Mount, Mode));
600 int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode)
605 ENTER("iMount XInode xMode", Mount, Inode, Mode);
608 mnt = VFS_GetMountByIdent(Mount);
610 LOG("Mount point ident invalid");
615 // Does the filesystem support this?
616 if( !mnt->Filesystem->GetNodeFromINode ) {
617 LOG("Filesystem does not support inode accesses");
623 node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode);
625 LOG("Unable to find inode");
630 LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
634 * \fn void VFS_Close(int FD)
635 * \brief Closes an open file handle
637 void VFS_Close(int FD)
642 h = VFS_GetHandle(FD);
644 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
648 #if VALIDATE_VFS_FUNCTIPONS
649 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
650 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
651 h->Node, h->Node->Close);
659 h->Mount->OpenHandleCount --;
665 * \brief Change current working directory
667 int VFS_ChDir(const char *Dest)
674 buf = VFS_GetAbsPath(Dest);
676 Log_Notice("VFS", "VFS_ChDir: Path expansion failed");
680 // Check if path exists
681 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
683 Log_Notice("VFS", "VFS_ChDir: Path is invalid");
687 // Get node so we can check for directory
688 h = VFS_GetHandle(fd);
689 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
690 Log("VFS_ChDir: Path is not a directory");
699 char **cwdptr = Threads_GetCWD();
700 // Free old working directory
701 if( *cwdptr ) free( *cwdptr );
706 Log_Debug("VFS", "Updated CWD to '%s'", buf);
712 * \fn int VFS_ChRoot(char *New)
713 * \brief Change current root directory
715 int VFS_ChRoot(const char *New)
721 if(New[0] == '/' && New[1] == '\0')
722 return 1; // What a useless thing to ask!
725 buf = VFS_GetAbsPath(New);
727 LOG("Path expansion failed");
731 // Check if path exists
732 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
734 LOG("Path is invalid");
738 // Get node so we can check for directory
739 h = VFS_GetHandle(fd);
740 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
741 LOG("Path is not a directory");
751 char **chroot_ptr = Threads_GetChroot();
752 if( *chroot_ptr ) free( *chroot_ptr );
756 LOG("Updated Root to '%s'", buf);