Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfiletree.cpp
More file actions
Latest commit
165 lines (143 loc) · 3.93 KB
/
Copy pathfiletree.cpp
File metadata and controls
165 lines (143 loc) · 3.93 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include"filetree.hpp"
#include<cassert>
#include<cerrno>
#include<cstdio>
#include<cstring>
#include<libgen.h>
#include<stdexcept>
#include<sys/stat.h>
#include<dirent.h>
File::File(constchar* path)
{
this->name = std::string(path);
FILE* f = fopen(path, "rb");
if (f == nullptr)
throwstd::runtime_error("diskbuilder: Could not open file " + std::string(path));
fseek(f, 0, SEEK_END);
this->size = ftell(f);
rewind(f);
this->data = std::unique_ptr<char[]> (newchar[size], std::default_delete<char[]> ());
if (this->size > 0)
{
size_t actual = fread(this->data.get(), this->size, 1, f);
if (actual != 1) {
throwstd::runtime_error("diskbuilder: Could not read from file " + std::string(path));
}
}
fclose(f);
}
Dir::Dir(constchar* path)
{
this->name = std::string(path);
/// ... ///
}
voidDir::print(int level) const
{
for (const Dir& d : subs)
{
printf ("Dir%*s ", level * 2, "");
printf("[%u entries] %s\n",
d.sectors_used(),
d.name.c_str());
d.print(level + 1);
}
for (const File& f : files)
{
printf("File%*s ", level * 2, "");
printf("[%08u b -> %06u sect] %s\n",
f.size,
f.sectors_used(),
f.name.c_str());
}
}
uint32_tDir::sectors_used() const
{
uint32_t cnt = this->size_helper;
for (constauto& dir : subs)
cnt += dir.sectors_used();
for (constauto& file : files)
cnt += file.sectors_used();
return cnt;
}
voidFileSys::print() const
{
root.print(0);
}
voidFileSys::add_dir(Dir& dvec)
{
// push work dir
char pwd_buffer[256];
getcwd(pwd_buffer, sizeof(pwd_buffer));
// go into directory
char cwd_buffer[256];
getcwd(cwd_buffer, sizeof(cwd_buffer));
strcat(cwd_buffer, "/");
strcat(cwd_buffer, dvec.name.c_str());
int res = chdir(cwd_buffer);
// throw immediately when unable to read directory
if (res < 0) {
fprintf(stderr, "Unable to enter directory %s: %s\n", cwd_buffer, strerror(errno));
throwstd::runtime_error("Unable to enter directory " + std::string(cwd_buffer));
}
auto* dir = opendir(cwd_buffer);
// throw immediately when unable to open directory
if (dir == nullptr) {
fprintf(stderr, "Unable to open directory %s: %s\n", cwd_buffer, strerror(errno));
throwstd::runtime_error("Unable to open directory " + std::string(cwd_buffer));
}
std::vector<std::string> sub_dirs;
std::vector<std::string> sub_files;
structdirent* ent;
while ((ent = readdir(dir)) != nullptr)
{
std::string name(ent->d_name);
if (name == ".." || name == ".") continue;
structstat buf;
int res = lstat(ent->d_name, &buf);
if (res < 0) {
fprintf(stderr, "Stat failed on %s with error %s\n",
ent->d_name, strerror(errno));
continue;
}
if (S_ISDIR(buf.st_mode)) {
sub_dirs.push_back(std::move(name));
}
elseif (S_ISREG(buf.st_mode)) {
sub_files.push_back(std::move(name));
}
else {
fprintf(stderr, "Encountered unknown entry %s\n", ent->d_name);
}
}
// close directory before adding more folders and files
res = closedir(dir);
if (res < 0) {
fprintf(stderr, "Unable to close directory: %s\n", strerror(errno));
throwstd::runtime_error("diskbuilder: Failed to close directory");
}
// add sub directories
for (constauto& dirname : sub_dirs) {
auto& d = dvec.add_dir(dirname.c_str());
add_dir(d);
}
// add files in current directory
for (constauto& filename : sub_files)
{
try {
dvec.add_file(filename.c_str());
} catch (std::exception& e) {
fprintf(stderr, "%s\n", e.what());
}
}
// pop work dir
res = chdir(pwd_buffer);
if (res < 0) {
fprintf(stderr, "Unable to return to parent directory %s: %s\n", pwd_buffer, strerror(errno));
throwstd::runtime_error("diskbuilder: Failed to return back to parent directory");
}
}
voidFileSys::gather(constchar* path)
{
root = Dir(path);
add_dir(root);
}