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;
19 extern tVFS_Handle *VFS_GetHandle(int FD);
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 char *chroot = CFGPTR(CFG_VFS_CHROOT);
37 char *cwd = CFGPTR(CFG_VFS_CWD);
44 ret = malloc(strlen(Path)+1);
46 Warning("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 Warning("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'\n", 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)
173 tVFS_Mount *longestMount = gVFS_RootMount; // Root is first
174 int cmp, retLength = 0;
176 tVFS_Node *curNode, *tmpNode;
179 ENTER("sPath pTruePath", Path, TruePath);
184 *TruePath = malloc(strlen(Path)+1);
185 strcpy(*TruePath, Path);
187 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
192 // For root we always fast return
193 if(Path[0] == '/' && Path[1] == '\0') {
195 *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
196 strcpy(*TruePath, gVFS_RootMount->MountPoint);
198 LEAVE('p', gVFS_RootMount->RootNode);
199 return gVFS_RootMount->RootNode;
202 // Check if there is an`ything mounted
204 Warning("WTF! There's nothing mounted?");
209 for(mnt = gVFS_Mounts;
214 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
216 // Length Check - If the length is smaller than the longest match sofar
217 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
219 cmp = strcmp(Path, mnt->MountPoint);
222 // Fast Break - Request Mount Root
225 *TruePath = malloc( mnt->MountPointLen+1 );
226 strcpy(*TruePath, mnt->MountPoint);
228 LEAVE('p', mnt->RootNode);
229 return mnt->RootNode;
232 // Not a match, continue
233 if(cmp != '/') continue;
237 // Save to shorter variable
240 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
245 *TruePath = malloc( mnt->MountPointLen+1 );
246 strcpy(*TruePath, mnt->MountPoint);
247 retLength = mnt->MountPointLen;
250 curNode = mnt->RootNode;
251 curNode->ReferenceCount ++;
253 ofs = mnt->MountPointLen+1;
254 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
256 char pathEle[nextSlash+1];
259 if(nextSlash == 0) continue;
261 memcpy(pathEle, &Path[ofs], nextSlash);
262 pathEle[nextSlash] = 0;
264 // Check permissions on root of filesystem
265 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
266 if(curNode->Close) curNode->Close( curNode );
271 //Log("Permissions fail on '%s'", Path);
276 // Check if the node has a FindDir method
277 if( !curNode->FindDir )
279 if(curNode->Close) curNode->Close(curNode);
284 //Log("FindDir fail on '%s'", Path);
288 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
290 tmpNode = curNode->FindDir(curNode, pathEle);
291 LOG("tmpNode = %p", tmpNode);
293 //LOG2("curNode->Close = %p", curNode->Close);
294 curNode->Close(curNode);
300 LOG("Node '%s' not found in dir '%s'", pathEle, Path);
305 //Log("Child fail on '%s' ('%s)", Path, pathEle);
310 // Handle Symbolic Links
311 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
317 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
319 if(curNode->Close) curNode->Close(curNode);
320 // No need to free *TruePath, see above
325 tmp = malloc( curNode->Size + 1 );
327 Log_Warning("VFS", "VFS_ParsePath - Malloc failure");
328 // No need to free *TruePath, see above
332 curNode->Read( curNode, 0, curNode->Size, tmp );
333 tmp[ curNode->Size ] = '\0';
335 // Parse Symlink Path
336 curNode = VFS_ParsePath(tmp, TruePath);
338 LOG("VFS", "*TruePath='%s'", *TruePath);
342 Log_Debug("VFS", "Symlink fail '%s'", tmp);
343 free(tmp); // Free temp string
344 if(TruePath) free(TruePath);
354 retLength = strlen(*TruePath);
360 // Handle Non-Directories
361 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
363 Warning("VFS_ParsePath - File in directory context");
364 if(TruePath) free(*TruePath);
369 // Check if path needs extending
370 if(!TruePath) continue;
372 // Increase buffer space
373 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
374 // Check if allocation succeeded
376 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
379 if(curNode->Close) curNode->Close(curNode);
385 (*TruePath)[retLength] = '/';
386 strcpy(*TruePath+retLength+1, pathEle);
388 LOG("*TruePath = '%s'", *TruePath);
391 retLength += nextSlash + 1;
394 if( !curNode->FindDir ) {
395 if(curNode->Close) curNode->Close(curNode);
400 Log("FindDir fail on '%s'", Path);
406 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
407 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
408 LOG("tmpNode = %p", tmpNode);
409 if(curNode->Close) curNode->Close(curNode);
410 // Check if file was found
412 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
413 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
414 if(TruePath) free(*TruePath);
415 if(curNode->Close) curNode->Close(curNode);
422 // Increase buffer space
423 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
424 // Check if allocation succeeded
426 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
428 if(tmpNode->Close) tmpNode->Close(curNode);
434 (*TruePath)[retLength] = '/';
435 strcpy(*TruePath + retLength + 1, &Path[ofs]);
437 //retLength += strlen(tmpNode->Name) + 1;
445 * \fn int VFS_Open(char *Path, Uint Mode)
448 int VFS_Open(char *Path, Uint Mode)
454 ENTER("sPath xMode", Path, Mode);
457 absPath = VFS_GetAbsPath(Path);
458 if(absPath == NULL) {
459 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
462 LOG("absPath = \"%s\"", absPath);
463 // Parse path and get mount point
464 node = VFS_ParsePath(absPath, NULL);
465 // Free generated path
469 LOG("Cannot find node");
474 // Check for symlinks
475 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
478 Warning("No read method on symlink");
482 absPath = malloc(node->Size+1); // Allocate Buffer
483 node->Read( node, 0, node->Size, absPath ); // Read Path
485 absPath[ node->Size ] = '\0'; // End String
486 if(node->Close) node->Close( node ); // Close old node
487 node = VFS_ParsePath(absPath, NULL); // Get new node
488 free( absPath ); // Free allocated path
492 LOG("Cannot find node");
498 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
499 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
500 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
505 if( !VFS_CheckACL(node, i) ) {
506 if(node->Close) node->Close( node );
507 Log("VFS_Open: Permissions Failed");
512 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
518 Log("VFS_Open: Out of handles");
525 * \brief Open a file from an open directory
527 int VFS_OpenChild(Uint *Errno, int FD, char *Name, Uint Mode)
534 h = VFS_GetHandle(FD);
536 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
537 if(Errno) *Errno = EINVAL;
542 // Check for directory
543 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
544 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
545 if(Errno) *Errno = ENOTDIR;
551 node = h->Node->FindDir(h->Node, Name);
553 if(Errno) *Errno = ENOENT;
559 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
560 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
561 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
564 if( !VFS_CheckACL(node, i) ) {
565 if(node->Close) node->Close( node );
566 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
567 if(Errno) *Errno = EACCES;
572 i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), node, Mode );
578 Log_Error("VFS", "VFS_OpenChild - Out of handles");
579 if(Errno) *Errno = ENFILE;
585 * \fn void VFS_Close(int FD)
586 * \brief Closes an open file handle
588 void VFS_Close(int FD)
593 h = VFS_GetHandle(FD);
595 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
599 #if VALIDATE_VFS_FUNCTIPONS
600 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
601 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
602 h->Node, h->Node->Close);
608 h->Node->Close( h->Node );
614 * \brief Change current working directory
616 int VFS_ChDir(char *Dest)
623 buf = VFS_GetAbsPath(Dest);
625 Log("VFS_ChDir: Path expansion failed");
629 // Check if path exists
630 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
632 Log("VFS_ChDir: Path is invalid");
636 // Get node so we can check for directory
637 h = VFS_GetHandle(fd);
638 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
639 Log("VFS_ChDir: Path is not a directory");
647 // Free old working directory
648 if( CFGPTR(CFG_VFS_CWD) )
649 free( CFGPTR(CFG_VFS_CWD) );
651 CFGPTR(CFG_VFS_CWD) = buf;
653 Log("Updated CWD to '%s'", buf);
659 * \fn int VFS_ChRoot(char *New)
660 * \brief Change current root directory
662 int VFS_ChRoot(char *New)
668 if(New[0] == '/' && New[1] == '\0')
669 return 1; // What a useless thing to ask!
672 buf = VFS_GetAbsPath(New);
674 LOG("Path expansion failed");
678 // Check if path exists
679 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
681 LOG("Path is invalid");
685 // Get node so we can check for directory
686 h = VFS_GetHandle(fd);
687 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
688 LOG("Path is not a directory");
696 // Free old working directory
697 if( CFGPTR(CFG_VFS_CHROOT) )
698 free( CFGPTR(CFG_VFS_CHROOT) );
700 CFGPTR(CFG_VFS_CHROOT) = buf;
702 LOG("Updated Root to '%s'", buf);