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 *cwd = CFGPTR(CFG_VFS_CWD);
45 ret = malloc(strlen(Path)+1);
47 Warning("VFS_GetAbsPath - malloc() returned NULL");
55 // Check if the path is already absolute
57 ret = malloc(pathLen + 1);
59 Warning("VFS_GetAbsPath - malloc() returned NULL");
71 // Prepend the current directory
72 ret = malloc( cwdLen + 1 + pathLen + 1 );
75 strcpy(&ret[cwdLen+1], Path);
79 pathComps[iPos++] = tmpStr = ret+1;
84 pathComps[iPos++] = tmpStr;
85 if(iPos == MAX_PATH_SLASHES) {
86 LOG("Path '%s' has too many elements", Path);
93 pathComps[iPos] = NULL;
97 while(pathComps[iPos])
99 tmpStr = pathComps[iPos];
100 // Always Increment iPos
103 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
110 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
115 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
121 pathComps[iPos2] = tmpStr;
124 pathComps[iPos2] = NULL;
129 while(pathComps[iPos])
131 tmpStr = pathComps[iPos];
132 while(*tmpStr && *tmpStr != '/')
134 ret[iPos2++] = *tmpStr;
146 Log("VFS_GetAbsPath: RETURN '%s'", ret);
151 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
152 * \brief Parses a path, resolving sysmlinks and applying permissions
154 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
157 tVFS_Mount *longestMount = gRootMount; // Root is first
158 int cmp, retLength = 0;
160 tVFS_Node *curNode, *tmpNode;
163 ENTER("sPath pTruePath", Path, TruePath);
168 *TruePath = malloc(strlen(Path)+1);
169 strcpy(*TruePath, Path);
171 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
175 // For root we always fast return
177 if(Path[0] == '/' && Path[1] == '\0') {
179 *TruePath = malloc( gRootMount->MountPointLen+1 );
180 strcpy(*TruePath, gRootMount->MountPoint);
182 LEAVE('p', gRootMount->RootNode);
183 return gRootMount->RootNode;
186 // Check if there is anything mounted
188 Warning("WTF! There's nothing mounted?");
198 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
200 // Length Check - If the length is smaller than the longest match sofar
201 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
203 cmp = strcmp(Path, mnt->MountPoint);
206 // Fast Break - Request Mount Root
209 *TruePath = malloc( mnt->MountPointLen+1 );
210 strcpy(*TruePath, mnt->MountPoint);
212 LEAVE('p', mnt->RootNode);
213 return mnt->RootNode;
216 // Not a match, continue
217 if(cmp != '/') continue;
222 /*if(!longestMount) {
223 Log("VFS_GetTruePath - ERROR: No Root Node\n");
227 // Save to shorter variable
230 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
235 *TruePath = malloc( mnt->MountPointLen+1 );
236 strcpy(*TruePath, mnt->MountPoint);
237 retLength = mnt->MountPointLen;
240 curNode = mnt->RootNode;
241 curNode->ReferenceCount ++;
243 ofs = mnt->MountPointLen+1;
244 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
247 Path[nextSlash] = '\0';
249 // Check for empty string
250 if( Path[ofs] == '\0' ) continue;
252 // Check permissions on root of filesystem
253 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
254 curNode->Close( curNode );
259 Log("Permissions fail on '%s'", Path);
264 // Check if the node has a FindDir method
265 if(!curNode->FindDir) {
266 if(curNode->Close) curNode->Close(curNode);
271 Path[nextSlash] = '/';
272 Log("FindDir fail on '%s'", Path);
276 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
278 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
279 LOG("tmpNode = %p", tmpNode);
281 curNode->Close(curNode);
286 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
291 Log("Child fail on '%s' ('%s)", Path, &Path[ofs]);
292 Path[nextSlash] = '/';
297 // Handle Symbolic Links
298 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
303 tmp = malloc( curNode->Size + 1 );
304 curNode->Read( curNode, 0, curNode->Size, tmp );
305 tmp[ curNode->Size ] = '\0';
307 // Parse Symlink Path
308 curNode = VFS_ParsePath(tmp, TruePath);
312 Log("Symlink fail '%s'", tmp);
313 free(tmp); // Free temp string
321 retLength = strlen(tmp);
323 free(tmp); // Free temp string
329 // Handle Non-Directories
330 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
332 Warning("VFS_ParsePath - File in directory context");
333 if(TruePath) free(*TruePath);
338 // Check if path needs extending
339 if(!TruePath) continue;
341 // Increase buffer space
342 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
343 // Check if allocation succeeded
345 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
347 if(curNode->Close) curNode->Close(curNode);
353 (*TruePath)[retLength] = '/';
354 strcpy(*TruePath+retLength+1, &Path[ofs]);
356 retLength += strlen(&Path[ofs])+1;
360 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
361 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
362 LOG("tmpNode = %p", tmpNode);
363 if(curNode->Close) curNode->Close(curNode);
364 // Check if file was found
366 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
367 Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
368 if(TruePath) free(*TruePath);
369 if(curNode->Close) curNode->Close(curNode);
376 // Increase buffer space
377 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
378 // Check if allocation succeeded
380 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
382 if(tmpNode->Close) tmpNode->Close(curNode);
388 (*TruePath)[retLength] = '/';
389 strcpy(*TruePath + retLength + 1, &Path[ofs]);
391 //retLength += strlen(tmpNode->Name) + 1;
399 * \fn int VFS_Open(char *Path, Uint Mode)
402 int VFS_Open(char *Path, Uint Mode)
408 ENTER("sPath xMode", Path, Mode);
411 absPath = VFS_GetAbsPath(Path);
412 LOG("absPath = \"%s\"", absPath);
413 // Parse path and get mount point
414 node = VFS_ParsePath(absPath, NULL);
415 // Free generated path
419 LOG("Cannot find node");
424 // Check for symlinks
425 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
428 Warning("No read method on symlink");
432 absPath = malloc(node->Size+1); // Allocate Buffer
433 node->Read( node, 0, node->Size, absPath ); // Read Path
435 absPath[ node->Size ] = '\0'; // End String
436 if(node->Close) node->Close( node ); // Close old node
437 node = VFS_ParsePath(absPath, NULL); // Get new node
438 free( absPath ); // Free allocated path
442 LOG("Cannot find node");
448 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
449 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
450 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
455 if( !VFS_CheckACL(node, i) ) {
457 Log("VFS_Open: Permissions Failed");
462 // Check for a user open
463 if(Mode & VFS_OPENFLAG_USER)
466 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
469 Log("Allocating %i user handles", CFGINT(CFG_VFS_MAXFILES));
470 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
471 for(addr = 0; addr < size; addr += 0x1000)
472 MM_Allocate( (Uint)gaUserHandles + addr );
473 memset( gaUserHandles, 0, size );
476 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
478 if(gaUserHandles[i].Node) continue;
479 gaUserHandles[i].Node = node;
480 gaUserHandles[i].Position = 0;
481 gaUserHandles[i].Mode = Mode;
488 // Allocate space if not already
489 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
492 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
493 for(addr = 0; addr < size; addr += 0x1000)
494 MM_Allocate( (Uint)gaKernelHandles + addr );
495 memset( gaKernelHandles, 0, size );
498 for(i=0;i<MAX_KERNEL_FILES;i++)
500 if(gaKernelHandles[i].Node) continue;
501 gaKernelHandles[i].Node = node;
502 gaKernelHandles[i].Position = 0;
503 gaKernelHandles[i].Mode = Mode;
504 LEAVE('x', i|VFS_KERNEL_FLAG);
505 return i|VFS_KERNEL_FLAG;
509 Log("VFS_Open: Out of handles");
515 * \fn void VFS_Close(int FD)
516 * \brief Closes an open file handle
518 void VFS_Close(int FD)
523 h = VFS_GetHandle(FD);
525 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
530 h->Node->Close( h->Node );
536 * \fn int VFS_ChDir(char *New)
537 * \brief Change current working directory
539 int VFS_ChDir(char *New)
546 buf = VFS_GetAbsPath(New);
548 Log("VFS_ChDir: Path expansion failed");
552 // Check if path exists
553 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
555 Log("VFS_ChDir: Path is invalid");
559 // Get node so we can check for directory
560 h = VFS_GetHandle(fd);
561 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
562 Log("VFS_ChDir: Path is not a directory");
570 // Free old working directory
571 if( CFGPTR(CFG_VFS_CWD) )
572 free( CFGPTR(CFG_VFS_CWD) );
574 CFGPTR(CFG_VFS_CWD) = buf;
576 //Log("Updated CWD to '%s'", buf);
582 * \fn tVFS_Handle *VFS_GetHandle(int FD)
583 * \brief Gets a pointer to the handle information structure
585 tVFS_Handle *VFS_GetHandle(int FD)
589 if(FD < 0) return NULL;
591 if(FD & VFS_KERNEL_FLAG) {
592 FD &= (VFS_KERNEL_FLAG - 1);
593 if(FD >= MAX_KERNEL_FILES) return NULL;
594 h = &gaKernelHandles[ FD ];
596 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
597 h = &gaUserHandles[ FD ];
600 if(h->Node == NULL) return NULL;