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
56 ret = malloc(pathLen + 1);
58 Warning("VFS_GetAbsPath - malloc() returned NULL");
65 // Prepend the current directory
66 ret = malloc(cwdLen+pathLen+2);
69 strcpy(&ret[cwdLen+1], Path);
73 read = write = 1; // Cwd has already been parsed
74 for(; read < baseLen+pathLen; read = pos+1)
76 pos = strpos( &ret[read], '/' );
77 // If we are in the last section, force a break at the end of the itteration
78 if(pos == -1) pos = baseLen+pathLen;
79 else pos += read; // Else, Adjust to absolute
81 //Log("pos-read = %i", pos-read);
86 //Log("&ret[read] = '%s'", &ret[read]);
88 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
90 if(strncmp(&ret[read], "..", pos-read) == 0)
92 // If there is no higher, silently ignore
97 // Reverse write pointer
98 write = slashOffsets[ --slashNum ];
104 // Only copy if the positions differ
106 Log("write = %i, read = %i", write, read);
107 memcpy( &ret[write], &ret[read], pos-read+1 );
110 if(slashNum < MAX_PATH_SLASHES)
111 slashOffsets[ slashNum++ ] = write;
113 LOG("Path '%s' has too many elements", Path);
119 // Increment write pointer
120 write += (pos-read)+1;
123 // `ret` should now be the absolute path
125 Log("VFS_GetAbsPath: RETURN '%s'", ret);
130 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
131 * \brief Parses a path, resolving sysmlinks and applying permissions
133 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
136 tVFS_Mount *longestMount = gRootMount; // Root is first
137 int cmp, retLength = 0;
139 tVFS_Node *curNode, *tmpNode;
142 ENTER("sPath pTruePath", Path, TruePath);
147 *TruePath = malloc(strlen(Path)+1);
148 strcpy(*TruePath, Path);
150 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
154 // For root we always fast return
156 if(Path[0] == '/' && Path[1] == '\0') {
158 *TruePath = malloc( gRootMount->MountPointLen+1 );
159 strcpy(*TruePath, gRootMount->MountPoint);
161 LEAVE('p', gRootMount->RootNode);
162 return gRootMount->RootNode;
165 // Check if there is anything mounted
166 if(!gMounts) return NULL;
174 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
176 // Length Check - If the length is smaller than the longest match sofar
177 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
179 cmp = strcmp(Path, mnt->MountPoint);
182 // Fast Break - Request Mount Root
185 *TruePath = malloc( mnt->MountPointLen+1 );
186 strcpy(*TruePath, mnt->MountPoint);
188 LEAVE('p', mnt->RootNode);
189 return mnt->RootNode;
192 // Not a match, continue
193 if(cmp != '/') continue;
198 /*if(!longestMount) {
199 Log("VFS_GetTruePath - ERROR: No Root Node\n");
203 // Save to shorter variable
206 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
211 *TruePath = malloc( mnt->MountPointLen+1 );
212 strcpy(*TruePath, mnt->MountPoint);
213 retLength = mnt->MountPointLen;
216 curNode = mnt->RootNode;
217 curNode->ReferenceCount ++;
219 ofs = mnt->MountPointLen+1;
220 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
223 Path[nextSlash] = '\0';
225 // Check for empty string
226 if( Path[ofs] == '\0' ) continue;
228 // Check permissions on root of filesystem
229 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
230 curNode->Close( curNode );
239 // Check if the node has a FindDir method
240 if(!curNode->FindDir) {
241 if(curNode->Close) curNode->Close(curNode);
246 Path[nextSlash] = '/';
250 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
252 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
253 LOG("tmpNode = %p", tmpNode);
255 curNode->Close(curNode);
260 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
265 Path[nextSlash] = '/';
270 // Handle Symbolic Links
271 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
276 tmp = malloc( curNode->Size + 1 );
277 curNode->Read( curNode, 0, curNode->Size, tmp );
278 tmp[ curNode->Size ] = '\0';
280 // Parse Symlink Path
281 curNode = VFS_ParsePath(tmp, TruePath);
285 free(tmp); // Free temp string
293 retLength = strlen(tmp);
295 free(tmp); // Free temp string
301 // Handle Non-Directories
302 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
304 Warning("VFS_ParsePath - File in directory context");
305 if(TruePath) free(*TruePath);
310 // Check if path needs extending
311 if(!TruePath) continue;
313 // Increase buffer space
314 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
315 // Check if allocation succeeded
317 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
319 if(curNode->Close) curNode->Close(curNode);
325 (*TruePath)[retLength] = '/';
326 strcpy(*TruePath+retLength+1, &Path[ofs]);
328 retLength += strlen(&Path[ofs])+1;
332 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
333 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
334 LOG("tmpNode = %p", tmpNode);
335 if(curNode->Close) curNode->Close(curNode);
336 // Check if file was found
338 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
339 if(TruePath) free(*TruePath);
340 if(curNode->Close) curNode->Close(curNode);
347 // Increase buffer space
348 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
349 // Check if allocation succeeded
351 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
353 if(tmpNode->Close) tmpNode->Close(curNode);
359 (*TruePath)[retLength] = '/';
360 strcpy(*TruePath + retLength + 1, &Path[ofs]);
362 //retLength += strlen(tmpNode->Name) + 1;
370 * \fn int VFS_Open(char *Path, Uint Mode)
373 int VFS_Open(char *Path, Uint Mode)
379 ENTER("sPath xMode", Path, Mode);
382 absPath = VFS_GetAbsPath(Path);
383 LOG("absPath = \"%s\"", absPath);
384 // Parse path and get mount point
385 node = VFS_ParsePath(absPath, NULL);
386 // Free generated path
390 LOG("Cannot find node");
395 // Check for symlinks
396 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
399 Warning("No read method on symlink");
403 absPath = malloc(node->Size+1); // Allocate Buffer
404 node->Read( node, 0, node->Size, absPath ); // Read Path
406 absPath[ node->Size ] = '\0'; // End String
407 if(node->Close) node->Close( node ); // Close old node
408 node = VFS_ParsePath(absPath, NULL); // Get new node
409 free( absPath ); // Free allocated path
418 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
419 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
420 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
425 if( !VFS_CheckACL(node, i) ) {
431 // Check for a user open
432 if(Mode & VFS_OPENFLAG_USER)
435 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
438 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
439 for(addr = 0; addr < size; addr += 0x1000)
440 MM_Allocate( (Uint)gaUserHandles + addr );
441 memset( gaUserHandles, 0, size );
444 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
446 if(gaUserHandles[i].Node) continue;
447 gaUserHandles[i].Node = node;
448 gaUserHandles[i].Position = 0;
449 gaUserHandles[i].Mode = Mode;
456 // Allocate space if not already
457 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
460 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
461 for(addr = 0; addr < size; addr += 0x1000)
462 MM_Allocate( (Uint)gaKernelHandles + addr );
463 memset( gaKernelHandles, 0, size );
466 for(i=0;i<MAX_KERNEL_FILES;i++)
468 if(gaKernelHandles[i].Node) continue;
469 gaKernelHandles[i].Node = node;
470 gaKernelHandles[i].Position = 0;
471 gaKernelHandles[i].Mode = Mode;
472 LEAVE('x', i|VFS_KERNEL_FLAG);
473 return i|VFS_KERNEL_FLAG;
482 * \fn void VFS_Close(int FD)
483 * \brief Closes an open file handle
485 void VFS_Close(int FD)
490 h = VFS_GetHandle(FD);
492 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
497 h->Node->Close( h->Node );
503 * \fn int VFS_ChDir(char *New)
504 * \brief Change current working directory
506 int VFS_ChDir(char *New)
513 buf = VFS_GetAbsPath(New);
515 Log("VFS_ChDir: Path expansion failed");
519 // Check if path exists
520 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
522 Log("VFS_ChDir: Path is invalid");
526 // Get node so we can check for directory
527 h = VFS_GetHandle(fd);
528 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
529 Log("VFS_ChDir: Path is not a directory");
537 // Free old working directory
538 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
540 CFGPTR(CFG_VFS_CWD) = buf;
542 Log("Updated CWD to '%s'", buf);
548 * \fn tVFS_Handle *VFS_GetHandle(int FD)
549 * \brief Gets a pointer to the handle information structure
551 tVFS_Handle *VFS_GetHandle(int FD)
555 if(FD < 0) return NULL;
557 if(FD & VFS_KERNEL_FLAG) {
558 FD &= (VFS_KERNEL_FLAG - 1);
559 if(FD >= MAX_KERNEL_FILES) return NULL;
560 h = &gaKernelHandles[ FD ];
562 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
563 h = &gaUserHandles[ FD ];
566 if(h->Node == NULL) return NULL;