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
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@ by real Git Bash) passes 100%. Supported:

### Windows/MSYS integration

- Paths: `/c/foo` ↔ `C:\foo`, `/tmp` → `%TEMP%`, `/usr/bin`, `/bin`, `/ucrt64` → Git's
install dir, `/dev/null`, `/dev/stdin|stdout|stderr|fd/N`, `/dev/tty`. Windows paths
- Paths: `/c/foo` ↔ `C:\foo`, `/tmp` → `%TMP%` (see below), `/usr/bin`, `/bin`, `/ucrt64` →
Git's install dir, `/dev/null`, `/dev/stdin|stdout|stderr|fd/N`, `/dev/tty`. Windows paths
(`C:\x`, `C:/x`) work everywhere too. `$PWD` and `pwd` show the MSYS form, `pwd -W` the
Windows one (`C:/...`).
- `/tmp` is the folder the MSYS runtime mounts there, which Windows' GetTempPathW gives at
startup: `TMP`, else `TEMP`, else your profile folder (`%USERPROFILE%`), else the Windows
folder. A value in MSYS form such as `TMP=/tmp` therefore means `\tmp` on the current drive,
as it does for Git Bash. gash creates that folder if it doesn't exist, and only if it can't
uses `%LOCALAPPDATA%\Temp` instead. Programs gash starts get `TMP` and `TEMP` set to the same
folder. MSYS programs still see another `/tmp` if an MSYS process from the same Git install
was already running with different `TMP`/`TEMP` values: they share one mount table, and the
first process to start sets `/tmp` for all of them.
- PATH is kept in MSYS form inside the shell and converted when launching programs.
- Arguments to native programs get MSYS2-style path conversion (`/c/x` → `C:/x`),
honoring `MSYS_NO_PATHCONV` and `MSYS2_ARG_CONV_EXCL` when they are exported or given as
Expand Down
3 changes: 2 additions & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ fn temp_file() -> io::Result<File> {
const FILE_ATTRIBUTE_TEMPORARY: u32 = 0x100;
const FILE_FLAG_DELETE_ON_CLOSE: u32 = 0x0400_0000;
static SEQ: AtomicU32 = AtomicU32::new(0);
let dir = std::env::temp_dir();
// The folder behind /tmp, which is created or replaced when the one TMP names doesn't exist.
let dir = &crate::path::mounts().temp;
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.subsec_nanos())
Expand Down
45 changes: 38 additions & 7 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! POSIX <-> Windows path translation, emulating the MSYS2/Git-for-Windows mount table:
//! /c/foo -> C:\foo
//! /tmp -> %TEMP%
//! /tmp -> %TMP%, else %TEMP% (see `pick_temp`)
//! /bin, /lib -> <root>\usr\bin, <root>\usr\lib
//! /usr/... -> <root>\usr\...
//! /dev/null -> NUL
Expand Down Expand Up @@ -76,13 +76,44 @@ pub fn find_msys_root() -> PathBuf {
PathBuf::from(r"C:\Program Files\Git")
}

/// The folder behind `/tmp`: the one the MSYS runtime mounts there.
///
/// MSYS builds its `/tmp` mount (`usertemp` in /etc/fstab) from GetTempPathW, which takes TMP,
/// then TEMP, then USERPROFILE, then the Windows folder, and makes the value a full path. So a
/// launcher that passes `TMP=/tmp` gets `C:\tmp` (on the current drive) in every MSYS process it
/// starts, and gash must use the same folder to agree with them about what is in `/tmp` (issue
/// #27).
///
/// MSYS mounts that folder whether or not it exists, and an MSYS program that started earlier with
/// the same environment may already be using it as `/tmp`, so a missing folder is created rather
/// than replaced. Only when that fails does gash take Windows' default per-user temp folder, and
/// `<root>\tmp` as a last resort: it is read-only for most users of a Git installed under Program
/// Files. MSYS programs gash starts get TMP/TEMP set to the folder chosen here, so they agree with
/// gash unless another MSYS process already running set up `/tmp` differently.
fn pick_temp(root: &Path) -> PathBuf {
let mut temp = crate::sys::temp_path().unwrap_or_default().into_os_string().into_string().unwrap_or_default();
while temp.len() > 3 && temp.ends_with(['\\', '/']) {
temp.pop();
}
let temp = PathBuf::from(temp);
if temp.is_absolute() && (temp.is_dir() || std::fs::create_dir_all(&temp).is_ok()) {
return temp;
}
let local = std::env::var_os("LOCALAPPDATA")
.map(PathBuf::from)
.or_else(|| std::env::var_os("USERPROFILE").map(|p| Path::new(&p).join("AppData").join("Local")));
if let Some(local) = local {
let p = local.join("Temp");
if p.is_absolute() && p.is_dir() {
return p;
}
}
root.join("tmp")
}

pub fn init() {
let root = find_msys_root();
let temp = std::env::var("TEMP")
.or_else(|_| std::env::var("TMP"))
.map(PathBuf::from)
.unwrap_or_else(|_| root.join("tmp"));
let temp = if temp.is_dir() { temp } else { root.join("tmp") };
let temp = pick_temp(&root);
let root_lc = norm_lc(&root);
let temp_lc = norm_lc(&temp);
let msys_bins = vec![norm_lc(&root.join("usr").join("bin")), norm_lc(&root.join("bin"))];
Expand All @@ -92,7 +123,7 @@ pub fn init() {
pub fn mounts() -> &'static Mounts {
MOUNTS.get_or_init(|| {
let root = find_msys_root();
let temp = std::env::temp_dir();
let temp = pick_temp(&root);
Mounts {
root_lc: norm_lc(&root),
temp_lc: norm_lc(&temp),
Expand Down
2 changes: 1 addition & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ impl Shell {
/// which reads TMP/TEMP, and `TMP=/tmp` makes that `C:\tmp`. All running MSYS processes of
/// an installation share one mount table, so the first to start decides `/tmp` for the rest.
/// MSYS turns TMP/TEMP back into POSIX form at startup, so the program still sees `/tmp`.
/// A child gash also reads TEMP to find the folder behind `/tmp`.
/// A child gash also reads TMP/TEMP to find the folder behind `/tmp`.
pub fn child_env(&mut self, extra: &[(String, String)], native: bool) -> Vec<(String, String)> {
let mut out: Vec<(String, String)> = Vec::new();
let mut seen: HashSet<String> = HashSet::new();
Expand Down
20 changes: 20 additions & 0 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ extern "system" {
pub fn GetConsoleWindow() -> HANDLE;
pub fn SetConsoleTitleW(title: *const u16) -> BOOL;
pub fn GetLastError() -> DWORD;
fn GetTempPathW(len: DWORD, buf: *mut u16) -> DWORD;
pub fn ReOpenFile(h: HANDLE, access: DWORD, share: DWORD, flags: DWORD) -> HANDLE;
pub fn SetStdHandle(n: DWORD, h: HANDLE) -> BOOL;
pub fn SetFilePointerEx(h: HANDLE, dist: i64, new_pos: *mut i64, method: DWORD) -> BOOL;
Expand Down Expand Up @@ -438,6 +439,25 @@ pub fn user_name() -> String {
"user".into()
}

/// The temporary folder as GetTempPathW gives it (TMP, then TEMP, then USERPROFILE, then the
/// Windows folder, as a full path ending in `\`). This is what the MSYS runtime mounts at `/tmp`.
/// Unlike `std::env::temp_dir`, which calls GetTempPath2W, it honors TMP/TEMP when running as
/// SYSTEM too.
pub fn temp_path() -> Option<std::path::PathBuf> {
use std::os::windows::ffi::OsStringExt;
let mut buf = vec![0u16; 261];
loop {
let n = unsafe { GetTempPathW(buf.len() as DWORD, buf.as_mut_ptr()) } as usize;
if n == 0 {
return None;
}
if n < buf.len() {
return Some(std::ffi::OsString::from_wide(&buf[..n]).into());
}
buf.resize(n, 0);
}
}

pub fn host_name() -> String {
std::env::var("COMPUTERNAME").unwrap_or_else(|_| "localhost".into())
}
Expand Down
Loading
Loading