3 * - Open, Close and ChDir
12 #define OPEN_MOUNT_ROOT 1
13 #define MAX_KERNEL_FILES 128
14 #define MAX_PATH_SLASHES 256
17 extern tVFS_Node gVFS_MemRoot;
18 extern tVFS_Mount *gVFS_RootMount;
21 tVFS_Handle *gaUserHandles = (void*)MM_PPD_VFS;
22 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
26 * \fn char *VFS_GetAbsPath(char *Path)
27 * \brief Create an absolute path from a relative one
29 char *VFS_GetAbsPath(char *Path)
32 int pathLen = strlen(Path);
33 char *pathComps[MAX_PATH_SLASHES];
37 char *chroot = CFGPTR(CFG_VFS_CHROOT);
39 char *cwd = CFGPTR(CFG_VFS_CWD);
46 ret = malloc(strlen(Path)+1);
48 Warning("VFS_GetAbsPath - malloc() returned NULL");
57 if( chroot == NULL ) {
61 chrootLen = strlen(chroot);
64 // Check if the path is already absolute
66 ret = malloc(pathLen + 1);
68 Warning("VFS_GetAbsPath - malloc() returned NULL");
80 // Prepend the current directory
81 ret = malloc( cwdLen + 1 + pathLen + 1 );
84 strcpy(&ret[cwdLen+1], Path);
85 //Log("ret = '%s'\n", ret);
89 pathComps[iPos++] = tmpStr = ret+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;
139 while(pathComps[iPos])
141 tmpStr = pathComps[iPos];
142 while(*tmpStr && *tmpStr != '/')
144 ret[iPos2++] = *tmpStr;
156 // Prepend the chroot
157 tmpStr = malloc(chrootLen + strlen(ret) + 1);
158 strcpy( tmpStr, chroot );
159 strcpy( tmpStr+chrootLen, ret );
164 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
169 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
170 * \brief Parses a path, resolving sysmlinks and applying permissions
172 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
175 tVFS_Mount *longestMount = gVFS_RootMount; // Root is first
176 int cmp, retLength = 0;
178 tVFS_Node *curNode, *tmpNode;
181 ENTER("sPath pTruePath", Path, TruePath);
186 *TruePath = malloc(strlen(Path)+1);
187 strcpy(*TruePath, Path);
189 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
193 // For root we always fast return
195 if(Path[0] == '/' && Path[1] == '\0') {
197 *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
198 strcpy(*TruePath, gVFS_RootMount->MountPoint);
200 LEAVE('p', gVFS_RootMount->RootNode);
201 return gVFS_RootMount->RootNode;
204 // Check if there is anything mounted
206 Warning("WTF! There's nothing mounted?");
211 for(mnt = gVFS_Mounts;
216 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
218 // Length Check - If the length is smaller than the longest match sofar
219 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
221 cmp = strcmp(Path, mnt->MountPoint);
224 // Fast Break - Request Mount Root
227 *TruePath = malloc( mnt->MountPointLen+1 );
228 strcpy(*TruePath, mnt->MountPoint);
230 LEAVE('p', mnt->RootNode);
231 return mnt->RootNode;
234 // Not a match, continue
235 if(cmp != '/') continue;
240 /*if(!longestMount) {
241 Log("VFS_ParsePath - ERROR: No Root Node\n");
245 // Save to shorter variable
248 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
253 *TruePath = malloc( mnt->MountPointLen+1 );
254 strcpy(*TruePath, mnt->MountPoint);
255 retLength = mnt->MountPointLen;
258 curNode = mnt->RootNode;
259 curNode->ReferenceCount ++;
261 ofs = mnt->MountPointLen+1;
262 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
265 Path[nextSlash] = '\0';
267 // Check for empty string
268 if( Path[ofs] == '\0' ) continue;
270 // Check permissions on root of filesystem
271 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
272 if(curNode->Close) curNode->Close( curNode );
277 //Log("Permissions fail on '%s'", Path);
282 // Check if the node has a FindDir method
283 if( !curNode->FindDir )
285 if(curNode->Close) curNode->Close(curNode);
290 Path[nextSlash] = '/';
291 //Log("FindDir fail on '%s'", Path);
295 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
297 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
298 LOG("tmpNode = %p", tmpNode);
299 if(curNode->Close) curNode->Close(curNode);
304 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
309 //Log("Child fail on '%s' ('%s)", Path, &Path[ofs]);
310 Path[nextSlash] = '/';
315 // Handle Symbolic Links
316 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
321 tmp = malloc( curNode->Size + 1 );
323 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
325 if(curNode->Close) curNode->Close(curNode);
329 curNode->Read( curNode, 0, curNode->Size, tmp );
330 tmp[ curNode->Size ] = '\0';
332 // Parse Symlink Path
333 curNode = VFS_ParsePath(tmp, TruePath);
337 Log("Symlink fail '%s'", tmp);
338 free(tmp); // Free temp string
346 retLength = strlen(tmp);
348 free(tmp); // Free temp string
354 // Handle Non-Directories
355 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
357 Warning("VFS_ParsePath - File in directory context");
358 if(TruePath) free(*TruePath);
363 // Check if path needs extending
364 if(!TruePath) continue;
366 // Increase buffer space
367 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
368 // Check if allocation succeeded
370 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
372 if(curNode->Close) curNode->Close(curNode);
378 (*TruePath)[retLength] = '/';
379 strcpy(*TruePath+retLength+1, &Path[ofs]);
381 retLength += strlen(&Path[ofs])+1;
385 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
386 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
387 LOG("tmpNode = %p", tmpNode);
388 if(curNode->Close) curNode->Close(curNode);
389 // Check if file was found
391 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
392 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
393 if(TruePath) free(*TruePath);
394 if(curNode->Close) curNode->Close(curNode);
401 // Increase buffer space
402 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
403 // Check if allocation succeeded
405 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
407 if(tmpNode->Close) tmpNode->Close(curNode);
413 (*TruePath)[retLength] = '/';
414 strcpy(*TruePath + retLength + 1, &Path[ofs]);
416 //retLength += strlen(tmpNode->Name) + 1;
424 * \fn int VFS_Open(char *Path, Uint Mode)
427 int VFS_Open(char *Path, Uint Mode)
433 ENTER("sPath xMode", Path, Mode);
436 absPath = VFS_GetAbsPath(Path);
437 LOG("absPath = \"%s\"", absPath);
438 // Parse path and get mount point
439 node = VFS_ParsePath(absPath, NULL);
440 // Free generated path
444 LOG("Cannot find node");
449 // Check for symlinks
450 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
453 Warning("No read method on symlink");
457 absPath = malloc(node->Size+1); // Allocate Buffer
458 node->Read( node, 0, node->Size, absPath ); // Read Path
460 absPath[ node->Size ] = '\0'; // End String
461 if(node->Close) node->Close( node ); // Close old node
462 node = VFS_ParsePath(absPath, NULL); // Get new node
463 free( absPath ); // Free allocated path
467 LOG("Cannot find node");
473 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
474 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
475 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
480 if( !VFS_CheckACL(node, i) ) {
481 if(node->Close) node->Close( node );
482 Log("VFS_Open: Permissions Failed");
487 // Check for a user open
488 if(Mode & VFS_OPENFLAG_USER)
491 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
494 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
495 for(addr = 0; addr < size; addr += 0x1000)
496 MM_Allocate( (Uint)gaUserHandles + addr );
497 memset( gaUserHandles, 0, size );
500 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
502 if(gaUserHandles[i].Node) continue;
503 gaUserHandles[i].Node = node;
504 gaUserHandles[i].Position = 0;
505 gaUserHandles[i].Mode = Mode;
512 // Allocate space if not already
513 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
516 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
517 for(addr = 0; addr < size; addr += 0x1000)
518 MM_Allocate( (Uint)gaKernelHandles + addr );
519 memset( gaKernelHandles, 0, size );
522 for(i=0;i<MAX_KERNEL_FILES;i++)
524 if(gaKernelHandles[i].Node) continue;
525 gaKernelHandles[i].Node = node;
526 gaKernelHandles[i].Position = 0;
527 gaKernelHandles[i].Mode = Mode;
528 LEAVE('x', i|VFS_KERNEL_FLAG);
529 return i|VFS_KERNEL_FLAG;
533 Log("VFS_Open: Out of handles");
539 * \fn void VFS_Close(int FD)
540 * \brief Closes an open file handle
542 void VFS_Close(int FD)
547 h = VFS_GetHandle(FD);
549 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
554 h->Node->Close( h->Node );
560 * \brief Change current working directory
562 int VFS_ChDir(char *Dest)
569 buf = VFS_GetAbsPath(Dest);
571 Log("VFS_ChDir: Path expansion failed");
575 // Check if path exists
576 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
578 Log("VFS_ChDir: Path is invalid");
582 // Get node so we can check for directory
583 h = VFS_GetHandle(fd);
584 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
585 Log("VFS_ChDir: Path is not a directory");
593 // Free old working directory
594 if( CFGPTR(CFG_VFS_CWD) )
595 free( CFGPTR(CFG_VFS_CWD) );
597 CFGPTR(CFG_VFS_CWD) = buf;
599 Log("Updated CWD to '%s'", buf);
605 * \fn int VFS_ChRoot(char *New)
606 * \brief Change current root directory
608 int VFS_ChRoot(char *New)
614 if(New[0] == '/' && New[1] == '\0')
615 return 1; // What a useless thing to ask!
618 buf = VFS_GetAbsPath(New);
620 LOG("Path expansion failed");
624 // Check if path exists
625 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
627 LOG("Path is invalid");
631 // Get node so we can check for directory
632 h = VFS_GetHandle(fd);
633 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
634 LOG("Path is not a directory");
642 // Free old working directory
643 if( CFGPTR(CFG_VFS_CHROOT) )
644 free( CFGPTR(CFG_VFS_CHROOT) );
646 CFGPTR(CFG_VFS_CHROOT) = buf;
648 LOG("Updated Root to '%s'", buf);
654 * \fn tVFS_Handle *VFS_GetHandle(int FD)
655 * \brief Gets a pointer to the handle information structure
657 tVFS_Handle *VFS_GetHandle(int FD)
661 if(FD < 0) return NULL;
663 if(FD & VFS_KERNEL_FLAG) {
664 FD &= (VFS_KERNEL_FLAG - 1);
665 if(FD >= MAX_KERNEL_FILES) return NULL;
666 h = &gaKernelHandles[ FD ];
668 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
669 h = &gaUserHandles[ FD ];
672 if(h->Node == NULL) return NULL;