Skip to content
Merged
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
28 changes: 27 additions & 1 deletion crates/fs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,33 @@ impl FileHandle for std::fs::File {

#[cfg(target_os = "windows")]
fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
anyhow::bail!("unimplemented")
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::os::windows::io::AsRawHandle;

use windows::Win32::Foundation::HANDLE;
use windows::Win32::Storage::FileSystem::{
FILE_NAME_NORMALIZED, GetFinalPathNameByHandleW,
};

let handle = HANDLE(self.as_raw_handle() as _);

// Query required buffer size (in wide chars)
let required_len =
unsafe { GetFinalPathNameByHandleW(handle, &mut [], FILE_NAME_NORMALIZED) };
if required_len == 0 {
anyhow::bail!("GetFinalPathNameByHandleW returned 0 length");
}

// Allocate buffer and retrieve the path
let mut buf: Vec<u16> = vec![0u16; required_len as usize + 1];
let written = unsafe { GetFinalPathNameByHandleW(handle, &mut buf, FILE_NAME_NORMALIZED) };
if written == 0 {
anyhow::bail!("GetFinalPathNameByHandleW failed to write path");
}

let os_str: OsString = OsString::from_wide(&buf[..written as usize]);
Ok(PathBuf::from(os_str))
}
}

Expand Down
Loading