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);
76 //Log("ret = '%s'\n", ret);
80 pathComps[iPos++] = tmpStr = ret+1;
85 pathComps[iPos++] = tmpStr;
86 if(iPos == MAX_PATH_SLASHES) {
87 LOG("Path '%s' has too many elements", Path);
94 pathComps[iPos] = NULL;
98 while(pathComps[iPos])
100 tmpStr = pathComps[iPos];
101 // Always Increment iPos
104 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
111 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
116 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
122 pathComps[iPos2] = tmpStr;
125 pathComps[iPos2] = NULL;
130 while(pathComps[iPos])
132 tmpStr = pathComps[iPos];
133 while(*tmpStr && *tmpStr != '/')
135 ret[iPos2++] = *tmpStr;
147 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
152 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
153 * \brief Parses a path, resolving sysmlinks and applying permissions
155 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
158 tVFS_Mount *longestMount = gRootMount; // Root is first
159 int cmp, retLength = 0;
161 tVFS_Node *curNode, *tmpNode;
164 ENTER("sPath pTruePath", Path, TruePath);
169 *TruePath = malloc(strlen(Path)+1);
170 strcpy(*TruePath, Path);
172 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
176 // For root we always fast return
178 if(Path[0] == '/' && Path[1] == '\0') {
180 *TruePath = malloc( gRootMount->MountPointLen+1 );
181 strcpy(*TruePath, gRootMount->MountPoint);
183 LEAVE('p', gRootMount->RootNode);
184 return gRootMount->RootNode;
187 // Check if there is anything mounted
189 Warning("WTF! There's nothing mounted?");
199 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
201 // Length Check - If the length is smaller than the longest match sofar
202 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
204 cmp = strcmp(Path, mnt->MountPoint);
207 // Fast Break - Request Mount Root
210 *TruePath = malloc( mnt->MountPointLen+1 );
211 strcpy(*TruePath, mnt->MountPoint);
213 LEAVE('p', mnt->RootNode);
214 return mnt->RootNode;
217 // Not a match, continue
218 if(cmp != '/') continue;
223 /*if(!longestMount) {
224 Log("VFS_ParsePath - ERROR: No Root Node\n");
228 // Save to shorter variable
231 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
236 *TruePath = malloc( mnt->MountPointLen+1 );
237 strcpy(*TruePath, mnt->MountPoint);
238 retLength = mnt->MountPointLen;
241 curNode = mnt->RootNode;
242 curNode->ReferenceCount ++;
244 ofs = mnt->MountPointLen+1;
245 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
248 Path[nextSlash] = '\0';
250 // Check for empty string
251 if( Path[ofs] == '\0' ) continue;
253 // Check permissions on root of filesystem
254 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
255 curNode->Close( curNode );
260 //Log("Permissions fail on '%s'", Path);
265 // Check if the node has a FindDir method
266 if(!curNode->FindDir) {
267 if(curNode->Close) curNode->Close(curNode);
272 Path[nextSlash] = '/';
273 //Log("FindDir fail on '%s'", Path);
277 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
279 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
280 LOG("tmpNode = %p", tmpNode);
282 curNode->Close(curNode);
287 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
292 //Log("Child fail on '%s' ('%s)", Path, &Path[ofs]);
293 Path[nextSlash] = '/';
298 // Handle Symbolic Links
299 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
304 tmp = malloc( curNode->Size + 1 );
305 curNode->Read( curNode, 0, curNode->Size, tmp );
306 tmp[ curNode->Size ] = '\0';
308 // Parse Symlink Path
309 curNode = VFS_ParsePath(tmp, TruePath);
313 Log("Symlink fail '%s'", tmp);
314 free(tmp); // Free temp string
322 retLength = strlen(tmp);
324 free(tmp); // Free temp string
330 // Handle Non-Directories
331 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
333 Warning("VFS_ParsePath - File in directory context");
334 if(TruePath) free(*TruePath);
339 // Check if path needs extending
340 if(!TruePath) continue;
342 // Increase buffer space
343 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
344 // Check if allocation succeeded
346 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
348 if(curNode->Close) curNode->Close(curNode);
354 (*TruePath)[retLength] = '/';
355 strcpy(*TruePath+retLength+1, &Path[ofs]);
357 retLength += strlen(&Path[ofs])+1;
361 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
362 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
363 LOG("tmpNode = %p", tmpNode);
364 if(curNode->Close) curNode->Close(curNode);
365 // Check if file was found
367 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
368 Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
369 if(TruePath) free(*TruePath);
370 if(curNode->Close) curNode->Close(curNode);
377 // Increase buffer space
378 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
379 // Check if allocation succeeded
381 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
383 if(tmpNode->Close) tmpNode->Close(curNode);
389 (*TruePath)[retLength] = '/';
390 strcpy(*TruePath + retLength + 1, &Path[ofs]);
392 //retLength += strlen(tmpNode->Name) + 1;
400 * \fn int VFS_Open(char *Path, Uint Mode)
403 int VFS_Open(char *Path, Uint Mode)
409 ENTER("sPath xMode", Path, Mode);
412 absPath = VFS_GetAbsPath(Path);
413 LOG("absPath = \"%s\"", absPath);
414 // Parse path and get mount point
415 node = VFS_ParsePath(absPath, NULL);
416 // Free generated path
420 LOG("Cannot find node");
425 // Check for symlinks
426 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
429 Warning("No read method on symlink");
433 absPath = malloc(node->Size+1); // Allocate Buffer
434 node->Read( node, 0, node->Size, absPath ); // Read Path
436 absPath[ node->Size ] = '\0'; // End String
437 if(node->Close) node->Close( node ); // Close old node
438 node = VFS_ParsePath(absPath, NULL); // Get new node
439 free( absPath ); // Free allocated path
443 LOG("Cannot find node");
449 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
450 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
451 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
456 if( !VFS_CheckACL(node, i) ) {
458 Log("VFS_Open: Permissions Failed");
463 // Check for a user open
464 if(Mode & VFS_OPENFLAG_USER)
467 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
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;