Skip to content

Commit

Permalink
feat: implemented more crash failsafes along with old data migration
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigTag committed Nov 18, 2024
1 parent c91550f commit 46d7498
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/wasm/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const NAVIGATION_DATA_DEFAULT_LOCATION: &str = ".\\bundled-navigation-data";
pub const NAVIGATION_DATA_WORK_LOCATION: &str = "\\work/navigation-data";
pub const NAVIGATION_DATA_OLD_WORK_LOCATION: &str = "\\work/NavigationData";
pub const NAVIGATION_TEST_LOCATION: &str = "\\work/navigraph-test";
37 changes: 37 additions & 0 deletions src/wasm/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ impl<'a> Dispatcher<'a> {
}

fn setup_packages(&self) -> Result<String, Box<dyn Error>> {
self.copy_old_data()?;

self.copy_bundles()?;

// Auto enable already activated cycle
Expand Down Expand Up @@ -307,6 +309,41 @@ impl<'a> Dispatcher<'a> {
Ok(true)
}

fn copy_old_data(&self) -> Result<(), Box<dyn Error>> {
let old_path = Path::new(consts::NAVIGATION_DATA_OLD_WORK_LOCATION);

if !util::path_exists(old_path) {
return Ok(())
}

let new_path = Path::new(consts::NAVIGATION_DATA_WORK_LOCATION);

let old_uuid = util::generate_uuid_from_path(&old_path.join("cycle.json"))?;

let package_list = self.list_packages(false, false);

let uuid_list: Vec<String> = package_list
.into_iter()
.map(|package| package.uuid)
.collect();

if uuid_list.contains(&old_uuid) {
return Ok(())
}

let fix_file = old_path.join("filethatfixeseverything");

if !util::path_exists(&fix_file) {
fs::File::create(fix_file)?;
}

util::copy_files_to_folder(&old_path, &new_path.join(old_uuid))?;

util::delete_folder_recursively(old_path, None)?;

Ok(())
}

fn delete_package(&self, uuid: String) -> io::Result<()> {
let package_path = Path::new(consts::NAVIGATION_DATA_WORK_LOCATION).join(uuid);

Expand Down
6 changes: 6 additions & 0 deletions src/wasm/src/download/zip_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ impl<R: io::Read + io::Seek> ZipFileHandler<R> {
return Err(format!("Package {} already exists", cycle_uuid).into());
}

let fix_file = temp_dir.join("filethatfixeseverything");

if !util::path_exists(&fix_file) {
fs::File::create(fix_file)?;
}

fs::rename(
temp_dir,
Path::new(consts::NAVIGATION_DATA_WORK_LOCATION).join(&cycle_uuid),
Expand Down
10 changes: 10 additions & 0 deletions src/wasm/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ pub fn delete_folder_recursively(path: &Path, batch_size: Option<usize>) -> io::
let path = entry.path();
let path_type = get_path_type(&path);

if path.file_name().unwrap() == "" {
eprintln!("[NAVIGRAPH]: Bugged entry");
continue;
}

if path_type == PathType::Directory {
delete_folder_recursively(&path, batch_size)?;
} else if path_type == PathType::File {
Expand Down Expand Up @@ -83,6 +88,11 @@ pub fn copy_files_to_folder(from: &Path, to: &Path) -> io::Result<()> {
let path = entry.path();
let path_type = get_path_type(&path);

if path.file_name().unwrap() == "" {
eprintln!("[NAVIGRAPH]: Bugged entry");
continue;
}

if path_type == PathType::Directory {
let new_dir = to.join(path.file_name().unwrap());
fs::create_dir(&new_dir)?;
Expand Down

0 comments on commit 46d7498

Please sign in to comment.