|
1 | 1 | #[cfg(feature = "color")]
|
2 | 2 | use colored::Colorize;
|
3 | 3 | use serde::{Deserialize, Serialize};
|
| 4 | +#[cfg(all(feature = "color", not(target_os = "windows")))] |
| 5 | +use std::os::unix::fs::PermissionsExt; |
| 6 | +#[cfg(all(feature = "color", not(target_os = "windows")))] |
| 7 | +use std::path::Path; |
4 | 8 | use std::path::PathBuf;
|
5 | 9 | use std::{fmt, usize};
|
6 | 10 |
|
@@ -108,13 +112,43 @@ impl fmt::Display for Binary {
|
108 | 112 | #[cfg(feature = "color")]
|
109 | 113 | impl fmt::Display for Binary {
|
110 | 114 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 115 | + #[cfg(target_os = "windows")] |
| 116 | + let filefmt = self.file.display().to_string().bright_blue(); |
| 117 | + #[cfg(not(target_os = "windows"))] |
| 118 | + let filefmt = match std::fs::metadata(&self.file) { |
| 119 | + Ok(md) => { |
| 120 | + #[cfg(target_os = "linux")] |
| 121 | + fn has_filecaps(file: &Path) -> bool { |
| 122 | + xattr::get(file, "security.capability") |
| 123 | + .unwrap_or(None) |
| 124 | + .is_some() |
| 125 | + } |
| 126 | + #[cfg(not(target_os = "linux"))] |
| 127 | + fn has_filecaps(_file: &Path) -> bool { |
| 128 | + false |
| 129 | + } |
| 130 | + |
| 131 | + let mode = md.permissions().mode(); |
| 132 | + if mode & 0o4000 == 0o4000 { |
| 133 | + self.file.display().to_string().white().on_red() |
| 134 | + } else if mode & 0o2000 == 0o2000 { |
| 135 | + self.file.display().to_string().black().on_yellow() |
| 136 | + } else if has_filecaps(&self.file) { |
| 137 | + self.file.display().to_string().black().on_blue() |
| 138 | + } else { |
| 139 | + self.file.display().to_string().bright_blue() |
| 140 | + } |
| 141 | + } |
| 142 | + Err(_) => self.file.display().to_string().bright_blue(), |
| 143 | + }; |
| 144 | + |
111 | 145 | write!(
|
112 | 146 | f,
|
113 | 147 | "{}: | {} | {} {}",
|
114 | 148 | self.binarytype,
|
115 | 149 | self.properties,
|
116 | 150 | "File:".bold().underline(),
|
117 |
| - self.file.display().to_string().bright_blue() |
| 151 | + filefmt |
118 | 152 | )
|
119 | 153 | }
|
120 | 154 | }
|
|
0 commit comments