(&self)
| 354 | } |
| 355 | |
| 356 | pub fn list_files(&self) -> anyhow::Result<Vec<PathBuf>> { |
| 357 | match &self.kind { |
| 358 | PathKind::Path => { |
| 359 | anyhow::ensure!( |
| 360 | matches!(self.compression, Compression::None), |
| 361 | "attempted to read compressed file as a directory: {}", |
| 362 | self.path.display() |
| 363 | ); |
| 364 | |
| 365 | let mut entries = vec![]; |
| 366 | for entry in std::fs::read_dir(&self.path) |
| 367 | .with_context(|| format!("failed to read: {}", self.path.display()))? |
| 368 | { |
| 369 | entries.push( |
| 370 | entry? |
| 371 | .path() |
| 372 | .file_name() |
| 373 | .ok_or_else(|| anyhow::format_err!("invalid file name"))? |
| 374 | .into(), |
| 375 | ); |
| 376 | } |
| 377 | |
| 378 | Ok(entries) |
| 379 | } |
| 380 | PathKind::Tar { subpath } => { |
| 381 | let subpath = subpath.as_deref().unwrap_or(Path::new("")); |
| 382 | let mut archive = tar::Archive::new(self.compression.open_reader(&self.path)?); |
| 383 | Ok(archive |
| 384 | .entries()? |
| 385 | .flat_map(|x| x) |
| 386 | .filter(|x| x.header().entry_type().is_file()) |
| 387 | .flat_map(|x| Some(x.path().ok()?.into_owned())) |
| 388 | .filter(|x| x.parent().unwrap_or(Path::new("")) == subpath) |
| 389 | .flat_map(|x| Some(x.file_name()?.into())) |
| 390 | .collect()) |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | pub fn read(&self) -> anyhow::Result<Vec<u8>> { |
| 396 | match &self.kind { |
no test coverage detected