3 * - Open, Close and ChDir
23 #define OPEN_MOUNT_ROOT 1
24 #define MAX_KERNEL_FILES 128
27 extern tVFS_Node gVFS_MemRoot;
28 extern tVFS_Mount *gRootMount;
31 tVFS_Handle *gaUserHandles = (void*)MM_PPD_VFS;
32 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
36 * \fn char *VFS_GetAbsPath(char *Path)
37 * \brief Create an absolute path from a relative one
39 char *VFS_GetAbsPath(char *Path)
42 int pathLen = strlen(Path);
44 int pos, slashNum=0, baseLen;
45 Uint slashOffsets[256];
46 char *cwd = CFGPTR(CFG_VFS_CWD);
53 ret = malloc(strlen(Path)+1);
55 Warning("VFS_GetAbsPath - malloc() returned NULL");
63 // Check if the path is already absolute
65 ret = malloc(pathLen + 1);
67 Warning("VFS_GetAbsPath - malloc() returned NULL");
74 // Prepend the current directory
75 ret = malloc(cwdLen+pathLen+1);
77 strcpy(&ret[cwdLen], Path);
79 // Pre-fill the slash positions
81 while( (pos = strpos( &ret[pos+1], '/' )) != -1 )
82 slashOffsets[slashNum++] = pos;
88 read = write = baseLen; // Cwd has already been parsed
89 for(; read < baseLen+pathLen; read = pos+1)
91 pos = strpos( &ret[read], '/' );
92 // If we are in the last section, force a break at the end of the itteration
93 if(pos == -1) pos = baseLen+pathLen;
94 else pos += read; // Else, Adjust to absolute
100 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
102 if(strncmp(&ret[read], "..", pos-read) == 0)
104 // If there is no higher, silently ignore
105 if(!slashNum) continue;
106 // Reverse write pointer
107 write = slashOffsets[ slashNum-- ];
113 // Only copy if the positions differ
115 memcpy( &ret[write], &ret[read], pos-read+1 );
119 slashOffsets[ slashNum++ ] = pos;
121 LOG("Path '%s' has too many elements", Path);
128 // `ret` should now be the absolute path
134 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
135 * \brief Parses a path, resolving sysmlinks and applying permissions
137 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
140 tVFS_Mount *longestMount = gRootMount; // Root is first
141 int cmp, retLength = 0;
143 tVFS_Node *curNode, *tmpNode;
146 ENTER("sPath pTruePath", Path, TruePath);
151 *TruePath = malloc(strlen(Path)+1);
152 strcpy(*TruePath, Path);
154 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
158 // For root we always fast return
160 if(Path[0] == '/' && Path[1] == '\0') {
162 *TruePath = malloc( gRootMount->MountPointLen+1 );
163 strcpy(*TruePath, gRootMount->MountPoint);
165 LEAVE('p', gRootMount->RootNode);
166 return gRootMount->RootNode;
169 // Check if there is anything mounted
170 if(!gMounts) return NULL;
178 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
180 // Length Check - If the length is smaller than the longest match sofar
181 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
183 cmp = strcmp(Path, mnt->MountPoint);
186 // Fast Break - Request Mount Root
189 *TruePath = malloc( mnt->MountPointLen+1 );
190 strcpy(*TruePath, mnt->MountPoint);
192 LEAVE('p', mnt->RootNode);
193 return mnt->RootNode;
196 // Not a match, continue
197 if(cmp != '/') continue;
202 /*if(!longestMount) {
203 Log("VFS_GetTruePath - ERROR: No Root Node\n");
207 // Save to shorter variable
210 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
215 *TruePath = malloc( mnt->MountPointLen+1 );
216 strcpy(*TruePath, mnt->MountPoint);
217 retLength = mnt->MountPointLen;
220 curNode = mnt->RootNode;
221 curNode->ReferenceCount ++;
223 ofs = mnt->MountPointLen+1;
224 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
227 Path[nextSlash] = '\0';
229 // Check for empty string
230 if( Path[ofs] == '\0' ) continue;
232 // Check permissions on root of filesystem
233 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
234 curNode->Close( curNode );
243 // Check if the node has a FindDir method
244 if(!curNode->FindDir) {
245 if(curNode->Close) curNode->Close(curNode);
250 Path[nextSlash] = '/';
254 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
256 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
257 LOG("tmpNode = %p", tmpNode);
259 curNode->Close(curNode);
264 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
269 Path[nextSlash] = '/';
274 // Handle Symbolic Links
275 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
280 tmp = malloc( curNode->Size + 1 );
281 curNode->Read( curNode, 0, curNode->Size, tmp );
282 tmp[ curNode->Size ] = '\0';
284 // Parse Symlink Path
285 curNode = VFS_ParsePath(tmp, TruePath);
289 free(tmp); // Free temp string
297 retLength = strlen(tmp);
299 free(tmp); // Free temp string
305 // Handle Non-Directories
306 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
308 Warning("VFS_ParsePath - File in directory context");
309 if(TruePath) free(*TruePath);
314 // Check if path needs extending
315 if(!TruePath) continue;
317 // Increase buffer space
318 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
319 // Check if allocation succeeded
321 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
323 if(curNode->Close) curNode->Close(curNode);
329 (*TruePath)[retLength] = '/';
330 strcpy(*TruePath+retLength+1, &Path[ofs]);
332 retLength += strlen(&Path[ofs])+1;
336 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
337 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
338 LOG("tmpNode = %p", tmpNode);
339 if(curNode->Close) curNode->Close(curNode);
340 // Check if file was found
342 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
343 if(TruePath) free(*TruePath);
344 if(curNode->Close) curNode->Close(curNode);
351 // Increase buffer space
352 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
353 // Check if allocation succeeded
355 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
357 if(tmpNode->Close) tmpNode->Close(curNode);
363 (*TruePath)[retLength] = '/';
364 strcpy(*TruePath + retLength + 1, &Path[ofs]);
366 //retLength += strlen(tmpNode->Name) + 1;
374 * \fn int VFS_Open(char *Path, Uint Mode)
377 int VFS_Open(char *Path, Uint Mode)
383 ENTER("sPath xMode", Path, Mode);
386 absPath = VFS_GetAbsPath(Path);
387 LOG("absPath = \"%s\"", absPath);
388 // Parse path and get mount point
389 node = VFS_ParsePath(absPath, NULL);
390 // Free generated path
394 LOG("Cannot find node");
399 // Check for symlinks
400 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
403 Warning("No read method on symlink");
407 absPath = malloc(node->Size+1); // Allocate Buffer
408 node->Read( node, 0, node->Size, absPath ); // Read Path
410 absPath[ node->Size ] = '\0'; // End String
411 if(node->Close) node->Close( node ); // Close old node
412 node = VFS_ParsePath(absPath, NULL); // Get new node
413 free( absPath ); // Free allocated path
422 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
423 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
424 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
429 if( !VFS_CheckACL(node, i) ) {
435 // Check for a user open
436 if(Mode & VFS_OPENFLAG_USER)
439 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
442 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
443 for(addr = 0; addr < size; addr += 0x1000)
444 MM_Allocate( (Uint)gaUserHandles + addr );
445 memset( gaUserHandles, 0, size );
448 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
450 if(gaUserHandles[i].Node) continue;
451 gaUserHandles[i].Node = node;
452 gaUserHandles[i].Position = 0;
453 gaUserHandles[i].Mode = Mode;
460 // Allocate space if not already
461 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
464 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
465 for(addr = 0; addr < size; addr += 0x1000)
466 MM_Allocate( (Uint)gaKernelHandles + addr );
467 memset( gaKernelHandles, 0, size );
470 for(i=0;i<MAX_KERNEL_FILES;i++)
472 if(gaKernelHandles[i].Node) continue;
473 gaKernelHandles[i].Node = node;
474 gaKernelHandles[i].Position = 0;
475 gaKernelHandles[i].Mode = Mode;
476 LEAVE('x', i|VFS_KERNEL_FLAG);
477 return i|VFS_KERNEL_FLAG;
486 * \fn void VFS_Close(int FD)
487 * \brief Closes an open file handle
489 void VFS_Close(int FD)
494 h = VFS_GetHandle(FD);
496 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
501 h->Node->Close( h->Node );
507 * \fn int VFS_ChDir(char *New)
508 * \brief Change current working directory
510 int VFS_ChDir(char *New)
517 buf = VFS_GetAbsPath(New);
519 Log("VFS_ChDir: Path expansion failed");
523 // Check if path exists
524 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
526 Log("VFS_ChDir: Path is invalid");
530 // Get node so we can check for directory
531 h = VFS_GetHandle(fd);
532 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
533 Log("VFS_ChDir: Path is not a directory");
541 // Free old working directory
542 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
544 CFGPTR(CFG_VFS_CWD) = buf;
546 Log("Updated CWD to '%s'", buf);
552 * \fn tVFS_Handle *VFS_GetHandle(int FD)
553 * \brief Gets a pointer to the handle information structure
555 tVFS_Handle *VFS_GetHandle(int FD)
559 if(FD < 0) return NULL;
561 if(FD & VFS_KERNEL_FLAG) {
562 FD &= (VFS_KERNEL_FLAG - 1);
563 if(FD >= MAX_KERNEL_FILES) return NULL;
564 h = &gaKernelHandles[ FD ];
566 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
567 h = &gaUserHandles[ FD ];
570 if(h->Node == NULL) return NULL;