Skip to content

Commit

Permalink
style: add some error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
daiyongxuan committed Oct 27, 2024
1 parent bf8a535 commit cf0fe99
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 47 deletions.
56 changes: 10 additions & 46 deletions builder/src/chunkdict_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use sha2::digest::Update;

use crate::finalize_blob;
use crate::Artifact;
use core::panic;
use std::fs::File;
use std::io::Read;
use std::io::Seek;
Expand Down Expand Up @@ -69,39 +68,6 @@ pub struct ChunkdictBlobInfo {
pub blob_meta_ci_offset: u64,
}

#[derive(Debug)]
pub struct BlobNodeReader {
blob: Arc<File>,
end: u64,
position: u64,
}

impl BlobNodeReader {
pub fn new(blob: Arc<File>, start: u64, end: u64) -> Result<Self> {
let mut reader = BlobNodeReader {
blob,
end,
position: start,
};
reader.blob.seek(std::io::SeekFrom::Start(start))?;
Ok(reader)
}
}

impl Read for BlobNodeReader {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
// EOF
if self.position > self.end {
return std::io::Result::Ok(0);
}
let max_read = (self.end - self.position) as usize;
let to_read = std::cmp::min(buf.len(), max_read);
let bytes_read = self.blob.read(&mut buf[..to_read])?;
self.position += bytes_read as u64;
std::io::Result::Ok(bytes_read)
}
}

/// Struct to generate chunkdict RAFS bootstrap.
pub struct Generator {}

Expand All @@ -126,8 +92,8 @@ impl PrefetchBlobState {
blob_info.set_compressor(ctx.compressor);
let mut blob_ctx = BlobContext::from(ctx, &blob_info, ChunkSource::Build)?;
blob_ctx.blob_meta_info_enabled = true;
let blob_writer = ArtifactWriter::new(crate::ArtifactStorage::SingleFile(
blobs_dir_path.join("prefetch-blob"),
let blob_writer = ArtifactWriter::new(crate::ArtifactStorage::FileDir(
blobs_dir_path.to_path_buf(),
))
.map(|writer| Box::new(writer) as Box<dyn Artifact>)?;
Ok(Self {
Expand Down Expand Up @@ -317,7 +283,7 @@ impl Generator {
.map(|entry| entry.blob_id())
.unwrap()
};
let blob_file = Arc::new(File::open(blobs_dir_path.join(blob_id)).unwrap());
let mut blob_file = Arc::new(File::open(blobs_dir_path.join(blob_id)).unwrap());
{
let mut child = tree_node.borrow_mut();
child.layer_idx = prefetch_state.blob_info.blob_index() as u16;
Expand All @@ -332,15 +298,13 @@ impl Generator {

for chunk in chunks {
let inner = Arc::make_mut(&mut chunk.inner);
let mut reader = BlobNodeReader::new(
Arc::clone(&blob_file),
inner.compressed_offset(),
inner.compressed_offset() + inner.compressed_size() as u64,
)
.unwrap();
let buf = &mut vec![0u8; inner.compressed_size() as usize];
reader.read_exact(buf).unwrap();
prefetch_state.blob_writer.write_all(buf).unwrap();

let mut buf = vec![0u8; inner.compressed_size() as usize];
blob_file
.seek(std::io::SeekFrom::Start(inner.compressed_offset()))
.unwrap();
blob_file.read_exact(&mut buf).unwrap();
prefetch_state.blob_writer.write_all(&buf).unwrap();
let info = batch
.generate_chunk_info(
blob_ctx.current_compressed_offset,
Expand Down
3 changes: 2 additions & 1 deletion src/bin/nydus-image/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,8 @@ impl Command {
blobs_dir_path.to_path_buf(),
prefetch_nodes,
)
.unwrap();
.with_context(|| "Failed to generate prefetch bootstrap")?;

Ok(())
}

Expand Down

0 comments on commit cf0fe99

Please sign in to comment.