3 * - Open, Close and ChDir
13 #define OPEN_MOUNT_ROOT 1
14 #define MAX_KERNEL_FILES 128
15 #define MAX_PATH_SLASHES 256
18 extern tVFS_Node gVFS_MemRoot;
19 extern tVFS_Mount *gVFS_RootMount;
22 tVFS_Handle *gaUserHandles = (void*)MM_PPD_VFS;
23 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
27 * \fn char *VFS_GetAbsPath(char *Path)
28 * \brief Create an absolute path from a relative one
30 char *VFS_GetAbsPath(char *Path)
33 int pathLen = strlen(Path);
34 char *pathComps[MAX_PATH_SLASHES];
38 char *chroot = CFGPTR(CFG_VFS_CHROOT);
40 char *cwd = CFGPTR(CFG_VFS_CWD);
47 ret = malloc(strlen(Path)+1);
49 Warning("VFS_GetAbsPath - malloc() returned NULL");
58 if( chroot == NULL ) {
62 chrootLen = strlen(chroot);
65 // Check if the path is already absolute
67 ret = malloc(pathLen + 1);
69 Warning("VFS_GetAbsPath - malloc() returned NULL");
81 // Prepend the current directory
82 ret = malloc( cwdLen + 1 + pathLen + 1 );
85 strcpy(&ret[cwdLen+1], Path);
86 //Log("ret = '%s'\n", ret);
90 pathComps[iPos++] = tmpStr = ret+1;
95 pathComps[iPos++] = tmpStr;
96 if(iPos == MAX_PATH_SLASHES) {
97 LOG("Path '%s' has too many elements", Path);
104 pathComps[iPos] = NULL;
108 while(pathComps[iPos])
110 tmpStr = pathComps[iPos];
111 // Always Increment iPos
114 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
121 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
126 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
132 pathComps[iPos2] = tmpStr;
135 pathComps[iPos2] = NULL;
140 while(pathComps[iPos])
142 tmpStr = pathComps[iPos];
143 while(*tmpStr && *tmpStr != '/')
145 ret[iPos2++] = *tmpStr;
157 // Prepend the chroot
158 tmpStr = malloc(chrootLen + strlen(ret) + 1);
159 strcpy( tmpStr, chroot );
160 strcpy( tmpStr+chrootLen, ret );
165 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
170 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
171 * \brief Parses a path, resolving sysmlinks and applying permissions
173 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
176 tVFS_Mount *longestMount = gVFS_RootMount; // Root is first
177 int cmp, retLength = 0;
179 tVFS_Node *curNode, *tmpNode;
182 ENTER("sPath pTruePath", Path, TruePath);
187 *TruePath = malloc(strlen(Path)+1);
188 strcpy(*TruePath, Path);
190 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
194 // 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 LEAVE('p', gVFS_RootMount->RootNode);
202 return gVFS_RootMount->RootNode;
205 // Check if there is anything mounted
207 Warning("WTF! There's nothing mounted?");
212 for(mnt = gVFS_Mounts;
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);
231 LEAVE('p', mnt->RootNode);
232 return mnt->RootNode;
235 // Not a match, continue
236 if(cmp != '/') continue;
241 /*if(!longestMount) {
242 Log("VFS_ParsePath - ERROR: No Root Node\n");
246 // Save to shorter variable
249 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
254 *TruePath = malloc( mnt->MountPointLen+1 );
255 strcpy(*TruePath, mnt->MountPoint);
256 retLength = mnt->MountPointLen;
259 curNode = mnt->RootNode;
260 curNode->ReferenceCount ++;
262 ofs = mnt->MountPointLen+1;
263 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
266 Path[nextSlash] = '\0';
268 // Check for empty string
269 if( Path[ofs] == '\0' ) continue;
271 // Check permissions on root of filesystem
272 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
273 if(curNode->Close) curNode->Close( curNode );
278 //Log("Permissions fail on '%s'", Path);
283 // Check if the node has a FindDir method
284 if( !curNode->FindDir )
286 if(curNode->Close) curNode->Close(curNode);
291 Path[nextSlash] = '/';
292 //Log("FindDir fail on '%s'", Path);
296 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
298 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
299 LOG("tmpNode = %p", tmpNode);
300 if(curNode->Close) curNode->Close(curNode);
305 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
310 //Log("Child fail on '%s' ('%s)", Path, &Path[ofs]);
311 Path[nextSlash] = '/';
316 // Handle Symbolic Links
317 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
322 tmp = malloc( curNode->Size + 1 );
324 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
326 if(curNode->Close) curNode->Close(curNode);
330 curNode->Read( curNode, 0, curNode->Size, tmp );
331 tmp[ curNode->Size ] = '\0';
333 // Parse Symlink Path
334 curNode = VFS_ParsePath(tmp, TruePath);
338 Log("Symlink fail '%s'", tmp);
339 free(tmp); // Free temp string
347 retLength = strlen(tmp);
349 free(tmp); // Free temp string
355 // Handle Non-Directories
356 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
358 Warning("VFS_ParsePath - File in directory context");
359 if(TruePath) free(*TruePath);
364 // Check if path needs extending
365 if(!TruePath) continue;
367 // Increase buffer space
368 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
369 // Check if allocation succeeded
371 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
373 if(curNode->Close) curNode->Close(curNode);
379 (*TruePath)[retLength] = '/';
380 strcpy(*TruePath+retLength+1, &Path[ofs]);
382 retLength += strlen(&Path[ofs])+1;
386 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
387 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
388 LOG("tmpNode = %p", tmpNode);
389 if(curNode->Close) curNode->Close(curNode);
390 // Check if file was found
392 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
393 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
394 if(TruePath) free(*TruePath);
395 if(curNode->Close) curNode->Close(curNode);
402 // Increase buffer space
403 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
404 // Check if allocation succeeded
406 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
408 if(tmpNode->Close) tmpNode->Close(curNode);
414 (*TruePath)[retLength] = '/';
415 strcpy(*TruePath + retLength + 1, &Path[ofs]);
417 //retLength += strlen(tmpNode->Name) + 1;
425 * \fn int VFS_Open(char *Path, Uint Mode)
428 int VFS_Open(char *Path, Uint Mode)
434 ENTER("sPath xMode", Path, Mode);
437 absPath = VFS_GetAbsPath(Path);
438 LOG("absPath = \"%s\"", absPath);
439 // Parse path and get mount point
440 node = VFS_ParsePath(absPath, NULL);
441 // Free generated path
445 LOG("Cannot find node");
450 // Check for symlinks
451 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
454 Warning("No read method on symlink");
458 absPath = malloc(node->Size+1); // Allocate Buffer
459 node->Read( node, 0, node->Size, absPath ); // Read Path
461 absPath[ node->Size ] = '\0'; // End String
462 if(node->Close) node->Close( node ); // Close old node
463 node = VFS_ParsePath(absPath, NULL); // Get new node
464 free( absPath ); // Free allocated path
468 LOG("Cannot find node");
474 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
475 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
476 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
481 if( !VFS_CheckACL(node, i) ) {
482 if(node->Close) node->Close( node );
483 Log("VFS_Open: Permissions Failed");
488 // Check for a user open
489 if(Mode & VFS_OPENFLAG_USER)
492 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
495 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
496 for(addr = 0; addr < size; addr += 0x1000)
497 MM_Allocate( (Uint)gaUserHandles + addr );
498 memset( gaUserHandles, 0, size );
501 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
503 if(gaUserHandles[i].Node) continue;
504 gaUserHandles[i].Node = node;
505 gaUserHandles[i].Position = 0;
506 gaUserHandles[i].Mode = Mode;
513 // Allocate space if not already
514 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
517 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
518 for(addr = 0; addr < size; addr += 0x1000)
519 MM_Allocate( (Uint)gaKernelHandles + addr );
520 memset( gaKernelHandles, 0, size );
523 for(i=0;i<MAX_KERNEL_FILES;i++)
525 if(gaKernelHandles[i].Node) continue;
526 gaKernelHandles[i].Node = node;
527 gaKernelHandles[i].Position = 0;
528 gaKernelHandles[i].Mode = Mode;
529 LEAVE('x', i|VFS_KERNEL_FLAG);
530 return i|VFS_KERNEL_FLAG;
534 Log("VFS_Open: Out of handles");
541 * \brief Open a file from an open directory
543 int VFS_OpenChild(Uint *Errno, int FD, char *Name, Uint Mode)
550 h = VFS_GetHandle(FD);
552 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
553 if(Errno) *Errno = EINVAL;
558 // Check for directory
559 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
560 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
561 if(Errno) *Errno = ENOTDIR;
567 node = h->Node->FindDir(h->Node, Name);
569 if(Errno) *Errno = ENOENT;
575 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
576 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
577 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
580 if( !VFS_CheckACL(node, i) ) {
581 if(node->Close) node->Close( node );
582 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
583 if(Errno) *Errno = EACCES;
588 // Check for a user open
589 if(Mode & VFS_OPENFLAG_USER)
592 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
595 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
596 for(addr = 0; addr < size; addr += 0x1000)
597 MM_Allocate( (Uint)gaUserHandles + addr );
598 memset( gaUserHandles, 0, size );
601 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
603 if(gaUserHandles[i].Node) continue;
604 gaUserHandles[i].Node = node;
605 gaUserHandles[i].Position = 0;
606 gaUserHandles[i].Mode = Mode;
613 // Allocate space if not already
614 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
617 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
618 for(addr = 0; addr < size; addr += 0x1000)
619 MM_Allocate( (Uint)gaKernelHandles + addr );
620 memset( gaKernelHandles, 0, size );
623 for(i=0;i<MAX_KERNEL_FILES;i++)
625 if(gaKernelHandles[i].Node) continue;
626 gaKernelHandles[i].Node = node;
627 gaKernelHandles[i].Position = 0;
628 gaKernelHandles[i].Mode = Mode;
629 LEAVE('x', i|VFS_KERNEL_FLAG);
630 return i|VFS_KERNEL_FLAG;
634 Log_Error("VFS", "VFS_OpenChild - Out of handles");
635 if(Errno) *Errno = ENFILE;
641 * \fn void VFS_Close(int FD)
642 * \brief Closes an open file handle
644 void VFS_Close(int FD)
649 h = VFS_GetHandle(FD);
651 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
655 #if VALIDATE_VFS_FUNCTIPONS
656 if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
657 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
658 h->Node, h->Node->Close);
664 h->Node->Close( h->Node );
670 * \brief Change current working directory
672 int VFS_ChDir(char *Dest)
679 buf = VFS_GetAbsPath(Dest);
681 Log("VFS_ChDir: Path expansion failed");
685 // Check if path exists
686 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
688 Log("VFS_ChDir: Path is invalid");
692 // Get node so we can check for directory
693 h = VFS_GetHandle(fd);
694 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
695 Log("VFS_ChDir: Path is not a directory");
703 // Free old working directory
704 if( CFGPTR(CFG_VFS_CWD) )
705 free( CFGPTR(CFG_VFS_CWD) );
707 CFGPTR(CFG_VFS_CWD) = buf;
709 Log("Updated CWD to '%s'", buf);
715 * \fn int VFS_ChRoot(char *New)
716 * \brief Change current root directory
718 int VFS_ChRoot(char *New)
724 if(New[0] == '/' && New[1] == '\0')
725 return 1; // What a useless thing to ask!
728 buf = VFS_GetAbsPath(New);
730 LOG("Path expansion failed");
734 // Check if path exists
735 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
737 LOG("Path is invalid");
741 // Get node so we can check for directory
742 h = VFS_GetHandle(fd);
743 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
744 LOG("Path is not a directory");
752 // Free old working directory
753 if( CFGPTR(CFG_VFS_CHROOT) )
754 free( CFGPTR(CFG_VFS_CHROOT) );
756 CFGPTR(CFG_VFS_CHROOT) = buf;
758 LOG("Updated Root to '%s'", buf);
764 * \fn tVFS_Handle *VFS_GetHandle(int FD)
765 * \brief Gets a pointer to the handle information structure
767 tVFS_Handle *VFS_GetHandle(int FD)
771 //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
773 if(FD < 0) return NULL;
775 if(FD & VFS_KERNEL_FLAG) {
776 FD &= (VFS_KERNEL_FLAG - 1);
777 if(FD >= MAX_KERNEL_FILES) return NULL;
778 h = &gaKernelHandles[ FD ];
780 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
781 h = &gaUserHandles[ FD ];
784 if(h->Node == NULL) return NULL;
785 //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);