// Load a chunk from its temporary storage file
Load a chunk from its temporary storage file
Load a chunk from its temporary storage file
pub fn load_chunk(prefix: &str, hash: &str, index: u32) -> Result<Vec<u8>, ProtocolError> {
    let mut data = vec![];
    if let (_, Some(chunk_size), Some(path)) = load_meta(prefix, hash)? {
        // let path = Path::new(&format!("{}/storage", prefix))
        //     .join(hash)
        //     .join(format!("{}", index));
        let mut file = File::open(&path).map_err(|err| ProtocolError::StorageError {
            action: format!("open chunk file {}", index),
            err,
        })?;
        file.seek(SeekFrom::Start(chunk_size * index as u64))
            .map_err(|err| ProtocolError::StorageError {
                action: format!("seek to chunk in file {}", &path),
                err,
            })?;
        file.take(chunk_size)
            .read_to_end(&mut data)
            .map_err(|err| ProtocolError::StorageError {
                action: format!("read chunk file {}", index),
                err,
            })?;
    } else {
        let path = Path::new(&format!("{}/storage", prefix))
            .join(hash)
            .join(format!("{}", index));
        File::open(path)
            .map_err(|err| ProtocolError::StorageError {
                action: format!("open chunk file {}", index),
                err,
            })?
            .read_to_end(&mut data)
            .map_err(|err| ProtocolError::StorageError {
                action: format!("read chunk file {}", index),
                err,
            })?;
    }
    Ok(data)
}
pub fn load_chunk ( prefix : & str , hash : & str , index : u32 ) -> Result < Vec < u8 > , ProtocolError > { let mut data = vec ! [ ] ; if let ( _ , Some ( chunk_size ) , Some ( path ) ) = load_meta ( prefix , hash ) ? { // let path = Path::new(&format!("{}/storage", prefix)) //     .join(hash) //     .join(format!("{}", index)); let mut file = File :: open ( & path ) . map_err ( | err | ProtocolError :: StorageError { action : format ! ( " " index ) , err , } ) ? ; file . seek ( SeekFrom :: Start ( chunk_size * index as u64 ) ) . map_err ( | err | ProtocolError :: StorageError { action : format ! ( " " path ) , err , } ) ? ; file . take ( chunk_size ) . read_to_end ( & mut data ) . map_err ( | err | ProtocolError :: StorageError { action : format ! ( " " index ) , err , } ) ? ; } else { let path = Path :: new ( & format ! ( " " prefix ) ) . join ( hash ) . join ( format ! ( " " index ) ) ; File :: open ( path ) . map_err ( | err | ProtocolError :: StorageError { action : format ! ( " " index ) , err , } ) ? . read_to_end ( & mut data ) . map_err ( | err | ProtocolError :: StorageError { action : format ! ( " " index ) , err , } ) ? ; } Ok ( data ) }
Load a chunk from its temporary storage file
Load a chunk from its temporary storage file
prefix
&str
hash
&str
index
u32