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=1, baseLen;
35 Uint slashOffsets[MAX_PATH_SLASHES] = {1};
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);
72 read = write = 1; // Cwd has already been parsed
73 for(; read < baseLen+pathLen; read = pos+1)
75 pos = strpos( &ret[read], '/' );
76 // If we are in the last section, force a break at the end of the itteration
77 if(pos == -1) pos = baseLen+pathLen;
78 else pos += read; // Else, Adjust to absolute
80 //Log("pos-read = %i", pos-read);
85 //Log("&ret[read] = '%s'", &ret[read]);
87 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
89 if(strncmp(&ret[read], "..", pos-read) == 0)
91 // If there is no higher, silently ignore
92 if(slashNum < 2) continue;
93 // Reverse write pointer
94 write = slashOffsets[ --slashNum - 1 ];
100 // Only copy if the positions differ
102 Log("write = %i, read = %i", write, read);
103 memcpy( &ret[write], &ret[read], pos-read+1 );
105 write += (pos-read)+1;
107 if(slashNum < MAX_PATH_SLASHES)
108 slashOffsets[ slashNum++ ] = write;
110 LOG("Path '%s' has too many elements", Path);
117 // `ret` should now be the absolute path
119 Log("VFS_GetAbsPath: RETURN '%s'", ret);
124 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
125 * \brief Parses a path, resolving sysmlinks and applying permissions
127 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
130 tVFS_Mount *longestMount = gRootMount; // Root is first
131 int cmp, retLength = 0;
133 tVFS_Node *curNode, *tmpNode;
136 ENTER("sPath pTruePath", Path, TruePath);
141 *TruePath = malloc(strlen(Path)+1);
142 strcpy(*TruePath, Path);
144 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
148 // For root we always fast return
150 if(Path[0] == '/' && Path[1] == '\0') {
152 *TruePath = malloc( gRootMount->MountPointLen+1 );
153 strcpy(*TruePath, gRootMount->MountPoint);
155 LEAVE('p', gRootMount->RootNode);
156 return gRootMount->RootNode;
159 // Check if there is anything mounted
160 if(!gMounts) return NULL;
168 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
170 // Length Check - If the length is smaller than the longest match sofar
171 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
173 cmp = strcmp(Path, mnt->MountPoint);
176 // Fast Break - Request Mount Root
179 *TruePath = malloc( mnt->MountPointLen+1 );
180 strcpy(*TruePath, mnt->MountPoint);
182 LEAVE('p', mnt->RootNode);
183 return mnt->RootNode;
186 // Not a match, continue
187 if(cmp != '/') continue;
192 /*if(!longestMount) {
193 Log("VFS_GetTruePath - ERROR: No Root Node\n");
197 // Save to shorter variable
200 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
205 *TruePath = malloc( mnt->MountPointLen+1 );
206 strcpy(*TruePath, mnt->MountPoint);
207 retLength = mnt->MountPointLen;
210 curNode = mnt->RootNode;
211 curNode->ReferenceCount ++;
213 ofs = mnt->MountPointLen+1;
214 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
217 Path[nextSlash] = '\0';
219 // Check for empty string
220 if( Path[ofs] == '\0' ) continue;
222 // Check permissions on root of filesystem
223 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
224 curNode->Close( curNode );
233 // Check if the node has a FindDir method
234 if(!curNode->FindDir) {
235 if(curNode->Close) curNode->Close(curNode);
240 Path[nextSlash] = '/';
244 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
246 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
247 LOG("tmpNode = %p", tmpNode);
249 curNode->Close(curNode);
254 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
259 Path[nextSlash] = '/';
264 // Handle Symbolic Links
265 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
270 tmp = malloc( curNode->Size + 1 );
271 curNode->Read( curNode, 0, curNode->Size, tmp );
272 tmp[ curNode->Size ] = '\0';
274 // Parse Symlink Path
275 curNode = VFS_ParsePath(tmp, TruePath);
279 free(tmp); // Free temp string
287 retLength = strlen(tmp);
289 free(tmp); // Free temp string
295 // Handle Non-Directories
296 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
298 Warning("VFS_ParsePath - File in directory context");
299 if(TruePath) free(*TruePath);
304 // Check if path needs extending
305 if(!TruePath) continue;
307 // Increase buffer space
308 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
309 // Check if allocation succeeded
311 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
313 if(curNode->Close) curNode->Close(curNode);
319 (*TruePath)[retLength] = '/';
320 strcpy(*TruePath+retLength+1, &Path[ofs]);
322 retLength += strlen(&Path[ofs])+1;
326 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
327 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
328 LOG("tmpNode = %p", tmpNode);
329 if(curNode->Close) curNode->Close(curNode);
330 // Check if file was found
332 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
333 if(TruePath) free(*TruePath);
334 if(curNode->Close) curNode->Close(curNode);
341 // Increase buffer space
342 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
343 // Check if allocation succeeded
345 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
347 if(tmpNode->Close) tmpNode->Close(curNode);
353 (*TruePath)[retLength] = '/';
354 strcpy(*TruePath + retLength + 1, &Path[ofs]);
356 //retLength += strlen(tmpNode->Name) + 1;
364 * \fn int VFS_Open(char *Path, Uint Mode)
367 int VFS_Open(char *Path, Uint Mode)
373 ENTER("sPath xMode", Path, Mode);
376 absPath = VFS_GetAbsPath(Path);
377 LOG("absPath = \"%s\"", absPath);
378 // Parse path and get mount point
379 node = VFS_ParsePath(absPath, NULL);
380 // Free generated path
384 LOG("Cannot find node");
389 // Check for symlinks
390 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
393 Warning("No read method on symlink");
397 absPath = malloc(node->Size+1); // Allocate Buffer
398 node->Read( node, 0, node->Size, absPath ); // Read Path
400 absPath[ node->Size ] = '\0'; // End String
401 if(node->Close) node->Close( node ); // Close old node
402 node = VFS_ParsePath(absPath, NULL); // Get new node
403 free( absPath ); // Free allocated path
412 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
413 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
414 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
419 if( !VFS_CheckACL(node, i) ) {
425 // Check for a user open
426 if(Mode & VFS_OPENFLAG_USER)
429 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
432 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
433 for(addr = 0; addr < size; addr += 0x1000)
434 MM_Allocate( (Uint)gaUserHandles + addr );
435 memset( gaUserHandles, 0, size );
438 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
440 if(gaUserHandles[i].Node) continue;
441 gaUserHandles[i].Node = node;
442 gaUserHandles[i].Position = 0;
443 gaUserHandles[i].Mode = Mode;
450 // Allocate space if not already
451 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
454 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
455 for(addr = 0; addr < size; addr += 0x1000)
456 MM_Allocate( (Uint)gaKernelHandles + addr );
457 memset( gaKernelHandles, 0, size );
460 for(i=0;i<MAX_KERNEL_FILES;i++)
462 if(gaKernelHandles[i].Node) continue;
463 gaKernelHandles[i].Node = node;
464 gaKernelHandles[i].Position = 0;
465 gaKernelHandles[i].Mode = Mode;
466 LEAVE('x', i|VFS_KERNEL_FLAG);
467 return i|VFS_KERNEL_FLAG;
476 * \fn void VFS_Close(int FD)
477 * \brief Closes an open file handle
479 void VFS_Close(int FD)
484 h = VFS_GetHandle(FD);
486 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
491 h->Node->Close( h->Node );
497 * \fn int VFS_ChDir(char *New)
498 * \brief Change current working directory
500 int VFS_ChDir(char *New)
507 buf = VFS_GetAbsPath(New);
509 Log("VFS_ChDir: Path expansion failed");
513 // Check if path exists
514 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
516 Log("VFS_ChDir: Path is invalid");
520 // Get node so we can check for directory
521 h = VFS_GetHandle(fd);
522 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
523 Log("VFS_ChDir: Path is not a directory");
531 // Free old working directory
532 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
534 CFGPTR(CFG_VFS_CWD) = buf;
536 Log("Updated CWD to '%s'", buf);
542 * \fn tVFS_Handle *VFS_GetHandle(int FD)
543 * \brief Gets a pointer to the handle information structure
545 tVFS_Handle *VFS_GetHandle(int FD)
549 if(FD < 0) return NULL;
551 if(FD & VFS_KERNEL_FLAG) {
552 FD &= (VFS_KERNEL_FLAG - 1);
553 if(FD >= MAX_KERNEL_FILES) return NULL;
554 h = &gaKernelHandles[ FD ];
556 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
557 h = &gaUserHandles[ FD ];
560 if(h->Node == NULL) return NULL;