Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #![doc = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("../README.md")]
2 #![doc(test(attr(
3 warn(unused),
4 deny(warnings),
5 // W/o this, we seem to get some bogus warning about `extern crate ..`.
6 allow(unused_extern_crates),
7 )))]
8
9 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#;
10
11 /// Get the path of the current user's home directory.
12 ///
13 /// See the library documentation for more information.
14 pub fn home_dirrust-analyzer cargo xdg-home 1.3.0 home_dir().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> {
15 match stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("HOME") {
16 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(home) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(home.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
17 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
18 #[cfg(unix)]
19 {
20 unixrust-analyzer cargo xdg-home 1.3.0 unix/::home_dirrust-analyzer cargo xdg-home 1.3.0 unix/home_dir().()
21 }
22
23 #[cfg(windows)]
24 {
25 win32::home_dir()
26 }
27 }
28 }
29 }
30
31 #[cfg(unix)]
32 mod unixrust-analyzer cargo xdg-home 1.3.0 unix/ {
33 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#};
34 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#;
35 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#;
36
37 pub(superrust-analyzer cargo xdg-home 1.3.0 crate/) fn home_dirrust-analyzer cargo xdg-home 1.3.0 unix/home_dir().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> {
38 let uid = unsafe { libcrust-analyzer cargo libc 0.2.189 crate/::geteuidrust-analyzer cargo libc 0.2.189 geteuid().() };
39
40 // SAFETY: Not initalizing references here so it's safe.
41 let mut passwd: libcrust-analyzer cargo libc 0.2.189 crate/::passwdrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/passwd# = unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::zeroedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/zeroed().() };
42 // This has to be enough for everyone.
43 let mut passwd_buf = [0_u8; 1024];
44 let mut result = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().();
45 let ret = unsafe {
46 libcrust-analyzer cargo libc 0.2.189 crate/::getpwuid_rrust-analyzer cargo libc 0.2.189 getpwuid_r().(
47 uid,
48 &mut passwd,
49 passwd_buf.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().() as *mut _,
50 passwd_buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(),
51 &mut result,
52 )
53 };
54 if ret rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().0 || result.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() || passwd.pw_dirrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/passwd#pw_dir..is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() {
55 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
56 }
57
58 // SAFETY: `getpwuid()->pw_dir` is a valid pointer to a c-string.
59 let home_dir = unsafe { CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_ptr().(passwd.pw_dirrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/passwd#pw_dir.) };
60
61 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().(OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::from_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]from_bytes().(home_dir.to_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]to_bytes().())))
62 }
63 }
64
65 #[cfg(windows)]
66 mod win32 {
67 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().};
68
69 use windows_sys::Win32::Foundation::S_OK;
70 use windows_sys::Win32::System::Com::CoTaskMemFree;
71 use windows_sys::Win32::UI::Shell::FOLDERID_Profile;
72 use windows_sys::Win32::UI::Shell::SHGetKnownFolderPath;
73
74 pub(super) fn home_dir() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> {
75 let rfid = FOLDERID_Profile;
76 let mut psz_path = null_mut();
77 let res = unsafe { SHGetKnownFolderPath(&rfid, 0, null_mut(), &mut psz_path as *mut _) };
78 if res != S_OK {
79 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
80 }
81
82 // Determine the length of the UTF-16 string.
83 let mut len = 0;
84 // SAFETY: `psz_path` guaranteed to be a valid pointer to a null-terminated UTF-16 string.
85 while unsafe { *(psz_path as *const u16).offset(len) } != 0 {
86 len += 1;
87 }
88 let slice = unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(psz_path, len as usize) };
89 let path = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf16(slice).ok()?;
90 unsafe {
91 CoTaskMemFree(psz_path as *mut _);
92 }
93
94 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::from(path))
95 }
96 }
97
98 #[cfg(test)]
99 mod testsrust-analyzer cargo xdg-home 1.3.0 tests/ {
100 use superrust-analyzer cargo xdg-home 1.3.0 crate/::*;
101
102 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
•103 #[ignore]
104 fn homerust-analyzer cargo xdg-home 1.3.0 tests/home().() {
105 let home = home_dirrust-analyzer cargo xdg-home 1.3.0 home_dir().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
106 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(home.is_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_dir().());
107
108 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(env_home) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("HOME") {
109 // If `HOME` is set, `home_dir` took the value from it.
110 let env_home = PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<String>`]from().(env_home);
111 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(home, env_home);
112
113 // With `HOME` unset, `home_dir` should still return the same value.
114 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::remove_varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/remove_var().("HOME");
115 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(home_dirrust-analyzer cargo xdg-home 1.3.0 home_dir().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), env_home);
116 }
117 }
118 }