mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-28 15:34:33 +00:00
More stable GetPathFromNode
This commit is contained in:
parent
1297aecc8a
commit
a0742cf987
@ -17,35 +17,74 @@ namespace FileSystem
|
|||||||
{
|
{
|
||||||
vfsdbg("GetPathFromNode( Node: \"%s\" )", Node->Name);
|
vfsdbg("GetPathFromNode( Node: \"%s\" )", Node->Name);
|
||||||
FileSystemNode *Parent = Node;
|
FileSystemNode *Parent = Node;
|
||||||
Vector<char *> Path;
|
char **Path = nullptr;
|
||||||
size_t Size = 1;
|
size_t Size = 1;
|
||||||
|
size_t PathSize = 0;
|
||||||
|
|
||||||
|
// Traverse the filesystem tree and build the path by adding each parent's Name field to the Path array
|
||||||
while (Parent != FileSystemRoot && Parent != nullptr)
|
while (Parent != FileSystemRoot && Parent != nullptr)
|
||||||
{
|
{
|
||||||
foreach (auto var in FileSystemRoot->Children)
|
bool Found = false;
|
||||||
if (var == Parent)
|
for (const auto &Children : FileSystemRoot->Children)
|
||||||
goto PathFromNodeContinue;
|
if (Children == Parent)
|
||||||
Path.push_back(Parent->Name);
|
{
|
||||||
Path.push_back((char *)"/");
|
Found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Found)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (strlen(Parent->Name) == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
char **new_path = new char *[PathSize + 1];
|
||||||
|
if (Path != nullptr)
|
||||||
|
{
|
||||||
|
memcpy(new_path, Path, sizeof(char *) * PathSize);
|
||||||
|
delete[] Path;
|
||||||
|
}
|
||||||
|
|
||||||
|
Path = new_path;
|
||||||
|
Path[PathSize] = Parent->Name;
|
||||||
|
PathSize++;
|
||||||
|
new_path = new char *[PathSize + 1];
|
||||||
|
memcpy(new_path, Path, sizeof(char *) * PathSize);
|
||||||
|
delete[] Path;
|
||||||
|
Path = new_path;
|
||||||
|
Path[PathSize] = (char *)"/";
|
||||||
|
PathSize++;
|
||||||
Parent = Parent->Parent;
|
Parent = Parent->Parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
PathFromNodeContinue:
|
// Calculate the total size of the final path string
|
||||||
Path.reverse();
|
for (size_t i = 0; i < PathSize; i++)
|
||||||
|
{
|
||||||
foreach (auto var in Path)
|
Size += strlen(Path[i]);
|
||||||
Size += strlen(var);
|
}
|
||||||
|
|
||||||
|
// Allocate a new string for the final path
|
||||||
char *FinalPath = new char[Size];
|
char *FinalPath = new char[Size];
|
||||||
|
size_t Offset = 0;
|
||||||
|
|
||||||
foreach (auto var in Path)
|
// Concatenate the elements of the Path array into the FinalPath string
|
||||||
strcat(FinalPath, var);
|
for (size_t i = PathSize - 1; i < PathSize; i--)
|
||||||
|
{
|
||||||
|
if (Path[i] == nullptr)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
size_t ElementSize = strlen(Path[i]);
|
||||||
|
memcpy(FinalPath + Offset, Path[i], ElementSize);
|
||||||
|
Offset += ElementSize;
|
||||||
|
}
|
||||||
|
|
||||||
// for (size_t i = 0; i < Path.size(); i++)
|
// Add a null terminator to the final path string
|
||||||
// strcat(FinalPath, Path[i]);
|
FinalPath[Size - 1] = '\0';
|
||||||
|
|
||||||
|
// Deallocate the Path array
|
||||||
|
delete[] Path;
|
||||||
|
|
||||||
// for (size_t i = 0; i < Path.size(); i++)
|
|
||||||
// Size += strlen(Path[i]);
|
|
||||||
vfsdbg("GetPathFromNode()->\"%s\"", FinalPath);
|
vfsdbg("GetPathFromNode()->\"%s\"", FinalPath);
|
||||||
return FinalPath;
|
return FinalPath;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user