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
199
200
201
202
203
204
205
206
207
use anyhow::{Error, Result};
use std::cell::RefCell;
use std::io::{Read, Seek};
use std::path::{Component, PathBuf};
use std::rc::Rc;
use crate::toc::directory_node::DirectoryNodeMut;
use crate::toc::node::Node;
use crate::toc::raw_toc_entry::{RawTocEntry, TOC_ENTRY_SIZE};
use crate::toc::{DirectoryNode, FileNode};
pub struct DirectoryTree {
toc_path: std::path::PathBuf,
directories: Vec<Rc<RefCell<DirectoryNode>>>,
files: Vec<Rc<RefCell<FileNode>>>,
root: Option<Rc<RefCell<DirectoryNode>>>,
}
impl DirectoryTree {
pub fn new(toc_path: std::path::PathBuf) -> Self {
Self {
toc_path,
directories: Vec::new(),
files: Vec::new(),
root: None,
}
}
pub fn directories(&self) -> Vec<Rc<RefCell<DirectoryNode>>> {
self.directories.clone()
}
pub fn files(&self) -> Vec<Rc<RefCell<FileNode>>> {
self.files.clone()
}
pub fn root(&self) -> Option<Rc<RefCell<DirectoryNode>>> {
self.root.clone()
}
pub fn is_loaded(&self) -> bool {
self.root.is_some()
}
pub fn read_toc(&mut self) -> Result<()> {
if self.is_loaded() {
return Ok(()); }
self.unread_toc();
let mut toc_reader = std::fs::File::open(&self.toc_path).unwrap();
let entry_count = (toc_reader.metadata().unwrap().len() as usize - 8) / TOC_ENTRY_SIZE;
toc_reader.seek(std::io::SeekFrom::Start(8)).unwrap();
self.files.reserve(entry_count);
self.directories.reserve(entry_count);
let mut file_count = 0;
let mut dir_count = 1;
let root = Rc::new(RefCell::new(DirectoryNode::root()));
self.directories.insert(0, root.clone());
for _ in 0..entry_count {
let mut buffer = [0 as u8; TOC_ENTRY_SIZE];
toc_reader.read_exact(&mut buffer).unwrap();
let entry = RawTocEntry::from(&buffer);
let entry_name_buffer = entry
.name
.iter()
.filter(|x| !x.is_ascii_control())
.cloned()
.collect::<Vec<u8>>();
let entry_name = String::from_utf8(entry_name_buffer).unwrap();
let parent_node = match self.directories.get(entry.parent_dir_index as usize) {
Some(parent_node) => parent_node.clone(),
_ => return Err(Error::msg("Failed to find parent directory")),
};
if entry.cache_offset == -1 {
let dir_node = Rc::new(RefCell::new(DirectoryNode::new(
entry_name,
parent_node.clone(),
)));
self.directories.insert(dir_count, dir_node.clone());
parent_node
.borrow_mut()
.add_child_directory(dir_node.clone());
dir_count += 1;
} else {
let file_node = Rc::new(RefCell::new(FileNode::new(
entry_name,
parent_node.clone(),
entry.cache_offset,
entry.timestamp,
entry.comp_len,
entry.len,
)));
self.files.insert(file_count, file_node.clone());
parent_node.borrow_mut().add_child_file(file_node.clone());
file_count += 1;
}
}
self.directories.shrink_to_fit();
self.files.shrink_to_fit();
self.root = Some(root.clone());
Ok(()) }
pub fn unread_toc(&mut self) {
self.directories.clear();
self.files.clear();
self.root = None;
}
pub fn get_directory_node(&self, path: PathBuf) -> Option<Rc<RefCell<DirectoryNode>>> {
if !self.is_loaded() {
return None;
}
if !path.has_root() {
panic!("Path must be absolute");
}
let mut components = path.components();
let mut current_node = self.root.clone().unwrap();
components.next();
for component in components {
match component {
Component::Normal(name) => {
let name = name.to_str().unwrap();
let child = current_node.borrow().get_child_directory(name);
if child.is_none() {
return None;
}
current_node = child.unwrap();
}
Component::ParentDir => {
let parent_node = current_node.borrow().parent_node();
current_node = match parent_node.upgrade() {
Some(parent_node) => parent_node,
_ => return None,
}
}
Component::CurDir => continue,
_ => return None,
}
}
Some(current_node)
}
pub fn get_file_node(&self, path: PathBuf) -> Option<Rc<RefCell<FileNode>>> {
if !self.is_loaded() {
return None;
}
if !path.has_root() {
panic!("Path must be absolute");
}
let mut path = path.clone();
let binding = path.clone();
let file_name = binding.file_name();
if file_name.is_none() {
return None;
}
let file_name = file_name.unwrap().to_str().unwrap();
path.pop();
let dir_node = self.get_directory_node(path.clone());
if dir_node.is_none() {
return None;
}
let dir_node = dir_node.unwrap();
let file_node = dir_node.borrow().get_child_file(file_name);
file_node
}
}