-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusz-7z-node.cpp
198 lines (178 loc) · 3.74 KB
/
fusz-7z-node.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const int Node::ROOT_NODE_INDEX = -1;
const int Node::NEW_NODE_INDEX = -2;
Node::Node(Node * parent, char const * name) : parent(parent), sname(name), is_dir(false), open_count(0), state(CLOSED) {
this->name = sname.c_str();
}
Node::~Node() {
delete buffer;
for (nodelist_t::iterator i = childs.begin(); i != childs.end(); i++) {
delete *i;
}
}
Node * Node::insert(char * path) {
char * path2 = path;
bool dir = false;
do {
if (*path2 == '/') {
dir = true;
*path2 = '\0';
}
} while(*path2++);
if (dir) {
for (nodelist_t::iterator i = childs.begin(); i != childs.end(); i++) {
Node * child = *i;
if (child->sname == path) {
return child->insert(path2);
}
}
// not found
Node * child = new Node(this, path);
childs.push_back(child);
return child->insert(path2);
}
else {
for (nodelist_t::iterator i = childs.begin(); i != childs.end(); i++) {
Node * child = *i;
if (child->sname == path) {
stringstream ss;
ss << "File " << path << " already exists !";
throw runtime_error(ss.str());
}
}
Node * child = new Node(this, path);
return child;
}
}
std::string Node::fullname() const {
if (parent != NULL) {
return sname;
}
return parent->fullname() + "/" + sname;
}
Node * Node::find(char const * path) const {
string s(path);
char const * path2 = path;
bool sub = false;
do {
if (*path2 == '/') {
sub = true;
}
} while(*path2++);
if (sub) {
for (nodelist_t::const_iterator i = childs.begin(); i != childs.end(); i++) {
Node * child = *i;
if (s.find(child->sname) == 0) {
return child->find(path2);
}
}
stringstream ss;
ss << "Expected " << path << " to be a child of " << fullname() << " but it is not";
throw runtime_error(ss.str());
}
else {
for (nodelist_t::const_iterator i = childs.begin(); i != childs.end(); i++) {
Node * child = *i;
if (child->sname == path) {
return child;
}
}
stringstream ss;
ss << "Child " << path << " not found in " << fullname() << " but it is not";
throw runtime_error(ss.str());
}
}
#if 0
int Node::open() {
#if defined(FUSE7Z_WRITE)
if (state == NEW) {
return 0;
}
if (state == OPENED) {
if (open_count == INT_MAX) {
return -EMFILE;
} else {
++open_count;
}
}
if (state == CLOSED) {
open_count = 1;
#endif
try {
buffer = new BigBuffer(data, id, stat.st_size);
#if defined(FUSE7Z_WRITE)
state = OPENED;
#endif
}
catch (std::bad_alloc&) {
return -ENOMEM;
}
catch (std::runtime_error&) {
return -EIO;
}
#if defined(FUSE7Z_WRITE)
}
#endif
return 0;
}
void Node::rename(char *fname) {
throw runtime_error("not implemented");
parse_name(fname);
parent->childs.remove(this);
}
void Node::rename_wo_reparenting(char *new_name) {
//parse_name(new_name);
}
int Node::read(char *buf, size_t size, offset_t offset) const {
return buffer->read(buf, size, offset);
}
#if defined(FUSE7Z_WRITE)
int Node::write(const char *buf, size_t size, offset_t offset) {
if (state == OPENED) {
state = CHANGED;
}
return buffer->write(buf, size, offset);
}
#endif
int Node::close() {
stat.st_size = buffer->len;
if (state == OPENED && --open_count == 0) {
delete buffer;
buffer = NULL;
state = CLOSED;
}
if (state == CHANGED) {
stat.st_mtime = time(NULL);
}
return 0;
}
#if 0
int Node::save() {
return buffer->saveToZip(stat.st_mtime, fullname, state == NEW, id);
}
#endif
#if 0
int Node::truncate(offset_t offset) {
if (state != CLOSED) {
if (state != NEW) {
state = CHANGED;
}
try {
buffer->truncate(offset);
return 0;
}
catch (const std::bad_alloc &) {
return EIO;
}
} else {
return EBADF;
}
}
#endif
offset_t Node::size() const {
if (state == NEW || state == OPENED || state == CHANGED) {
return buffer->len;
} else {
return stat.st_size;
}
}
#endif