pub fn initialize_file(
    prefix: &str,
    source_path: &str,
    transfer_chunk_size: usize,
    hash_chunk_size: usize,
) -> Result<(String, u32, u32), ProtocolError> {
    let storage_path = format!("{}/storage", prefix);

    // Confirm file exists
    let metadata = fs::metadata(source_path).map_err(|err| ProtocolError::StorageError {
        action: format!("stat file {}", source_path),
        err,
    })?;

    // Create necessary storage directory
    fs::create_dir_all(&storage_path).map_err(|err| ProtocolError::StorageError {
        action: format!("create dir {}", storage_path),
        err,
    })?;

    // Calculate hash of temp file
    let hash = calc_file_hash(&source_path, hash_chunk_size)?;

    let file_size = metadata.len() as u64;
    let index = (file_size / transfer_chunk_size as u64) as u32
        + ((file_size % transfer_chunk_size as u64) > 0) as u32;

    store_meta(
        prefix,
        &hash,
        index,
        Some(transfer_chunk_size as u64),
        Some(&source_path),
    )?;

    if let Ok(meta) = fs::metadata(source_path) {
        Ok((hash, index, meta.mode()))
    } else {
        Ok((hash, index, 0o644))
    }
}
/// Create temporary folder for chunks
/// Stream copy file from mutable space to immutable space
/// Move folder to hash of contents
/// Import file into chunked storage for transfer
Create temporary folder for chunks
Stream copy file from mutable space to immutable space
Move folder to hash of contents
Import file into chunked storage for transfer
Create temporary folder for chunks Stream copy file from mutable space to immutable space Move folder to hash of contents Import file into chunked storage for transfer
pub fn initialize_file(
    prefix: &str,
    source_path: &str,
    transfer_chunk_size: usize,
    hash_chunk_size: usize,
) -> Result<(String, u32, u32), ProtocolError> {
    let storage_path = format!("{}/storage", prefix);
    // Confirm file exists
    let metadata = fs::metadata(source_path).map_err(|err| ProtocolError::StorageError {
        action: format!("stat file {}", source_path),
        err,
    })?;
    // Create necessary storage directory
    fs::create_dir_all(&storage_path).map_err(|err| ProtocolError::StorageError {
        action: format!("create dir {}", storage_path),
        err,
    })?;
    // Calculate hash of temp file
    let hash = calc_file_hash(&source_path, hash_chunk_size)?;
    let file_size = metadata.len() as u64;
    let index = (file_size / transfer_chunk_size as u64) as u32
        + ((file_size % transfer_chunk_size as u64) > 0) as u32;
    store_meta(
        prefix,
        &hash,
        index,
        Some(transfer_chunk_size as u64),
        Some(&source_path),
    )?;
    if let Ok(meta) = fs::metadata(source_path) {
        Ok((hash, index, meta.mode()))
    } else {
        Ok((hash, index, 0o644))
    }
}
pub fn initialize_file ( prefix : & str , source_path : & str , transfer_chunk_size : usize , hash_chunk_size : usize , ) -> Result < ( String , u32 , u32 ) , ProtocolError > { let storage_path = format ! ( " " prefix ) ; // Confirm file exists let metadata = fs :: metadata ( source_path ) . map_err ( | err | ProtocolError :: StorageError { action : format ! ( " " source_path ) , err , } ) ? ; // Create necessary storage directory fs :: create_dir_all ( & storage_path ) . map_err ( | err | ProtocolError :: StorageError { action : format ! ( " " storage_path ) , err , } ) ? ; // Calculate hash of temp file let hash = calc_file_hash ( & source_path , hash_chunk_size ) ? ; let file_size = metadata . len ( ) as u64 ; let index = ( file_size / transfer_chunk_size as u64 ) as u32 + ( ( file_size % transfer_chunk_size as u64 ) > 0 ) as u32 ; store_meta ( prefix , & hash , index , Some ( transfer_chunk_size as u64 ) , Some ( & source_path ) , ) ? ; if let Ok ( meta ) = fs :: metadata ( source_path ) { Ok ( ( hash , index , meta . mode ( ) ) ) } else { Ok ( ( hash , index , 0o644 ) ) } }
Create temporary folder for chunks
Stream copy file from mutable space to immutable space
Move folder to hash of contents
Import file into chunked storage for transfer
Create temporary folder for chunks Stream copy file from mutable space to immutable space Move folder to hash of contents Import file into chunked storage for transfer
prefix
&str
source_path
&str
transfer_chunk_size
usize
hash_chunk_size
usize
prefix
&str
source_path
&str
transfer_chunk_size
usize
hash_chunk_size
usize