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);
59 // Check if the path is already absolute
61 ret = malloc(pathLen + 1);
66 // Prepend the current directory
67 ret = malloc(cwdLen+pathLen+1);
69 strcpy(&ret[cwdLen], Path);
71 // Pre-fill the slash positions
73 while( (pos = strpos( &ret[pos+1], '/' )) != -1 )
74 slashOffsets[slashNum++] = pos;
80 read = write = baseLen; // Cwd has already been parsed
81 for(; read < baseLen+pathLen; read = pos+1)
83 pos = strpos( &ret[read], '/' );
84 // If we are in the last section, force a break at the end of the itteration
85 if(pos == -1) pos = baseLen+pathLen;
86 else pos += read; // Else, Adjust to absolute
92 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
94 if(strncmp(&ret[read], "..", pos-read) == 0)
96 // If there is no higher, silently ignore
97 if(!slashNum) continue;
98 // Reverse write pointer
99 write = slashOffsets[ slashNum-- ];
105 // Only copy if the positions differ
107 memcpy( &ret[write], &ret[read], pos-read+1 );
111 slashOffsets[ slashNum++ ] = pos;
113 LOG("Path '%s' has too many elements", Path);
120 // `ret` should now be the absolute path
126 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
127 * \brief Parses a path, resolving sysmlinks and applying permissions
129 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
132 tVFS_Mount *longestMount = gRootMount; // Root is first
133 int cmp, retLength = 0;
135 tVFS_Node *curNode, *tmpNode;
138 ENTER("sPath pTruePath", Path, TruePath);
143 *TruePath = malloc(strlen(Path)+1);
144 strcpy(*TruePath, Path);
146 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
150 // For root we always fast return
152 if(Path[0] == '/' && Path[1] == '\0') {
154 *TruePath = malloc( gRootMount->MountPointLen+1 );
155 strcpy(*TruePath, gRootMount->MountPoint);
157 LEAVE('p', gRootMount->RootNode);
158 return gRootMount->RootNode;
161 // Check if there is anything mounted
162 if(!gMounts) return NULL;
170 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
172 // Length Check - If the length is smaller than the longest match sofar
173 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
175 cmp = strcmp(Path, mnt->MountPoint);
178 // Fast Break - Request Mount Root
181 *TruePath = malloc( mnt->MountPointLen+1 );
182 strcpy(*TruePath, mnt->MountPoint);
184 LEAVE('p', mnt->RootNode);
185 return mnt->RootNode;
188 // Not a match, continue
189 if(cmp != '/') continue;
194 /*if(!longestMount) {
195 Log("VFS_GetTruePath - ERROR: No Root Node\n");
199 // Save to shorter variable
202 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
207 *TruePath = malloc( mnt->MountPointLen+1 );
208 strcpy(*TruePath, mnt->MountPoint);
209 retLength = mnt->MountPointLen;
212 curNode = mnt->RootNode;
213 curNode->ReferenceCount ++;
215 ofs = mnt->MountPointLen+1;
216 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
219 Path[nextSlash] = '\0';
221 // Check for empty string
222 if( Path[ofs] == '\0' ) continue;
224 // Check permissions on root of filesystem
225 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
226 curNode->Close( curNode );
235 // Check if the node has a FindDir method
236 if(!curNode->FindDir) {
237 if(curNode->Close) curNode->Close(curNode);
242 Path[nextSlash] = '/';
246 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
248 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
249 LOG("tmpNode = %p", tmpNode);
251 curNode->Close(curNode);
256 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
261 Path[nextSlash] = '/';
266 // Handle Symbolic Links
267 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
272 tmp = malloc( curNode->Size + 1 );
273 curNode->Read( curNode, 0, curNode->Size, tmp );
274 tmp[ curNode->Size ] = '\0';
276 // Parse Symlink Path
277 curNode = VFS_ParsePath(tmp, TruePath);
281 free(tmp); // Free temp string
289 retLength = strlen(tmp);
291 free(tmp); // Free temp string
297 // Handle Non-Directories
298 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
300 Warning("VFS_ParsePath - File in directory context");
301 if(TruePath) free(*TruePath);
306 // Check if path needs extending
307 if(!TruePath) continue;
309 // Increase buffer space
310 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
311 // Check if allocation succeeded
313 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
315 if(curNode->Close) curNode->Close(curNode);
321 (*TruePath)[retLength] = '/';
322 strcpy(*TruePath+retLength+1, &Path[ofs]);
324 retLength += strlen(&Path[ofs])+1;
328 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
329 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
330 LOG("tmpNode = %p", tmpNode);
331 if(curNode->Close) curNode->Close(curNode);
332 // Check if file was found
334 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
335 if(TruePath) free(*TruePath);
336 if(curNode->Close) curNode->Close(curNode);
343 // Increase buffer space
344 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
345 // Check if allocation succeeded
347 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
349 if(tmpNode->Close) tmpNode->Close(curNode);
355 (*TruePath)[retLength] = '/';
356 strcpy(*TruePath + retLength + 1, &Path[ofs]);
358 //retLength += strlen(tmpNode->Name) + 1;
366 * \fn int VFS_Open(char *Path, Uint Mode)
369 int VFS_Open(char *Path, Uint Mode)
375 ENTER("sPath xMode", Path, Mode);
378 absPath = VFS_GetAbsPath(Path);
379 LOG("absPath = \"%s\"", absPath);
380 // Parse path and get mount point
381 node = VFS_ParsePath(absPath, NULL);
382 // Free generated path
386 LOG("Cannot find node");
391 // Check for symlinks
392 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
395 Warning("No read method on symlink");
399 absPath = malloc(node->Size+1); // Allocate Buffer
400 node->Read( node, 0, node->Size, absPath ); // Read Path
402 absPath[ node->Size ] = '\0'; // End String
403 if(node->Close) node->Close( node ); // Close old node
404 node = VFS_ParsePath(absPath, NULL); // Get new node
405 free( absPath ); // Free allocated path
414 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
415 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
416 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
421 if( !VFS_CheckACL(node, i) ) {
427 // Check for a user open
428 if(Mode & VFS_OPENFLAG_USER)
431 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
434 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
435 for(addr = 0; addr < size; addr += 0x1000)
436 MM_Allocate( (Uint)gaUserHandles + addr );
437 memset( gaUserHandles, 0, size );
440 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
442 if(gaUserHandles[i].Node) continue;
443 gaUserHandles[i].Node = node;
444 gaUserHandles[i].Position = 0;
445 gaUserHandles[i].Mode = Mode;
452 // Allocate space if not already
453 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
456 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
457 for(addr = 0; addr < size; addr += 0x1000)
458 MM_Allocate( (Uint)gaKernelHandles + addr );
459 memset( gaKernelHandles, 0, size );
462 for(i=0;i<MAX_KERNEL_FILES;i++)
464 if(gaKernelHandles[i].Node) continue;
465 gaKernelHandles[i].Node = node;
466 gaKernelHandles[i].Position = 0;
467 gaKernelHandles[i].Mode = Mode;
468 LEAVE('x', i|VFS_KERNEL_FLAG);
469 return i|VFS_KERNEL_FLAG;
478 * \fn void VFS_Close(int FD)
479 * \brief Closes an open file handle
481 void VFS_Close(int FD)
486 h = VFS_GetHandle(FD);
488 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
493 h->Node->Close( h->Node );
499 * \fn int VFS_ChDir(char *New)
500 * \brief Change current working directory
502 int VFS_ChDir(char *New)
508 buf = VFS_GetAbsPath(New);
509 if(buf == NULL) return -1;
511 // Check if path is valid
512 node = VFS_ParsePath(buf, NULL);
515 // Check if is a directory
516 if( !(node->Flags & VFS_FFLAG_DIRECTORY) ) {
517 if(node->Close) node->Close(node);
521 if(node->Close) node->Close(node);
526 // Free old and set new
527 free( CFGPTR(CFG_VFS_CWD) );
528 CFGPTR(CFG_VFS_CWD) = buf;
534 * \fn tVFS_Handle *VFS_GetHandle(int FD)
535 * \brief Gets a pointer to the handle information structure
537 tVFS_Handle *VFS_GetHandle(int FD)
541 if(FD < 0) return NULL;
543 if(FD & VFS_KERNEL_FLAG) {
544 FD &= (VFS_KERNEL_FLAG - 1);
545 if(FD >= MAX_KERNEL_FILES) return NULL;
546 h = &gaKernelHandles[ FD ];
548 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
549 h = &gaUserHandles[ FD ];
552 if(h->Node == NULL) return NULL;