| 1 | // Copyright (C) 2020 Francois Marier |
| 2 | // |
| 3 | // This program is free software: you can redistribute it and/or modify |
| 4 | // it under the terms of the GNU General Public License as published by |
| 5 | // the Free Software Foundation, either version 3 of the License, or |
| 6 | // (at your option) any later version. |
| 7 | // |
| 8 | // This program is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #![forbid(unsafe_code)] |
| 17 | |
| 18 | mod main_testrust-analyzer cargo safe-rm 1.1.0 main_test/; |
| 19 | |
| 20 | use globrust-analyzer cargo glob 0.3.0 crate/::globrust-analyzer cargo glob 0.3.0 glob().; |
| 21 | 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/::{OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#}; |
| 22 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/, Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#}; |
| 23 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#}; |
| 24 | 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/::{selfrust-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#}; |
| 25 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/; |
| 26 | |
| 27 | const GLOBAL_CONFIGrust-analyzer cargo safe-rm 1.1.0 GLOBAL_CONFIG.: &str = "/etc/safe-rm.conf"; |
| 28 | const LOCAL_GLOBAL_CONFIGrust-analyzer cargo safe-rm 1.1.0 LOCAL_GLOBAL_CONFIG.: &str = "/usr/local/etc/safe-rm.conf"; |
| 29 | const USER_CONFIGrust-analyzer cargo safe-rm 1.1.0 USER_CONFIG.: &str = ".config/safe-rm"; |
| 30 | const LEGACY_USER_CONFIGrust-analyzer cargo safe-rm 1.1.0 LEGACY_USER_CONFIG.: &str = ".safe-rm"; |
| 31 | |
| 32 | const REAL_RMrust-analyzer cargo safe-rm 1.1.0 REAL_RM.: &str = "/bin/rm"; |
| 33 | |
| 34 | const DEFAULT_PATHSrust-analyzer cargo safe-rm 1.1.0 DEFAULT_PATHS.: &[&str] = &[ |
| 35 | "/bin", |
| 36 | "/boot", |
| 37 | "/dev", |
| 38 | "/etc", |
| 39 | "/home", |
| 40 | "/initrd", |
| 41 | "/lib", |
| 42 | "/lib32", |
| 43 | "/lib64", |
| 44 | "/proc", |
| 45 | "/root", |
| 46 | "/sbin", |
| 47 | "/sys", |
| 48 | "/usr", |
| 49 | "/usr/bin", |
| 50 | "/usr/include", |
| 51 | "/usr/lib", |
| 52 | "/usr/local", |
| 53 | "/usr/local/bin", |
| 54 | "/usr/local/include", |
| 55 | "/usr/local/sbin", |
| 56 | "/usr/local/share", |
| 57 | "/usr/sbin", |
| 58 | "/usr/share", |
| 59 | "/usr/src", |
| 60 | "/var", |
| 61 | ]; |
| 62 | |
| 63 | const MAX_GLOB_EXPANSIONrust-analyzer cargo safe-rm 1.1.0 MAX_GLOB_EXPANSION.: usize = 256; |
| 64 | |
| 65 | fn read_configrust-analyzer cargo safe-rm 1.1.0 read_config().<P: 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#>>(filename: P) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>> { |
| 66 | let mut paths = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 67 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().filename.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() { |
| 68 | // Not all config files are expected to be present. |
| 69 | // If they're missing, we silently skip them. |
| 70 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(paths); |
| 71 | } |
| 72 | let f = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&filename).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| { |
| 73 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 74 | "safe-rm: Could not open configuration file: {}", |
| 75 | filename.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().() |
| 76 | ); |
| 77 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 78 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 79 | |
| 80 | let reader = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(f); |
| 81 | for line_result in reader.linesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#lines().() { |
| 82 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(line_paths) = parse_linerust-analyzer cargo safe-rm 1.1.0 parse_line().(filename.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), line_result) { |
| 83 | paths.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(line_paths.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().()); |
| 84 | } |
| 85 | } |
| 86 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(paths) |
| 87 | } |
| 88 | |
| 89 | fn parse_linerust-analyzer cargo safe-rm 1.1.0 parse_line().(filename: pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Display#, line_result: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>> { |
| 90 | let line = line_result.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| { |
| 91 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("safe-rm: Ignoring unreadable line in {}.", filename); |
| 92 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 93 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 94 | let entries = globrust-analyzer cargo glob 0.3.0 glob().(&line).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| { |
| 95 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 96 | "safe-rm: Invalid glob pattern \"{}\" found in {} and ignored.", |
| 97 | line, filename |
| 98 | ); |
| 99 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 100 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 101 | |
| 102 | let mut paths = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 103 | |
| 104 | for entry in entries { |
| 105 | match entry { |
| 106 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path) => { |
| 107 | if paths.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().MAX_GLOB_EXPANSIONrust-analyzer cargo safe-rm 1.1.0 MAX_GLOB_EXPANSION. { |
| 108 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 109 | "safe-rm: Glob \"{}\" found in {} expands to more than {} paths. Ignoring the rest.", |
| 110 | line, filename, MAX_GLOB_EXPANSIONrust-analyzer cargo safe-rm 1.1.0 MAX_GLOB_EXPANSION. |
| 111 | ); |
| 112 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(paths); |
| 113 | } |
| 114 | paths.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(path); |
| 115 | } |
| 116 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 117 | "safe-rm: Ignored unreadable path while expanding glob \"{}\" from {}.", |
| 118 | line, filename |
| 119 | ), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(paths) |
| 124 | } |
| 125 | |
| 126 | fn symlink_canonicalizerust-analyzer cargo safe-rm 1.1.0 symlink_canonicalize().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> 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#> { |
| 127 | // Relative paths need to be prefixed by "./" to have a parent dir. |
| 128 | let mut explicit_path = path.to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().(); |
| 129 | if explicit_path.is_relativerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_relative().() { |
| 130 | explicit_path = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(".").joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(path); |
| 131 | } |
| 132 | |
| 133 | // Convert from relative to absolute path but don't follow the symlink. |
| 134 | // We do this by: |
| 135 | // 1. splitting directory and base file name |
| 136 | // 2. canonicalizing the directory |
| 137 | // 3. recombining directory and file name |
| 138 | let parent: 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#> = match explicit_path.parentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]parent().() { |
| 139 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(dir) => match dir.canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]canonicalize().() { |
| 140 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(normalized_parent) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(normalized_parent), |
| 141 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 142 | }, |
| 143 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => 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().("/")), |
| 144 | }; |
| 145 | return match parent { |
| 146 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(dir) => match path.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().() { |
| 147 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(file_name) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(file_name)), |
| 148 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => match dir.parentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]parent().() { |
| 149 | // file_name == ".." |
| 150 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent_dir) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent_dir.to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()), |
| 151 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => 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().("/")), // Stop at the root. |
| 152 | }, |
| 153 | }, |
| 154 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 155 | }; |
| 156 | } |
| 157 | |
| 158 | fn normalize_pathrust-analyzer cargo safe-rm 1.1.0 normalize_path().(arg: &OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#) -> OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString# { |
| 159 | let path = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(arg); |
| 160 | |
| 161 | // Handle symlinks. |
| 162 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(metadata) = path.symlink_metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]symlink_metadata().() { |
| 163 | if metadata.file_typerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]file_type().().is_symlinkrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[FileType]is_symlink().() { |
| 164 | return match symlink_canonicalizerust-analyzer cargo safe-rm 1.1.0 symlink_canonicalize().(&path) { |
| 165 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(normalized_path) => normalized_path.into_os_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]into_os_string().(), |
| 166 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<&T>`]from().(arg), |
| 167 | }; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Handle normal files. |
| 172 | match path.canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]canonicalize().() { |
| 173 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(normalized_pathname) => normalized_pathname.into_os_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]into_os_string().(), |
| 174 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<&T>`]from().(arg), |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | fn filter_argumentsrust-analyzer cargo safe-rm 1.1.0 filter_arguments().( |
| 179 | args: impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#>, |
| 180 | protected_paths: &[PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#], |
| 181 | ) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#> { |
| 182 | let mut filtered_args = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 183 | for arg in args { |
| 184 | if protected_paths.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&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<OsString>`]from().(normalize_pathrust-analyzer cargo safe-rm 1.1.0 normalize_path().(&arg))) { |
| 185 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("safe-rm: Skipping {}.", arg.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().()); |
| 186 | } else { |
| 187 | filtered_args.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(arg); |
| 188 | } |
| 189 | } |
| 190 | filtered_args |
| 191 | } |
| 192 | |
| 193 | fn read_config_filesrust-analyzer cargo safe-rm 1.1.0 read_config_files().(globals: &[&str], locals: &[&str]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 194 | let mut protected_paths = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 195 | |
| 196 | for config_file in globals { |
| 197 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(paths) = read_configrust-analyzer cargo safe-rm 1.1.0 read_config().(config_file) { |
| 198 | protected_paths.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(paths.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().()); |
| 199 | } |
| 200 | } |
| 201 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value) = 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") { |
| 202 | let home_dir = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&value); |
| 203 | for config_file in locals { |
| 204 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(paths) = read_configrust-analyzer cargo safe-rm 1.1.0 read_config().(&home_dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(config_file))) { |
| 205 | protected_paths.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(paths.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().()); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if protected_paths.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 211 | for path in DEFAULT_PATHSrust-analyzer cargo safe-rm 1.1.0 DEFAULT_PATHS. { |
| 212 | protected_paths.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(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().(path)); |
| 213 | } |
| 214 | } |
| 215 | protected_paths.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().(); |
| 216 | protected_paths.deduprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]dedup().(); |
| 217 | |
| 218 | protected_paths |
| 219 | } |
| 220 | |
| 221 | fn runrust-analyzer cargo safe-rm 1.1.0 run().( |
| 222 | rm_binary: &str, |
| 223 | args: impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#>, |
| 224 | globals: &[&str], |
| 225 | locals: &[&str], |
| 226 | ) -> i32 { |
| 227 | let protected_paths = read_config_filesrust-analyzer cargo safe-rm 1.1.0 read_config_files().(globals, locals); |
| 228 | let filtered_args = filter_argumentsrust-analyzer cargo safe-rm 1.1.0 filter_arguments().(args, &protected_paths); |
| 229 | |
| 230 | // Run the real rm command, returning with the same error code. |
| 231 | match processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().(rm_binary) |
| 232 | .argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]args().(&filtered_args) |
| 233 | .statusrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]status().() |
| 234 | { |
| 235 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(status) => status.coderust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[ExitStatus]code().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(1), |
| 236 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => { |
| 237 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("safe-rm: Failed to run the {} command.", REAL_RMrust-analyzer cargo safe-rm 1.1.0 REAL_RM.); |
| 238 | 1 |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | fn ensure_real_rm_is_callablerust-analyzer cargo safe-rm 1.1.0 ensure_real_rm_is_callable().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 244 | // Make sure we're not calling ourselves recursively. |
| 245 | if fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/canonicalize().(REAL_RMrust-analyzer cargo safe-rm 1.1.0 REAL_RM.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`PartialEq<Self>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`PartialEq<Self>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`PartialEq<Self>`]eq().fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/canonicalize().(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/::current_exerust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/current_exe().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 246 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("safe-rm: Cannot find the real \"rm\" binary."); |
| 247 | processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1); |
| 248 | } |
| 249 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 250 | } |
| 251 | |
| 252 | fn mainrust-analyzer cargo safe-rm 1.1.0 main().() { |
| 253 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) = ensure_real_rm_is_callablerust-analyzer cargo safe-rm 1.1.0 ensure_real_rm_is_callable().() { |
| 254 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 255 | "safe-rm: Cannot check that the real \"rm\" binary is callable: {}", |
| 256 | e |
| 257 | ); |
| 258 | } |
| 259 | processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(runrust-analyzer cargo safe-rm 1.1.0 run().( |
| 260 | REAL_RMrust-analyzer cargo safe-rm 1.1.0 REAL_RM., |
| 261 | 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/::args_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/args_os().().skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(1), |
| 262 | &[GLOBAL_CONFIGrust-analyzer cargo safe-rm 1.1.0 GLOBAL_CONFIG., LOCAL_GLOBAL_CONFIGrust-analyzer cargo safe-rm 1.1.0 LOCAL_GLOBAL_CONFIG.], |
| 263 | &[USER_CONFIGrust-analyzer cargo safe-rm 1.1.0 USER_CONFIG., LEGACY_USER_CONFIGrust-analyzer cargo safe-rm 1.1.0 LEGACY_USER_CONFIG.], |
| 264 | )); |
| 265 | } |