Compare commits

...

10 commits

13 changed files with 740 additions and 630 deletions

View file

@ -0,0 +1,37 @@
name: build
on: push
env:
DEV_SHELL_NAME: CI
CI: true
jobs:
build-cargo:
runs-on: flakes-action
steps:
- uses: actions/checkout@v3
name: Checkout
- name:
run: |
nix develop -c cargo build --release
build-nix:
runs-on: flakes-action
steps:
- uses: actions/checkout@v3
name: Checkout
- name:
run: |
nix build
check-nix:
runs-on: flakes-action
steps:
- uses: actions/checkout@v3
name: Checkout
- name: Metadata
run: |
nix flake metadata
- name: Statix
run: |
nix develop -c statix check
- name: Flake check
run: |
nix flake check

1018
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -3,31 +3,36 @@ name = "bakare"
version = "0.1.0"
authors = ["Cyryl Płotnicki <cyplo@cyplo.dev>"]
edition = "2021"
rust-version = "1.56"
rust-version = "1.63"
license = "AGPL-3.0"
description = "modern and simple, yet efficient backup solution"
[dependencies]
anyhow = "1.0"
base64 = "0.13"
base64 = "0.21"
blake = "2"
chacha20poly1305 = "0.9"
fail = "0.4"
chacha20poly1305 = "0.10"
fail = "0.5"
femme = "2"
hex = "0.4"
log = "0.4"
nix = "0.23"
rand = "0.8"
reed-solomon = "0.2"
seahorse = "2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
sha2 = "0.9"
sha2 = "0.10"
tempfile = "3"
uuid = { version = "0.8", features = ["v4"] }
uuid = { version = "1", features = ["v4"] }
walkdir = "2"
[dependencies.nix]
version = "0.27"
default-features = false
features = ["process"]
[dev-dependencies]
criterion = "0.3"
criterion = "0.5"
pretty_assertions = "1"
proptest = "1"
two-rusty-forks = "0.4"

View file

@ -1,29 +1,13 @@
{
"nodes": {
"crate2nix": {
"flake": false,
"locked": {
"lastModified": 1634898841,
"narHash": "sha256-CZgjBo0rYeQHiIfnFD5wj9vmI/O24IaCT0yzPnW0FSQ=",
"owner": "kolloch",
"repo": "crate2nix",
"rev": "d8566765a23c5f8f8e50a726bb0db7957452b5e8",
"type": "github"
},
"original": {
"owner": "kolloch",
"repo": "crate2nix",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1627913399,
"narHash": "sha256-hY8g6H2KFL8ownSiFeMOjwPC8P0ueXpCVEbxgda3pko=",
"lastModified": 1696426674,
"narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "12c64ca55c1014cdc1b16ed5a804aa8576601ff2",
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
"type": "github"
},
"original": {
@ -32,87 +16,85 @@
"type": "github"
}
},
"flake-utils": {
"naersk": {
"inputs": {
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1623875721,
"narHash": "sha256-A8BU7bjS5GirpAUv4QA+QnJ4CceLHkcXdRp4xITDB0s=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "f7e004a55b120c02ecb6219596820fcd32ca8772",
"lastModified": 1698420672,
"narHash": "sha256-/TdeHMPRjjdJub7p7+w55vyABrsJlt5QkznPYy55vKA=",
"owner": "nix-community",
"repo": "naersk",
"rev": "aeb58d5e8faead8980a807c840232697982d47b9",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"owner": "nix-community",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1634758644,
"narHash": "sha256-H3UW/msC6wadg28lcgZv2Ge/P7dWxesL6i37a0GOeyM=",
"owner": "nixos",
"lastModified": 1699065553,
"narHash": "sha256-j8UmH8fqXcOgL6WrlMcvV2m2XQ6OzU0IBucyuJ0vnyQ=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "70904d4a9927a4d6e05c72c4aaac4370e05107f3",
"rev": "8ab9c53eee434651ce170dee1d9727b974e9a6b6",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-21.05",
"repo": "nixpkgs",
"type": "github"
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1628186154,
"narHash": "sha256-r2d0wvywFnL9z4iptztdFMhaUIAaGzrSs7kSok0PgmE=",
"lastModified": 1699065553,
"narHash": "sha256-j8UmH8fqXcOgL6WrlMcvV2m2XQ6OzU0IBucyuJ0vnyQ=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "06552b72346632b6943c8032e57e702ea12413bf",
"rev": "8ab9c53eee434651ce170dee1d9727b974e9a6b6",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"crate2nix": "crate2nix",
"flake-compat": "flake-compat",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay",
"naersk": "naersk",
"nixpkgs": "nixpkgs_2",
"utils": "utils"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs_2"
},
"systems": {
"locked": {
"lastModified": 1634869268,
"narHash": "sha256-RVAcEFlFU3877Mm4q/nbXGEYTDg/wQNhzmXGMTV6wBs=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "c02c2d86354327317546501af001886fbb53d374",
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1634851050,
"narHash": "sha256-N83GlSGPJJdcqhUxSCS/WwW5pksYf3VP1M13cDRTSVA=",
"lastModified": 1694529238,
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c91f3de5adaf1de973b797ef7485e441a65b8935",
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
"type": "github"
},
"original": {

132
flake.nix
View file

@ -1,95 +1,59 @@
{
description = "A simple yet robust commandline random password generator.";
description = "Bakare: modern and simple, yet efficient backup solution";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-21.05";
utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
crate2nix = {
url = "github:kolloch/crate2nix";
flake = false;
};
naersk.url = "github:nix-community/naersk";
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};
outputs = { self, nixpkgs, utils, rust-overlay, crate2nix, ... }:
let
name = "bakare";
rustChannel = "stable";
in
utils.lib.eachDefaultSystem
(system:
let
# Imports
pkgs = import nixpkgs {
inherit system;
overlays = [
rust-overlay.overlay
(self: super: {
# Because rust-overlay bundles multiple rust packages into one
# derivation, specify that mega-bundle here, so that crate2nix
# will use them automatically.
rustc = self.rust-bin.${rustChannel}.latest.default;
cargo = self.rust-bin.${rustChannel}.latest.default;
})
];
};
inherit (import "${crate2nix}/tools.nix" { inherit pkgs; })
generatedCargoNix;
outputs = {
self,
nixpkgs,
utils,
naersk,
flake-compat,
}:
utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages."${system}";
naersk-lib = naersk.lib."${system}";
in rec {
# `nix build`
packages.bakare = naersk-lib.buildPackage {
pname = "bakare";
root = ./.;
};
defaultPackage = packages.bakare;
# Create the cargo2nix project
project = pkgs.callPackage
(generatedCargoNix {
inherit name;
src = ./.;
})
{
# Individual crate overrides go here
# Example: https://github.com/balsoft/simple-osd-daemons/blob/6f85144934c0c1382c7a4d3a2bbb80106776e270/flake.nix#L28-L50
defaultCrateOverrides = pkgs.defaultCrateOverrides // {
# The himalaya crate itself is overriden here. Typically we
# configure non-Rust dependencies (see below) here.
${name} = oldAttrs: {
inherit buildInputs nativeBuildInputs;
};
};
};
buildInputs = with pkgs; [ openssl.dev cacert openssh zlib ];
nativeBuildInputs = with pkgs; [ rustc cargo pkgconfig git ];
in
rec {
packages.${name} = project.rootCrate.build;
# `nix build`
defaultPackage = packages.${name};
# `nix run`
apps.${name} = utils.lib.mkApp {
inherit name;
drv = packages.${name};
};
defaultApp = apps.${name};
# `nix develop`
devShell = pkgs.mkShell
{
inputsFrom = builtins.attrValues self.packages.${system};
buildInputs = buildInputs ++ (with pkgs;
[
nixpkgs-fmt
cargo-watch
cargo-edit
cargo-outdated
pkgs.rust-bin.${rustChannel}.latest.rust-analysis
pkgs.rust-bin.${rustChannel}.latest.rls
]);
RUST_SRC_PATH = "${pkgs.rust-bin.${rustChannel}.latest.rust-src}/lib/rustlib/src/rust/library";
};
}
);
}
# `nix run`
apps.bakare = utils.lib.mkApp {drv = packages.bakare;};
defaultApp = apps.bakare;
# `nix develop`
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
cacert
cargo
cargo-edit
cargo-mutants
cargo-nextest
cargo-outdated
cargo-release
cargo-watch
clippy
git
llvmPackages_13.llvm
nixpkgs-fmt
openssh
openssl
pkg-config
rustc
rustfmt
statix
];
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
};
});
}

View file

@ -1,12 +1,12 @@
(import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{
src = ./.;

View file

@ -6,9 +6,8 @@ use std::{
path::{Path, PathBuf},
};
use chacha20poly1305::aead::{Aead, NewAead};
use chacha20poly1305::{Key, XChaCha20Poly1305, XNonce}; // Or `XChaCha20Poly1305`
use chacha20poly1305::{aead::Aead, Key, KeyInit, XChaCha20Poly1305, XNonce};
use nix::unistd::getpid;
use uuid::Uuid;
use crate::index::item::IndexItem;
@ -18,7 +17,6 @@ use crate::repository::ItemId;
use anyhow::Result;
use anyhow::*;
use lock::Lock;
use nix::unistd::getpid;
use std::{cmp::max, io::Write};
impl Index {

View file

@ -96,14 +96,14 @@ impl Lock {
let lock_file_path = Lock::lock_file_path(index_directory, lock_id)?;
fail_point!("create-lock-file", |e: Option<String>| Err(anyhow!(e.unwrap())));
let mut file = File::create(lock_file_path)?;
let lock_id_text = lock_id.to_hyphenated().to_string();
let lock_id_text = lock_id.as_hyphenated().to_string();
let lock_id_bytes = lock_id_text.as_bytes();
Ok(file.write_all(lock_id_bytes)?)
}
fn lock_file_path(path: &Path, lock_id: Uuid) -> Result<PathBuf> {
let file_name = format!("{}{}", lock_id, FILE_EXTENSION);
Ok(path.join(&file_name))
Ok(path.join(file_name))
}
}

View file

@ -1 +1,13 @@
fn main() {}
use seahorse::App;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let app = App::new(env!("CARGO_PKG_NAME"))
.description(env!("CARGO_PKG_DESCRIPTION"))
.author(env!("CARGO_PKG_AUTHORS"))
.version(env!("CARGO_PKG_VERSION"))
.action(|c| println!("Hello, {:?}", c.args));
app.run(args);
}

View file

@ -39,7 +39,7 @@ impl RepositoryItem {
let original_source_path = Path::new(self.original_source_path());
let source_path_relative = original_source_path.strip_prefix("/")?;
let target_path = save_to.join(&source_path_relative);
let target_path = save_to.join(source_path_relative);
let parent = target_path
.parent()
.ok_or_else(|| anyhow!("cannot compute parent path for {}", &target_path.to_string_lossy()))?;

View file

@ -43,13 +43,15 @@ pub struct RepositoryItemIterator<'a> {
//TODO: move to serializers::base64
mod base64 {
use ::base64;
use base64::{engine::general_purpose, Engine};
use serde::{de, Deserialize, Deserializer, Serializer};
pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&base64::encode(bytes))
let encoded = general_purpose::STANDARD.encode(bytes);
serializer.serialize_str(&encoded)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
@ -57,7 +59,7 @@ mod base64 {
D: Deserializer<'de>,
{
let s = <&str>::deserialize(deserializer)?;
base64::decode(s).map_err(de::Error::custom)
general_purpose::STANDARD.decode(s).map_err(de::Error::custom)
}
}
@ -97,7 +99,7 @@ impl Debug for ItemId {
}
}
impl<'a> Repository {
impl Repository {
pub fn init(path: &Path, secret: &str) -> Result<Repository> {
fs::create_dir_all(path)?;
let mut index = Index::new()?;
@ -129,7 +131,7 @@ impl<'a> Repository {
pub fn store(&mut self, source_path: &Path) -> Result<()> {
let id = Repository::calculate_id(source_path)?;
let destination = self.data_dir()?;
let destination = destination.join(&id.to_string());
let destination = destination.join(id.to_string());
if !source_path.metadata()?.is_file() {
return Ok(());
@ -139,9 +141,9 @@ impl<'a> Repository {
.ok_or_else(|| anyhow!("cannot compute parent path for {}", &destination.to_string_lossy()))?;
fs::create_dir_all(parent)?;
if !destination.exists() {
fs::copy(&source_path, &destination)?;
fs::copy(source_path, &destination)?;
}
let relative_path = destination.strip_prefix(&self.path())?;
let relative_path = destination.strip_prefix(self.path())?;
self.index.remember(source_path, &relative_path.to_string_lossy(), id);
Ok(())
}

View file

@ -50,7 +50,7 @@ pub mod in_memory {
restore_engine.restore(&item.unwrap())?;
let source_file_relative_path = Path::new(source_file_full_path).strip_prefix("/")?;
let restored_file_path = restore_target.path().join(&source_file_relative_path);
let restored_file_path = restore_target.path().join(source_file_relative_path);
assert_target_file_contents(&restored_file_path, contents)
}
@ -67,7 +67,7 @@ pub mod in_memory {
let restore_engine = restore::Engine::new(&mut restore_repository, restore_target.path())?;
restore_engine.restore(&old_item.unwrap())?;
let source_file_relative_path = Path::new(source_file_full_path).strip_prefix("/")?;
let restored_file_path = restore_target.path().join(&source_file_relative_path);
let restored_file_path = restore_target.path().join(source_file_relative_path);
assert_target_file_contents(&restored_file_path, old_contents)
}

View file

@ -22,7 +22,7 @@ mod must {
let dir = tempdir()?;
let repository_path = dir.path();
let repository_path = repository_path.join(&format!("repository-{}", getpid()));
let repository_path = repository_path.join(format!("repository-{}", getpid()));
let secret = "some secret";
Repository::init(&repository_path, secret)?;