~gpanders/passage

9bddfe8e03b39ceccfa736e88924d3b4cb59748f — Gregory Anders 4 months ago bfe9b09
Re-encrypt all items in store
2 files changed, 28 insertions(+), 12 deletions(-)

M src/cmd/list.rs
M src/store.rs
M src/cmd/list.rs => src/cmd/list.rs +1 -1
@@ 34,7 34,7 @@ fn tree(root: &Path, depth: usize, prefix: &str) -> io::Result<()> {

        if path.is_dir() {
            println!("{}", file_name.blue().bold());
            let next_prefix = prefix.to_string().clone() + if is_last { "    " } else { "│   " };
            let next_prefix = prefix.to_string() + if is_last { "    " } else { "│   " };
            tree(&path, depth + 1, &next_prefix)?;
        } else {
            let file_name = match file_name.strip_suffix(".age") {

M src/store.rs => src/store.rs +27 -11
@@ 2,7 2,7 @@ use age::x25519::{Identity, Recipient};
use std::fs::{self, DirEntry, File, OpenOptions};
use std::io::prelude::*;
use std::io::{self, BufReader};
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use crate::{crypt, error::Error, key};



@@ 34,6 34,31 @@ impl PasswordStore {
            .exists()
    }

    pub fn items(&self) -> io::Result<Vec<DirEntry>> {
        fn scan(dir: &Path, entries: &mut Vec<DirEntry>) -> io::Result<()> {
            for entry in fs::read_dir(&dir)?
                .filter_map(|e| e.ok())
                .collect::<Vec<DirEntry>>()
                .into_iter()
            {
                let path = entry.path();
                if path.is_dir() {
                    scan(&path, entries)?;
                } else if let Some(ext) = path.extension() {
                    if ext == ".age" {
                        entries.push(entry);
                    }
                }
            }

            Ok(())
        }

        let mut entries: Vec<DirEntry> = vec![];
        scan(&self.dir, &mut entries)?;
        Ok(entries)
    }

    pub fn insert(&self, name: &str, secret: &str) -> Result<(), Error> {
        let path = self.dir.join(PathBuf::from(name.to_string() + ".age"));
        fs::create_dir_all(&path.parent().unwrap())?;


@@ 88,16 113,7 @@ impl PasswordStore {
    }

    pub fn reencrypt(&self, key: &Identity) -> Result<(), Error> {
        let items: Vec<DirEntry> = fs::read_dir(&self.dir)?
            .filter_map(|e| e.ok())
            .filter(|e| {
                e.file_name()
                    .to_str()
                    .map_or(false, |s| s.ends_with(".age"))
            })
            .collect();

        for item in items {
        for item in self.items()? {
            let mut cypher = vec![];
            File::open(item.path())?.read_to_end(&mut cypher)?;