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=0, baseLen;
35 Uint slashOffsets[MAX_PATH_SLASHES];
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) continue;
93 // Reverse write pointer
94 write = slashOffsets[ --slashNum ];
100 // Only copy if the positions differ
102 Log("write = %i, read = %i", write, read);
103 memcpy( &ret[write], &ret[read], pos-read+1 );
106 if(slashNum < MAX_PATH_SLASHES)
107 slashOffsets[ slashNum++ ] = pos;
109 LOG("Path '%s' has too many elements", Path);
116 // `ret` should now be the absolute path
118 Log("VFS_GetAbsPath: RETURN '%s'", ret);
123 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
124 * \brief Parses a path, resolving sysmlinks and applying permissions
126 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
129 tVFS_Mount *longestMount = gRootMount; // Root is first
130 int cmp, retLength = 0;
132 tVFS_Node *curNode, *tmpNode;
135 ENTER("sPath pTruePath", Path, TruePath);
140 *TruePath = malloc(strlen(Path)+1);
141 strcpy(*TruePath, Path);
143 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
147 // For root we always fast return
149 if(Path[0] == '/' && Path[1] == '\0') {
151 *TruePath = malloc( gRootMount->MountPointLen+1 );
152 strcpy(*TruePath, gRootMount->MountPoint);
154 LEAVE('p', gRootMount->RootNode);
155 return gRootMount->RootNode;
158 // Check if there is anything mounted
159 if(!gMounts) return NULL;
167 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
169 // Length Check - If the length is smaller than the longest match sofar
170 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
172 cmp = strcmp(Path, mnt->MountPoint);
175 // Fast Break - Request Mount Root
178 *TruePath = malloc( mnt->MountPointLen+1 );
179 strcpy(*TruePath, mnt->MountPoint);
181 LEAVE('p', mnt->RootNode);
182 return mnt->RootNode;
185 // Not a match, continue
186 if(cmp != '/') continue;
191 /*if(!longestMount) {
192 Log("VFS_GetTruePath - ERROR: No Root Node\n");
196 // Save to shorter variable
199 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
204 *TruePath = malloc( mnt->MountPointLen+1 );
205 strcpy(*TruePath, mnt->MountPoint);
206 retLength = mnt->MountPointLen;
209 curNode = mnt->RootNode;
210 curNode->ReferenceCount ++;
212 ofs = mnt->MountPointLen+1;
213 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
216 Path[nextSlash] = '\0';
218 // Check for empty string
219 if( Path[ofs] == '\0' ) continue;
221 // Check permissions on root of filesystem
222 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
223 curNode->Close( curNode );
232 // Check if the node has a FindDir method
233 if(!curNode->FindDir) {
234 if(curNode->Close) curNode->Close(curNode);
239 Path[nextSlash] = '/';
243 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
245 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
246 LOG("tmpNode = %p", tmpNode);
248 curNode->Close(curNode);
253 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
258 Path[nextSlash] = '/';
263 // Handle Symbolic Links
264 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
269 tmp = malloc( curNode->Size + 1 );
270 curNode->Read( curNode, 0, curNode->Size, tmp );
271 tmp[ curNode->Size ] = '\0';
273 // Parse Symlink Path
274 curNode = VFS_ParsePath(tmp, TruePath);
278 free(tmp); // Free temp string
286 retLength = strlen(tmp);
288 free(tmp); // Free temp string
294 // Handle Non-Directories
295 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
297 Warning("VFS_ParsePath - File in directory context");
298 if(TruePath) free(*TruePath);
303 // Check if path needs extending
304 if(!TruePath) continue;
306 // Increase buffer space
307 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
308 // Check if allocation succeeded
310 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
312 if(curNode->Close) curNode->Close(curNode);
318 (*TruePath)[retLength] = '/';
319 strcpy(*TruePath+retLength+1, &Path[ofs]);
321 retLength += strlen(&Path[ofs])+1;
325 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
326 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
327 LOG("tmpNode = %p", tmpNode);
328 if(curNode->Close) curNode->Close(curNode);
329 // Check if file was found
331 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
332 if(TruePath) free(*TruePath);
333 if(curNode->Close) curNode->Close(curNode);
340 // Increase buffer space
341 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
342 // Check if allocation succeeded
344 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
346 if(tmpNode->Close) tmpNode->Close(curNode);
352 (*TruePath)[retLength] = '/';
353 strcpy(*TruePath + retLength + 1, &Path[ofs]);
355 //retLength += strlen(tmpNode->Name) + 1;
363 * \fn int VFS_Open(char *Path, Uint Mode)
366 int VFS_Open(char *Path, Uint Mode)
372 ENTER("sPath xMode", Path, Mode);
375 absPath = VFS_GetAbsPath(Path);
376 LOG("absPath = \"%s\"", absPath);
377 // Parse path and get mount point
378 node = VFS_ParsePath(absPath, NULL);
379 // Free generated path
383 LOG("Cannot find node");
388 // Check for symlinks
389 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
392 Warning("No read method on symlink");
396 absPath = malloc(node->Size+1); // Allocate Buffer
397 node->Read( node, 0, node->Size, absPath ); // Read Path
399 absPath[ node->Size ] = '\0'; // End String
400 if(node->Close) node->Close( node ); // Close old node
401 node = VFS_ParsePath(absPath, NULL); // Get new node
402 free( absPath ); // Free allocated path
411 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
412 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
413 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
418 if( !VFS_CheckACL(node, i) ) {
424 // Check for a user open
425 if(Mode & VFS_OPENFLAG_USER)
428 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
431 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
432 for(addr = 0; addr < size; addr += 0x1000)
433 MM_Allocate( (Uint)gaUserHandles + addr );
434 memset( gaUserHandles, 0, size );
437 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
439 if(gaUserHandles[i].Node) continue;
440 gaUserHandles[i].Node = node;
441 gaUserHandles[i].Position = 0;
442 gaUserHandles[i].Mode = Mode;
449 // Allocate space if not already
450 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
453 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
454 for(addr = 0; addr < size; addr += 0x1000)
455 MM_Allocate( (Uint)gaKernelHandles + addr );
456 memset( gaKernelHandles, 0, size );
459 for(i=0;i<MAX_KERNEL_FILES;i++)
461 if(gaKernelHandles[i].Node) continue;
462 gaKernelHandles[i].Node = node;
463 gaKernelHandles[i].Position = 0;
464 gaKernelHandles[i].Mode = Mode;
465 LEAVE('x', i|VFS_KERNEL_FLAG);
466 return i|VFS_KERNEL_FLAG;
475 * \fn void VFS_Close(int FD)
476 * \brief Closes an open file handle
478 void VFS_Close(int FD)
483 h = VFS_GetHandle(FD);
485 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
490 h->Node->Close( h->Node );
496 * \fn int VFS_ChDir(char *New)
497 * \brief Change current working directory
499 int VFS_ChDir(char *New)
506 buf = VFS_GetAbsPath(New);
508 Log("VFS_ChDir: Path expansion failed");
512 // Check if path exists
513 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
515 Log("VFS_ChDir: Path is invalid");
519 // Get node so we can check for directory
520 h = VFS_GetHandle(fd);
521 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
522 Log("VFS_ChDir: Path is not a directory");
530 // Free old working directory
531 if( CFGPTR(CFG_VFS_CWD) ) free( CFGPTR(CFG_VFS_CWD) );
533 CFGPTR(CFG_VFS_CWD) = buf;
535 Log("Updated CWD to '%s'", buf);
541 * \fn tVFS_Handle *VFS_GetHandle(int FD)
542 * \brief Gets a pointer to the handle information structure
544 tVFS_Handle *VFS_GetHandle(int FD)
548 if(FD < 0) return NULL;
550 if(FD & VFS_KERNEL_FLAG) {
551 FD &= (VFS_KERNEL_FLAG - 1);
552 if(FD >= MAX_KERNEL_FILES) return NULL;
553 h = &gaKernelHandles[ FD ];
555 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
556 h = &gaUserHandles[ FD ];
559 if(h->Node == NULL) return NULL;