1extern crate option_ext;
2
3use std::ffi::OsString;
4use std::path::PathBuf;
5
6// we don't need to explicitly handle empty strings in the code above,
7// because an empty string is not considered to be a absolute path here.
8pub fn is_absolute_path(path: OsString) -> Option<PathBuf> {
9 let path: PathBuf = PathBuf::from(path);
10 if path.is_absolute() {
11 Some(path)
12 } else {
13 None
14 }
15}
16
17#[cfg(all(unix, not(target_os = "redox")))]
18extern crate libc;
19
20#[cfg(all(unix, not(target_os = "redox")))]
21mod target_unix_not_redox {
22
23use std::env;
24use std::ffi::{CStr, OsString};
25use std::mem;
26use std::os::unix::ffi::OsStringExt;
27use std::path::PathBuf;
28use std::ptr;
29
30use super::libc;
31
32// https://github.com/rust-lang/rust/blob/2682b88c526d493edeb2d3f2df358f44db69b73f/library/std/src/sys/unix/os.rs#L595
33pub fn home_dir() -> Option<PathBuf> {
34 return env::var_os("HOME")
35 .and_then(|h| if h.is_empty() { None } else { Some(h) })
36 .or_else(|| unsafe { fallback() })
37 .map(PathBuf::from);
38
39 #[cfg(any(target_os = "android", target_os = "ios", target_os = "emscripten"))]
40 unsafe fn fallback() -> Option<OsString> {
41 None
42 }
43 #[cfg(not(any(target_os = "android", target_os = "ios", target_os = "emscripten")))]
44 unsafe fn fallback() -> Option<OsString> {
45 let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) {
46 n if n < 0 => 512 as usize,
47 n => n as usize,
48 };
49 let mut buf = Vec::with_capacity(amt);
50 let mut passwd: libc::passwd = mem::zeroed();
51 let mut result = ptr::null_mut();
52 match libc::getpwuid_r(
53 libc::getuid(),
54 &mut passwd,
55 buf.as_mut_ptr(),
56 buf.capacity(),
57 &mut result,
58 ) {
59 0 if !result.is_null() => {
60 let ptr = passwd.pw_dir as *const _;
61 let bytes = CStr::from_ptr(ptr).to_bytes();
62 if bytes.is_empty() {
63 None
64 } else {
65 Some(OsStringExt::from_vec(bytes.to_vec()))
66 }
67 }
68 _ => None,
69 }
70 }
71}
72
73}
74
75#[cfg(all(unix, not(target_os = "redox")))]
76pub use self::target_unix_not_redox::home_dir;
77
78#[cfg(target_os = "redox")]
79extern crate redox_users;
80
81#[cfg(target_os = "redox")]
82mod target_redox {
83
84use std::path::PathBuf;
85
86use super::redox_users::{All, AllUsers, Config};
87
88pub fn home_dir() -> Option<PathBuf> {
89 let current_uid = redox_users::get_uid().ok()?;
90 let users = AllUsers::basic(Config::default()).ok()?;
91 let user = users.get_by_id(current_uid)?;
92
93 Some(PathBuf::from(user.home.clone()))
94}
95
96}
97
98#[cfg(target_os = "redox")]
99pub use self::target_redox::home_dir;
100
101#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
102mod xdg_user_dirs;
103
104#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
105mod target_unix_not_mac {
106
107use std::collections::HashMap;
108use std::env;
109use std::path::{Path, PathBuf};
110
111use super::{home_dir, is_absolute_path};
112use super::xdg_user_dirs;
113
114fn user_dir_file(home_dir: &Path) -> PathBuf {
115 env::var_os("XDG_CONFIG_HOME").and_then(is_absolute_path).unwrap_or_else(|| home_dir.join(".config")).join("user-dirs.dirs")
116}
117
118// this could be optimized further to not create a map and instead retrieve the requested path only
119pub fn user_dir(user_dir_name: &str) -> Option<PathBuf> {
120 if let Some(home_dir) = home_dir() {
121 xdg_user_dirs::single(&home_dir, &user_dir_file(&home_dir), user_dir_name).remove(user_dir_name)
122 } else {
123 None
124 }
125}
126
127pub fn user_dirs(home_dir_path: &Path) -> HashMap<String, PathBuf> {
128 xdg_user_dirs::all(home_dir_path, &user_dir_file(home_dir_path))
129}
130
131}
132
133#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
134pub use self::target_unix_not_mac::{user_dir, user_dirs};
135
136#[cfg(target_os = "windows")]
137extern crate windows_sys as windows;
138
139#[cfg(target_os = "windows")]
140mod target_windows {
141
142use std::ffi::c_void;
143use std::ffi::OsString;
144use std::os::windows::ffi::OsStringExt;
145use std::path::PathBuf;
146use std::slice;
147
148use super::windows::Win32;
149use super::windows::Win32::UI::Shell;
150
151pub fn known_folder(folder_id: windows::core::GUID) -> Option<PathBuf> {
152 unsafe {
153 let mut path_ptr: windows::core::PWSTR = std::ptr::null_mut();
154 let result = Shell::SHGetKnownFolderPath(
155 &folder_id,
156 0,
157 Win32::Foundation::HANDLE::default(),
158 &mut path_ptr
159 );
160 if result == 0 {
161 let len = windows::Win32::Globalization::lstrlenW(path_ptr) as usize;
162 let path = slice::from_raw_parts(path_ptr, len);
163 let ostr: OsString = OsStringExt::from_wide(path);
164 windows::Win32::System::Com::CoTaskMemFree(path_ptr as *const c_void);
165 Some(PathBuf::from(ostr))
166 } else {
167 windows::Win32::System::Com::CoTaskMemFree(path_ptr as *const c_void);
168 None
169 }
170 }
171}
172
173pub fn known_folder_profile() -> Option<PathBuf> {
174 known_folder(Shell::FOLDERID_Profile)
175}
176
177pub fn known_folder_roaming_app_data() -> Option<PathBuf> {
178 known_folder(Shell::FOLDERID_RoamingAppData)
179}
180
181pub fn known_folder_local_app_data() -> Option<PathBuf> {
182 known_folder(Shell::FOLDERID_LocalAppData)
183}
184
185pub fn known_folder_music() -> Option<PathBuf> {
186 known_folder(Shell::FOLDERID_Music)
187}
188
189pub fn known_folder_desktop() -> Option<PathBuf> {
190 known_folder(Shell::FOLDERID_Desktop)
191}
192
193pub fn known_folder_documents() -> Option<PathBuf> {
194 known_folder(Shell::FOLDERID_Documents)
195}
196
197pub fn known_folder_downloads() -> Option<PathBuf> {
198 known_folder(Shell::FOLDERID_Downloads)
199}
200
201pub fn known_folder_pictures() -> Option<PathBuf> {
202 known_folder(Shell::FOLDERID_Pictures)
203}
204
205pub fn known_folder_public() -> Option<PathBuf> {
206 known_folder(Shell::FOLDERID_Public)
207}
208pub fn known_folder_templates() -> Option<PathBuf> {
209 known_folder(Shell::FOLDERID_Templates)
210}
211pub fn known_folder_videos() -> Option<PathBuf> {
212 known_folder(Shell::FOLDERID_Videos)
213}
214
215}
216
217#[cfg(target_os = "windows")]
218pub use self::target_windows::{
219 known_folder, known_folder_profile, known_folder_roaming_app_data, known_folder_local_app_data,
220 known_folder_music, known_folder_desktop, known_folder_documents, known_folder_downloads,
221 known_folder_pictures, known_folder_public, known_folder_templates, known_folder_videos
222};
223