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, pos-read+1 = %i", write, read, pos-read+1);
107 memcpy( &ret[write], &ret[read], pos-read+1 );
108 Log("ret = '%s'", ret);
111 if(slashNum < MAX_PATH_SLASHES)
112 slashOffsets[ slashNum++ ] = write;
114 LOG("Path '%s' has too many elements", Path);
120 // Increment write pointer
121 write += (pos-read)+1;
124 // `ret` should now be the absolute path
126 Log("VFS_GetAbsPath: RETURN '%s'", ret);
131 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
132 * \brief Parses a path, resolving sysmlinks and applying permissions
134 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
137 tVFS_Mount *longestMount = gRootMount; // Root is first
138 int cmp, retLength = 0;
140 tVFS_Node *curNode, *tmpNode;
143 ENTER("sPath pTruePath", Path, TruePath);
148 *TruePath = malloc(strlen(Path)+1);
149 strcpy(*TruePath, Path);
151 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
155 // For root we always fast return
157 if(Path[0] == '/' && Path[1] == '\0') {
159 *TruePath = malloc( gRootMount->MountPointLen+1 );
160 strcpy(*TruePath, gRootMount->MountPoint);
162 LEAVE('p', gRootMount->RootNode);
163 return gRootMount->RootNode;
166 // Check if there is anything mounted
167 if(!gMounts) return NULL;
175 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
177 // Length Check - If the length is smaller than the longest match sofar
178 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
180 cmp = strcmp(Path, mnt->MountPoint);
183 // Fast Break - Request Mount Root
186 *TruePath = malloc( mnt->MountPointLen+1 );
187 strcpy(*TruePath, mnt->MountPoint);
189 LEAVE('p', mnt->RootNode);
190 return mnt->RootNode;
193 // Not a match, continue
194 if(cmp != '/') continue;
199 /*if(!longestMount) {
200 Log("VFS_GetTruePath - ERROR: No Root Node\n");
204 // Save to shorter variable
207 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
212 *TruePath = malloc( mnt->MountPointLen+1 );
213 strcpy(*TruePath, mnt->MountPoint);
214 retLength = mnt->MountPointLen;
217 curNode = mnt->RootNode;
218 curNode->ReferenceCount ++;
220 ofs = mnt->MountPointLen+1;
221 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
224 Path[nextSlash] = '\0';
226 // Check for empty string
227 if( Path[ofs] == '\0' ) continue;
229 // Check permissions on root of filesystem
230 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
231 curNode->Close( curNode );
240 // Check if the node has a FindDir method
241 if(!curNode->FindDir) {
242 if(curNode->Close) curNode->Close(curNode);
247 Path[nextSlash] = '/';
251 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
253 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
254 LOG("tmpNode = %p", tmpNode);
256 curNode->Close(curNode);
261 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
266 Path[nextSlash] = '/';
271 // Handle Symbolic Links
272 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
277 tmp = malloc( curNode->Size + 1 );
278 curNode->Read( curNode, 0, curNode->Size, tmp );
279 tmp[ curNode->Size ] = '\0';
281 // Parse Symlink Path
282 curNode = VFS_ParsePath(tmp, TruePath);
286 free(tmp); // Free temp string
294 retLength = strlen(tmp);
296 free(tmp); // Free temp string
302 // Handle Non-Directories
303 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
305 Warning("VFS_ParsePath - File in directory context");
306 if(TruePath) free(*TruePath);
311 // Check if path needs extending
312 if(!TruePath) continue;
314 // Increase buffer space
315 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
316 // Check if allocation succeeded
318 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
320 if(curNode->Close) curNode->Close(curNode);
326 (*TruePath)[retLength] = '/';
327 strcpy(*TruePath+retLength+1, &Path[ofs]);
329 retLength += strlen(&Path[ofs])+1;
333 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
334 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
335 LOG("tmpNode = %p", tmpNode);
336 if(curNode->Close) curNode->Close(curNode);
337 // Check if file was found
339 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
340 if(TruePath) free(*TruePath);
341 if(curNode->Close) curNode->Close(curNode);
348 // Increase buffer space
349 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
350 // Check if allocation succeeded
352 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
354 if(tmpNode->Close) tmpNode->Close(curNode);
360 (*TruePath)[retLength] = '/';
361 strcpy(*TruePath + retLength + 1, &Path[ofs]);
363 //retLength += strlen(tmpNode->Name) + 1;
371 * \fn int VFS_Open(char *Path, Uint Mode)
374 int VFS_Open(char *Path, Uint Mode)
380 ENTER("sPath xMode", Path, Mode);
383 absPath = VFS_GetAbsPath(Path);
384 LOG("absPath = \"%s\"", absPath);
385 // Parse path and get mount point
386 node = VFS_ParsePath(absPath, NULL);
387 // Free generated path
391 LOG("Cannot find node");
396 // Check for symlinks
397 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
400 Warning("No read method on symlink");
404 absPath = malloc(node->Size+1); // Allocate Buffer
405 node->Read( node, 0, node->Size, absPath ); // Read Path
407 absPath[ node->Size ] = '\0'; // End String
408 if(node->Close) node->Close( node ); // Close old node
409 node = VFS_ParsePath(absPath, NULL); // Get new node
410 free( absPath ); // Free allocated path
419 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
420 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
421 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
426 if( !VFS_CheckACL(node, i) ) {
432 // Check for a user open
433 if(Mode & VFS_OPENFLAG_USER)
436 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
439 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
440 for(addr = 0; addr < size; addr += 0x1000)
441 MM_Allocate( (Uint)gaUserHandles + addr );
442 memset( gaUserHandles, 0, size );
445 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
447 if(gaUserHandles[i].Node) continue;
448 gaUserHandles[i].Node = node;
449 gaUserHandles[i].Position = 0;
450 gaUserHandles[i].Mode = Mode;
457 // Allocate space if not already
458 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
461 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
462 for(addr = 0; addr < size; addr += 0x1000)
463 MM_Allocate( (Uint)gaKernelHandles + addr );
464 memset( gaKernelHandles, 0, size );
467 for(i=0;i<MAX_KERNEL_FILES;i++)
469 if(gaKernelHandles[i].Node) continue;
470 gaKernelHandles[i].Node = node;
471 gaKernelHandles[i].Position = 0;
472 gaKernelHandles[i].Mode = Mode;
473 LEAVE('x', i|VFS_KERNEL_FLAG);
474 return i|VFS_KERNEL_FLAG;
483 * \fn void VFS_Close(int FD)
484 * \brief Closes an open file handle
486 void VFS_Close(int FD)
491 h = VFS_GetHandle(FD);
493 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
498 h->Node->Close( h->Node );
504 * \fn int VFS_ChDir(char *New)
505 * \brief Change current working directory
507 int VFS_ChDir(char *New)
514 buf = VFS_GetAbsPath(New);
516 Log("VFS_ChDir: Path expansion failed");
520 // Check if path exists
521 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
523 Log("VFS_ChDir: Path is invalid");
527 // Get node so we can check for directory
528 h = VFS_GetHandle(fd);
529 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
530 Log("VFS_ChDir: Path is not a directory");
538 // Free old working directory
539 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
541 CFGPTR(CFG_VFS_CWD) = buf;
543 Log("Updated CWD to '%s'", buf);
549 * \fn tVFS_Handle *VFS_GetHandle(int FD)
550 * \brief Gets a pointer to the handle information structure
552 tVFS_Handle *VFS_GetHandle(int FD)
556 if(FD < 0) return NULL;
558 if(FD & VFS_KERNEL_FLAG) {
559 FD &= (VFS_KERNEL_FLAG - 1);
560 if(FD >= MAX_KERNEL_FILES) return NULL;
561 h = &gaKernelHandles[ FD ];
563 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
564 h = &gaUserHandles[ FD ];
567 if(h->Node == NULL) return NULL;