Skip to content

Commit 46d7498

Browse files
committed
feat: implemented more crash failsafes along with old data migration
1 parent c91550f commit 46d7498

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/wasm/src/consts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub const NAVIGATION_DATA_DEFAULT_LOCATION: &str = ".\\bundled-navigation-data";
22
pub const NAVIGATION_DATA_WORK_LOCATION: &str = "\\work/navigation-data";
3+
pub const NAVIGATION_DATA_OLD_WORK_LOCATION: &str = "\\work/NavigationData";
34
pub const NAVIGATION_TEST_LOCATION: &str = "\\work/navigraph-test";

src/wasm/src/dispatcher.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ impl<'a> Dispatcher<'a> {
212212
}
213213

214214
fn setup_packages(&self) -> Result<String, Box<dyn Error>> {
215+
self.copy_old_data()?;
216+
215217
self.copy_bundles()?;
216218

217219
// Auto enable already activated cycle
@@ -307,6 +309,41 @@ impl<'a> Dispatcher<'a> {
307309
Ok(true)
308310
}
309311

312+
fn copy_old_data(&self) -> Result<(), Box<dyn Error>> {
313+
let old_path = Path::new(consts::NAVIGATION_DATA_OLD_WORK_LOCATION);
314+
315+
if !util::path_exists(old_path) {
316+
return Ok(())
317+
}
318+
319+
let new_path = Path::new(consts::NAVIGATION_DATA_WORK_LOCATION);
320+
321+
let old_uuid = util::generate_uuid_from_path(&old_path.join("cycle.json"))?;
322+
323+
let package_list = self.list_packages(false, false);
324+
325+
let uuid_list: Vec<String> = package_list
326+
.into_iter()
327+
.map(|package| package.uuid)
328+
.collect();
329+
330+
if uuid_list.contains(&old_uuid) {
331+
return Ok(())
332+
}
333+
334+
let fix_file = old_path.join("filethatfixeseverything");
335+
336+
if !util::path_exists(&fix_file) {
337+
fs::File::create(fix_file)?;
338+
}
339+
340+
util::copy_files_to_folder(&old_path, &new_path.join(old_uuid))?;
341+
342+
util::delete_folder_recursively(old_path, None)?;
343+
344+
Ok(())
345+
}
346+
310347
fn delete_package(&self, uuid: String) -> io::Result<()> {
311348
let package_path = Path::new(consts::NAVIGATION_DATA_WORK_LOCATION).join(uuid);
312349

src/wasm/src/download/zip_handler.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ impl<R: io::Read + io::Seek> ZipFileHandler<R> {
9999
return Err(format!("Package {} already exists", cycle_uuid).into());
100100
}
101101

102+
let fix_file = temp_dir.join("filethatfixeseverything");
103+
104+
if !util::path_exists(&fix_file) {
105+
fs::File::create(fix_file)?;
106+
}
107+
102108
fs::rename(
103109
temp_dir,
104110
Path::new(consts::NAVIGATION_DATA_WORK_LOCATION).join(&cycle_uuid),

src/wasm/src/util.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ pub fn delete_folder_recursively(path: &Path, batch_size: Option<usize>) -> io::
3535
let path = entry.path();
3636
let path_type = get_path_type(&path);
3737

38+
if path.file_name().unwrap() == "" {
39+
eprintln!("[NAVIGRAPH]: Bugged entry");
40+
continue;
41+
}
42+
3843
if path_type == PathType::Directory {
3944
delete_folder_recursively(&path, batch_size)?;
4045
} else if path_type == PathType::File {
@@ -83,6 +88,11 @@ pub fn copy_files_to_folder(from: &Path, to: &Path) -> io::Result<()> {
8388
let path = entry.path();
8489
let path_type = get_path_type(&path);
8590

91+
if path.file_name().unwrap() == "" {
92+
eprintln!("[NAVIGRAPH]: Bugged entry");
93+
continue;
94+
}
95+
8696
if path_type == PathType::Directory {
8797
let new_dir = to.join(path.file_name().unwrap());
8898
fs::create_dir(&new_dir)?;

0 commit comments

Comments
 (0)