Skip to content

Commit

Permalink
libstd: win: Replace GetUserProfileDirectoryW by SHGetFolderPathW
Browse files Browse the repository at this point in the history
The later is allowed on UWP
  • Loading branch information
chouquette committed Apr 25, 2019
1 parent 0214d3c commit afc92f2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
19 changes: 12 additions & 7 deletions src/libstd/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ pub const SECURITY_SQOS_PRESENT: DWORD = 0x00100000;

pub const FIONBIO: c_ulong = 0x8004667e;

pub const CSIDL_PROFILE: c_int = 0x0028;

#[repr(C)]
#[allow(dead_code)] // we only use some variants
pub enum SHGFP_TYPE {
SHGFP_TYPE_CURRENT = 0,
SHGFP_TYPE_DEFAULT = 1
}

#[cfg(target_arch = "arm")]
const ARM_MAX_BREAKPOINTS: usize = 8;
#[cfg(target_arch = "arm")]
Expand Down Expand Up @@ -154,7 +163,6 @@ pub const WSAECONNREFUSED: c_int = 10061;

pub const MAX_PROTOCOL_CHAIN: DWORD = 7;

pub const TOKEN_READ: DWORD = 0x20008;
pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
pub const FSCTL_GET_REPARSE_POINT: DWORD = 0x900a8;
pub const IO_REPARSE_TAG_SYMLINK: DWORD = 0xa000000c;
Expand Down Expand Up @@ -1056,9 +1064,6 @@ extern "system" {
pub fn GetCommandLineW() -> *mut LPCWSTR;
pub fn GetTempPathW(nBufferLength: DWORD,
lpBuffer: LPCWSTR) -> DWORD;
pub fn OpenProcessToken(ProcessHandle: HANDLE,
DesiredAccess: DWORD,
TokenHandle: *mut HANDLE) -> BOOL;
pub fn GetCurrentProcess() -> HANDLE;
pub fn GetCurrentThread() -> HANDLE;
pub fn GetStdHandle(which: DWORD) -> HANDLE;
Expand All @@ -1083,9 +1088,9 @@ extern "system" {
pub fn SwitchToThread() -> BOOL;
pub fn Sleep(dwMilliseconds: DWORD);
pub fn GetProcessId(handle: HANDLE) -> DWORD;
pub fn GetUserProfileDirectoryW(hToken: HANDLE,
lpProfileDir: LPWSTR,
lpcchSize: *mut DWORD) -> BOOL;
pub fn SHGetFolderPathW(handle: HANDLE, csidl: c_int,
hToken: HANDLE, dwFlags: DWORD,
pszPath: LPWSTR) -> DWORD;
#[cfg(not(target_os = "uwp"))]
pub fn SetHandleInformation(hObject: HANDLE,
dwMask: DWORD,
Expand Down
20 changes: 7 additions & 13 deletions src/libstd/sys/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::path::{self, PathBuf};
use crate::ptr;
use crate::slice;
use crate::sys::{c, cvt};
use crate::sys::handle::Handle;

use super::to_u16s;

Expand Down Expand Up @@ -288,19 +287,14 @@ pub fn home_dir() -> Option<PathBuf> {
crate::env::var_os("HOME").or_else(|| {
crate::env::var_os("USERPROFILE")
}).map(PathBuf::from).or_else(|| unsafe {
let me = c::GetCurrentProcess();
let mut token = ptr::null_mut();
if c::OpenProcessToken(me, c::TOKEN_READ, &mut token) == 0 {
return None
let mut bufArray = [0u16; 260]; // MAX_PATH
let buf = bufArray[..].as_mut_ptr();
match c::SHGetFolderPathW(ptr::null_mut(), c::CSIDL_PROFILE,
ptr::null_mut(), c::SHGFP_TYPE::SHGFP_TYPE_CURRENT as u32,
buf) {
0 => Some(super::os2path(&bufArray[..])),
_ => None
}
let _handle = Handle::new(token);
super::fill_utf16_buf(|buf, mut sz| {
match c::GetUserProfileDirectoryW(token, buf, &mut sz) {
0 if c::GetLastError() != c::ERROR_INSUFFICIENT_BUFFER => 0,
0 => sz,
_ => sz - 1, // sz includes the null terminator
}
}, super::os2path).ok()
})
}

Expand Down

0 comments on commit afc92f2

Please sign in to comment.