Skip to content

Commit

Permalink
WSL: More debugging output (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
htngr committed Sep 16, 2024
1 parent 046fc1b commit 1b4be68
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
2 changes: 1 addition & 1 deletion codchi/src/config/codchi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl CodchiConfig {
log::trace!("Read codchi config: {cfg:?}");
Ok(cfg)
});
result.expect("Failed initializing Driver.")
result.expect("Failed initializing Driver")
}
}

Expand Down
27 changes: 25 additions & 2 deletions codchi/src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ impl MachineDriver for Machine {
.join_machine(&self.config.name)
.get_or_create()?
.to_path_buf(),
|| self.start(),
|| {
// give windows time to setup WSL filesystem as a network drive
thread::sleep(Duration::from_millis(200));
self.start()
},
)
}

Expand All @@ -161,11 +165,30 @@ impl MachineDriver for Machine {
);
env.insert("MACHINE_NAME".to_string(), self.config.name.clone());

// machine must run to write env file into it...
let env_path = machine::CODCHI_ENV_TMP.to_host_path(&machine_name(&self.config.name));
for _try in 0..5 {
if env_path
.parent()
.ok_or(anyhow::anyhow!("Missing parent"))
.and_then(|par| {
par.get_or_create()?;
Ok(())
})
.trace_err("Failed accessing WSL file system via network")
.is_ok()
{
break;
}
log::warn!("Failed to access WSL file system via network path '{env_path:?}'");
thread::sleep(Duration::from_millis(200));
}

let mut env_file = OpenOptions::new()
.truncate(true)
.write(true)
.create(true)
.open(machine::CODCHI_ENV_TMP.to_host_path(&machine_name(&self.config.name)))?;
.open(env_path)?;

for (key, value) in &env {
writeln!(env_file, r#"export CODCHI_{key}="{value}""#)?;
Expand Down
27 changes: 23 additions & 4 deletions codchi/src/platform/windows/wsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,34 @@ pub fn import<T, F: FnMut() -> Result<T>>(
.ok_or(anyhow!("FOLDERID_ProgramData missing"))?
.join(consts::APP_NAME)
.join(rootfs_name);
assert!(
fs::metadata(&msix_path).is_ok(),
"WSL rootfs for {name} missing in MSIX. Search path was: {msix_path:?}"
);
if fs::metadata(&msix_path).is_err() {
bail!(
"WSL rootfs for {name} missing in MSIX. \
Search path was: {msix_path:?}. \
Directory contents: {:?}",
msix_path
.parent()
.ok_or(anyhow!("Missing parent"))
.and_then(|p| p.list_dir())
);
}

let tmp_path = host::DIR_RUNTIME.get_or_create()?.join(rootfs_name);
make_writeable_if_exists(&tmp_path)?;
fs::copy(&msix_path, &tmp_path)?;

if fs::metadata(&tmp_path).is_err() {
bail!(
"WSL rootfs for {name} missing in tmp path. \
Search path was: {tmp_path:?}. \
Directory contents: {:?}",
tmp_path
.parent()
.ok_or(anyhow!("Missing parent"))
.and_then(|p| p.list_dir())
);
}

wsl_command()
.arg("--import")
.arg(name)
Expand Down
22 changes: 21 additions & 1 deletion codchi/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::bail;
use anyhow::{bail, Context};
use std::{
env,
fmt::{Debug, Display},
Expand Down Expand Up @@ -242,6 +242,26 @@ pub trait PathExt: AsRef<Path> + Sized + Debug {
fs::metadata(self)?;
Ok(())
}

/// Create the directory recursively if it doesn't exist and return its path
fn list_dir(&self) -> anyhow::Result<Vec<String>> {
if let Ok(meta) = fs::metadata(&self) {
if meta.is_dir() {
let mut filenames = vec![];

for entry in fs::read_dir(&self)? {
let entry =
entry.with_context(|| format!("Failed to read an entry in {:?}", self))?;
filenames.push(entry.file_name().to_string_lossy().to_string());
}
return Ok(filenames);
} else {
bail!("Path '{self:?}' is not a directory");
}
} else {
bail!("Couldn't find path '{self:?}'");
}
}
}

impl<P: AsRef<Path> + Debug> PathExt for P {}
Expand Down

0 comments on commit 1b4be68

Please sign in to comment.