Skip to content

Commit a0a4e5a

Browse files
committed
fix: replace certain fs calls to use workaround
since you can't get file metadata in msfs, certain functions don't work. a few utility functions were added to replace those, and work properly
1 parent be694b3 commit a0a4e5a

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
Building:
1+
# Building:
22

33
- Install Docker
44
- Run `.\build.bat` to build, and `.\run_cargo_cmd.bat` followed by a Cargo command to run a specified command (e.g `.\run_cargo_cmd.bat clippy`)
5+
6+
7+
# Warning
8+
The only file system functions that properly work are `fs::remove_file`, `fs::File::create`, `fs::create_dir`, `fs::remove_dir`, `fs::remove_file`. All the other functions regarding the filesystem do not work due to the MSFS implementation of WASI

Sources/Code/WASM/navdata_updater/src/dispatcher.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ impl<'a> Dispatcher<'a> {
5050
})
5151
.expect("Failed to register NAVIGRAPH_SetDownloadOptions");
5252
}
53+
{
54+
let captured_downloader = self.downloader.clone();
55+
self.commbus
56+
.register("NAVIGRAPH_DeleteAllNavdata", move |_| {
57+
captured_downloader.delete_all_navdata()
58+
})
59+
.expect("Failed to register NAVIGRAPH_DeleteAllNavdata");
60+
}
5361
}
5462

5563
fn handle_update(&mut self) {

Sources/Code/WASM/navdata_updater/src/download/downloader.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use std::cell::RefCell;
22
use std::collections::HashMap;
33
use std::fs;
44
use std::io::Cursor;
5-
use std::path::PathBuf;
5+
use std::path::{Path, PathBuf};
66
use std::rc::Rc;
77

88
use msfs::{commbus::*, network::*};
99

10-
use crate::download::zip_handler::ZipFileHandler;
10+
use crate::{download::zip_handler::ZipFileHandler, util};
1111

1212
pub struct DownloadOptions {
1313
batch_size: usize,
@@ -158,8 +158,20 @@ impl NavdataDownloader {
158158
));
159159
return;
160160
}
161-
// Create the directory if it doesn't exist
161+
162162
let path = PathBuf::from(format!("\\work/navdata/{}", folder));
163+
// If the directory exists, delete it
164+
if util::path_exists(&path) {
165+
match util::delete_folder_recursively(&path) {
166+
Ok(_) => (),
167+
Err(e) => {
168+
let mut status = self.status.borrow_mut();
169+
*status = DownloadStatus::Failed(format!("Failed to delete directory: {}", e));
170+
return;
171+
}
172+
}
173+
}
174+
// Re create the directory
163175
if let Err(e) = fs::create_dir_all(&path) {
164176
let mut status = self.status.borrow_mut();
165177
*status = DownloadStatus::Failed(format!("Failed to create directory: {}", e));
@@ -260,4 +272,11 @@ impl NavdataDownloader {
260272
}
261273
self.update_and_get_status();
262274
}
275+
276+
pub fn delete_all_navdata(&self) {
277+
let path = Path::new("\\work/navdata");
278+
if util::path_exists(path) {
279+
let _ = util::delete_folder_recursively(path);
280+
}
281+
}
263282
}

Sources/Code/WASM/navdata_updater/src/download/zip_handler.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::fs;
22
use std::io;
33
use std::path::PathBuf;
44

5+
use crate::util;
6+
57
pub struct ZipFileHandler<R: io::Read + io::Seek> {
68
pub zip_archive: Option<zip::ZipArchive<R>>,
79
path_buf: PathBuf,
@@ -46,14 +48,10 @@ impl<R: io::Read + io::Seek> ZipFileHandler<R> {
4648
fs::create_dir_all(outpath).unwrap();
4749
} else {
4850
if let Some(p) = outpath.parent() {
49-
if !p.exists() {
51+
if !util::path_exists(p) {
5052
fs::create_dir_all(p).unwrap();
5153
}
5254
}
53-
// If file already exists, delete it so we can overwrite it
54-
if outpath.exists() {
55-
fs::remove_file(&outpath).unwrap();
56-
}
5755
let mut outfile = fs::File::create(outpath).unwrap();
5856
io::copy(&mut file, &mut outfile).unwrap();
5957
}

Sources/Code/WASM/navdata_updater/src/util.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ pub fn get_path_type(path: &Path) -> PathType {
2121
}
2222
PathType::DoesNotExist
2323
}
24+
25+
pub fn path_exists(path: &Path) -> bool {
26+
get_path_type(path) != PathType::DoesNotExist
27+
}
28+
29+
pub fn delete_folder_recursively(path: &Path) -> io::Result<()> {
30+
// Make sure we are deleting a directory (and in turn that it exists)
31+
if get_path_type(path) != PathType::Directory {
32+
return Ok(());
33+
}
34+
// We need to collect the entries into a vector since we can't iterate over them while deleting them
35+
for entry in fs::read_dir(path)?.collect::<Vec<_>>() {
36+
let entry = entry?;
37+
let path = entry.path();
38+
if get_path_type(&path) == PathType::Directory {
39+
delete_folder_recursively(&path)?;
40+
} else {
41+
fs::remove_file(&path)?;
42+
}
43+
}
44+
fs::remove_dir(path)?;
45+
Ok(())
46+
}

0 commit comments

Comments
 (0)