pub fn sync(&self) -> Result<()> {
        // if we have no keys, don't overwrite the authorized_keys file.
        // if the user wants to delete all their ssh keys, we won't help them
        if self.keys.is_empty() {
            return Err(ErrorKind::NoKeysFound(format!("{:?}", self.authorized_keys_dir())).into());
        }

        // switch users
        let _guard = switch_user(&self.user)?;

        // get our staging directory
        let stage_filename = self.stage_file();
        let mut stage_file = File::create(&stage_filename).chain_err(|| {
            format!(
                "failed to create or truncate staging file '{:?}'",
                stage_filename
            )
        })?;

        // note that this file is auto-generated
        writeln!(stage_file, "# auto-generated by update-ssh-keys")
            .chain_err(|| format!("failed to write to file '{:?}'", stage_filename))?;

        // write all the keys to the staging file
        for keyset in self.keys.values() {
            // if the keyset is disabled, skip it
            if keyset.disabled {
                continue;
            }
            for key in &keyset.keys {
                // only write the key to authorized_keys if it is valid
                if let AuthorizedKeyEntry::Valid { ref key } = *key {
                    writeln!(stage_file, "{}", key)
                        .chain_err(|| format!("failed to write to file '{:?}'", stage_filename))?;
                }
            }
        }

        stage_file
            .sync_all()
            .chain_err(|| format!("failed to sync file '{:?}'", stage_filename))?;
        drop(stage_file);

        // destroy the old authorized keys file and move the staging one to that
        // location
        fs::rename(&stage_filename, &self.authorized_keys_file()).chain_err(|| {
            format!(
                "failed to move '{:?}' to '{:?}'",
                stage_filename,
                self.authorized_keys_file()
            )
        })?;

        let parent_path = stage_filename.parent().ok_or_else(|| {
            format!(
                "failed to sync parent directory of '{}'",
                stage_filename.display()
            )
        })?;
        let parent_dir_file = File::open(parent_path)
            .chain_err(|| format!("failed to open '{}' for syncing", parent_path.display()))?;
        parent_dir_file
            .sync_all()
            .chain_err(|| format!("failed to sync '{}'", parent_path.display()))?;

        Ok(())
    }
/// sync writes all the keys we have to authorized_keys. it writes the
/// current state to a staging file and then moves that staging file to the
/// authorized_keys path
sync writes all the keys we have to authorized_keys. it writes the
current state to a staging file and then moves that staging file to the
authorized_keys path
sync writes all the keys we have to authorized_keys . it writes the current state to a staging file and then moves that staging file to the authorized_keys path
pub fn sync(&self) -> Result<()> {
        // if we have no keys, don't overwrite the authorized_keys file.
        // if the user wants to delete all their ssh keys, we won't help them
        if self.keys.is_empty() {
            return Err(ErrorKind::NoKeysFound(format!("{:?}", self.authorized_keys_dir())).into());
        }
        // switch users
        let _guard = switch_user(&self.user)?;
        // get our staging directory
        let stage_filename = self.stage_file();
        let mut stage_file = File::create(&stage_filename).chain_err(|| {
            format!(
                "failed to create or truncate staging file '{:?}'",
                stage_filename
            )
        })?;
        // note that this file is auto-generated
        writeln!(stage_file, "# auto-generated by update-ssh-keys")
            .chain_err(|| format!("failed to write to file '{:?}'", stage_filename))?;
        // write all the keys to the staging file
        for keyset in self.keys.values() {
            // if the keyset is disabled, skip it
            if keyset.disabled {
                continue;
            }
            for key in &keyset.keys {
                // only write the key to authorized_keys if it is valid
                if let AuthorizedKeyEntry::Valid { ref key } = *key {
                    writeln!(stage_file, "{}", key)
                        .chain_err(|| format!("failed to write to file '{:?}'", stage_filename))?;
                }
            }
        }
        stage_file
            .sync_all()
            .chain_err(|| format!("failed to sync file '{:?}'", stage_filename))?;
        drop(stage_file);
        // destroy the old authorized keys file and move the staging one to that
        // location
        fs::rename(&stage_filename, &self.authorized_keys_file()).chain_err(|| {
            format!(
                "failed to move '{:?}' to '{:?}'",
                stage_filename,
                self.authorized_keys_file()
            )
        })?;
        let parent_path = stage_filename.parent().ok_or_else(|| {
            format!(
                "failed to sync parent directory of '{}'",
                stage_filename.display()
            )
        })?;
        let parent_dir_file = File::open(parent_path)
            .chain_err(|| format!("failed to open '{}' for syncing", parent_path.display()))?;
        parent_dir_file
            .sync_all()
            .chain_err(|| format!("failed to sync '{}'", parent_path.display()))?;
        Ok(())
    }
pub fn sync ( & self ) -> Result < ( ) > { // if we have no keys, don't overwrite the authorized_keys file. // if the user wants to delete all their ssh keys, we won't help them if self . keys . is_empty ( ) { return Err ( ErrorKind :: NoKeysFound ( format ! ( " " self authorized_keys_dir ( ) ) ) . into ( ) ) ; } // switch users let _guard = switch_user ( & self . user ) ? ; // get our staging directory let stage_filename = self . stage_file ( ) ; let mut stage_file = File :: create ( & stage_filename ) . chain_err ( | | { format ! ( " " stage_filename ) } ) ? ; // note that this file is auto-generated writeln ! ( stage_file " " ) . chain_err ( | | format ! ( " " stage_filename ) ) ? ; // write all the keys to the staging file for keyset in self . keys . values ( ) { // if the keyset is disabled, skip it if keyset . disabled { continue ; } for key in & keyset . keys { // only write the key to authorized_keys if it is valid if let AuthorizedKeyEntry :: Valid { ref key } = * key { writeln ! ( stage_file " " key ) . chain_err ( | | format ! ( " " stage_filename ) ) ? ; } } } stage_file . sync_all ( ) . chain_err ( | | format ! ( " " stage_filename ) ) ? ; drop ( stage_file ) ; // destroy the old authorized keys file and move the staging one to that // location fs :: rename ( & stage_filename , & self . authorized_keys_file ( ) ) . chain_err ( | | { format ! ( " " stage_filename self authorized_keys_file ( ) ) } ) ? ; let parent_path = stage_filename . parent ( ) . ok_or_else ( | | { format ! ( " " stage_filename display ( ) ) } ) ? ; let parent_dir_file = File :: open ( parent_path ) . chain_err ( | | format ! ( " " parent_path display ( ) ) ) ? ; parent_dir_file . sync_all ( ) . chain_err ( | | format ! ( " " parent_path display ( ) ) ) ? ; Ok ( ( ) ) }
sync writes all the keys we have to authorized_keys.
sync writes all the keys we have to authorized_keys .
&self
&self