Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/crates/diagnostics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ description = "Structured log catalog, channels, records, queries and the bounde
test-support = []

[dependencies]
lithe-platform = { path = "../platform" }
lithe-protocol = { path = "../protocol" }
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
regex = "1.11"
Expand Down
7 changes: 1 addition & 6 deletions rust/crates/diagnostics/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,7 @@ pub fn validate_segment(value: &str) -> Result<(), LogError> {
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || b"._-".contains(&c));
// Win32 strips trailing dots and reserves device basenames even with an extension.
// Reject them on every host so the same identity always names the same file.
let basename = value.split('.').next().unwrap_or("");
let device = matches!(basename, "con" | "prn" | "aux" | "nul")
|| (basename.len() == 4
&& (basename.starts_with("com") || basename.starts_with("lpt"))
&& matches!(basename.as_bytes()[3], b'1'..=b'9'));
if !valid || value.ends_with('.') || device {
if !valid || !lithe_platform::names::is_portable_windows_component(value) {
return Err(LogError::new(-32033, "Invalid channel segment"));
}
Ok(())
Expand Down
1 change: 1 addition & 0 deletions rust/crates/git-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ libc = "0.2"
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Pipes", "Win32_System_JobObjects", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Threading", "Win32_Security"] }

[dependencies]
lithe-platform = { path = "../platform" }
rand = "0.8"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
88 changes: 30 additions & 58 deletions rust/crates/git-host/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,42 @@
//! Native executable resolution and per-child Git environment configuration.
use lithe_platform::fs::is_launchable_file;
use lithe_platform::paths::slash_separated;
use lithe_platform::Platform;
use std::path::PathBuf;
use std::process::Command;

/// Resolves a user-selected executable or the first runnable Git on PATH.
pub fn executable(selected: Option<&str>) -> Option<PathBuf> {
let platform = Platform::HOST;
let candidates = match selected {
Some(path) => vec![PathBuf::from(path)],
None => std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default())
.flat_map(|directory| {
#[cfg(windows)]
let names = ["git.exe"];
#[cfg(not(windows))]
let names = ["git"];
names.map(|name| directory.join(name))
})
.collect(),
None => {
let name = platform.executable_name("git");
std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default())
.map(|directory| directory.join(&name))
.collect()
}
};
candidates
.into_iter()
.find(|path| {
let Ok(metadata) = path.metadata() else {
return false;
};
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
metadata.is_file() && metadata.permissions().mode() & 0o111 != 0
}
#[cfg(not(unix))]
{
metadata.is_file()
}
path.metadata()
.is_ok_and(|metadata| is_launchable_file(&metadata))
})
.map(launch_path)
.map(|path| launch_path(path, platform))
}

/// Some Windows hosts (disk-encryption or endpoint-security filters) make a Git started through a
/// backslash path print nothing to a captured pipe, while the same executable started through a
/// forward-slash path behaves normally. Windows accepts both separators, so launch with `/`.
fn launch_path(path: PathBuf) -> PathBuf {
#[cfg(windows)]
{
if let Some(text) = path.to_str() {
return PathBuf::from(forward_slash_path(text));
/// Verbatim (`\\?\`) and UNC (`\\server`) paths keep their native form because `/` is not valid
/// there.
fn launch_path(path: PathBuf, platform: Platform) -> PathBuf {
match path.to_str() {
Some(text) if !text.starts_with(r"\\") => {
PathBuf::from(slash_separated(text, platform).into_owned())
}
}
path
}

/// Verbatim (`\\?\`) and UNC (`\\server`) paths keep their native form because `/` is not valid there.
#[cfg(any(windows, test))]
fn forward_slash_path(path: &str) -> String {
if path.starts_with(r"\\") {
path.to_owned()
} else {
path.replace('\\', "/")
_ => path,
}
}

Expand Down Expand Up @@ -158,34 +140,24 @@ mod tests {
}

#[test]
fn launch_paths_use_forward_slashes_except_unc_and_verbatim() {
fn windows_launch_paths_use_forward_slashes_except_unc_and_verbatim() {
let launch = |path: &str| launch_path(PathBuf::from(path), Platform::Windows);
assert_eq!(
forward_slash_path(r"D:\Program Files\Git\cmd\git.exe"),
"D:/Program Files/Git/cmd/git.exe"
launch(r"D:\Program Files\Git\cmd\git.exe"),
PathBuf::from("D:/Program Files/Git/cmd/git.exe")
);
assert_eq!(
forward_slash_path("D:/Git/cmd/git.exe"),
"D:/Git/cmd/git.exe"
launch("D:/Git/cmd/git.exe"),
PathBuf::from("D:/Git/cmd/git.exe")
);
for native in [r"\\server\share\git.exe", r"\\?\D:\Git\cmd\git.exe"] {
assert_eq!(forward_slash_path(native), native);
assert_eq!(launch(native), PathBuf::from(native));
}
}

// Regression: a backslash launch path makes some Windows hosts capture empty Git output, so the
// resolved executable must reach the process spawner with forward slashes.
#[cfg(windows)]
#[test]
fn selected_executable_is_resolved_with_forward_slashes() {
let file = std::env::temp_dir().join(format!(
"lithe-git-host-launch-path-{}.exe",
std::process::id()
));
std::fs::write(&file, b"").unwrap();
let resolved = executable(file.to_str());
std::fs::remove_file(&file).unwrap();
let resolved = resolved.unwrap().to_string_lossy().into_owned();
assert!(!resolved.contains('\\'), "{resolved}");
assert!(resolved.ends_with(".exe"), "{resolved}");
fn unix_launch_paths_keep_backslashes_as_name_characters() {
let path = PathBuf::from(r"/opt/odd\name/git");
assert_eq!(launch_path(path.clone(), Platform::Unix), path);
}
}
7 changes: 1 addition & 6 deletions rust/crates/git-host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,7 @@ impl InputFile {
));
let mut options = OpenOptions::new();
options.read(true).write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
match options.open(&path) {
match lithe_platform::fs::owner_only_file(&mut options).open(&path) {
Ok(file) => {
let mut owned = Self { file, path };
owned.file.write_all(input)?;
Expand Down
Loading
Loading