Skip to content

Commit

Permalink
Remove dependency on wsl crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Seeker14491 committed Jun 12, 2021
1 parent 165b933 commit 9a36c6e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 0 additions & 1 deletion opener/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ version-sync = "0.9"

[target.'cfg(target_os = "linux")'.dependencies]
bstr = "0.2"
wsl = "0.1"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["shellapi"] }
2 changes: 1 addition & 1 deletion opener/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Error for OpenError {

#[cfg(target_os = "linux")]
fn is_wsl() -> bool {
wsl::is_wsl()
sys::is_wsl()
}

#[cfg(not(target_os = "linux"))]
Expand Down
32 changes: 31 additions & 1 deletion opener/src/linux_and_more.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::OpenError;
use std::ffi::OsStr;
use std::io;
use std::io::Write;
use std::process::{Child, Command, Stdio};
use std::{fs, io};

const XDG_OPEN_SCRIPT: &[u8] = include_bytes!("xdg-open");

Expand Down Expand Up @@ -76,3 +76,33 @@ fn open_with_internal_xdg_open(path: &OsStr) -> Result<Child, OpenError> {

Ok(sh)
}

pub(crate) fn is_wsl() -> bool {
if is_docker() {
return false;
}

if let Ok(true) = fs::read_to_string("/proc/sys/kernel/osrelease")
.map(|osrelease| osrelease.to_ascii_lowercase().contains("microsoft"))
{
return true;
}

if let Ok(true) = fs::read_to_string("/proc/version")
.map(|version| version.to_ascii_lowercase().contains("microsoft"))
{
return true;
}

false
}

fn is_docker() -> bool {
let has_docker_env = fs::metadata("/.dockerenv").is_ok();

let has_docker_cgroup = fs::read_to_string("/proc/self/cgroup")
.map(|cgroup| cgroup.to_ascii_lowercase().contains("docker"))
.unwrap_or(false);

has_docker_env || has_docker_cgroup
}

0 comments on commit 9a36c6e

Please sign in to comment.