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);
34 int pos, slashNum=0, baseLen;
35 Uint slashOffsets[MAX_PATH_SLASHES];
36 char *cwd = CFGPTR(CFG_VFS_CWD);
43 ret = malloc(strlen(Path)+1);
45 Warning("VFS_GetAbsPath - malloc() returned NULL");
53 // Check if the path is already absolute
55 ret = malloc(pathLen + 1);
57 Warning("VFS_GetAbsPath - malloc() returned NULL");
64 // Prepend the current directory
65 ret = malloc(cwdLen+pathLen+2);
68 strcpy(&ret[cwdLen+1], Path);
70 // Pre-fill the slash positions
71 read = 1; slashNum = 0;
72 while( (pos = strpos( &ret[read], '/' )) != -1 && slashNum < MAX_PATH_SLASHES )
75 slashOffsets[slashNum++] = pos;
82 read = write = baseLen; // Cwd has already been parsed
83 for(; read < baseLen+pathLen; read = pos+1)
85 pos = strpos( &ret[read], '/' );
86 // If we are in the last section, force a break at the end of the itteration
87 if(pos == -1) pos = baseLen+pathLen;
88 else pos += read; // Else, Adjust to absolute
94 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
96 if(strncmp(&ret[read], "..", pos-read) == 0)
98 // If there is no higher, silently ignore
99 if(!slashNum) continue;
100 // Reverse write pointer
101 write = slashOffsets[ slashNum-- ];
107 // Only copy if the positions differ
109 memcpy( &ret[write], &ret[read], pos-read+1 );
112 if(slashNum < MAX_PATH_SLASHES)
113 slashOffsets[ slashNum++ ] = pos;
115 LOG("Path '%s' has too many elements", Path);
122 // `ret` should now be the absolute path
128 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
129 * \brief Parses a path, resolving sysmlinks and applying permissions
131 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
134 tVFS_Mount *longestMount = gRootMount; // Root is first
135 int cmp, retLength = 0;
137 tVFS_Node *curNode, *tmpNode;
140 ENTER("sPath pTruePath", Path, TruePath);
145 *TruePath = malloc(strlen(Path)+1);
146 strcpy(*TruePath, Path);
148 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
152 // For root we always fast return
154 if(Path[0] == '/' && Path[1] == '\0') {
156 *TruePath = malloc( gRootMount->MountPointLen+1 );
157 strcpy(*TruePath, gRootMount->MountPoint);
159 LEAVE('p', gRootMount->RootNode);
160 return gRootMount->RootNode;
163 // Check if there is anything mounted
164 if(!gMounts) return NULL;
172 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
174 // Length Check - If the length is smaller than the longest match sofar
175 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
177 cmp = strcmp(Path, mnt->MountPoint);
180 // Fast Break - Request Mount Root
183 *TruePath = malloc( mnt->MountPointLen+1 );
184 strcpy(*TruePath, mnt->MountPoint);
186 LEAVE('p', mnt->RootNode);
187 return mnt->RootNode;
190 // Not a match, continue
191 if(cmp != '/') continue;
196 /*if(!longestMount) {
197 Log("VFS_GetTruePath - ERROR: No Root Node\n");
201 // Save to shorter variable
204 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
209 *TruePath = malloc( mnt->MountPointLen+1 );
210 strcpy(*TruePath, mnt->MountPoint);
211 retLength = mnt->MountPointLen;
214 curNode = mnt->RootNode;
215 curNode->ReferenceCount ++;
217 ofs = mnt->MountPointLen+1;
218 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
221 Path[nextSlash] = '\0';
223 // Check for empty string
224 if( Path[ofs] == '\0' ) continue;
226 // Check permissions on root of filesystem
227 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
228 curNode->Close( curNode );
237 // Check if the node has a FindDir method
238 if(!curNode->FindDir) {
239 if(curNode->Close) curNode->Close(curNode);
244 Path[nextSlash] = '/';
248 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
250 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
251 LOG("tmpNode = %p", tmpNode);
253 curNode->Close(curNode);
258 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
263 Path[nextSlash] = '/';
268 // Handle Symbolic Links
269 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
274 tmp = malloc( curNode->Size + 1 );
275 curNode->Read( curNode, 0, curNode->Size, tmp );
276 tmp[ curNode->Size ] = '\0';
278 // Parse Symlink Path
279 curNode = VFS_ParsePath(tmp, TruePath);
283 free(tmp); // Free temp string
291 retLength = strlen(tmp);
293 free(tmp); // Free temp string
299 // Handle Non-Directories
300 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
302 Warning("VFS_ParsePath - File in directory context");
303 if(TruePath) free(*TruePath);
308 // Check if path needs extending
309 if(!TruePath) continue;
311 // Increase buffer space
312 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
313 // Check if allocation succeeded
315 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
317 if(curNode->Close) curNode->Close(curNode);
323 (*TruePath)[retLength] = '/';
324 strcpy(*TruePath+retLength+1, &Path[ofs]);
326 retLength += strlen(&Path[ofs])+1;
330 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
331 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
332 LOG("tmpNode = %p", tmpNode);
333 if(curNode->Close) curNode->Close(curNode);
334 // Check if file was found
336 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
337 if(TruePath) free(*TruePath);
338 if(curNode->Close) curNode->Close(curNode);
345 // Increase buffer space
346 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
347 // Check if allocation succeeded
349 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
351 if(tmpNode->Close) tmpNode->Close(curNode);
357 (*TruePath)[retLength] = '/';
358 strcpy(*TruePath + retLength + 1, &Path[ofs]);
360 //retLength += strlen(tmpNode->Name) + 1;
368 * \fn int VFS_Open(char *Path, Uint Mode)
371 int VFS_Open(char *Path, Uint Mode)
377 ENTER("sPath xMode", Path, Mode);
380 absPath = VFS_GetAbsPath(Path);
381 LOG("absPath = \"%s\"", absPath);
382 // Parse path and get mount point
383 node = VFS_ParsePath(absPath, NULL);
384 // Free generated path
388 LOG("Cannot find node");
393 // Check for symlinks
394 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
397 Warning("No read method on symlink");
401 absPath = malloc(node->Size+1); // Allocate Buffer
402 node->Read( node, 0, node->Size, absPath ); // Read Path
404 absPath[ node->Size ] = '\0'; // End String
405 if(node->Close) node->Close( node ); // Close old node
406 node = VFS_ParsePath(absPath, NULL); // Get new node
407 free( absPath ); // Free allocated path
416 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
417 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
418 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
423 if( !VFS_CheckACL(node, i) ) {
429 // Check for a user open
430 if(Mode & VFS_OPENFLAG_USER)
433 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
436 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
437 for(addr = 0; addr < size; addr += 0x1000)
438 MM_Allocate( (Uint)gaUserHandles + addr );
439 memset( gaUserHandles, 0, size );
442 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
444 if(gaUserHandles[i].Node) continue;
445 gaUserHandles[i].Node = node;
446 gaUserHandles[i].Position = 0;
447 gaUserHandles[i].Mode = Mode;
454 // Allocate space if not already
455 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
458 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
459 for(addr = 0; addr < size; addr += 0x1000)
460 MM_Allocate( (Uint)gaKernelHandles + addr );
461 memset( gaKernelHandles, 0, size );
464 for(i=0;i<MAX_KERNEL_FILES;i++)
466 if(gaKernelHandles[i].Node) continue;
467 gaKernelHandles[i].Node = node;
468 gaKernelHandles[i].Position = 0;
469 gaKernelHandles[i].Mode = Mode;
470 LEAVE('x', i|VFS_KERNEL_FLAG);
471 return i|VFS_KERNEL_FLAG;
480 * \fn void VFS_Close(int FD)
481 * \brief Closes an open file handle
483 void VFS_Close(int FD)
488 h = VFS_GetHandle(FD);
490 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
495 h->Node->Close( h->Node );
501 * \fn int VFS_ChDir(char *New)
502 * \brief Change current working directory
504 int VFS_ChDir(char *New)
511 buf = VFS_GetAbsPath(New);
513 Log("VFS_ChDir: Path expansion failed");
517 // Check if path exists
518 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
520 Log("VFS_ChDir: Path is invalid");
524 // Get node so we can check for directory
525 h = VFS_GetHandle(fd);
526 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
527 Log("VFS_ChDir: Path is not a directory");
535 // Free old working directory
536 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
538 CFGPTR(CFG_VFS_CWD) = buf;
540 Log("Updated CWD to '%s'", buf);
546 * \fn tVFS_Handle *VFS_GetHandle(int FD)
547 * \brief Gets a pointer to the handle information structure
549 tVFS_Handle *VFS_GetHandle(int FD)
553 if(FD < 0) return NULL;
555 if(FD & VFS_KERNEL_FLAG) {
556 FD &= (VFS_KERNEL_FLAG - 1);
557 if(FD >= MAX_KERNEL_FILES) return NULL;
558 h = &gaKernelHandles[ FD ];
560 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
561 h = &gaUserHandles[ FD ];
564 if(h->Node == NULL) return NULL;