pub fn open(user: User, create: bool, ssh_dir: Option<PathBuf>) -> Result<Self> {
        // switch users
        let _guard = switch_user(&user)?;
        // make a new file lock and lock it
        let lock = FileLock::try_new(&lock_file(&user))?;
        lock.lock()?;

        let ssh_dir = ssh_dir.unwrap_or_else(|| default_ssh_dir(&user));
        let akd = authorized_keys_dir(&ssh_dir);

        let keys = if akd.is_dir() {
            // read the existing keysets from the dir
            AuthorizedKeys::read_all_keys(&akd)?
        } else if !akd.exists() && create {
            // read the existing keyset from the file
            let filename = authorized_keys_file(&ssh_dir);
            if filename.exists() {
                let file = File::open(&filename).chain_err(|| {
                    format!("failed to open authorized keys file: '{:?}'", filename)
                })?;
                let mut keys = HashMap::new();
                keys.insert(
                    PRESERVED_KEYS_FILE.to_string(),
                    AuthorizedKeySet {
                        filename: PRESERVED_KEYS_FILE.to_string(),
                        disabled: false,
                        keys: AuthorizedKeys::read_keys(file)?,
                    },
                );
                keys
            } else {
                // if the authorized_keys file doesn't exist, we don't start
                // with any keys
                HashMap::new()
            }
        } else {
            // either the akd doesn't exist and create is false, or it exists
            // and is not a directory
            return Err(format!("'{:?}' doesn't exist or is not a directory", akd).into());
        };

        Ok(AuthorizedKeys {
            ssh_dir,
            user,
            keys,
            lock,
        })
    }
/// open creates a new authorized_keys object. if there is an existing
/// authorized_keys directory on disk it reads all the keys from that. if
/// there is no directory already and we are told to create it, we add the
/// existing authorized keys file as an entry, if it exists.
///
/// before open actually does any of that, it switches it's uid for the span
/// of the function and then switched back. it also opens a file lock on the
/// directory that other instances of `update-ssh-keys` will respect. the
/// file lock will automatically close when this structure goes out of
/// scope. you can make sure it is unlocked by calling `drop` yourself in
/// cases where you think the memory may leak (like if you are tossing boxes
/// around etc).
///
/// open blocks until it can grab the file lock.
///
/// open returns an error if any file operations fail, if it failes to parse
/// any of the public keys in the existing files, if it failes to change
/// users, if it failes to grab the lock, or if create is false but the
/// directory doesn't exist.
open creates a new authorized_keys object. if there is an existing
authorized_keys directory on disk it reads all the keys from that. if
there is no directory already and we are told to create it, we add the
existing authorized keys file as an entry, if it exists.

before open actually does any of that, it switches it's uid for the span
of the function and then switched back. it also opens a file lock on the
directory that other instances of `update-ssh-keys` will respect. the
file lock will automatically close when this structure goes out of
scope. you can make sure it is unlocked by calling `drop` yourself in
cases where you think the memory may leak (like if you are tossing boxes
around etc).

open blocks until it can grab the file lock.

open returns an error if any file operations fail, if it failes to parse
any of the public keys in the existing files, if it failes to change
users, if it failes to grab the lock, or if create is false but the
directory doesn't exist.
open creates a new authorized_keys object . if there is an existing authorized_keys directory on disk it reads all the keys from that . if there is no directory already and we are told to create it we add the existing authorized keys file as an entry if it exists . before open actually does any of that it switches it ' s uid for the span of the function and then switched back . it also opens a file lock on the directory that other instances of ` update - ssh - keys ` will respect . the file lock will automatically close when this structure goes out of scope . you can make sure it is unlocked by calling ` drop ` yourself in cases where you think the memory may leak ( like if you are tossing boxes around etc ) . open blocks until it can grab the file lock . open returns an error if any file operations fail if it failes to parse any of the public keys in the existing files if it failes to change users if it failes to grab the lock or if create is false but the directory doesn ' t exist .
pub fn open(user: User, create: bool, ssh_dir: Option<PathBuf>) -> Result<Self> {
        // switch users
        let _guard = switch_user(&user)?;
        // make a new file lock and lock it
        let lock = FileLock::try_new(&lock_file(&user))?;
        lock.lock()?;
        let ssh_dir = ssh_dir.unwrap_or_else(|| default_ssh_dir(&user));
        let akd = authorized_keys_dir(&ssh_dir);
        let keys = if akd.is_dir() {
            // read the existing keysets from the dir
            AuthorizedKeys::read_all_keys(&akd)?
        } else if !akd.exists() && create {
            // read the existing keyset from the file
            let filename = authorized_keys_file(&ssh_dir);
            if filename.exists() {
                let file = File::open(&filename).chain_err(|| {
                    format!("failed to open authorized keys file: '{:?}'", filename)
                })?;
                let mut keys = HashMap::new();
                keys.insert(
                    PRESERVED_KEYS_FILE.to_string(),
                    AuthorizedKeySet {
                        filename: PRESERVED_KEYS_FILE.to_string(),
                        disabled: false,
                        keys: AuthorizedKeys::read_keys(file)?,
                    },
                );
                keys
            } else {
                // if the authorized_keys file doesn't exist, we don't start
                // with any keys
                HashMap::new()
            }
        } else {
            // either the akd doesn't exist and create is false, or it exists
            // and is not a directory
            return Err(format!("'{:?}' doesn't exist or is not a directory", akd).into());
        };
        Ok(AuthorizedKeys {
            ssh_dir,
            user,
            keys,
            lock,
        })
    }
pub fn open ( user : User , create : bool , ssh_dir : Option < PathBuf > ) -> Result < Self > { // switch users let _guard = switch_user ( & user ) ? ; // make a new file lock and lock it let lock = FileLock :: try_new ( & lock_file ( & user ) ) ? ; lock . lock ( ) ? ; let ssh_dir = ssh_dir . unwrap_or_else ( | | default_ssh_dir ( & user ) ) ; let akd = authorized_keys_dir ( & ssh_dir ) ; let keys = if akd . is_dir ( ) { // read the existing keysets from the dir AuthorizedKeys :: read_all_keys ( & akd ) ? } else if ! akd . exists ( ) && create { // read the existing keyset from the file let filename = authorized_keys_file ( & ssh_dir ) ; if filename . exists ( ) { let file = File :: open ( & filename ) . chain_err ( | | { format ! ( " " filename ) } ) ? ; let mut keys = HashMap :: new ( ) ; keys . insert ( PRESERVED_KEYS_FILE . to_string ( ) , AuthorizedKeySet { filename : PRESERVED_KEYS_FILE . to_string ( ) , disabled : false , keys : AuthorizedKeys :: read_keys ( file ) ? , } , ) ; keys } else { // if the authorized_keys file doesn't exist, we don't start // with any keys HashMap :: new ( ) } } else { // either the akd doesn't exist and create is false, or it exists // and is not a directory return Err ( format ! ( " " akd ) . into ( ) ) ; } ; Ok ( AuthorizedKeys { ssh_dir , user , keys , lock , } ) }
open creates a new authorized_keys object.
open creates a new authorized_keys object .
user
User
create
bool
ssh_dir
Option<PathBuf>
user
User
create
bool
ssh_dir
Option<PathBuf>