Skip to content

Commit 9a36c6e

Browse files
committed
Remove dependency on wsl crate
1 parent 165b933 commit 9a36c6e

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

opener/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ version-sync = "0.9"
2020

2121
[target.'cfg(target_os = "linux")'.dependencies]
2222
bstr = "0.2"
23-
wsl = "0.1"
2423

2524
[target.'cfg(windows)'.dependencies]
2625
winapi = { version = "0.3", features = ["shellapi"] }

opener/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl Error for OpenError {
169169

170170
#[cfg(target_os = "linux")]
171171
fn is_wsl() -> bool {
172-
wsl::is_wsl()
172+
sys::is_wsl()
173173
}
174174

175175
#[cfg(not(target_os = "linux"))]

opener/src/linux_and_more.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::OpenError;
22
use std::ffi::OsStr;
3-
use std::io;
43
use std::io::Write;
54
use std::process::{Child, Command, Stdio};
5+
use std::{fs, io};
66

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

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

7777
Ok(sh)
7878
}
79+
80+
pub(crate) fn is_wsl() -> bool {
81+
if is_docker() {
82+
return false;
83+
}
84+
85+
if let Ok(true) = fs::read_to_string("/proc/sys/kernel/osrelease")
86+
.map(|osrelease| osrelease.to_ascii_lowercase().contains("microsoft"))
87+
{
88+
return true;
89+
}
90+
91+
if let Ok(true) = fs::read_to_string("/proc/version")
92+
.map(|version| version.to_ascii_lowercase().contains("microsoft"))
93+
{
94+
return true;
95+
}
96+
97+
false
98+
}
99+
100+
fn is_docker() -> bool {
101+
let has_docker_env = fs::metadata("/.dockerenv").is_ok();
102+
103+
let has_docker_cgroup = fs::read_to_string("/proc/self/cgroup")
104+
.map(|cgroup| cgroup.to_ascii_lowercase().contains("docker"))
105+
.unwrap_or(false);
106+
107+
has_docker_env || has_docker_cgroup
108+
}

0 commit comments

Comments
 (0)