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");
65 endLen = cwdLen + pathLen + 2;
66 // Prepend the current directory
70 strcpy(&ret[cwdLen+1], Path);
74 read = write = 1; // Cwd has already been parsed
75 for(; read < endLen; read = pos+1)
77 pos = strpos( &ret[read], '/' );
78 // If we are in the last section, force a break at the end of the itteration
79 if(pos == -1) pos = endLen;
80 else pos += read; // Else, Adjust to absolute
86 if(strncmp(&ret[read], ".", pos-read) == 0) continue;
88 if(strncmp(&ret[read], "..", pos-read) == 0)
90 // If there is no higher, silently ignore
95 // Reverse write pointer
96 write = slashOffsets[ --slashNum ];
102 // Only copy if the positions differ
104 //Log("write = %i, read = %i, pos-read+1 = %i", write, read, pos-read+1);
105 memcpy( &ret[write], &ret[read], pos-read+1 );
106 //Log("ret = '%s'", ret);
109 if(slashNum < MAX_PATH_SLASHES)
110 slashOffsets[ slashNum++ ] = write;
112 LOG("Path '%s' has too many elements", Path);
118 // Increment write pointer
119 write += (pos-read)+1;
122 // `ret` should now be the absolute path
124 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
129 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
130 * \brief Parses a path, resolving sysmlinks and applying permissions
132 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
135 tVFS_Mount *longestMount = gRootMount; // Root is first
136 int cmp, retLength = 0;
138 tVFS_Node *curNode, *tmpNode;
141 ENTER("sPath pTruePath", Path, TruePath);
146 *TruePath = malloc(strlen(Path)+1);
147 strcpy(*TruePath, Path);
149 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
153 // For root we always fast return
155 if(Path[0] == '/' && Path[1] == '\0') {
157 *TruePath = malloc( gRootMount->MountPointLen+1 );
158 strcpy(*TruePath, gRootMount->MountPoint);
160 LEAVE('p', gRootMount->RootNode);
161 return gRootMount->RootNode;
164 // Check if there is anything mounted
165 if(!gMounts) return NULL;
173 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
175 // Length Check - If the length is smaller than the longest match sofar
176 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
178 cmp = strcmp(Path, mnt->MountPoint);
181 // Fast Break - Request Mount Root
184 *TruePath = malloc( mnt->MountPointLen+1 );
185 strcpy(*TruePath, mnt->MountPoint);
187 LEAVE('p', mnt->RootNode);
188 return mnt->RootNode;
191 // Not a match, continue
192 if(cmp != '/') continue;
197 /*if(!longestMount) {
198 Log("VFS_GetTruePath - ERROR: No Root Node\n");
202 // Save to shorter variable
205 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
210 *TruePath = malloc( mnt->MountPointLen+1 );
211 strcpy(*TruePath, mnt->MountPoint);
212 retLength = mnt->MountPointLen;
215 curNode = mnt->RootNode;
216 curNode->ReferenceCount ++;
218 ofs = mnt->MountPointLen+1;
219 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
222 Path[nextSlash] = '\0';
224 // Check for empty string
225 if( Path[ofs] == '\0' ) continue;
227 // Check permissions on root of filesystem
228 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
229 curNode->Close( curNode );
238 // Check if the node has a FindDir method
239 if(!curNode->FindDir) {
240 if(curNode->Close) curNode->Close(curNode);
245 Path[nextSlash] = '/';
249 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
251 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
252 LOG("tmpNode = %p", tmpNode);
254 curNode->Close(curNode);
259 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
264 Path[nextSlash] = '/';
269 // Handle Symbolic Links
270 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
275 tmp = malloc( curNode->Size + 1 );
276 curNode->Read( curNode, 0, curNode->Size, tmp );
277 tmp[ curNode->Size ] = '\0';
279 // Parse Symlink Path
280 curNode = VFS_ParsePath(tmp, TruePath);
284 free(tmp); // Free temp string
292 retLength = strlen(tmp);
294 free(tmp); // Free temp string
300 // Handle Non-Directories
301 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
303 Warning("VFS_ParsePath - File in directory context");
304 if(TruePath) free(*TruePath);
309 // Check if path needs extending
310 if(!TruePath) continue;
312 // Increase buffer space
313 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
314 // Check if allocation succeeded
316 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
318 if(curNode->Close) curNode->Close(curNode);
324 (*TruePath)[retLength] = '/';
325 strcpy(*TruePath+retLength+1, &Path[ofs]);
327 retLength += strlen(&Path[ofs])+1;
331 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
332 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
333 LOG("tmpNode = %p", tmpNode);
334 if(curNode->Close) curNode->Close(curNode);
335 // Check if file was found
337 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
338 if(TruePath) free(*TruePath);
339 if(curNode->Close) curNode->Close(curNode);
346 // Increase buffer space
347 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
348 // Check if allocation succeeded
350 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
352 if(tmpNode->Close) tmpNode->Close(curNode);
358 (*TruePath)[retLength] = '/';
359 strcpy(*TruePath + retLength + 1, &Path[ofs]);
361 //retLength += strlen(tmpNode->Name) + 1;
369 * \fn int VFS_Open(char *Path, Uint Mode)
372 int VFS_Open(char *Path, Uint Mode)
378 ENTER("sPath xMode", Path, Mode);
381 absPath = VFS_GetAbsPath(Path);
382 LOG("absPath = \"%s\"", absPath);
383 // Parse path and get mount point
384 node = VFS_ParsePath(absPath, NULL);
385 // Free generated path
389 LOG("Cannot find node");
394 // Check for symlinks
395 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
398 Warning("No read method on symlink");
402 absPath = malloc(node->Size+1); // Allocate Buffer
403 node->Read( node, 0, node->Size, absPath ); // Read Path
405 absPath[ node->Size ] = '\0'; // End String
406 if(node->Close) node->Close( node ); // Close old node
407 node = VFS_ParsePath(absPath, NULL); // Get new node
408 free( absPath ); // Free allocated path
417 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
418 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
419 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
424 if( !VFS_CheckACL(node, i) ) {
430 // Check for a user open
431 if(Mode & VFS_OPENFLAG_USER)
434 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
437 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
438 for(addr = 0; addr < size; addr += 0x1000)
439 MM_Allocate( (Uint)gaUserHandles + addr );
440 memset( gaUserHandles, 0, size );
443 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
445 if(gaUserHandles[i].Node) continue;
446 gaUserHandles[i].Node = node;
447 gaUserHandles[i].Position = 0;
448 gaUserHandles[i].Mode = Mode;
455 // Allocate space if not already
456 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
459 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
460 for(addr = 0; addr < size; addr += 0x1000)
461 MM_Allocate( (Uint)gaKernelHandles + addr );
462 memset( gaKernelHandles, 0, size );
465 for(i=0;i<MAX_KERNEL_FILES;i++)
467 if(gaKernelHandles[i].Node) continue;
468 gaKernelHandles[i].Node = node;
469 gaKernelHandles[i].Position = 0;
470 gaKernelHandles[i].Mode = Mode;
471 LEAVE('x', i|VFS_KERNEL_FLAG);
472 return i|VFS_KERNEL_FLAG;
481 * \fn void VFS_Close(int FD)
482 * \brief Closes an open file handle
484 void VFS_Close(int FD)
489 h = VFS_GetHandle(FD);
491 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
496 h->Node->Close( h->Node );
502 * \fn int VFS_ChDir(char *New)
503 * \brief Change current working directory
505 int VFS_ChDir(char *New)
512 buf = VFS_GetAbsPath(New);
514 Log("VFS_ChDir: Path expansion failed");
518 // Check if path exists
519 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
521 Log("VFS_ChDir: Path is invalid");
525 // Get node so we can check for directory
526 h = VFS_GetHandle(fd);
527 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
528 Log("VFS_ChDir: Path is not a directory");
536 // Free old working directory
537 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
539 CFGPTR(CFG_VFS_CWD) = buf;
541 Log("Updated CWD to '%s'", buf);
547 * \fn tVFS_Handle *VFS_GetHandle(int FD)
548 * \brief Gets a pointer to the handle information structure
550 tVFS_Handle *VFS_GetHandle(int FD)
554 if(FD < 0) return NULL;
556 if(FD & VFS_KERNEL_FLAG) {
557 FD &= (VFS_KERNEL_FLAG - 1);
558 if(FD >= MAX_KERNEL_FILES) return NULL;
559 h = &gaKernelHandles[ FD ];
561 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
562 h = &gaUserHandles[ FD ];
565 if(h->Node == NULL) return NULL;