diff options
author | Jörg Thalheim <joerg@thalheim.io> | 2023-06-15 19:08:41 +0200 |
---|---|---|
committer | Jörg Thalheim <joerg@thalheim.io> | 2023-06-15 19:11:54 +0200 |
commit | 9090bf3c36bbfaffffd6581f2ac2a8dcc2e4c5cd (patch) | |
tree | b5535f403caef7fedd9c1ef468350018581c979c | |
parent | 3ab89de5fa2503810772539e958c42a29837947f (diff) | |
download | bcachefs-tools-9090bf3c36bbfaffffd6581f2ac2a8dcc2e4c5cd.tar.gz |
fix reading keys from non-tty inputs
read_password_from_tty doesn't have a fallback if input is a pipe.
This makes scripting and integration testing harder.
Noticed while updating our nixos integration test.
Signed-off-by: Jörg Thalheim <joerg@thalheim.io>
-rw-r--r-- | rust-src/src/key.rs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/rust-src/src/key.rs b/rust-src/src/key.rs index 2af34b13..2b4fc45e 100644 --- a/rust-src/src/key.rs +++ b/rust-src/src/key.rs @@ -72,7 +72,13 @@ fn ask_for_key(sb: &bch_sb_handle) -> anyhow::Result<()> { let bch_key_magic = BCH_KEY_MAGIC.as_bytes().read_u64::<LittleEndian>().unwrap(); let crypt = sb.sb().crypt().unwrap(); - let pass = rpassword::read_password_from_tty(Some("Enter passphrase: "))?; + let pass = if atty::is(atty::Stream::Stdin) { + rpassword::read_password_from_tty(Some("Enter passphrase: "))? + } else { + let mut line = String::new(); + std::io::stdin().read_line(&mut line)?; + line + }; let pass = std::ffi::CString::new(pass.trim_end())?; // bind to keep the CString alive let mut output: bch_key = unsafe { bcachefs::derive_passphrase( |