Make the file creation utilities take a reference to Sq.

This commit is contained in:
Justus Winter 2024-10-02 18:05:40 +02:00
parent 7b05d6ad49
commit fc76b99c55
No known key found for this signature in database
GPG Key ID: 686F55B4AB2B3386
26 changed files with 50 additions and 47 deletions

View File

@ -154,7 +154,7 @@ fn decode(sq: Sq, command: &cli::autocrypt::DecodeCommand)
{
let input = command.input.open()?;
let mut output = command.output.create_pgp_safe(
sq.force,
&sq,
command.binary,
armor::Kind::PublicKey,
)?;
@ -173,7 +173,7 @@ fn encode_sender(sq: Sq, command: &cli::autocrypt::EncodeSenderCommand)
-> Result<()>
{
let input = command.input.open()?;
let mut output = command.output.create_safe(sq.force)?;
let mut output = command.output.create_safe(&sq)?;
let cert = Cert::from_buffered_reader(input)?;
let addr = command.address.clone()
.or_else(|| {

View File

@ -32,7 +32,7 @@ pub fn dispatch(sq: Sq, mut cmd: export::Command) -> Result<()> {
let output = FileOrStdout::default();
let mut sink = output.create_pgp_safe(
sq.force,
&sq,
cmd.binary,
armor::Kind::PublicKey,
)?;

View File

@ -246,7 +246,7 @@ pub fn lint(mut sq: Sq, args: Command) -> Result<()> {
};
Some(output.create_pgp_safe(
sq.force, args.binary,
&sq, args.binary,
if args.export_secret_keys {
armor::Kind::SecretKey
} else {

View File

@ -40,7 +40,7 @@ pub fn dispatch(sq: Sq, command: cli::decrypt::Command) -> Result<()> {
tracer!(TRACE, "decrypt::dispatch");
let mut input = command.input.open()?;
let mut output = command.output.create_safe(sq.force)?;
let mut output = command.output.create_safe(&sq)?;
let certs = load_certs(
command.sender_cert_file.iter().map(|s| s.as_ref()),

View File

@ -53,7 +53,7 @@ pub fn dispatch(sq: Sq, command: cli::encrypt::Command) -> Result<()> {
}
let output = command.output.create_pgp_safe(
sq.force,
&sq,
command.binary,
armor::Kind::Message,
)?;

View File

@ -45,7 +45,7 @@ pub fn dispatch(mut sq: Sq, c: inspect::Command)
// sq inspect does not have --output, but commands::inspect does.
// Work around this mismatch by always creating a stdout output.
let output_type = FileOrStdout::default();
let output = &mut output_type.create_unsafe(sq.force)?;
let output = &mut output_type.create_unsafe(&sq)?;
let print_certifications = c.certifications;

View File

@ -271,7 +271,7 @@ fn update(
if let Some(sink) = command.output {
let path = sink.path().map(Clone::clone);
let mut output = sink.for_secrets().create_safe(sq.force)?;
let mut output = sink.for_secrets().create_safe(&sq)?;
if command.binary {
key.as_tsk().serialize(&mut output)?;
} else {

View File

@ -390,7 +390,7 @@ pub fn bind(sq: Sq, mut command: cli::key::subkey::BindCommand) -> Result<()>
}
if let Some(output) = command.output {
let mut sink = output.for_secrets().create_safe(sq.force)?;
let mut sink = output.for_secrets().create_safe(&sq)?;
if command.binary {
cert.as_tsk().serialize(&mut sink)?;
} else {

View File

@ -180,7 +180,7 @@ pub fn generate(
.collect();
headers.insert(0, ("Comment", "Revocation certificate for"));
let w = rev_path.create_safe(sq.force)?;
let w = rev_path.create_safe(&sq)?;
let mut w = Writer::with_headers(w, Kind::PublicKey, headers)?;
Packet::from(cert.primary_key().key().clone()).serialize(&mut w)?;
Packet::Signature(rev).serialize(&mut w)?;
@ -198,7 +198,7 @@ pub fn generate(
Some(ref output_file) => {
// Write the key to a file or to stdout.
let w = output_file.clone().for_secrets()
.create_safe(sq.force)?;
.create_safe(&sq)?;
let mut w = Writer::with_headers(w, Kind::SecretKey, headers)?;
cert.as_tsk().serialize(&mut w)?;
w.finalize()?;

View File

@ -320,7 +320,7 @@ fn subkey_add(
.attach_cert()?;
if let Some(output) = command.output {
let mut sink = output.for_secrets().create_safe(sq.force)?;
let mut sink = output.for_secrets().create_safe(&sq)?;
if command.binary {
new_cert.as_tsk().serialize(&mut sink)?;
} else {

View File

@ -332,7 +332,7 @@ fn userid_add(
let cert = cert.insert_packets(add)?;
if let Some(output) = command.output {
let mut sink = output.for_secrets().create_safe(sq.force)?;
let mut sink = output.for_secrets().create_safe(&sq)?;
if command.binary {
cert.as_tsk().serialize(&mut sink)?;
} else {

View File

@ -682,7 +682,7 @@ impl Response {
-> Result<()>
{
if let Some(file) = &output {
let mut output = file.create_safe(sq.force)?;
let mut output = file.create_safe(&sq)?;
serialize_keyring(&mut output, certs, binary)?;
} else {
import_certs(&mut sq, certs)?;

View File

@ -202,7 +202,7 @@ pub fn certify(sq: Sq, mut c: certify::Command)
if let Some(output) = c.output {
// And export it.
let mut message = output.create_pgp_safe(
sq.force,
&sq,
c.binary,
sequoia_openpgp::armor::Kind::PublicKey,
)?;

View File

@ -59,7 +59,7 @@ pub fn dispatch(sq: Sq, command: cli::sign::Command) -> Result<()> {
if let Some(merge) = command.merge {
let output = output.create_pgp_safe(
sq.force,
&sq,
binary,
armor::Kind::Message,
)?;
@ -74,7 +74,7 @@ pub fn dispatch(sq: Sq, command: cli::sign::Command) -> Result<()> {
}
if command.clearsign {
let output = output.create_safe(sq.force)?;
let output = output.create_safe(&sq)?;
clearsign(sq, input, output, signers, &notations)?;
} else {
sign(sq,
@ -166,7 +166,7 @@ fn sign_data<'a, 'store, 'rstore>(
let tmp_path = tmp_file.path().into();
(Box::new(tmp_file), sigs, Some(tmp_path))
} else {
(output_path.create_safe(sq.force)?, Vec::new(), None)
(output_path.create_safe(&sq)?, Vec::new(), None)
};
// Stream an OpenPGP message.
@ -249,7 +249,7 @@ fn sign_message<'a, 'store, 'rstore>(
-> Result<()>
{
let mut output = output.create_pgp_safe(
sq.force,
&sq,
binary,
armor::Kind::Message,
)?;

View File

@ -71,7 +71,7 @@ pub fn dispatch(sq: Sq, command: cli::toolbox::armor::Command)
&& (want_kind.is_none() || want_kind == have_kind)
{
// It is already armored and has the correct kind.
let mut output = command.output.create_safe(sq.force)?;
let mut output = command.output.create_safe(&sq)?;
io::copy(&mut input, &mut output)?;
return Ok(());
}
@ -85,7 +85,7 @@ pub fn dispatch(sq: Sq, command: cli::toolbox::armor::Command)
let want_kind = want_kind.expect("given or detected");
let mut output =
command.output.create_pgp_safe(sq.force, false, want_kind)?;
command.output.create_pgp_safe(&sq, false, want_kind)?;
if already_armored {
// Dearmor and copy to change the type.

View File

@ -13,7 +13,7 @@ pub fn dispatch(sq: Sq, command: cli::toolbox::dearmor::Command)
tracer!(TRACE, "dearmor::dispatch");
let mut input = command.input.open()?;
let mut output = command.output.create_safe(sq.force)?;
let mut output = command.output.create_safe(&sq)?;
let mut filter = armor::Reader::from_buffered_reader(&mut input, None)?;
io::copy(&mut filter, &mut output)?;

View File

@ -14,7 +14,7 @@ pub fn dispatch(
command: cli::toolbox::extract_cert::Command,
) -> Result<()> {
let input = command.input.open()?;
let mut output = command.output.create_safe(sq.force)?;
let mut output = command.output.create_safe(&sq)?;
let cert = Cert::from_buffered_reader(input)?;
if command.binary {

View File

@ -185,7 +185,7 @@ fn filter<F>(sq: &Sq, inputs: Vec<PathBuf>, output: FileOrStdout,
}
let mut output = output.for_secrets().create_pgp_safe(
sq.force,
&sq,
binary,
if ! to_certificate && certs.iter().any(|c| c.is_tsk()) {
armor::Kind::SecretKey
@ -353,7 +353,7 @@ fn merge(sq: &Sq, inputs: Vec<PathBuf>, output: FileOrStdout,
}
let mut output = output.for_secrets().create_pgp_safe(
sq.force,
&sq,
binary,
if certs.values().any(|c| c.as_ref().map(Cert::is_tsk).unwrap_or(false))
{

View File

@ -46,7 +46,7 @@ pub fn dispatch(sq: Sq, command: Command)
Subcommands::Dump(command) => {
let mut input = command.input.open()?;
let output_type = command.output;
let mut output = output_type.create_unsafe(sq.force)?;
let mut output = output_type.create_unsafe(&sq)?;
let width = if let Some((width, _)) = terminal_size() {
Some(width.0.into())
@ -65,7 +65,7 @@ pub fn dispatch(sq: Sq, command: Command)
Subcommands::Decrypt(command) => {
let mut input = command.input.open()?;
let mut output = command.output.create_pgp_safe(
sq.force,
&sq,
command.binary,
openpgp::armor::Kind::Message,
)?;
@ -226,9 +226,9 @@ pub fn join(sq: Sq, c: JoinCommand) -> Result<()> {
let mut sink = if c.binary {
// No need for any auto-detection.
Some(output.create_pgp_safe(
sq.force, true, openpgp::armor::Kind::File)?)
&sq, true, openpgp::armor::Kind::File)?)
} else if let Some(kind) = kind {
Some(output.create_pgp_safe(sq.force, false, kind)?)
Some(output.create_pgp_safe(&sq, false, kind)?)
} else {
None // Defer.
};
@ -253,7 +253,7 @@ pub fn join(sq: Sq, c: JoinCommand) -> Result<()> {
};
*sink = Some(
output.create_pgp_safe(sq.force, false, kind)?
output.create_pgp_safe(&sq, false, kind)?
);
}

View File

@ -106,7 +106,7 @@ signatures on other User IDs to make the key valid again.",
}
if let Some(output) = command.output {
let mut sink = output.for_secrets().create_safe(sq.force)?;
let mut sink = output.for_secrets().create_safe(&sq)?;
if command.binary {
cert.as_tsk().serialize(&mut sink)?;
} else {

View File

@ -22,7 +22,7 @@ pub fn dispatch(sq: Sq, command: cli::verify::Command)
tracer!(TRACE, "verify::dispatch");
let mut input = command.input.open()?;
let mut output = command.output.create_safe(sq.force)?;
let mut output = command.output.create_safe(&sq)?;
let mut detached = if let Some(f) = command.detached {
Some(File::open(f)?)
} else {

View File

@ -13,7 +13,10 @@ use sequoia_openpgp::{
serialize::stream::{Armorer, Message},
};
use crate::cli::types::FileOrStdout;
use crate::{
cli::types::FileOrStdout,
sq::Sq,
};
impl FileOrStdout {
/// Opens the file (or stdout) for writing data that is safe for
@ -23,9 +26,9 @@ impl FileOrStdout {
/// authenticated payloads.
pub fn create_safe(
&self,
force: bool,
sq: &Sq,
) -> Result<Box<dyn Write + Sync + Send>> {
self.create(force)
self.create(sq)
}
/// Opens the file (or stdout) for writing data that is NOT safe
@ -35,10 +38,10 @@ impl FileOrStdout {
/// warning once.
pub fn create_unsafe(
&self,
force: bool,
sq: &Sq,
) -> Result<Box<dyn Write + Sync + Send>> {
CliWarningOnce::warn();
self.create(force)
self.create(sq)
}
/// Opens the file (or stdout) for writing data that is safe for
@ -48,7 +51,7 @@ impl FileOrStdout {
/// implicitly configures this output to emit secret keys.
pub fn create_pgp_safe<'a>(
&self,
force: bool,
sq: &Sq,
binary: bool,
kind: armor::Kind,
) -> Result<Message<'a>> {
@ -58,7 +61,7 @@ impl FileOrStdout {
if kind == armor::Kind::SecretKey {
o = o.for_secrets();
}
let sink = o.create_safe(force)?;
let sink = o.create_safe(sq)?;
let mut message = Message::new(sink);
if ! binary {
@ -69,8 +72,8 @@ impl FileOrStdout {
/// Helper function, do not use directly. Instead, use create_or_stdout_safe
/// or create_or_stdout_unsafe.
fn create(&self, force: bool) -> Result<Box<dyn Write + Sync + Send>> {
let sink = self._create_sink(force)?;
fn create(&self, sq: &Sq) -> Result<Box<dyn Write + Sync + Send>> {
let sink = self._create_sink(sq)?;
if self.is_for_secrets() || ! cfg!(debug_assertions) {
// We either expect secrets, or we are in release mode.
Ok(sink)
@ -80,10 +83,10 @@ impl FileOrStdout {
Ok(Box::new(SecretLeakDetector::new(sink)))
}
}
fn _create_sink(&self, force: bool) -> Result<Box<dyn Write + Sync + Send>>
fn _create_sink(&self, sq: &Sq) -> Result<Box<dyn Write + Sync + Send>>
{
if let Some(path) = self.path() {
if !path.exists() || force {
if !path.exists() || sq.force {
Ok(Box::new(
OpenOptions::new()
.write(true)

View File

@ -54,7 +54,7 @@ pub fn delete(sq: Sq,
stripped.into_iter().map(|stripped| Packet::from(stripped)))?;
let output = output.unwrap_or_else(|| FileOrStdout::new(None));
let mut output = output.for_secrets().create_safe(sq.force)?;
let mut output = output.for_secrets().create_safe(&sq)?;
if binary {
cert.as_tsk().serialize(&mut output)?;
} else {

View File

@ -236,7 +236,7 @@ pub fn expire(sq: Sq,
if let Some(sink) = output {
let path = sink.path().map(Clone::clone);
let mut output = sink.for_secrets().create_safe(sq.force)?;
let mut output = sink.for_secrets().create_safe(&sq)?;
if binary {
key.as_tsk().serialize(&mut output)?;
} else {

View File

@ -154,7 +154,7 @@ pub fn password(sq: Sq,
let cert = cert.insert_packets(packets)?;
let output = output.unwrap_or_else(|| FileOrStdout::new(None));
let mut output = output.for_secrets().create_safe(sq.force)?;
let mut output = output.for_secrets().create_safe(&sq)?;
if binary {
cert.as_tsk().serialize(&mut output)?;
} else {

View File

@ -48,7 +48,7 @@ pub trait RevocationOutput {
const COMMENT_WIDTH: usize = 70;
if let Some(output) = output {
let mut output = output.create_safe(sq.force)?;
let mut output = output.create_safe(sq)?;
// First, build a minimal revocation certificate containing
// the primary key, the revoked component, and the revocation