pub fn add_to_index(filepath: &str) -> Result<(), String> {
    let (_header, mut items) = index_util::parse_index()?;

    // filepath should be normalized to avoid false negatives
    for item in items.iter() {
        if filepath == item.filename {
            return Err("This file already exists in the index".to_string());
        }
    }

    items = update_index_items(items);

    let object_hash = write_hash_object(filepath);

    let mut ctime = 0u32;
    let mut mtime = 0u32;
    let mut size = 0u32;

    if let Ok(metadata) = fs::metadata(filepath) {
        ctime = metadata
            .created()
            .unwrap_or_else(|_| SystemTime::now())
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_else(|_| Duration::new(0, 0))
            .as_secs() as u32;
        mtime = metadata
            .modified()
            .unwrap_or_else(|_| SystemTime::now())
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_else(|_| Duration::new(0, 0))
            .as_secs() as u32;
        size = metadata.len() as u32;
    }

    let new_item = IndexFile {
        ctime,
        ctime_fractions: 0,
        mtime,
        mtime_fractions: 0,
        dev: None,
        ino: None,
        permissions: "644".to_string(),
        uid: None,
        gid: None,
        size,
        object_hash,
        filename: filepath.to_string(),
    };
    items.push(new_item);

    match index_util::write_index(items) {
        Ok(_) => Ok(()),
        Err(e) => Err(e.to_string()),
    }
}
/// Add specified file to the index and update existing items
///
/// Will fail for duplicate items
Add specified file to the index and update existing items
Will fail for duplicate items
Add specified file to the index and update existing items Will fail for duplicate items
pub fn add_to_index(filepath: &str) -> Result<(), String> {
    let (_header, mut items) = index_util::parse_index()?;
    // filepath should be normalized to avoid false negatives
    for item in items.iter() {
        if filepath == item.filename {
            return Err("This file already exists in the index".to_string());
        }
    }
    items = update_index_items(items);
    let object_hash = write_hash_object(filepath);
    let mut ctime = 0u32;
    let mut mtime = 0u32;
    let mut size = 0u32;
    if let Ok(metadata) = fs::metadata(filepath) {
        ctime = metadata
            .created()
            .unwrap_or_else(|_| SystemTime::now())
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_else(|_| Duration::new(0, 0))
            .as_secs() as u32;
        mtime = metadata
            .modified()
            .unwrap_or_else(|_| SystemTime::now())
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or_else(|_| Duration::new(0, 0))
            .as_secs() as u32;
        size = metadata.len() as u32;
    }
    let new_item = IndexFile {
        ctime,
        ctime_fractions: 0,
        mtime,
        mtime_fractions: 0,
        dev: None,
        ino: None,
        permissions: "644".to_string(),
        uid: None,
        gid: None,
        size,
        object_hash,
        filename: filepath.to_string(),
    };
    items.push(new_item);
    match index_util::write_index(items) {
        Ok(_) => Ok(()),
        Err(e) => Err(e.to_string()),
    }
}
pub fn add_to_index ( filepath : & str ) -> Result < ( ) , String > { let ( _header , mut items ) = index_util :: parse_index ( ) ? ; // filepath should be normalized to avoid false negatives for item in items . iter ( ) { if filepath == item . filename { return Err ( " " . to_string ( ) ) ; } } items = update_index_items ( items ) ; let object_hash = write_hash_object ( filepath ) ; let mut ctime = 0u32 ; let mut mtime = 0u32 ; let mut size = 0u32 ; if let Ok ( metadata ) = fs :: metadata ( filepath ) { ctime = metadata . created ( ) . unwrap_or_else ( | _ | SystemTime :: now ( ) ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap_or_else ( | _ | Duration :: new ( 0 , 0 ) ) . as_secs ( ) as u32 ; mtime = metadata . modified ( ) . unwrap_or_else ( | _ | SystemTime :: now ( ) ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap_or_else ( | _ | Duration :: new ( 0 , 0 ) ) . as_secs ( ) as u32 ; size = metadata . len ( ) as u32 ; } let new_item = IndexFile { ctime , ctime_fractions : 0 , mtime , mtime_fractions : 0 , dev : None , ino : None , permissions : " " . to_string ( ) , uid : None , gid : None , size , object_hash , filename : filepath . to_string ( ) , } ; items . push ( new_item ) ; match index_util :: write_index ( items ) { Ok ( _ ) => Ok ( ( ) ) , Err ( e ) => Err ( e . to_string ( ) ) , } }
Add specified file to the index and update existing items
Will fail for duplicate items
Add specified file to the index and update existing items Will fail for duplicate items
filepath
&str
filepath
&str