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 *gRootMount;
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);
47 ret = malloc(strlen(Path)+1);
49 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(chrootLen + pathLen + 1);
68 Warning("VFS_GetAbsPath - malloc() returned NULL");
72 strcpy(ret+chrootLen, Path);
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 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
162 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
163 * \brief Parses a path, resolving sysmlinks and applying permissions
165 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
168 tVFS_Mount *longestMount = gRootMount; // Root is first
169 int cmp, retLength = 0;
171 tVFS_Node *curNode, *tmpNode;
174 ENTER("sPath pTruePath", Path, TruePath);
179 *TruePath = malloc(strlen(Path)+1);
180 strcpy(*TruePath, Path);
182 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
186 // For root we always fast return
188 if(Path[0] == '/' && Path[1] == '\0') {
190 *TruePath = malloc( gRootMount->MountPointLen+1 );
191 strcpy(*TruePath, gRootMount->MountPoint);
193 LEAVE('p', gRootMount->RootNode);
194 return gRootMount->RootNode;
197 // Check if there is anything mounted
199 Warning("WTF! There's nothing mounted?");
209 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
211 // Length Check - If the length is smaller than the longest match sofar
212 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
214 cmp = strcmp(Path, mnt->MountPoint);
217 // Fast Break - Request Mount Root
220 *TruePath = malloc( mnt->MountPointLen+1 );
221 strcpy(*TruePath, mnt->MountPoint);
223 LEAVE('p', mnt->RootNode);
224 return mnt->RootNode;
227 // Not a match, continue
228 if(cmp != '/') continue;
233 /*if(!longestMount) {
234 Log("VFS_ParsePath - ERROR: No Root Node\n");
238 // Save to shorter variable
241 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
246 *TruePath = malloc( mnt->MountPointLen+1 );
247 strcpy(*TruePath, mnt->MountPoint);
248 retLength = mnt->MountPointLen;
251 curNode = mnt->RootNode;
252 curNode->ReferenceCount ++;
254 ofs = mnt->MountPointLen+1;
255 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
258 Path[nextSlash] = '\0';
260 // Check for empty string
261 if( Path[ofs] == '\0' ) continue;
263 // Check permissions on root of filesystem
264 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
265 curNode->Close( curNode );
270 //Log("Permissions fail on '%s'", Path);
275 // Check if the node has a FindDir method
276 if(!curNode->FindDir) {
277 if(curNode->Close) curNode->Close(curNode);
282 Path[nextSlash] = '/';
283 //Log("FindDir fail on '%s'", Path);
287 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
289 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
290 LOG("tmpNode = %p", tmpNode);
292 curNode->Close(curNode);
297 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
302 //Log("Child fail on '%s' ('%s)", Path, &Path[ofs]);
303 Path[nextSlash] = '/';
308 // Handle Symbolic Links
309 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
314 tmp = malloc( curNode->Size + 1 );
315 curNode->Read( curNode, 0, curNode->Size, tmp );
316 tmp[ curNode->Size ] = '\0';
318 // Parse Symlink Path
319 curNode = VFS_ParsePath(tmp, TruePath);
323 Log("Symlink fail '%s'", tmp);
324 free(tmp); // Free temp string
332 retLength = strlen(tmp);
334 free(tmp); // Free temp string
340 // Handle Non-Directories
341 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
343 Warning("VFS_ParsePath - File in directory context");
344 if(TruePath) free(*TruePath);
349 // Check if path needs extending
350 if(!TruePath) continue;
352 // Increase buffer space
353 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
354 // Check if allocation succeeded
356 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
358 if(curNode->Close) curNode->Close(curNode);
364 (*TruePath)[retLength] = '/';
365 strcpy(*TruePath+retLength+1, &Path[ofs]);
367 retLength += strlen(&Path[ofs])+1;
371 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
372 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
373 LOG("tmpNode = %p", tmpNode);
374 if(curNode->Close) curNode->Close(curNode);
375 // Check if file was found
377 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
378 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
379 if(TruePath) free(*TruePath);
380 if(curNode->Close) curNode->Close(curNode);
387 // Increase buffer space
388 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
389 // Check if allocation succeeded
391 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
393 if(tmpNode->Close) tmpNode->Close(curNode);
399 (*TruePath)[retLength] = '/';
400 strcpy(*TruePath + retLength + 1, &Path[ofs]);
402 //retLength += strlen(tmpNode->Name) + 1;
410 * \fn int VFS_Open(char *Path, Uint Mode)
413 int VFS_Open(char *Path, Uint Mode)
419 ENTER("sPath xMode", Path, Mode);
422 absPath = VFS_GetAbsPath(Path);
423 LOG("absPath = \"%s\"", absPath);
424 // Parse path and get mount point
425 node = VFS_ParsePath(absPath, NULL);
426 // Free generated path
430 LOG("Cannot find node");
435 // Check for symlinks
436 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
439 Warning("No read method on symlink");
443 absPath = malloc(node->Size+1); // Allocate Buffer
444 node->Read( node, 0, node->Size, absPath ); // Read Path
446 absPath[ node->Size ] = '\0'; // End String
447 if(node->Close) node->Close( node ); // Close old node
448 node = VFS_ParsePath(absPath, NULL); // Get new node
449 free( absPath ); // Free allocated path
453 LOG("Cannot find node");
459 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
460 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
461 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
466 if( !VFS_CheckACL(node, i) ) {
468 Log("VFS_Open: Permissions Failed");
473 // Check for a user open
474 if(Mode & VFS_OPENFLAG_USER)
477 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
480 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
481 for(addr = 0; addr < size; addr += 0x1000)
482 MM_Allocate( (Uint)gaUserHandles + addr );
483 memset( gaUserHandles, 0, size );
486 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
488 if(gaUserHandles[i].Node) continue;
489 gaUserHandles[i].Node = node;
490 gaUserHandles[i].Position = 0;
491 gaUserHandles[i].Mode = Mode;
498 // Allocate space if not already
499 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
502 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
503 for(addr = 0; addr < size; addr += 0x1000)
504 MM_Allocate( (Uint)gaKernelHandles + addr );
505 memset( gaKernelHandles, 0, size );
508 for(i=0;i<MAX_KERNEL_FILES;i++)
510 if(gaKernelHandles[i].Node) continue;
511 gaKernelHandles[i].Node = node;
512 gaKernelHandles[i].Position = 0;
513 gaKernelHandles[i].Mode = Mode;
514 LEAVE('x', i|VFS_KERNEL_FLAG);
515 return i|VFS_KERNEL_FLAG;
519 Log("VFS_Open: Out of handles");
525 * \fn void VFS_Close(int FD)
526 * \brief Closes an open file handle
528 void VFS_Close(int FD)
533 h = VFS_GetHandle(FD);
535 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
540 h->Node->Close( h->Node );
546 * \fn int VFS_ChDir(char *New)
547 * \brief Change current working directory
549 int VFS_ChDir(char *New)
556 buf = VFS_GetAbsPath(New);
558 Log("VFS_ChDir: Path expansion failed");
562 // Check if path exists
563 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
565 Log("VFS_ChDir: Path is invalid");
569 // Get node so we can check for directory
570 h = VFS_GetHandle(fd);
571 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
572 Log("VFS_ChDir: Path is not a directory");
580 // Free old working directory
581 if( CFGPTR(CFG_VFS_CWD) )
582 free( CFGPTR(CFG_VFS_CWD) );
584 CFGPTR(CFG_VFS_CWD) = buf;
586 Log("Updated CWD to '%s'", buf);
592 * \fn int VFS_ChRoot(char *New)
593 * \brief Change current root directory
595 int VFS_ChRoot(char *New)
601 if(New[0] == '/' && New[1] == '\0')
602 return 1; // What a useless thing to ask!
605 buf = VFS_GetAbsPath(New);
607 LOG("Path expansion failed");
611 // Check if path exists
612 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
614 LOG("Path is invalid");
618 // Get node so we can check for directory
619 h = VFS_GetHandle(fd);
620 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
621 LOG("Path is not a directory");
629 // Free old working directory
630 if( CFGPTR(CFG_VFS_CHROOT) )
631 free( CFGPTR(CFG_VFS_CHROOT) );
633 CFGPTR(CFG_VFS_CHROOT) = buf;
635 LOG("Updated Root to '%s'", buf);
641 * \fn tVFS_Handle *VFS_GetHandle(int FD)
642 * \brief Gets a pointer to the handle information structure
644 tVFS_Handle *VFS_GetHandle(int FD)
648 if(FD < 0) return NULL;
650 if(FD & VFS_KERNEL_FLAG) {
651 FD &= (VFS_KERNEL_FLAG - 1);
652 if(FD >= MAX_KERNEL_FILES) return NULL;
653 h = &gaKernelHandles[ FD ];
655 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
656 h = &gaUserHandles[ FD ];
659 if(h->Node == NULL) return NULL;