| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 2 | ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#, |
| 3 | opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/, |
| 4 | 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/::preluderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/prelude/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#, |
| 5 | pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}, |
| 6 | strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/, |
| 7 | }; |
| 8 | |
| 9 | use superrust-analyzer cargo sudo-rs 0.2.14 common/::{Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#, SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#}; |
| 10 | |
| 11 | /// A `PathBuf` guaranteed to not contain null bytes and be UTF-8 encoded |
| 12 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 13 | #[cfg_attr(test, derive(Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:))] |
| 14 | pub struct SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath# { |
| 15 | innerrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#inner.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 16 | } |
| 17 | |
| 18 | impl SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath# { |
| 19 | pub fn newrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]new().(path: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath], Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> { |
| 20 | let bytes = path.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().().as_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]as_bytes().(); |
| 21 | if bytes.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&0) { |
| 22 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::PathValidationrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#PathValidation#(path)); |
| 23 | } |
| 24 | |
| 25 | // check this through a reference so we can return `path` in the error case |
| 26 | if strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(bytes).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 27 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::PathValidationrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#PathValidation#(path)); |
| 28 | } |
| 29 | |
| 30 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath] { |
| 31 | // NOTE(unwrap): UTF-8 encoding is checked above |
| 32 | innerrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#inner.: path.into_os_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]into_os_string().().into_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString]into_string().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | pub fn from_cli_stringrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]from_cli_string().(cli_string: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath] { |
| 37 | Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]::newrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]new().(cli_string.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 38 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("strings that come in from CLI should not have interior null bytes") |
| 39 | } |
| 40 | |
| 41 | /// Resolve the use of a '~' that occurs in this `SudoPathBuf`; based on the sudoers context |
| 42 | pub fn expand_tilde_in_pathrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]expand_tilde_in_path().(&self, default_username: &SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#, Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> { |
| 43 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prefix) = self.innerrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#inner..strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().('~') { |
| 44 | let (username, relpath) = prefix.split_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_once().('/').unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().((prefix, "")); |
| 45 | |
| 46 | let username = if username.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 47 | default_username.clonerust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Clone]clone().() |
| 48 | } else { |
| 49 | SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#::newrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]new().(username.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 50 | }; |
| 51 | |
| 52 | let home_dir = craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_name().(username.as_cstrrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]as_cstr().()) |
| 53 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 54 | .flattenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<Option<T>>`]flatten().() |
| 55 | .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::UserNotFoundrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#UserNotFound#(username.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 56 | .homerust-analyzer cargo sudo-rs 0.2.14 system/User#home.; |
| 57 | let path = home_dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(relpath); |
| 58 | |
| 59 | Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]::newrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]new().(path) |
| 60 | } else { |
| 61 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.clonerust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][Clone]clone().()) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#> for PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# { |
| 67 | fn fromrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[PathBuf][`From<SudoPath>`]from().(value: SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[PathBuf][`From<SudoPath>`] { |
| 68 | value.innerrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#inner..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#> for SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath# { |
| 73 | fn as_refrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`AsRef<Path>`]as_ref().(&self) -> &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path# { |
| 74 | self.innerrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#inner..as_refrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[String][`AsRef<Path>`]as_ref().() |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath# { |
| 79 | type Targetrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][Deref]Target# = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#; |
| 80 | |
| 81 | fn derefrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][Deref]deref().(&self) -> &Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# { |
| 82 | self.as_refrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`AsRef<Path>`]as_ref().() |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> for SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath# { |
| 87 | type Errorrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`TryFrom<String>`]Error# = Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#; |
| 88 | |
| 89 | fn try_fromrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`TryFrom<String>`]try_from().(value: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`TryFrom<String>`], Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`TryFrom<String>`]::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#> { |
| 90 | Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`TryFrom<String>`]::newrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]new().(value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#> for OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString# { |
| 95 | fn fromrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[OsString][`From<SudoPath>`]from().(value: SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[OsString][`From<SudoPath>`] { |
| 96 | value.innerrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath#inner..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[cfg(test)] |
| 101 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'_ str> for SudoPathrust-analyzer cargo sudo-rs 0.2.14 common/path/SudoPath# { |
| 102 | fn fromrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`From<&str>`]from().(value: &'_ str) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`From<&str>`] { |
| 103 | Selfrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath][`From<&str>`]::newrust-analyzer cargo sudo-rs 0.2.14 common/path/impl#[SudoPath]new().(value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 104 | } |
| 105 | } |