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) continue;
94 if(strncmp(&ret[read], "..", pos-read) == 0)
96 // If there is no higher, silently ignore
101 // Reverse write pointer
102 write = slashOffsets[ --slashNum ];
108 // Only copy if the positions differ
110 //Log("write = %i, read = %i, pos-read+1 = %i", write, read, pos-read+1);
111 memcpy( &ret[write], &ret[read], pos-read+1 );
112 //Log("ret = '%s'", ret);
115 if(slashNum < MAX_PATH_SLASHES)
116 slashOffsets[ slashNum++ ] = write;
118 LOG("Path '%s' has too many elements", Path);
124 // Increment write pointer
125 write += (pos-read)+1;
128 ret[write] = '\0'; // Cap string (to deal with . or .. being the last terms)
129 // `ret` should now be the absolute path
131 //Log("VFS_GetAbsPath: RETURN '%s'", ret);
136 * \fn char *VFS_ParsePath(char *Path, char **TruePath)
137 * \brief Parses a path, resolving sysmlinks and applying permissions
139 tVFS_Node *VFS_ParsePath(char *Path, char **TruePath)
142 tVFS_Mount *longestMount = gRootMount; // Root is first
143 int cmp, retLength = 0;
145 tVFS_Node *curNode, *tmpNode;
148 ENTER("sPath pTruePath", Path, TruePath);
153 *TruePath = malloc(strlen(Path)+1);
154 strcpy(*TruePath, Path);
156 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
160 // For root we always fast return
162 if(Path[0] == '/' && Path[1] == '\0') {
164 *TruePath = malloc( gRootMount->MountPointLen+1 );
165 strcpy(*TruePath, gRootMount->MountPoint);
167 LEAVE('p', gRootMount->RootNode);
168 return gRootMount->RootNode;
171 // Check if there is anything mounted
172 if(!gMounts) return NULL;
180 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
182 // Length Check - If the length is smaller than the longest match sofar
183 if(mnt->MountPointLen < longestMount->MountPointLen) continue;
185 cmp = strcmp(Path, mnt->MountPoint);
188 // Fast Break - Request Mount Root
191 *TruePath = malloc( mnt->MountPointLen+1 );
192 strcpy(*TruePath, mnt->MountPoint);
194 LEAVE('p', mnt->RootNode);
195 return mnt->RootNode;
198 // Not a match, continue
199 if(cmp != '/') continue;
204 /*if(!longestMount) {
205 Log("VFS_GetTruePath - ERROR: No Root Node\n");
209 // Save to shorter variable
212 LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
217 *TruePath = malloc( mnt->MountPointLen+1 );
218 strcpy(*TruePath, mnt->MountPoint);
219 retLength = mnt->MountPointLen;
222 curNode = mnt->RootNode;
223 curNode->ReferenceCount ++;
225 ofs = mnt->MountPointLen+1;
226 for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; Path[nextSlash]='/',ofs = nextSlash + 1)
229 Path[nextSlash] = '\0';
231 // Check for empty string
232 if( Path[ofs] == '\0' ) continue;
234 // Check permissions on root of filesystem
235 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
236 curNode->Close( curNode );
245 // Check if the node has a FindDir method
246 if(!curNode->FindDir) {
247 if(curNode->Close) curNode->Close(curNode);
252 Path[nextSlash] = '/';
256 LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
258 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
259 LOG("tmpNode = %p", tmpNode);
261 curNode->Close(curNode);
266 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
271 Path[nextSlash] = '/';
276 // Handle Symbolic Links
277 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
282 tmp = malloc( curNode->Size + 1 );
283 curNode->Read( curNode, 0, curNode->Size, tmp );
284 tmp[ curNode->Size ] = '\0';
286 // Parse Symlink Path
287 curNode = VFS_ParsePath(tmp, TruePath);
291 free(tmp); // Free temp string
299 retLength = strlen(tmp);
301 free(tmp); // Free temp string
307 // Handle Non-Directories
308 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
310 Warning("VFS_ParsePath - File in directory context");
311 if(TruePath) free(*TruePath);
316 // Check if path needs extending
317 if(!TruePath) continue;
319 // Increase buffer space
320 tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 );
321 // Check if allocation succeeded
323 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
325 if(curNode->Close) curNode->Close(curNode);
331 (*TruePath)[retLength] = '/';
332 strcpy(*TruePath+retLength+1, &Path[ofs]);
334 retLength += strlen(&Path[ofs])+1;
338 LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
339 tmpNode = curNode->FindDir(curNode, &Path[ofs]);
340 LOG("tmpNode = %p", tmpNode);
341 if(curNode->Close) curNode->Close(curNode);
342 // Check if file was found
344 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
345 if(TruePath) free(*TruePath);
346 if(curNode->Close) curNode->Close(curNode);
353 // Increase buffer space
354 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
355 // Check if allocation succeeded
357 Warning("VFS_ParsePath - Unable to reallocate true path buffer");
359 if(tmpNode->Close) tmpNode->Close(curNode);
365 (*TruePath)[retLength] = '/';
366 strcpy(*TruePath + retLength + 1, &Path[ofs]);
368 //retLength += strlen(tmpNode->Name) + 1;
376 * \fn int VFS_Open(char *Path, Uint Mode)
379 int VFS_Open(char *Path, Uint Mode)
385 ENTER("sPath xMode", Path, Mode);
388 absPath = VFS_GetAbsPath(Path);
389 LOG("absPath = \"%s\"", absPath);
390 // Parse path and get mount point
391 node = VFS_ParsePath(absPath, NULL);
392 // Free generated path
396 LOG("Cannot find node");
401 // Check for symlinks
402 if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
405 Warning("No read method on symlink");
409 absPath = malloc(node->Size+1); // Allocate Buffer
410 node->Read( node, 0, node->Size, absPath ); // Read Path
412 absPath[ node->Size ] = '\0'; // End String
413 if(node->Close) node->Close( node ); // Close old node
414 node = VFS_ParsePath(absPath, NULL); // Get new node
415 free( absPath ); // Free allocated path
424 i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
425 i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
426 i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
431 if( !VFS_CheckACL(node, i) ) {
437 // Check for a user open
438 if(Mode & VFS_OPENFLAG_USER)
441 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
444 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
445 for(addr = 0; addr < size; addr += 0x1000)
446 MM_Allocate( (Uint)gaUserHandles + addr );
447 memset( gaUserHandles, 0, size );
450 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
452 if(gaUserHandles[i].Node) continue;
453 gaUserHandles[i].Node = node;
454 gaUserHandles[i].Position = 0;
455 gaUserHandles[i].Mode = Mode;
462 // Allocate space if not already
463 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
466 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
467 for(addr = 0; addr < size; addr += 0x1000)
468 MM_Allocate( (Uint)gaKernelHandles + addr );
469 memset( gaKernelHandles, 0, size );
472 for(i=0;i<MAX_KERNEL_FILES;i++)
474 if(gaKernelHandles[i].Node) continue;
475 gaKernelHandles[i].Node = node;
476 gaKernelHandles[i].Position = 0;
477 gaKernelHandles[i].Mode = Mode;
478 LEAVE('x', i|VFS_KERNEL_FLAG);
479 return i|VFS_KERNEL_FLAG;
488 * \fn void VFS_Close(int FD)
489 * \brief Closes an open file handle
491 void VFS_Close(int FD)
496 h = VFS_GetHandle(FD);
498 Warning("Invalid file handle passed to VFS_Close, 0x%x\n", FD);
503 h->Node->Close( h->Node );
509 * \fn int VFS_ChDir(char *New)
510 * \brief Change current working directory
512 int VFS_ChDir(char *New)
519 buf = VFS_GetAbsPath(New);
521 Log("VFS_ChDir: Path expansion failed");
525 // Check if path exists
526 fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
528 Log("VFS_ChDir: Path is invalid");
532 // Get node so we can check for directory
533 h = VFS_GetHandle(fd);
534 if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
535 Log("VFS_ChDir: Path is not a directory");
543 // Free old working directory
544 if( CFGPTR(CFG_VFS_CWD) )
545 free( CFGPTR(CFG_VFS_CWD) );
547 CFGPTR(CFG_VFS_CWD) = buf;
549 Log("Updated CWD to '%s'", buf);
555 * \fn tVFS_Handle *VFS_GetHandle(int FD)
556 * \brief Gets a pointer to the handle information structure
558 tVFS_Handle *VFS_GetHandle(int FD)
562 if(FD < 0) return NULL;
564 if(FD & VFS_KERNEL_FLAG) {
565 FD &= (VFS_KERNEL_FLAG - 1);
566 if(FD >= MAX_KERNEL_FILES) return NULL;
567 h = &gaKernelHandles[ FD ];
569 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
570 h = &gaUserHandles[ FD ];
573 if(h->Node == NULL) return NULL;