pub fn validate_file(
    prefix: &str,
    hash: &str,
    num_chunks: Option<u32>,
) -> Result<(bool, Vec<u32>), ProtocolError> {
    let num_chunks = if let Some(num) = num_chunks {
        store_meta(prefix, hash, num, None, None)?;
        num
    } else {
        let (num, ..) = load_meta(prefix, hash)?;
        num
    };

    let mut missing_ranges: Vec<u32> = vec![];

    let hash_path = Path::new(&format!("{}/storage", prefix)).join(hash);

    let mut prev_entry: i32 = -1;

    let entries = fs::read_dir(hash_path.clone()).map_err(|err| ProtocolError::StorageError {
        action: format!("read {:?} directory", hash_path),
        err,
    })?;

    let mut converted_entries: Vec<i32> = entries
        .filter_map(|entry| entry.ok())
        .filter_map(|entry| {
            match entry
                .file_name()
                .into_string()
                .map_err(|err| {
                    ProtocolError::StorageParseError(format!(
                        "Failed to parse file name: {:?}",
                        err
                    ))
                })
                .and_then(|val| {
                    val.parse::<i32>().map_err(|err| {
                        ProtocolError::StorageParseError(format!(
                            "Failed to parse chunk_number {:?}",
                            err
                        ))
                    })
                }) {
                Ok(num) => Some(num),
                _ => None,
            }
        })
        .collect();

    converted_entries.sort();

    let mut max_entries = 186;
    for &entry_num in converted_entries.iter() {
        //println!("checking {} vs {}", entry_num, prev_entry);
        // Check for non-sequential dir entries to detect missing chunk ranges
        if entry_num - prev_entry > 1 {
            // Add start of range (inclusive)
            missing_ranges.push((prev_entry + 1) as u32);
            // Add end of range (non-inclusive)
            missing_ranges.push(entry_num as u32);

            max_entries -= 1;

            if max_entries == 0 {
                break;
            }
        }

        prev_entry = entry_num;
    }

    // Check for a trailing range
    // Ex. Last known chunk is 5, but there are 10 chunks.
    //     We will already have added '6', so we need to add '10'
    //     to close it out.
    if max_entries != 0 && (num_chunks as i32) - prev_entry != 1 {
        // Add start of range
        missing_ranges.push((prev_entry + 1) as u32);
        // Add end of range
        missing_ranges.push(num_chunks as u32);
    }

    Ok((missing_ranges.is_empty(), missing_ranges))
}
// Check if all of a files chunks are present in the temporary directory
Check if all of a files chunks are present in the temporary directory
Check if all of a files chunks are present in the temporary directory
pub fn validate_file(
    prefix: &str,
    hash: &str,
    num_chunks: Option<u32>,
) -> Result<(bool, Vec<u32>), ProtocolError> {
    let num_chunks = if let Some(num) = num_chunks {
        store_meta(prefix, hash, num, None, None)?;
        num
    } else {
        let (num, ..) = load_meta(prefix, hash)?;
        num
    };
    let mut missing_ranges: Vec<u32> = vec![];
    let hash_path = Path::new(&format!("{}/storage", prefix)).join(hash);
    let mut prev_entry: i32 = -1;
    let entries = fs::read_dir(hash_path.clone()).map_err(|err| ProtocolError::StorageError {
        action: format!("read {:?} directory", hash_path),
        err,
    })?;
    let mut converted_entries: Vec<i32> = entries
        .filter_map(|entry| entry.ok())
        .filter_map(|entry| {
            match entry
                .file_name()
                .into_string()
                .map_err(|err| {
                    ProtocolError::StorageParseError(format!(
                        "Failed to parse file name: {:?}",
                        err
                    ))
                })
                .and_then(|val| {
                    val.parse::<i32>().map_err(|err| {
                        ProtocolError::StorageParseError(format!(
                            "Failed to parse chunk_number {:?}",
                            err
                        ))
                    })
                }) {
                Ok(num) => Some(num),
                _ => None,
            }
        })
        .collect();
    converted_entries.sort();
    let mut max_entries = 186;
    for &entry_num in converted_entries.iter() {
        //println!("checking {} vs {}", entry_num, prev_entry);
        // Check for non-sequential dir entries to detect missing chunk ranges
        if entry_num - prev_entry > 1 {
            // Add start of range (inclusive)
            missing_ranges.push((prev_entry + 1) as u32);
            // Add end of range (non-inclusive)
            missing_ranges.push(entry_num as u32);
            max_entries -= 1;
            if max_entries == 0 {
                break;
            }
        }
        prev_entry = entry_num;
    }
    // Check for a trailing range
    // Ex. Last known chunk is 5, but there are 10 chunks.
    //     We will already have added '6', so we need to add '10'
    //     to close it out.
    if max_entries != 0 && (num_chunks as i32) - prev_entry != 1 {
        // Add start of range
        missing_ranges.push((prev_entry + 1) as u32);
        // Add end of range
        missing_ranges.push(num_chunks as u32);
    }
    Ok((missing_ranges.is_empty(), missing_ranges))
}
pub fn validate_file ( prefix : & str , hash : & str , num_chunks : Option < u32 > , ) -> Result < ( bool , Vec < u32 > ) , ProtocolError > { let num_chunks = if let Some ( num ) = num_chunks { store_meta ( prefix , hash , num , None , None ) ? ; num } else { let ( num , .. ) = load_meta ( prefix , hash ) ? ; num } ; let mut missing_ranges : Vec < u32 > = vec ! [ ] ; let hash_path = Path :: new ( & format ! ( " " prefix ) ) . join ( hash ) ; let mut prev_entry : i32 = - 1 ; let entries = fs :: read_dir ( hash_path . clone ( ) ) . map_err ( | err | ProtocolError :: StorageError { action : format ! ( " " hash_path ) , err , } ) ? ; let mut converted_entries : Vec < i32 > = entries . filter_map ( | entry | entry . ok ( ) ) . filter_map ( | entry | { match entry . file_name ( ) . into_string ( ) . map_err ( | err | { ProtocolError :: StorageParseError ( format ! ( " " err ) ) } ) . and_then ( | val | { val . parse :: < i32 > ( ) . map_err ( | err | { ProtocolError :: StorageParseError ( format ! ( " " err ) ) } ) } ) { Ok ( num ) => Some ( num ) , _ => None , } } ) . collect ( ) ; converted_entries . sort ( ) ; let mut max_entries = 186 ; for & entry_num in converted_entries . iter ( ) { //println!("checking {} vs {}", entry_num, prev_entry); // Check for non-sequential dir entries to detect missing chunk ranges if entry_num - prev_entry > 1 { // Add start of range (inclusive) missing_ranges . push ( ( prev_entry + 1 ) as u32 ) ; // Add end of range (non-inclusive) missing_ranges . push ( entry_num as u32 ) ; max_entries -= 1 ; if max_entries == 0 { break ; } } prev_entry = entry_num ; } // Check for a trailing range // Ex. Last known chunk is 5, but there are 10 chunks. //     We will already have added '6', so we need to add '10' //     to close it out. if max_entries != 0 && ( num_chunks as i32 ) - prev_entry != 1 { // Add start of range missing_ranges . push ( ( prev_entry + 1 ) as u32 ) ; // Add end of range missing_ranges . push ( num_chunks as u32 ) ; } Ok ( ( missing_ranges . is_empty ( ) , missing_ranges ) ) }
Check if all of a files chunks are present in the temporary directory
Check if all of a files chunks are present in the temporary directory
prefix
&str
hash
&str
num_chunks
Option<u32>
prefix
&str
hash
&str
num_chunks
Option<u32>