| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#; |
| 2 | |
| 3 | use rustc_hashrust-analyzer cargo rustc-hash 2.1.3 crate/::FxHashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap# as HashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap#; |
| 4 | |
| 5 | use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::errorrust-analyzer cargo bottom 0.14.9 collection/error/::{CollectionErrorrust-analyzer cargo bottom 0.14.9 collection/error/CollectionError#, CollectionResultrust-analyzer cargo bottom 0.14.9 collection/error/CollectionResult#}; |
| 6 | |
| 7 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 8 | pub struct UserTablerust-analyzer cargo bottom 0.14.9 collection/processes/unix/user_table/UserTable# { |
| 9 | pub uid_user_mappingrust-analyzer cargo bottom 0.14.9 collection/processes/unix/user_table/UserTable#uid_user_mapping.: HashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap#<libcrust-analyzer cargo libc 0.2.189 crate/::uid_trust-analyzer cargo libc 0.2.189 unix/uid_t#, Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<str>>, |
| 10 | } |
| 11 | |
| 12 | impl UserTablerust-analyzer cargo bottom 0.14.9 collection/processes/unix/user_table/UserTable# { |
| 13 | /// Get the username associated with a UID. On first access of a name, it |
| 14 | /// will be cached for future accesses. |
| 15 | pub fn uid_to_usernamerust-analyzer cargo bottom 0.14.9 collection/processes/unix/user_table/impl#[UserTable]uid_to_username().(&mut self, uid: libcrust-analyzer cargo libc 0.2.189 crate/::uid_trust-analyzer cargo libc 0.2.189 unix/uid_t#) -> CollectionResultrust-analyzer cargo bottom 0.14.9 collection/error/CollectionResult#<Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<str>> { |
| 16 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(user) = self.uid_user_mappingrust-analyzer cargo bottom 0.14.9 collection/processes/unix/user_table/UserTable#uid_user_mapping..getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&uid) { |
| 17 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(user.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().()) |
| 18 | } else { |
| 19 | // SAFETY: getpwuid returns a null pointer if no passwd entry is found for the |
| 20 | // uid which we check. |
| 21 | let passwd = unsafe { libcrust-analyzer cargo libc 0.2.189 crate/::getpwuidrust-analyzer cargo libc 0.2.189 getpwuid().(uid) }; |
| 22 | |
| 23 | if passwd.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() { |
| 24 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("passwd is inaccessible".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 25 | } else { |
| 26 | // SAFETY: We return early if passwd is null. |
| 27 | let username: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<str> = unsafe { 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#::from_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_ptr().((*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().passwd).pw_namerust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/passwd#pw_name.) } |
| 28 | .to_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]to_str().() |
| 29 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| CollectionErrorrust-analyzer cargo bottom 0.14.9 collection/error/CollectionError#::Generalrust-analyzer cargo bottom 0.14.9 collection/error/CollectionError#General#(err.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 30 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 31 | |
| 32 | self.uid_user_mappingrust-analyzer cargo bottom 0.14.9 collection/processes/unix/user_table/UserTable#uid_user_mapping..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(uid, username.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().()); |
| 33 | |
| 34 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(username) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |