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);
36 Uint slashOffsets[MAX_PATH_SLASHES];
37 char *cwd = CFGPTR(CFG_VFS_CWD);
44 ret = malloc(strlen(Path)+1);
46 Warning("VFS_GetAbsPath - malloc() returned NULL");
54 // Check if the path is already absolute
57 ret = malloc(pathLen + 1);
59 Warning("VFS_GetAbsPath - malloc() returned NULL");
65 endLen = cwdLen + pathLen + 2;
66 // Prepend the current directory
70 strcpy(&ret[cwdLen+1], Path);
74 read = write = 1; // Cwd has already been parsed
75 for(; read < endLen; read = pos+1)
77 pos = strpos( &ret[read], '/' );
78 // If we are in the last section, force a break at the end of the itteration
79 if(pos == -1) pos = endLen;
80 else pos += read; // Else, Adjust to absolute
82 //Log("pos-read = %i", pos-read);
87 //Log("&ret[read] = '%s'", &ret[read]);
89 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
91 if(strncmp(&ret[read], "..", pos-read) == 0)
93 // If there is no higher, silently ignore
98 // Reverse write pointer
99 write = slashOffsets[ --slashNum ];
105 // Only copy if the positions differ
107 Log("write = %i, read = %i, pos-read+1 = %i", write, read, pos-read+1);
108 memcpy( &ret[write], &ret[read], pos-read+1 );
109 Log("ret = '%s'", ret);
112 if(slashNum < MAX_PATH_SLASHES)
113 slashOffsets[ slashNum++ ] = write;
115 LOG("Path '%s' has too many elements", Path);
121 // Increment write pointer
122 write += (pos-read)+1;
125 // `ret` should now be the absolute path
127 Log("VFS_GetAbsPath: RETURN '%s'", ret);
132 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
133 * \brief Parses a path, resolving sysmlinks and applying permissions
135 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
138 tVFS_Mount *longestMount = gRootMount; // Root is first
139 int cmp, retLength = 0;
141 tVFS_Node *curNode, *tmpNode;
144 ENTER("sPath pTruePath", Path, TruePath);
149 *TruePath = malloc(strlen(Path)+1);
150 strcpy(*TruePath, Path);
152 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
156 // For root we always fast return
158 if(Path[0] == '/' && Path[1] == '\0') {
160 *TruePath = malloc( gRootMount->MountPointLen+1 );
161 strcpy(*TruePath, gRootMount->MountPoint);
163 LEAVE('p', gRootMount->RootNode);
164 return gRootMount->RootNode;
167 // Check if there is anything mounted
168 if(!gMounts) return NULL;
176 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
178 // Length Check - If the length is smaller than the longest match sofar
179 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
181 cmp = strcmp(Path, mnt->MountPoint);
184 // Fast Break - Request Mount Root
187 *TruePath = malloc( mnt->MountPointLen+1 );
188 strcpy(*TruePath, mnt->MountPoint);
190 LEAVE('p', mnt->RootNode);
191 return mnt->RootNode;
194 // Not a match, continue
195 if(cmp != '/') continue;
200 /*if(!longestMount) {
201 Log("VFS_GetTruePath - ERROR: No Root Node\n");
205 // Save to shorter variable
208 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
213 *TruePath = malloc( mnt->MountPointLen+1 );
214 strcpy(*TruePath, mnt->MountPoint);
215 retLength = mnt->MountPointLen;
218 curNode = mnt->RootNode;
219 curNode->ReferenceCount ++;
221 ofs = mnt->MountPointLen+1;
222 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
225 Path[nextSlash] = '\0';
227 // Check for empty string
228 if( Path[ofs] == '\0' ) continue;
230 // Check permissions on root of filesystem
231 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
232 curNode->Close( curNode );
241 // Check if the node has a FindDir method
242 if(!curNode->FindDir) {
243 if(curNode->Close) curNode->Close(curNode);
248 Path[nextSlash] = '/';
252 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
254 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
255 LOG("tmpNode = %p", tmpNode);
257 curNode->Close(curNode);
262 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
267 Path[nextSlash] = '/';
272 // Handle Symbolic Links
273 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
278 tmp = malloc( curNode->Size + 1 );
279 curNode->Read( curNode, 0, curNode->Size, tmp );
280 tmp[ curNode->Size ] = '\0';
282 // Parse Symlink Path
283 curNode = VFS_ParsePath(tmp, TruePath);
287 free(tmp); // Free temp string
295 retLength = strlen(tmp);
297 free(tmp); // Free temp string
303 // Handle Non-Directories
304 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
306 Warning("VFS_ParsePath - File in directory context");
307 if(TruePath) free(*TruePath);
312 // Check if path needs extending
313 if(!TruePath) continue;
315 // Increase buffer space
316 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
317 // Check if allocation succeeded
319 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
321 if(curNode->Close) curNode->Close(curNode);
327 (*TruePath)[retLength] = '/';
328 strcpy(*TruePath+retLength+1, &Path[ofs]);
330 retLength += strlen(&Path[ofs])+1;
334 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
335 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
336 LOG("tmpNode = %p", tmpNode);
337 if(curNode->Close) curNode->Close(curNode);
338 // Check if file was found
340 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
341 if(TruePath) free(*TruePath);
342 if(curNode->Close) curNode->Close(curNode);
349 // Increase buffer space
350 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
351 // Check if allocation succeeded
353 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
355 if(tmpNode->Close) tmpNode->Close(curNode);
361 (*TruePath)[retLength] = '/';
362 strcpy(*TruePath + retLength + 1, &Path[ofs]);
364 //retLength += strlen(tmpNode->Name) + 1;
372 * \fn int VFS_Open(char *Path, Uint Mode)
375 int VFS_Open(char *Path, Uint Mode)
381 ENTER("sPath xMode", Path, Mode);
384 absPath = VFS_GetAbsPath(Path);
385 LOG("absPath = \"%s\"", absPath);
386 // Parse path and get mount point
387 node = VFS_ParsePath(absPath, NULL);
388 // Free generated path
392 LOG("Cannot find node");
397 // Check for symlinks
398 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
401 Warning("No read method on symlink");
405 absPath = malloc(node->Size+1); // Allocate Buffer
406 node->Read( node, 0, node->Size, absPath ); // Read Path
408 absPath[ node->Size ] = '\0'; // End String
409 if(node->Close) node->Close( node ); // Close old node
410 node = VFS_ParsePath(absPath, NULL); // Get new node
411 free( absPath ); // Free allocated path
420 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
421 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
422 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
427 if( !VFS_CheckACL(node, i) ) {
433 // Check for a user open
434 if(Mode & VFS_OPENFLAG_USER)
437 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
440 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
441 for(addr = 0; addr < size; addr += 0x1000)
442 MM_Allocate( (Uint)gaUserHandles + addr );
443 memset( gaUserHandles, 0, size );
446 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
448 if(gaUserHandles[i].Node) continue;
449 gaUserHandles[i].Node = node;
450 gaUserHandles[i].Position = 0;
451 gaUserHandles[i].Mode = Mode;
458 // Allocate space if not already
459 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
462 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
463 for(addr = 0; addr < size; addr += 0x1000)
464 MM_Allocate( (Uint)gaKernelHandles + addr );
465 memset( gaKernelHandles, 0, size );
468 for(i=0;i<MAX_KERNEL_FILES;i++)
470 if(gaKernelHandles[i].Node) continue;
471 gaKernelHandles[i].Node = node;
472 gaKernelHandles[i].Position = 0;
473 gaKernelHandles[i].Mode = Mode;
474 LEAVE('x', i|VFS_KERNEL_FLAG);
475 return i|VFS_KERNEL_FLAG;
484 * \fn void VFS_Close(int FD)
485 * \brief Closes an open file handle
487 void VFS_Close(int FD)
492 h = VFS_GetHandle(FD);
494 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
499 h->Node->Close( h->Node );
505 * \fn int VFS_ChDir(char *New)
506 * \brief Change current working directory
508 int VFS_ChDir(char *New)
515 buf = VFS_GetAbsPath(New);
517 Log("VFS_ChDir: Path expansion failed");
521 // Check if path exists
522 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
524 Log("VFS_ChDir: Path is invalid");
528 // Get node so we can check for directory
529 h = VFS_GetHandle(fd);
530 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
531 Log("VFS_ChDir: Path is not a directory");
539 // Free old working directory
540 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
542 CFGPTR(CFG_VFS_CWD) = buf;
544 Log("Updated CWD to '%s'", buf);
550 * \fn tVFS_Handle *VFS_GetHandle(int FD)
551 * \brief Gets a pointer to the handle information structure
553 tVFS_Handle *VFS_GetHandle(int FD)
557 if(FD < 0) return NULL;
559 if(FD & VFS_KERNEL_FLAG) {
560 FD &= (VFS_KERNEL_FLAG - 1);
561 if(FD >= MAX_KERNEL_FILES) return NULL;
562 h = &gaKernelHandles[ FD ];
564 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
565 h = &gaUserHandles[ FD ];
568 if(h->Node == NULL) return NULL;