From c4d67105f28ae44b7c141e4bced0ee84648b6aa2 Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Tue, 27 Feb 2018 19:39:14 -0800 Subject: [PATCH] add checkDir for keyring ensureDir will create the dir, which for calls like List, Sign, Remove, etc. should not occur. It should only perform the actions if something exists there Signed-off-by: Derek McQuay --- keyring/keyring.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/keyring/keyring.go b/keyring/keyring.go index 43dc428..9abfc9e 100644 --- a/keyring/keyring.go +++ b/keyring/keyring.go @@ -78,8 +78,8 @@ func NewKeyPair(root, name, email string) error { // ListKeys prints keyring information to w. func ListKeys(root string, w io.Writer) error { - if err := ensureDir(root); err != nil { - return errors.Wrap(err, "can't find or create pgp dir") + if err := checkDir(root); err != nil { + return errors.Wrap(err, "can't find pgp dir") } srn, prn := getNames(root) secs, pubs, err := getELs(srn, prn) @@ -105,8 +105,8 @@ func ListKeys(root string, w io.Writer) error { // Export prints pubkey information associated with email to w. func Export(root string, w io.Writer, email string) error { - if err := ensureDir(root); err != nil { - return errors.Wrap(err, "can't find or create pgp dir") + if err := checkDir(root); err != nil { + return errors.Wrap(err, "can't find pgp dir") } srn, prn := getNames(root) _, pubs, err := getELs(srn, prn) @@ -142,8 +142,8 @@ func Import(root string, w io.Reader) error { return errors.Wrap(err, "reading keyring") } - if err := ensureDir(root); err != nil { - return errors.Wrap(err, "can't find or create pgp dir") + if err := checkDir(root); err != nil { + return errors.Wrap(err, "can't find pgp dir") } srn, prn := getNames(root) _, pubs, err := getELs(srn, prn) @@ -185,8 +185,8 @@ func Import(root string, w io.Reader) error { // Sign takes an id and a reader and writes the signature for that id to sig. func Sign(root, id string, in io.Reader, sig io.Writer) error { - if err := ensureDir(root); err != nil { - return errors.Wrap(err, "can't find or create pgp dir") + if err := checkDir(root); err != nil { + return errors.Wrap(err, "can't find pgp dir") } srn, prn := getNames(root) secs, _, err := getELs(srn, prn) @@ -206,8 +206,8 @@ func Sign(root, id string, in io.Reader, sig io.Writer) error { // Verify verifies a file's deatched signature. func Verify(root string, file, sig io.Reader) error { - if err := ensureDir(root); err != nil { - return errors.Wrap(err, "can't find or create pgp dir") + if err := checkDir(root); err != nil { + return errors.Wrap(err, "can't find pgp dir") } srn, prn := getNames(root) _, pubs, err := getELs(srn, prn) @@ -225,8 +225,8 @@ func Verify(root string, file, sig io.Reader) error { // It skips public keys that have matching secret keys, and does not effect // private keys. func Remove(root string, id string) error { - if err := ensureDir(root); err != nil { - return errors.Wrap(err, "can't find or create pgp dir") + if err := checkDir(root); err != nil { + return errors.Wrap(err, "can't find pgp dir") } srn, prn := getNames(root) secs, pubs, err := getELs(srn, prn) @@ -266,6 +266,14 @@ func pGPDir(root string) string { return filepath.Join(root, "var", "lib", "pm", "pgp") } +func checkDir(root string) error { + d := pGPDir(root) + if !fs.Exists(d) { + return fmt.Errorf("pgp dir does not exist") + } + return nil +} + func ensureDir(root string) error { d := pGPDir(root) if !fs.Exists(d) {