3 * - Open, Close and ChDir
12 #define OPEN_MOUNT_ROOT 1
13 #define MAX_PATH_SLASHES 256
16 extern tVFS_Node gVFS_MemRoot;
17 extern tVFS_Mount *gVFS_RootMount;
18 extern int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode);
22 * \fn char *VFS_GetAbsPath(const char *Path)
23 * \brief Create an absolute path from a relative one
25 char *VFS_GetAbsPath(const char *Path)
28 int pathLen = strlen(Path);
29 char *pathComps[MAX_PATH_SLASHES];
33 const char *chroot = CFGPTR(CFG_VFS_CHROOT);
35 const char *cwd = CFGPTR(CFG_VFS_CWD);
42 ret = malloc(strlen(Path)+1);
44 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
53 if( chroot == NULL ) {
57 chrootLen = strlen(chroot);
60 // Check if the path is already absolute
62 ret = malloc(pathLen + 1);
64 Log_Warning("VFS", "VFS_GetAbsPath: malloc() returned NULL");
76 // Prepend the current directory
77 ret = malloc( cwdLen + 1 + pathLen + 1 );
80 strcpy(&ret[cwdLen+1], Path);
81 //Log("ret = '%s'\n", ret);
85 pathComps[iPos++] = tmpStr = ret+1;
90 pathComps[iPos++] = tmpStr;
91 if(iPos == MAX_PATH_SLASHES) {
92 LOG("Path '%s' has too many elements", Path);
99 pathComps[iPos] = NULL;
103 while(pathComps[iPos])
105 tmpStr = pathComps[iPos];
106 // Always Increment iPos
109 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
116 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
121 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
127 pathComps[iPos2] = tmpStr;
130 pathComps[iPos2] = NULL;
135 while(pathComps[iPos])
137 tmpStr = pathComps[iPos];
138 while(*tmpStr && *tmpStr != '/')
140 ret[iPos2++] = *tmpStr;
152 // Prepend the chroot
153 tmpStr = malloc(chrootLen + strlen(ret) + 1);
154 strcpy( tmpStr, chroot );
155 strcpy( tmpStr+chrootLen, ret );
160 //Log("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)
171 tVFS_Mount *longestMount = gVFS_RootMount; // Root is first
172 int cmp, retLength = 0;
174 tVFS_Node *curNode, *tmpNode;
177 ENTER("sPath pTruePath", Path, TruePath);
182 *TruePath = malloc(strlen(Path)+1);
183 strcpy(*TruePath, Path);
185 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 an`ything mounted
202 Warning("WTF! There's nothing mounted?");
207 for(mnt = gVFS_Mounts;
212 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
214 // Length Check - If the length is smaller than the longest match sofar
215 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
217 cmp = strcmp(Path, mnt->MountPoint);
220 // Fast Break - Request Mount Root
223 *TruePath = malloc( mnt->MountPointLen+1 );
224 strcpy(*TruePath, mnt->MountPoint);
226 LEAVE('p', mnt->RootNode);
227 return mnt->RootNode;
230 // Not a match, continue
231 if(cmp != '/') continue;
235 // Save to shorter variable
238 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
243 *TruePath = malloc( mnt->MountPointLen+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 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
317 if(curNode->Close) curNode->Close(curNode);
318 // No need to free *TruePath, see above
323 tmp = malloc( curNode->Size + 1 );
325 Log_Warning("VFS", "VFS_ParsePath - Malloc failure");
326 // No need to free *TruePath, see above
330 curNode->Read( curNode, 0, curNode->Size, tmp );
331 tmp[ curNode->Size ] = '\0';
333 // Parse Symlink Path
334 curNode = VFS_ParsePath(tmp, TruePath);
336 LOG("VFS", "*TruePath='%s'", *TruePath);
340 Log_Debug("VFS", "Symlink fail '%s'", tmp);
341 free(tmp); // Free temp string
342 if(TruePath) free(TruePath);
352 retLength = strlen(*TruePath);
358 // Handle Non-Directories
359 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
361 Warning("VFS_ParsePath - File in directory context");
362 if(TruePath) free(*TruePath);
367 // Check if path needs extending
368 if(!TruePath) continue;
370 // Increase buffer space
371 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
372 // Check if allocation succeeded
374 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
377 if(curNode->Close) curNode->Close(curNode);
383 (*TruePath)[retLength] = '/';
384 strcpy(*TruePath+retLength+1, pathEle);
386 LOG("*TruePath = '%s'", *TruePath);
389 retLength += nextSlash + 1;
392 if( !curNode->FindDir ) {
393 if(curNode->Close) curNode->Close(curNode);
398 Log("FindDir fail on '%s'", Path);
404 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
405 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
406 LOG("tmpNode = %p", tmpNode);
407 if(curNode->Close) curNode->Close(curNode);
408 // Check if file was found
410 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
411 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
412 if(TruePath) free(*TruePath);
413 if(curNode->Close) curNode->Close(curNode);
420 // Increase buffer space
421 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
422 // Check if allocation succeeded
424 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
426 if(tmpNode->Close) tmpNode->Close(curNode);
432 (*TruePath)[retLength] = '/';
433 strcpy(*TruePath + retLength + 1, &Path[ofs]);
435 //retLength += strlen(tmpNode->Name) + 1;
443 * \fn int VFS_Open(const char *Path, Uint Mode)
446 int VFS_Open(const char *Path, Uint Mode)
452 ENTER("sPath xMode", Path, Mode);
455 absPath = VFS_GetAbsPath(Path);
456 if(absPath == NULL) {
457 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
460 LOG("absPath = \"%s\"", absPath);
461 // Parse path and get mount point
462 node = VFS_ParsePath(absPath, NULL);
463 // Free generated path
467 LOG("Cannot find node");
472 // Check for symlinks
473 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
476 Warning("No read method on symlink");
480 absPath = malloc(node->Size+1); // Allocate Buffer
481 node->Read( node, 0, node->Size, absPath ); // Read Path
483 absPath[ node->Size ] = '\0'; // End String
484 if(node->Close) node->Close( node ); // Close old node
485 node = VFS_ParsePath(absPath, NULL); // Get new node
486 free( absPath ); // Free allocated path
490 LOG("Cannot find node");
496 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
497 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
498 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
503 if( !VFS_CheckACL(node, i) ) {
504 if(node->Close) node->Close( node );
505 Log("VFS_Open: Permissions Failed");
510 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
516 Log("VFS_Open: Out of handles");
523 * \brief Open a file from an open directory
525 int VFS_OpenChild(Uint *Errno, int FD, const char *Name, Uint Mode)
532 h = VFS_GetHandle(FD);
534 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
535 if(Errno) *Errno = EINVAL;
540 // Check for directory
541 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
542 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
543 if(Errno) *Errno = ENOTDIR;
549 node = h->Node->FindDir(h->Node, Name);
551 if(Errno) *Errno = ENOENT;
557 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
558 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
559 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
562 if( !VFS_CheckACL(node, i) ) {
563 if(node->Close) node->Close( node );
564 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
565 if(Errno) *Errno = EACCES;
570 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
576 Log_Error("VFS", "VFS_OpenChild - Out of handles");
577 if(Errno) *Errno = ENFILE;
583 * \fn void VFS_Close(int FD)
584 * \brief Closes an open file handle
586 void VFS_Close(int FD)
591 h = VFS_GetHandle(FD);
593 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
597 #if VALIDATE_VFS_FUNCTIPONS
598 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
599 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
600 h->Node, h->Node->Close);
606 h->Node->Close( h->Node );
612 * \brief Change current working directory
614 int VFS_ChDir(char *Dest)
621 buf = VFS_GetAbsPath(Dest);
623 Log("VFS_ChDir: Path expansion failed");
627 // Check if path exists
628 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
630 Log("VFS_ChDir: Path is invalid");
634 // Get node so we can check for directory
635 h = VFS_GetHandle(fd);
636 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
637 Log("VFS_ChDir: Path is not a directory");
645 // Free old working directory
646 if( CFGPTR(CFG_VFS_CWD) )
647 free( CFGPTR(CFG_VFS_CWD) );
649 CFGPTR(CFG_VFS_CWD) = buf;
651 Log("Updated CWD to '%s'", buf);
657 * \fn int VFS_ChRoot(char *New)
658 * \brief Change current root directory
660 int VFS_ChRoot(char *New)
666 if(New[0] == '/' && New[1] == '\0')
667 return 1; // What a useless thing to ask!
670 buf = VFS_GetAbsPath(New);
672 LOG("Path expansion failed");
676 // Check if path exists
677 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
679 LOG("Path is invalid");
683 // Get node so we can check for directory
684 h = VFS_GetHandle(fd);
685 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
686 LOG("Path is not a directory");
694 // Free old working directory
695 if( CFGPTR(CFG_VFS_CHROOT) )
696 free( CFGPTR(CFG_VFS_CHROOT) );
698 CFGPTR(CFG_VFS_CHROOT) = buf;
700 LOG("Updated Root to '%s'", buf);