This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.cpp
101 lines (86 loc) · 2.44 KB
/
tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include <filesystem.hpp>
#include "../../kernel.h"
void tree_loop(FileNode *rootNode, int depth = 0)
{
// foreach (auto Child in rootNode->GetChildren(true))
// {
// Display->UpdateBuffer();
// if (Child->Stat.IsType(DIRECTORY) || Child->Stat.IsType(MOUNTPOINT))
// {
// printf("%*s%*s%*s|- %s\n",
// depth, "",
// depth, "",
// depth, "",
// Child->FileName);
// tree_loop(Child, depth + 1);
// }
// else
// printf("%*s%*s%*s|- %s\n",
// depth, "",
// depth, "",
// depth, "",
// Child->FileName);
// }
kdirent *dirBuffer = new kdirent[16];
ssize_t read = 0;
off_t offset = 0;
while ((read = rootNode->ReadDir(dirBuffer, sizeof(kdirent) * 16, offset, LONG_MAX)) > 0)
{
if (read / sizeof(kdirent) == 0)
break;
off_t bufOffset = 0;
for (size_t i = 0; i < read / sizeof(kdirent); i++)
{
kdirent *dirent = (kdirent *)((uintptr_t)dirBuffer + bufOffset);
if (dirent->d_reclen == 0)
break;
bufOffset += dirent->d_reclen;
if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
continue;
FileNode *node = fs->GetByPath(dirent->d_name, rootNode);
if (node == nullptr)
continue;
for (int i = 0; i < depth; i++)
printf(" ");
printf("|- %s\n", dirent->d_name);
if (node->IsDirectory())
tree_loop(node, depth + 1);
}
offset += read;
}
delete[] dirBuffer;
}
void cmd_tree(const char *args)
{
FileNode *rootNode = thisProcess->CWD;
if (args[0] == '\0')
{
if (rootNode == nullptr)
rootNode = fs->GetRoot(0);
}
else
{
rootNode = fs->GetByPath(args, nullptr);
if (rootNode == nullptr)
{
printf("ls: %s: No such file or directory\n", args);
return;
}
}
printf("%s\n", rootNode->Name.c_str());
tree_loop(rootNode);
}