-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
181 lines (159 loc) · 5.92 KB
/
Copy pathlib.rs
File metadata and controls
181 lines (159 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell
//
// JanusKey CLI: Provably Reversible File Operations
// Through Maximal Principle Reduction (MPR)
//
// Core types are provided by reversible-core. This crate adds:
// - Filesystem operation execution (operations.rs)
// - Key management (keys.rs)
// - Audit trail (attestation.rs)
// - Secure deletion (obliteration.rs, delta.rs)
// - CLI interface (main.rs, keys_cli.rs)
#![forbid(unsafe_code)]
pub mod attestation;
pub mod delta;
pub mod keys;
pub mod obliteration;
pub mod operations;
// Re-export core types from reversible-core for backward compatibility
pub use reversible_core::content_store::{self, ContentHash, ContentStore};
/// Error module — re-exports reversible-core error types with JanusKey naming
pub mod error {
pub use reversible_core::error::ReversibleError as JanusError;
pub use reversible_core::error::Result;
}
pub use error::{JanusError, Result};
pub use reversible_core::manifest::{self, ManifestEmitter};
pub use reversible_core::metadata::{self, MetadataStore, OperationMetadata, OperationType};
pub use reversible_core::transaction::{
self, Transaction, TransactionManager, TransactionPreview,
};
pub use reversible_core::ReversibleExecutor;
pub use attestation::{AuditEntry, AuditEventType, AuditLog, IntegrityReport, KeyEventDetails};
pub use keys::{KeyAlgorithm, KeyError, KeyManager, KeyMetadata, KeyPurpose, KeyState};
pub use operations::{FileOperation, OperationExecutor};
/// JanusKey configuration
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Config {
/// Path to JanusKey metadata storage
pub storage_path: std::path::PathBuf,
/// Enable compression for stored content
pub compression: bool,
/// Maximum number of operations to keep in history
pub max_history: usize,
/// Auto-confirm dangerous operations
pub auto_confirm: bool,
/// Default to dry-run mode
pub dry_run_default: bool,
/// Enable audit trail
pub audit_enabled: bool,
}
impl Default for Config {
fn default() -> Self {
let storage_path = dirs::data_local_dir()
.unwrap_or_else(|| std::path::PathBuf::from("."))
.join("januskey");
Self {
storage_path,
compression: true,
max_history: 10000,
auto_confirm: false,
dry_run_default: false,
audit_enabled: true,
}
}
}
impl Config {
/// Load config from directory's .januskey/config.json or use defaults
pub fn load(dir: &std::path::Path) -> Self {
let config_path = dir.join(".januskey").join("config.json");
if config_path.exists() {
if let Ok(content) = ({ use std::io::Read; std::fs::File::open(&config_path).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }) {
if let Ok(config) = serde_json::from_str(&content) {
return config;
}
}
}
Self::default()
}
/// Save config to directory
pub fn save(&self, dir: &std::path::Path) -> Result<()> {
let config_dir = dir.join(".januskey");
std::fs::create_dir_all(&config_dir)?;
let config_path = config_dir.join("config.json");
let content = serde_json::to_string_pretty(self)?;
std::fs::write(config_path, content)?;
Ok(())
}
}
/// Main JanusKey instance for a directory
pub struct JanusKey {
/// Working directory
pub root: std::path::PathBuf,
/// Configuration
pub config: Config,
/// Content-addressed storage
pub content_store: ContentStore,
/// Metadata/operation log store
pub metadata_store: MetadataStore,
/// Transaction manager
pub transaction_manager: TransactionManager,
}
impl JanusKey {
/// Initialize JanusKey for a directory
pub fn init(root: &std::path::Path) -> Result<Self> {
let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
let jk_dir = root.join(".januskey");
std::fs::create_dir_all(&jk_dir)?;
let config = Config::load(&root);
config.save(&root)?;
let content_store = ContentStore::new(jk_dir.join("content"), config.compression)?;
let metadata_store = MetadataStore::new(jk_dir.join("metadata.json"))?;
let transaction_manager = TransactionManager::new(jk_dir.join("transactions"))?;
Ok(Self {
root,
config,
content_store,
metadata_store,
transaction_manager,
})
}
/// Open existing JanusKey directory
pub fn open(root: &std::path::Path) -> Result<Self> {
let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
let jk_dir = root.join(".januskey");
if !jk_dir.exists() {
return Err(JanusError::NotInitialized(root.display().to_string()));
}
let config = Config::load(&root);
let content_store = ContentStore::new(jk_dir.join("content"), config.compression)?;
let metadata_store = MetadataStore::new(jk_dir.join("metadata.json"))?;
let transaction_manager = TransactionManager::new(jk_dir.join("transactions"))?;
Ok(Self {
root,
config,
content_store,
metadata_store,
transaction_manager,
})
}
/// Check if directory is initialized
pub fn is_initialized(root: &std::path::Path) -> bool {
root.join(".januskey").exists()
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_init_and_open() {
let tmp = TempDir::new().unwrap();
let jk = JanusKey::init(tmp.path()).unwrap();
assert!(JanusKey::is_initialized(tmp.path()));
let jk2 = JanusKey::open(tmp.path()).unwrap();
assert_eq!(jk.root, jk2.root);
}
}