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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use anyhow::{bail, Result};
use bp7::Bundle;
use log::{debug, error, info};
use sanitize_filename_reader_friendly::sanitize;
use std::path::{Path, PathBuf};
use std::{convert::TryInto, fs};
use walkdir::{DirEntry, WalkDir};
use crate::db::BundleEntry;
#[derive(Debug, Clone)]
pub struct D7sFs {
base: String,
}
impl D7sFs {
pub fn open(base: &str) -> Result<Self> {
let me = Self { base: base.into() };
me.setup()?;
Ok(me)
}
fn setup(&self) -> Result<()> {
let basepath = Path::new(&self.base);
fs::create_dir_all(basepath)?;
fs::create_dir_all(self.path_single())?;
fs::create_dir_all(self.path_administrative())?;
fs::create_dir_all(self.path_group())?;
let version_file = basepath.join("version.txt");
if version_file.exists() {
let version: u32 = fs::read_to_string(version_file)?.parse()?;
if version < crate::D7S_VERSION {
info!("old filesystem structure detected, upgrade needed");
unimplemented!();
} else if version > crate::D7S_VERSION {
error!("filesystem structure is newer, upgrade program to newest version");
bail!("outdated program version");
}
}
let version_file = basepath.join("version.txt");
fs::write(version_file, format!("{}", crate::D7S_VERSION))?;
Ok(())
}
pub fn path_single(&self) -> PathBuf {
let basepath = Path::new(&self.base);
basepath.join("single")
}
pub fn path_administrative(&self) -> PathBuf {
let basepath = Path::new(&self.base);
basepath.join("adm")
}
pub fn path_group(&self) -> PathBuf {
let basepath = Path::new(&self.base);
basepath.join("group")
}
pub fn path_for_bundle(&self, bndl: &Bundle) -> PathBuf {
let dst = sanitize(
&bndl
.primary
.destination
.node()
.unwrap_or_else(|| "none".to_owned()),
);
if bndl.is_administrative_record() {
self.path_administrative().join(&dst)
} else {
match &bndl.primary.destination {
bp7::EndpointID::Dtn(_, addr) => {
if addr.is_non_singleton() {
self.path_group().join(&dst)
} else {
self.path_single().join(&dst)
}
}
bp7::EndpointID::DtnNone(_, _) => {
unimplemented!()
}
bp7::EndpointID::Ipn(_, _addr) => {
unimplemented!()
}
}
}
}
pub fn path_for_bundle_with_filename(&self, bndl: &Bundle) -> PathBuf {
let filename = format!("{}.bundle", sanitize(&bndl.id()));
self.path_for_bundle(bndl).join(&filename)
}
pub fn exists(&self, bndl: &Bundle) -> bool {
self.path_for_bundle_with_filename(bndl).exists()
}
pub fn save_bundle(&self, bndl: &mut Bundle) -> Result<(u64, String)> {
let bid = bndl.id();
let filename = format!("{}.bundle", sanitize(&bid));
let dest_path = self.path_for_bundle(bndl);
fs::create_dir_all(&dest_path)?;
let dest_path = dest_path.join(&filename);
if dest_path.exists() {
debug!("File {} already exists, skipping", filename);
} else {
fs::write(&dest_path, bndl.to_cbor())?;
debug!("saved {} to {}", bid, dest_path.to_string_lossy());
}
Ok((
fs::metadata(&dest_path)?.len(),
dest_path.to_string_lossy().into(),
))
}
pub fn remove_bundle(&self, bid: &str) -> Result<()> {
if let Some(filename) = self.find_file_by_bid(bid) {
fs::remove_file(filename)?;
} else {
bail!("bundle ID not found");
}
Ok(())
}
pub fn find_file_by_bid(&self, bid: &str) -> Option<PathBuf> {
let target = format!("{}.bundle", sanitize(bid));
for entry in WalkDir::new(&self.base)
.into_iter()
.filter_map(|e| e.ok())
.filter(|f| f.file_name().to_str().unwrap_or_default() == target)
{
return Some(entry.into_path());
}
None
}
pub fn all_bids(&self) -> Vec<String> {
let mut bids = Vec::new();
for entry in WalkDir::new(&self.base)
.into_iter()
.filter_map(|e| e.ok())
.filter(|f| {
f.file_name()
.to_str()
.unwrap_or_default()
.ends_with(".bundle")
})
{
let filename = entry
.file_name()
.to_str()
.unwrap()
.rsplit('.')
.collect::<Vec<&str>>()[1]
.to_string();
if filename.starts_with("dtn_") {
let bid = if filename.starts_with("dtn_none") {
filename.replacen('_', ":", 1)
} else {
filename.replacen('_', "://", 1)
};
let bid = bid.replacen('_', "/", 1);
bids.push(bid);
} else {
unimplemented!("only dtn bundle scheme support at the moment!");
}
}
bids
}
pub fn get_bundle(&self, bid: &str) -> Result<Bundle> {
if let Some(filename) = self.find_file_by_bid(bid) {
let buffer = fs::read(filename)?;
let bndl: Bundle = buffer.try_into()?;
Ok(bndl)
} else {
bail!("bundle ID not found");
}
}
fn check_file_from_store(
&self,
entry: DirEntry,
db: &crate::D7DB,
) -> Result<Option<(String, BundleEntry)>> {
let (filebase, _extension) = entry
.file_name()
.to_str()
.unwrap()
.rsplit_once('.')
.unwrap();
let res = if filebase.starts_with("dtn") {
let bid = filebase.replace('_', "/").replacen("dtn", "dtn:/", 1);
let is_in_db = db.exists(&bid);
debug!("{} in db: {}", entry.path().display(), is_in_db);
if !is_in_db {
let buf = std::fs::read(entry.path())?;
let bundle_size = buf.len();
let bndl: Bundle = buf.try_into()?;
let mut be = BundleEntry::from(&bndl);
be.size = bundle_size as u64;
info!("adding {} to db", bndl.id());
Some((bndl.id(), be))
} else {
debug!("{} already in store", &bid);
None
}
} else {
None
};
Ok(res)
}
pub fn sync_to_db(&self, db: &crate::D7DB) -> Result<()> {
info!("syncing fs to db");
let mut bes = Vec::new();
for entry in WalkDir::new(&self.base)
.into_iter()
.filter_map(|e| e.ok())
.filter(|f| {
f.file_name()
.to_str()
.unwrap_or_default()
.ends_with(".bundle")
})
{
let file_path = entry.path().to_string_lossy().to_string();
if let Ok(Some((bid, be))) = self.check_file_from_store(entry, db) {
bes.push((bid, be, Some(file_path)));
}
}
db.insert_bulk(&bes)?;
Ok(())
}
pub fn import_hex(&self, hexstr: &str) -> Result<(Bundle, u64, String)> {
let mut bndl: Bundle = bp7::helpers::unhexify(hexstr)?.try_into()?;
let (bundle_size, path) = self.save_bundle(&mut bndl)?;
Ok((bndl, bundle_size, path))
}
pub fn import_vec(&self, buf: Vec<u8>) -> Result<(Bundle, u64, String)> {
let mut bndl: Bundle = buf.try_into()?;
let (bundle_size, path) = self.save_bundle(&mut bndl)?;
Ok((bndl, bundle_size, path))
}
}