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");
71 endLen = cwdLen + pathLen + 2;
72 // Prepend the current directory
76 strcpy(&ret[cwdLen+1], Path);
80 read = write = 1; // Cwd has already been parsed
81 for(; read < endLen; 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 = endLen;
86 else pos += read; // Else, Adjust to absolute
92 if(strncmp(&ret[read], ".", pos-read) == 0) {
97 if(strncmp(&ret[read], "..", pos-read) == 0)
99 // If there is no higher, silently ignore
105 // Reverse write pointer
106 write = slashOffsets[ --slashNum ];
113 // Only copy if the positions differ
115 //Log("write = %i, read = %i, pos-read+1 = %i", write, read, pos-read+1);
116 memcpy( &ret[write], &ret[read], pos-read+1 );
117 //Log("ret = '%s'", ret);
120 if(slashNum < MAX_PATH_SLASHES)
121 slashOffsets[ slashNum++ ] = write;
123 LOG("Path '%s' has too many elements", Path);
129 // Increment write pointer
130 write += (pos-read)+1;
133 // `ret` should now be the absolute path
135 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
140 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
141 * \brief Parses a path, resolving sysmlinks and applying permissions
143 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
146 tVFS_Mount *longestMount = gRootMount; // Root is first
147 int cmp, retLength = 0;
149 tVFS_Node *curNode, *tmpNode;
152 ENTER("sPath pTruePath", Path, TruePath);
157 *TruePath = malloc(strlen(Path)+1);
158 strcpy(*TruePath, Path);
160 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
164 // For root we always fast return
166 if(Path[0] == '/' && Path[1] == '\0') {
168 *TruePath = malloc( gRootMount->MountPointLen+1 );
169 strcpy(*TruePath, gRootMount->MountPoint);
171 LEAVE('p', gRootMount->RootNode);
172 return gRootMount->RootNode;
175 // Check if there is anything mounted
176 if(!gMounts) return NULL;
184 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
186 // Length Check - If the length is smaller than the longest match sofar
187 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
189 cmp = strcmp(Path, mnt->MountPoint);
192 // Fast Break - Request Mount Root
195 *TruePath = malloc( mnt->MountPointLen+1 );
196 strcpy(*TruePath, mnt->MountPoint);
198 LEAVE('p', mnt->RootNode);
199 return mnt->RootNode;
202 // Not a match, continue
203 if(cmp != '/') continue;
208 /*if(!longestMount) {
209 Log("VFS_GetTruePath - ERROR: No Root Node\n");
213 // Save to shorter variable
216 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
221 *TruePath = malloc( mnt->MountPointLen+1 );
222 strcpy(*TruePath, mnt->MountPoint);
223 retLength = mnt->MountPointLen;
226 curNode = mnt->RootNode;
227 curNode->ReferenceCount ++;
229 ofs = mnt->MountPointLen+1;
230 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
233 Path[nextSlash] = '\0';
235 // Check for empty string
236 if( Path[ofs] == '\0' ) continue;
238 // Check permissions on root of filesystem
239 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
240 curNode->Close( curNode );
249 // Check if the node has a FindDir method
250 if(!curNode->FindDir) {
251 if(curNode->Close) curNode->Close(curNode);
256 Path[nextSlash] = '/';
260 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
262 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
263 LOG("tmpNode = %p", tmpNode);
265 curNode->Close(curNode);
270 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
275 Path[nextSlash] = '/';
280 // Handle Symbolic Links
281 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
286 tmp = malloc( curNode->Size + 1 );
287 curNode->Read( curNode, 0, curNode->Size, tmp );
288 tmp[ curNode->Size ] = '\0';
290 // Parse Symlink Path
291 curNode = VFS_ParsePath(tmp, TruePath);
295 free(tmp); // Free temp string
303 retLength = strlen(tmp);
305 free(tmp); // Free temp string
311 // Handle Non-Directories
312 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
314 Warning("VFS_ParsePath - File in directory context");
315 if(TruePath) free(*TruePath);
320 // Check if path needs extending
321 if(!TruePath) continue;
323 // Increase buffer space
324 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
325 // Check if allocation succeeded
327 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
329 if(curNode->Close) curNode->Close(curNode);
335 (*TruePath)[retLength] = '/';
336 strcpy(*TruePath+retLength+1, &Path[ofs]);
338 retLength += strlen(&Path[ofs])+1;
342 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
343 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
344 LOG("tmpNode = %p", tmpNode);
345 if(curNode->Close) curNode->Close(curNode);
346 // Check if file was found
348 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
349 if(TruePath) free(*TruePath);
350 if(curNode->Close) curNode->Close(curNode);
357 // Increase buffer space
358 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
359 // Check if allocation succeeded
361 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
363 if(tmpNode->Close) tmpNode->Close(curNode);
369 (*TruePath)[retLength] = '/';
370 strcpy(*TruePath + retLength + 1, &Path[ofs]);
372 //retLength += strlen(tmpNode->Name) + 1;
380 * \fn int VFS_Open(char *Path, Uint Mode)
383 int VFS_Open(char *Path, Uint Mode)
389 ENTER("sPath xMode", Path, Mode);
392 absPath = VFS_GetAbsPath(Path);
393 LOG("absPath = \"%s\"", absPath);
394 // Parse path and get mount point
395 node = VFS_ParsePath(absPath, NULL);
396 // Free generated path
400 LOG("Cannot find node");
405 // Check for symlinks
406 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
409 Warning("No read method on symlink");
413 absPath = malloc(node->Size+1); // Allocate Buffer
414 node->Read( node, 0, node->Size, absPath ); // Read Path
416 absPath[ node->Size ] = '\0'; // End String
417 if(node->Close) node->Close( node ); // Close old node
418 node = VFS_ParsePath(absPath, NULL); // Get new node
419 free( absPath ); // Free allocated path
428 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
429 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
430 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
435 if( !VFS_CheckACL(node, i) ) {
441 // Check for a user open
442 if(Mode & VFS_OPENFLAG_USER)
445 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
448 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
449 for(addr = 0; addr < size; addr += 0x1000)
450 MM_Allocate( (Uint)gaUserHandles + addr );
451 memset( gaUserHandles, 0, size );
454 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
456 if(gaUserHandles[i].Node) continue;
457 gaUserHandles[i].Node = node;
458 gaUserHandles[i].Position = 0;
459 gaUserHandles[i].Mode = Mode;
466 // Allocate space if not already
467 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
470 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
471 for(addr = 0; addr < size; addr += 0x1000)
472 MM_Allocate( (Uint)gaKernelHandles + addr );
473 memset( gaKernelHandles, 0, size );
476 for(i=0;i<MAX_KERNEL_FILES;i++)
478 if(gaKernelHandles[i].Node) continue;
479 gaKernelHandles[i].Node = node;
480 gaKernelHandles[i].Position = 0;
481 gaKernelHandles[i].Mode = Mode;
482 LEAVE('x', i|VFS_KERNEL_FLAG);
483 return i|VFS_KERNEL_FLAG;
492 * \fn void VFS_Close(int FD)
493 * \brief Closes an open file handle
495 void VFS_Close(int FD)
500 h = VFS_GetHandle(FD);
502 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
507 h->Node->Close( h->Node );
513 * \fn int VFS_ChDir(char *New)
514 * \brief Change current working directory
516 int VFS_ChDir(char *New)
523 buf = VFS_GetAbsPath(New);
525 Log("VFS_ChDir: Path expansion failed");
529 // Check if path exists
530 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
532 Log("VFS_ChDir: Path is invalid");
536 // Get node so we can check for directory
537 h = VFS_GetHandle(fd);
538 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
539 Log("VFS_ChDir: Path is not a directory");
547 // Free old working directory
548 if( CFGPTR(CFG_VFS_CWD) )
549 free( CFGPTR(CFG_VFS_CWD) );
551 CFGPTR(CFG_VFS_CWD) = buf;
553 Log("Updated CWD to '%s'", buf);
559 * \fn tVFS_Handle *VFS_GetHandle(int FD)
560 * \brief Gets a pointer to the handle information structure
562 tVFS_Handle *VFS_GetHandle(int FD)
566 if(FD < 0) return NULL;
568 if(FD & VFS_KERNEL_FLAG) {
569 FD &= (VFS_KERNEL_FLAG - 1);
570 if(FD >= MAX_KERNEL_FILES) return NULL;
571 h = &gaKernelHandles[ FD ];
573 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
574 h = &gaUserHandles[ FD ];
577 if(h->Node == NULL) return NULL;