-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.cpp
163 lines (128 loc) · 3.19 KB
/
FileSystem.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
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
#include "FileSystem.hpp"
#include "FileSetDesc.hpp"
#include "FileEntry.hpp"
#include "FileIdentifier.hpp"
#include <unistd.h>
#include <fcntl.h>
FileSystem::FileSystem(const extend_ad &from, int block_size, int fd)
: location(from), block_size(block_size), fd(fd), root(NULL)
{
#ifdef NDEBUG
debug = false;
#else
debug = true;
#endif
}
bool FileSystem::loadRoot() {
if (root)
return true;
root = loadDescriptor<FileSetDesc>(Tag::FileSetDesc);
if (!root)
return false;
if (debug)
std::cout << root->toString() << std::endl;
return true;
}
bool FileSystem::loadFirstDir()
{
debug = true;
if (!loadRoot())
return false;
const long_ad &addr = root->getRootDir();
root_node = FileEntry::fullLoad(*this, addr.location.block_nbr, fd);
current_node = root_node;
return true;
}
template <typename T>
T *FileSystem::loadDescriptor(Tag::Type type) {
T *descriptor = new T();
Tag tag = getTagSector(type);
if (tag.type == 0) {
std::cerr << "Error: " << descriptor->getName()
<< " not found" << std::endl;
delete descriptor;
return NULL;
}
if (!descriptor->loadFromFd(tag, fd)) {
std::cerr << "Error: Unable to load "
<< descriptor->getName() << std::endl;
delete descriptor;
return NULL;
}
return descriptor;
}
Tag FileSystem::getTagSector(int type) {
extend_ad ext = location;
unsigned char buffer[16];
for (uint32_t i = ext.location; i < ext.location + ext.length; i++) {
Tag tag(i - ext.location);
if (lseek(fd, i * 2048, SEEK_SET) != i * 2048) {
std::cerr << "Error: Unable to seek to sector " << i << std::endl;
return Tag(0);
}
if (read(fd, buffer, 16) != 16) {
std::cerr << "Error: Unable to read sector " << i << std::endl;
return Tag(0);
}
tag.setData(buffer);
if (tag.type == type || type == 0) {
if (!tag.isValid()) {
std::cerr << "Error: Tag Type " << type << " is not valid." << std::endl;
return Tag(0);
}
return tag;
}
if (tag.type == Tag::TermDesc) {
if (true) {
std::cout << "Terminating Tag -- Tag " << type << " not found" << std::endl;
}
return Tag(0);
}
}
if (true) {
std::cout << "Terminating Tag -- Tag " << type << " not found" << std::endl;
}
return Tag(0);
}
bool FileSystem::goTo(uint32_t sector)
{
lseek(fd, location.location * 2048, SEEK_SET);
lseek(fd, sector * block_size, SEEK_CUR);
return true;
}
charspec FileSystem::getCharset() const
{
return root->getCharset();
}
FileEntry *FileSystem::getCurrentNode()
{
return current_node;
}
bool FileSystem::move(const std::string &name)
{
FileIdentifier *FID = current_node->searchFID(name);
if (!FID)
return false;
FileEntry *target = FID->loadTarget(*this, fd);
if (!target)
return false;
current_node = target;
return true;
}
void FileSystem::setCurrentNode(FileEntry *fe)
{
current_node = fe;
}
bool FileSystem::copy(const std::string &name, int fd_target)
{
FileIdentifier *FID = current_node->searchFID(name);
if (!FID) {
std::cerr << "Error: file " << name << " not found!" << std::endl;
return false;
}
return FID->loadTarget(*this, fd)->copyFileContent(*this, fd, fd_target);
}
std::vector<FileIdentifier::InfoDir*> FileSystem::getDirInfo()
{
return getCurrentNode()->getInfoDir(*this, fd);
}