| 1 | use std::path::{Path, PathBuf}; |
| 2 | |
| 3 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 4 | pub enum NativePath { |
| 5 | Unix(Vec<u8>), |
| 6 | Windows(Vec<u16>), |
| 7 | } |
| 8 | impl NativePath { |
| 9 | pub fn from_path(path: &Path) -> Self { |
| 10 | #[cfg(unix)] |
| 11 | { |
| 12 | use std::os::unix::ffi::OsStrExt; |
| 13 | Self::Unix(path.as_os_str().as_bytes().to_vec()) |
| 14 | } |
| 15 | #[cfg(windows)] |
| 16 | { |
| 17 | use std::os::windows::ffi::OsStrExt; |
| 18 | Self::Windows(path.as_os_str().encode_wide().collect()) |
| 19 | } |
| 20 | } |
| 21 | pub fn to_path(&self) -> Result<PathBuf, ErrorCode> { |
| 22 | #[cfg(unix)] |
| 23 | if let Self::Unix(bytes) = self { |
| 24 | use std::os::unix::ffi::OsStringExt; |
| 25 | if bytes.contains(&0) { |
| 26 | return Err(ErrorCode::InvalidRequest); |
| 27 | } |
| 28 | let path = PathBuf::from(std::ffi::OsString::from_vec(bytes.clone())); |
| 29 | return if path.is_absolute() { |
| 30 | Ok(path) |
| 31 | } else { |
| 32 | Err(ErrorCode::InvalidRequest) |
| 33 | }; |
| 34 | } |
| 35 | #[cfg(windows)] |
| 36 | if let Self::Windows(units) = self { |
| 37 | use std::os::windows::ffi::OsStringExt; |
| 38 | if units.contains(&0) { |
| 39 | return Err(ErrorCode::InvalidRequest); |
| 40 | } |
| 41 | let path = PathBuf::from(std::ffi::OsString::from_wide(units)); |
| 42 | let text = path.to_string_lossy(); |
| 43 | if path.is_absolute() && !text.starts_with("\\\\") && !text.starts_with("//") { |
| 44 | return Ok(path); |
| 45 | } |
| 46 | return Err(ErrorCode::InvalidRequest); |
| 47 | } |
| 48 | Err(ErrorCode::UnsupportedFeature) |
| 49 | } |
| 50 | } |
| 51 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 52 | pub struct Limits { |
| 53 | pub input: u64, |
| 54 | pub output: u64, |
| 55 | pub entries: u32, |
| 56 | } |
| 57 | impl Default for Limits { |
| 58 | fn default() -> Self { |
| 59 | Self { |
| 60 | input: u64::MAX, |
| 61 | output: 16 * 1024 * 1024 * 1024, |
| 62 | entries: 10_000, |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | impl Limits { |
| 67 | pub(crate) fn legacy(self) -> Self { |
| 68 | Self { |
| 69 | input: self.input.min(64 * 1024 * 1024), |
| 70 | output: self.output.min(128 * 1024 * 1024), |
| 71 | entries: self.entries, |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | pub fn valid(self) -> bool { |
| 76 | let hard = Self::default(); |
| 77 | self.input > 0 |
| 78 | && self.input <= hard.input |
| 79 | && self.output > 0 |
| 80 | && self.output <= hard.output |
| 81 | && self.entries > 0 |
| 82 | && self.entries <= hard.entries |
| 83 | } |
| 84 | } |
| 85 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 86 | pub enum Format { |
| 87 | Zip, |
| 88 | Tar, |
| 89 | Gzip, |
| 90 | TarGzip, |
| 91 | SevenZip, |
| 92 | } |
| 93 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 94 | pub enum SourceObject { |
| 95 | Unix { |
| 96 | device: u64, |
| 97 | inode: u64, |
| 98 | }, |
| 99 | Windows { |
| 100 | volume_serial: u64, |
| 101 | file_id: [u8; 16], |
| 102 | }, |
| 103 | } |
| 104 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 105 | pub struct ListId { |
| 106 | pub catalog_version: u16, |
| 107 | pub format: Format, |
| 108 | pub source_object: SourceObject, |
| 109 | pub archive_bytes: u64, |
| 110 | pub archive_sha256: [u8; 32], |
| 111 | } |
| 112 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 113 | pub enum Selection { |
| 114 | All, |
| 115 | EntryIds(Vec<u32>), |
| 116 | } |
| 117 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 118 | pub enum Method { |
| 119 | Store, |
| 120 | Deflate, |
| 121 | } |
| 122 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 123 | pub enum Encryption { |
| 124 | None, |
| 125 | Aes256, |
| 126 | } |
| 127 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 128 | pub struct Source { |
| 129 | pub path: NativePath, |
| 130 | pub archive_name: String, |
| 131 | } |
| 132 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 133 | pub enum Action { |
| 134 | List { |
| 135 | archive: NativePath, |
| 136 | }, |
| 137 | Extract { |
| 138 | archive: NativePath, |
| 139 | destination: NativePath, |
| 140 | selection: Selection, |
| 141 | list_id: Option<ListId>, |
| 142 | gzip_output_name: Option<String>, |
| 143 | }, |
| 144 | CreateZip { |
| 145 | sources: Vec<Source>, |
| 146 | output_file: NativePath, |
| 147 | method: Method, |
| 148 | encryption: Encryption, |
| 149 | }, |
| 150 | } |
| 151 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 152 | pub enum ActionKind { |
| 153 | List, |
| 154 | Extract, |
| 155 | CreateZip, |
| 156 | } |
| 157 | impl Action { |
| 158 | pub fn kind(&self) -> ActionKind { |
| 159 | match self { |
| 160 | Self::List { .. } => ActionKind::List, |
| 161 | Self::Extract { .. } => ActionKind::Extract, |
| 162 | Self::CreateZip { .. } => ActionKind::CreateZip, |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 167 | pub struct Start { |
| 168 | pub action: Action, |
| 169 | pub limits: Limits, |
| 170 | } |
| 171 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 172 | pub enum EntryKind { |
| 173 | File, |
| 174 | Directory, |
| 175 | Link, |
| 176 | Other, |
| 177 | } |
| 178 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 179 | pub struct Entry { |
| 180 | pub entry_id: u32, |
| 181 | pub name: String, |
| 182 | pub kind: EntryKind, |
| 183 | pub size: u64, |
| 184 | pub compressed_size: u64, |
| 185 | pub encrypted: bool, |
| 186 | } |
| 187 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 188 | pub enum Stage { |
| 189 | Scan, |
| 190 | Read, |
| 191 | Decode, |
| 192 | Write, |
| 193 | Verify, |
| 194 | Commit, |
| 195 | } |
| 196 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 197 | pub enum Disposition { |
| 198 | Created, |
| 199 | PartialRetained, |
| 200 | Removed, |
| 201 | CommitUncertain, |
| 202 | } |
| 203 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 204 | pub enum Outcome { |
| 205 | Completed, |
| 206 | Failed, |
| 207 | Cancelled, |
| 208 | } |
| 209 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 210 | pub enum ErrorCode { |
| 211 | None, |
| 212 | Integrity, |
| 213 | UnsupportedFormat, |
| 214 | UnsupportedFeature, |
| 215 | WrongPassword, |
| 216 | UnsafePath, |
| 217 | Conflict, |
| 218 | Limit, |
| 219 | Io, |
| 220 | Protocol, |
| 221 | Internal, |
| 222 | SourceChanged, |
| 223 | InvalidRequest, |
| 224 | } |
| 225 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 226 | pub enum Publication { |
| 227 | None, |
| 228 | Partial, |
| 229 | Complete, |
| 230 | Uncertain, |
| 231 | } |
| 232 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 233 | pub enum Durability { |
| 234 | NotApplicable, |
| 235 | Synced, |
| 236 | Unconfirmed, |
| 237 | Failed, |
| 238 | } |
| 239 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 240 | pub enum Cleanup { |
| 241 | Complete, |
| 242 | Failed, |
| 243 | Unconfirmed, |
| 244 | } |
| 245 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 246 | pub enum CancelStatus { |
| 247 | Requested, |
| 248 | AlreadyRequested, |
| 249 | TooLate, |
| 250 | Finished, |
| 251 | } |
| 252 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 253 | pub enum PasswordPurpose { |
| 254 | Decrypt, |
| 255 | Encrypt, |
| 256 | } |
| 257 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 258 | pub struct Terminal { |
| 259 | pub outcome: Outcome, |
| 260 | pub error: ErrorCode, |
| 261 | pub publication: Publication, |
| 262 | pub created_count: u64, |
| 263 | pub retained_partial_count: u64, |
| 264 | pub durability: Durability, |
| 265 | pub cleanup: Cleanup, |
| 266 | pub detail: String, |
| 267 | } |
| 268 | pub struct Secret(pub Vec<u8>); |
| 269 | impl std::fmt::Debug for Secret { |
| 270 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 271 | f.write_str("[redacted]") |
| 272 | } |
| 273 | } |
| 274 | impl Drop for Secret { |
| 275 | fn drop(&mut self) { |
| 276 | zeroize::Zeroize::zeroize(&mut self.0); |
| 277 | } |
| 278 | } |
| 279 | #[derive(Debug)] |
| 280 | pub enum Message { |
| 281 | Start(Start), |
| 282 | Password { |
| 283 | challenge_id: u64, |
| 284 | secret: Secret, |
| 285 | }, |
| 286 | Cancel, |
| 287 | Accepted { |
| 288 | action: ActionKind, |
| 289 | encryption: bool, |
| 290 | }, |
| 291 | Entry(Entry), |
| 292 | ListEnd { |
| 293 | entry_count: u32, |
| 294 | list_id: ListId, |
| 295 | }, |
| 296 | PasswordRequired { |
| 297 | challenge_id: u64, |
| 298 | attempt: u32, |
| 299 | purpose: PasswordPurpose, |
| 300 | }, |
| 301 | Progress { |
| 302 | stage: Stage, |
| 303 | processed_bytes: u64, |
| 304 | total_bytes: Option<u64>, |
| 305 | completed_entries: u32, |
| 306 | }, |
| 307 | Output { |
| 308 | entry_id: Option<u32>, |
| 309 | relative_name: String, |
| 310 | disposition: Disposition, |
| 311 | }, |
| 312 | Terminal(Terminal), |
| 313 | CancelAck { |
| 314 | request_sequence: u64, |
| 315 | status: CancelStatus, |
| 316 | }, |
| 317 | } |