1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
use std::{fmt::Debug, sync::Arc};
use shared_buffer::OwnedBuffer;
use crate::{
container::ContainerError, PathSegment, PathSegmentError, PathSegments, Timestamps,
ToPathSegments,
};
/// A WEBC volume.
///
/// A `Volume` represents a collection of files and directories, providing
/// methods to read file contents and traverse directories.
///
/// # Example
///
/// ```
/// #[cfg(not(feature = "v3"))]
/// # fn main() {}
/// #[cfg(feature = "v3")]
/// # fn main() {
/// use webc::{Metadata, Volume};
/// # use webc::{
/// # compat::Container,
/// # PathSegment,
/// # v3::{
/// # write::{Directory, Writer},
/// # read::OwnedReader,
/// # SignatureAlgorithm,
/// # Timestamps
/// # },
/// # };
/// # use sha2::Digest;
///
/// fn get_webc_volume() -> Volume {
/// /* ... */
/// # let dir = Directory::new(
/// # [
/// # (
/// # PathSegment::parse("path").unwrap(),
/// # Directory::new(
/// # [
/// # (
/// # PathSegment::parse("to").unwrap(),
/// # Directory::new(
/// # [
/// # (PathSegment::parse("file.txt").unwrap(), b"Hello, World!".to_vec().into()),
/// # ].into_iter().collect(),
/// # Timestamps::default(),
/// # ).into(),
/// # ),
/// # ].into_iter().collect(),
/// Timestamps::default(),
/// # ).into(),
/// # ),
/// # (
/// # PathSegment::parse("another.txt").unwrap(),
/// # b"Another".to_vec().into(),
/// # ),
/// # ].into_iter().collect(),
/// # Timestamps::default(),
/// # );
/// # let serialized = Writer::default()
/// # .write_manifest(&webc::metadata::Manifest::default()).unwrap()
/// # .write_atoms(std::collections::BTreeMap::new()).unwrap()
/// # .with_volume("my_volume", dir).unwrap()
/// # .finish(SignatureAlgorithm::None).unwrap();
/// # Container::from_bytes(serialized).unwrap().get_volume("my_volume").unwrap()
/// }
/// let another_hash: [u8; 32] = sha2::Sha256::digest(b"Another").into();
/// let file_hash: [u8; 32] = sha2::Sha256::digest(b"Hello, World!").into();
/// let to_hash: [u8; 32] = sha2::Sha256::digest(&file_hash).into();
/// let path_hash: [u8; 32] = sha2::Sha256::digest(&to_hash).into();
///
/// let volume = get_webc_volume();
/// // Accessing file content.
/// let (content, hash) = volume.read_file("/path/to/file.txt").unwrap();
/// assert_eq!(content, b"Hello, World!");
/// assert_eq!(hash, Some(file_hash));
/// // Inspect directories.
/// let timestamps = Some(webc::Timestamps::default());
/// let entries = volume.read_dir("/").unwrap();
///
/// assert_eq!(entries.len(), 2);
/// assert_eq!(entries[0], (
/// PathSegment::parse("another.txt").unwrap(),
/// Some(another_hash),
/// Metadata::File { length: 7, timestamps },
/// ));
/// assert_eq!(entries[1], (
/// PathSegment::parse("path").unwrap(),
/// Some(path_hash),
/// Metadata::Dir { timestamps },
/// ));
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Volume {
imp: Arc<dyn AbstractVolume + Send + Sync + 'static>,
}
impl Volume {
#[allow(dead_code)]
pub(crate) fn new(volume: impl AbstractVolume + Send + Sync + 'static) -> Self {
Volume {
imp: Arc::new(volume),
}
}
/// Get the metadata of an item at the given path.
///
/// Returns `None` if the item does not exist in the volume or an internal
/// error occurred.
pub fn metadata(&self, path: impl ToPathSegments) -> Option<Metadata> {
let path = path.to_path_segments().ok()?;
self.imp.metadata(&path)
}
/// Read the contents of a directory at the given path.
///
/// Returns a vector of directory entries, including their metadata, if the
/// path is a directory.
///
/// Returns `None` if the path does not exist or is not a directory.
pub fn read_dir(
&self,
path: impl ToPathSegments,
) -> Option<Vec<(PathSegment, Option<[u8; 32]>, Metadata)>> {
let path = path.to_path_segments().ok()?;
self.imp.read_dir(&path)
}
/// Read the contents of a file at the given path.
///
/// Returns `None` if the path is not valid or the file is not found.
pub fn read_file(&self, path: impl ToPathSegments) -> Option<(OwnedBuffer, Option<[u8; 32]>)> {
let path = path.to_path_segments().ok()?;
self.imp.read_file(&path)
}
/// Unpack a subdirectory of this volume into a local directory.
///
/// Use '/' as the volume_path to unpack the entire volume.
#[allow(clippy::result_large_err)]
pub fn unpack(
&self,
volume_path: impl ToPathSegments,
out_dir: &std::path::Path,
) -> Result<(), ContainerError> {
std::fs::create_dir_all(out_dir).map_err(|err| ContainerError::Open {
path: out_dir.to_path_buf(),
error: err,
})?;
let path = volume_path.to_path_segments()?;
for (name, _, entry) in self.read_dir(&path).unwrap_or_default() {
match entry {
Metadata::Dir { .. } => {
let out_nested = out_dir.join(name.as_str());
self.unpack(path.join(name), &out_nested)?;
}
Metadata::File { .. } => {
let out_path = out_dir.join(name.as_str());
let p = path.join(name.clone());
if let Some((f, _)) = self.read_file(p) {
std::fs::write(&out_path, f.as_slice()).map_err(|err| {
ContainerError::Open {
path: out_path,
error: err,
}
})?;
}
}
}
}
Ok(())
}
}
/// Metadata describing the properties of a file or directory.
#[derive(Debug, Copy, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum Metadata {
/// A directory
Dir {
/// Timestamps fo the directory
timestamps: Option<Timestamps>,
},
/// A file with a specified length.
File {
/// The number of bytes in this file.
length: usize,
/// Timestamps of the file
timestamps: Option<Timestamps>,
},
}
impl Metadata {
/// Returns `true` if the metadata represents a directory.
pub fn is_dir(self) -> bool {
matches!(self, Metadata::Dir { .. })
}
/// Returns `true` if the metadata represents a file.
pub fn is_file(self) -> bool {
matches!(self, Metadata::File { .. })
}
/// Returns the timestamps of the directory or file.
pub fn timestamps(&self) -> Option<Timestamps> {
let timestamps = match self {
Metadata::Dir { timestamps } => timestamps,
Metadata::File { timestamps, .. } => timestamps,
};
*timestamps
}
/// Returnes mutable ref to the timestamps of the directory or file.
pub fn timestamps_mut(&mut self) -> Option<&mut Timestamps> {
let timestamps = match self {
Metadata::Dir { timestamps } => timestamps.as_mut(),
Metadata::File { timestamps, .. } => timestamps.as_mut(),
};
timestamps
}
}
// TODO: This will probably need redesigning later on to make `DirEntry`
// "remember" where it is within a directory structure. The current design
// turns directory walking into an O(n²) operation.
pub(crate) trait AbstractVolume: Debug {
fn metadata(&self, path: &PathSegments) -> Option<Metadata>;
fn read_dir(
&self,
path: &PathSegments,
) -> Option<Vec<(PathSegment, Option<[u8; 32]>, Metadata)>>;
fn read_file(&self, path: &PathSegments) -> Option<(OwnedBuffer, Option<[u8; 32]>)>;
}
#[cfg(feature = "v2")]
mod v2 {
use super::*;
impl AbstractVolume for crate::v2::read::VolumeSection {
fn metadata(&self, path: &PathSegments) -> Option<Metadata> {
let entry = self.header().find(path).ok().flatten()?;
Some(v2_metadata(&entry))
}
fn read_dir(
&self,
path: &PathSegments,
) -> Option<Vec<(PathSegment, Option<[u8; 32]>, Metadata)>> {
let meta = self
.header()
.find(path)
.ok()
.flatten()
.and_then(|entry| entry.into_dir())?;
let mut entries = Vec::new();
for (name, entry) in meta.entries().flatten() {
let segment: PathSegment = name.parse().unwrap();
let meta = v2_metadata(&entry);
entries.push((segment, None, meta));
}
Some(entries)
}
fn read_file(&self, path: &PathSegments) -> Option<(OwnedBuffer, Option<[u8; 32]>)> {
self.lookup_file(path).map(|b| (b.into(), None)).ok()
}
}
fn v2_metadata(header_entry: &crate::v2::read::volume_header::HeaderEntry<'_>) -> Metadata {
match header_entry {
crate::v2::read::volume_header::HeaderEntry::Directory(_) => {
Metadata::Dir { timestamps: None }
}
crate::v2::read::volume_header::HeaderEntry::File(
crate::v2::read::volume_header::FileMetadata {
start_offset,
end_offset,
..
},
) => Metadata::File {
length: end_offset - start_offset,
timestamps: None,
},
}
}
}
#[cfg(feature = "v3")]
mod v3 {
use super::*;
impl AbstractVolume for crate::v3::read::VolumeSection {
fn metadata(&self, path: &PathSegments) -> Option<Metadata> {
let (entry, _) = self.header().find(path).ok().flatten()?;
Some(v3_metadata(&entry))
}
fn read_dir(
&self,
path: &PathSegments,
) -> Option<Vec<(PathSegment, Option<[u8; 32]>, Metadata)>> {
let meta = self
.header()
.find(path)
.ok()
.flatten()
.and_then(|(entry, _)| entry.into_dir())?;
let mut entries = Vec::new();
for (name, hash, entry) in meta.entries().flatten() {
let segment: PathSegment = name.parse().unwrap();
let meta = v3_metadata(&entry);
entries.push((segment, Some(hash), meta));
}
Some(entries)
}
fn read_file(&self, path: &PathSegments) -> Option<(OwnedBuffer, Option<[u8; 32]>)> {
self.lookup_file(path)
.map(|(b, h)| (b.into(), Some(h)))
.ok()
}
}
fn v3_metadata(header_entry: &crate::v3::read::volume_header::HeaderEntry<'_>) -> Metadata {
match header_entry {
crate::v3::read::volume_header::HeaderEntry::Directory(dir) => Metadata::Dir {
timestamps: Some(dir.timestamps().into()),
},
crate::v3::read::volume_header::HeaderEntry::File(
crate::v3::read::volume_header::FileMetadata {
start_offset,
end_offset,
timestamps,
..
},
) => Metadata::File {
length: end_offset - start_offset,
timestamps: Some((*timestamps).into()),
},
}
}
}
#[cfg(feature = "v1")]
pub(crate) mod v1 {
use super::*;
#[derive(Debug)]
pub(crate) struct VolumeV1 {
// SAFETY: order is important here because volume has references into
// bytes.
pub(crate) volume: crate::v1::Volume<'static>,
pub(crate) buffer: OwnedBuffer,
}
impl VolumeV1 {
fn get_shared(&self, needle: &[u8]) -> OwnedBuffer {
if needle.is_empty() {
return OwnedBuffer::new();
}
let range = crate::utils::subslice_offsets(&self.buffer, needle);
self.buffer.slice(range)
}
}
impl AbstractVolume for VolumeV1 {
fn metadata(&self, path: &PathSegments) -> Option<Metadata> {
let path = path.to_string();
if let Ok(bytes) = self.volume.get_file(&path) {
return Some(Metadata::File {
length: bytes.len(),
timestamps: None,
});
}
if self.volume.read_dir(&path).is_ok() {
return Some(Metadata::Dir { timestamps: None });
}
None
}
fn read_dir(
&self,
path: &PathSegments,
) -> Option<Vec<(PathSegment, Option<[u8; 32]>, Metadata)>> {
let path = path.to_string();
let mut entries = Vec::new();
for entry in self.volume.read_dir(&path).ok()? {
let name: PathSegment = match entry.text.parse() {
Ok(p) => p,
Err(_) => continue,
};
let meta = v1_metadata(&entry);
entries.push((name, None, meta));
}
Some(entries)
}
fn read_file(&self, path: &PathSegments) -> Option<(OwnedBuffer, Option<[u8; 32]>)> {
let path = path.to_string();
let bytes = self.volume.get_file(&path).ok()?;
let owned_bytes = self.get_shared(bytes);
Some((owned_bytes, None))
}
}
fn v1_metadata(entry: &crate::v1::FsEntry<'_>) -> Metadata {
match entry.fs_type {
crate::v1::FsEntryType::File => Metadata::File {
length: entry.get_len().try_into().unwrap(),
timestamps: None,
},
crate::v1::FsEntryType::Dir => Metadata::Dir { timestamps: None },
}
}
}
#[cfg(feature = "package")]
mod package {
use super::*;
impl AbstractVolume for crate::wasmer_package::Volume {
fn metadata(&self, path: &PathSegments) -> Option<Metadata> {
self.metadata(path)
}
fn read_dir(
&self,
path: &PathSegments,
) -> Option<Vec<(PathSegment, Option<[u8; 32]>, Metadata)>> {
self.read_dir(path)
}
fn read_file(&self, path: &PathSegments) -> Option<(OwnedBuffer, Option<[u8; 32]>)> {
// FIXME: Package impl should be checked
self.read_file(path).map(|b| (b, None))
}
}
}
/// Errors that may occur when doing [`Volume`] operations.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum VolumeError {
/// The item wasn't found.
#[error("The item wasn't found")]
NotFound,
/// The provided path wasn't valid.
#[error("Invalid path")]
Path(#[from] PathSegmentError),
/// A non-directory was found where a directory was expected.
#[error("Not a directory")]
NotADirectory,
/// A non-file was found where a file was expected.
#[error("Not a file")]
NotAFile,
}
#[cfg(test)]
mod tests {
#[cfg(feature = "v1")]
mod v1 {
use super::super::*;
use crate::{v1::DirOrFile, volume::v1::VolumeV1};
use std::collections::BTreeMap;
fn owned_volume_v1(entries: BTreeMap<DirOrFile, Vec<u8>>) -> VolumeV1 {
let bytes = crate::v1::Volume::serialize_files(entries);
let borrowed_volume = crate::v1::Volume::parse(&bytes).unwrap();
VolumeV1 {
// SAFETY: We need to transmute the lifetime away.
volume: unsafe { std::mem::transmute(borrowed_volume) },
buffer: bytes.into(),
}
}
#[test]
fn v1_owned() {
let mut dir = BTreeMap::new();
dir.insert(DirOrFile::Dir("/path".into()), Vec::new());
dir.insert(DirOrFile::Dir("/path/to".into()), Vec::new());
dir.insert(
DirOrFile::File("/path/to/file.txt".into()),
b"Hello, World!".to_vec(),
);
dir.insert(DirOrFile::Dir("/path/to".into()), Vec::new());
dir.insert(DirOrFile::File("/another.txt".into()), b"Another".to_vec());
let volume = owned_volume_v1(dir);
let volume = Volume::new(volume);
assert!(volume.read_file("").is_none());
assert_eq!(
volume.read_file("/another.txt").unwrap(),
(b"Another".as_slice().into(), None)
);
assert_eq!(
volume.metadata("/another.txt").unwrap(),
Metadata::File {
length: 7,
timestamps: None
}
);
assert_eq!(
volume.read_file("/path/to/file.txt").unwrap(),
(b"Hello, World!".as_slice().into(), None),
);
assert_eq!(
volume.metadata("/path/to").unwrap(),
Metadata::Dir { timestamps: None },
);
assert_eq!(
volume.read_dir("/").unwrap(),
vec![
(
PathSegment::parse("another.txt").unwrap(),
None,
Metadata::File {
length: 7,
timestamps: None
}
),
(
PathSegment::parse("path").unwrap(),
None,
Metadata::Dir { timestamps: None }
),
],
);
assert_eq!(
volume.read_dir("/path").unwrap(),
vec![(
PathSegment::parse("to").unwrap(),
None,
Metadata::Dir { timestamps: None }
)],
);
assert_eq!(
volume.read_dir("/path/to/").unwrap(),
vec![(
PathSegment::parse("file.txt").unwrap(),
None,
Metadata::File {
length: 13,
timestamps: None
}
)],
);
}
}
#[cfg(feature = "v3")]
mod v3 {
use sha2::Digest;
use super::super::*;
use crate::{metadata::Manifest, v3::write::Writer};
use std::collections::BTreeMap;
fn v3_volume(
volume: crate::v3::write::Directory<'static>,
) -> crate::v3::read::VolumeSection {
let manifest = Manifest::default();
let mut writer = Writer::default()
.write_manifest(&manifest)
.unwrap()
.write_atoms(BTreeMap::new())
.unwrap();
writer.write_volume("volume", volume).unwrap();
let serialized = writer.finish(crate::v3::SignatureAlgorithm::None).unwrap();
let reader = crate::v3::read::OwnedReader::parse(serialized).unwrap();
reader.get_volume("volume").unwrap()
}
#[test]
fn v3() {
let dir = dir_map! {
"path" => dir_map! {
"to" => dir_map! {
"file.txt" => b"Hello, World!",
}
},
"another.txt" => b"Another",
};
let timestamps = Some(Timestamps::default());
let volume = v3_volume(dir);
let volume = Volume::new(volume);
let another_hash: [u8; 32] = sha2::Sha256::digest(b"Another").into();
let file_hash: [u8; 32] = sha2::Sha256::digest(b"Hello, World!").into();
let to_hash: [u8; 32] = sha2::Sha256::digest(&file_hash).into();
let path_hash: [u8; 32] = sha2::Sha256::digest(&to_hash).into();
assert!(volume.read_file("").is_none());
assert_eq!(
volume.read_file("/another.txt").unwrap(),
(b"Another".as_slice().into(), Some(another_hash))
);
assert_eq!(
volume.metadata("/another.txt").unwrap(),
Metadata::File {
length: 7,
timestamps
}
);
assert_eq!(
volume.read_file("/path/to/file.txt").unwrap(),
(b"Hello, World!".as_slice().into(), Some(file_hash)),
);
assert_eq!(
volume.read_dir("/").unwrap(),
vec![
(
PathSegment::parse("another.txt").unwrap(),
Some(another_hash),
Metadata::File {
length: 7,
timestamps
},
),
(
PathSegment::parse("path").unwrap(),
Some(path_hash),
Metadata::Dir { timestamps }
),
],
);
assert_eq!(
volume.read_dir("/path").unwrap(),
vec![(
PathSegment::parse("to").unwrap(),
Some(to_hash),
Metadata::Dir { timestamps }
)],
);
assert_eq!(
volume.read_dir("/path/to/").unwrap(),
vec![(
PathSegment::parse("file.txt").unwrap(),
Some(file_hash),
Metadata::File {
length: 13,
timestamps
}
)],
);
}
}
}