-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.rs
More file actions
416 lines (370 loc) · 12.5 KB
/
Copy pathmetadata.rs
File metadata and controls
416 lines (370 loc) · 12.5 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// 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
//
// Metadata Store: Operation log with complete reverse information
// Implements the formal model from the JanusKey white paper
use crate::content_store::ContentHash;
use crate::error::{ReversibleError, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};
use uuid::Uuid;
/// Operation type identifier.
///
/// Each variant has a known inverse (per absolute-zero CNO theory):
/// - Delete ↔ Create
/// - Modify is self-inverse (stores both old and new content)
/// - Move is self-inverse (swap source/destination)
/// - Copy → Delete (of the copy)
/// - Chmod/Chown are self-inverse (store old values)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum OperationType {
Delete,
Modify,
Move,
Copy,
Chmod,
Chown,
Create,
}
impl OperationType {
/// Get the inverse operation type.
///
/// Per absolute-zero: `op ; inverse(op) ≡ CNO`
pub fn inverse(&self) -> Self {
match self {
Self::Delete => Self::Create,
Self::Create => Self::Delete,
Self::Modify => Self::Modify,
Self::Move => Self::Move,
Self::Copy => Self::Delete,
Self::Chmod => Self::Chmod,
Self::Chown => Self::Chown,
}
}
}
impl std::fmt::Display for OperationType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Delete => write!(f, "DELETE"),
Self::Modify => write!(f, "MODIFY"),
Self::Move => write!(f, "MOVE"),
Self::Copy => write!(f, "COPY"),
Self::Chmod => write!(f, "CHMOD"),
Self::Chown => write!(f, "CHOWN"),
Self::Create => write!(f, "CREATE"),
}
}
}
/// File metadata (permissions, timestamps, owner).
///
/// Captured before destructive operations to enable perfect reversal.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileMetadata {
/// Unix permissions (e.g., 0o644)
pub permissions: u32,
/// File owner (username or uid)
pub owner: String,
/// File group (groupname or gid)
pub group: String,
/// Original file size
pub size: u64,
/// Last modification time
pub modified: DateTime<Utc>,
/// Is this a symbolic link?
pub is_symlink: bool,
/// Symlink target if is_symlink
pub symlink_target: Option<String>,
}
impl FileMetadata {
/// Capture metadata from a file path
pub fn from_path(path: &Path) -> Result<Self> {
let metadata = fs::symlink_metadata(path)?;
#[cfg(unix)]
let (permissions, owner, group) = {
use std::os::unix::fs::MetadataExt;
(
metadata.mode(),
metadata.uid().to_string(),
metadata.gid().to_string(),
)
};
#[cfg(not(unix))]
let (permissions, owner, group) = (0o644, "unknown".to_string(), "unknown".to_string());
let is_symlink = metadata.file_type().is_symlink();
let symlink_target = if is_symlink {
fs::read_link(path)
.ok()
.map(|p| p.to_string_lossy().to_string())
} else {
None
};
Ok(Self {
permissions,
owner,
group,
size: metadata.len(),
modified: DateTime::from(metadata.modified()?),
is_symlink,
symlink_target,
})
}
/// Apply metadata to a file (restore permissions)
#[cfg(unix)]
pub fn apply(&self, path: &Path) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
let perms = fs::Permissions::from_mode(self.permissions);
fs::set_permissions(path, perms)?;
Ok(())
}
#[cfg(not(unix))]
pub fn apply(&self, _path: &Path) -> Result<()> {
Ok(())
}
}
/// Complete metadata for an operation (sufficient for reversal).
///
/// Contains all information needed to perfectly reverse the operation,
/// including content hashes (referencing the ContentStore), file metadata,
/// and transaction membership.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationMetadata {
/// Unique operation ID
pub id: String,
/// Operation type
pub op_type: OperationType,
/// When the operation occurred
pub timestamp: DateTime<Utc>,
/// User who performed the operation
pub user: String,
/// Primary path affected
pub path: PathBuf,
/// Secondary path (for move/copy operations)
pub path_secondary: Option<PathBuf>,
/// Hash of original content (for delete/modify — references ContentStore)
pub content_hash: Option<ContentHash>,
/// Hash of new content (for modify — references ContentStore)
pub new_content_hash: Option<ContentHash>,
/// Original file metadata (permissions, owner, timestamps)
pub original_metadata: Option<FileMetadata>,
/// New metadata (for chmod/chown)
pub new_metadata: Option<FileMetadata>,
/// Transaction ID if part of a transaction
pub transaction_id: Option<String>,
/// Whether this operation has been undone
pub undone: bool,
/// ID of the undo operation (if undone)
pub undo_operation_id: Option<String>,
}
impl OperationMetadata {
/// Create new operation metadata with generated ID
pub fn new(op_type: OperationType, path: PathBuf) -> Self {
Self {
id: Uuid::new_v4().to_string(),
op_type,
timestamp: Utc::now(),
user: whoami::username(),
path,
path_secondary: None,
content_hash: None,
new_content_hash: None,
original_metadata: None,
new_metadata: None,
transaction_id: None,
undone: false,
undo_operation_id: None,
}
}
/// Builder: set secondary path
pub fn with_secondary_path(mut self, path: PathBuf) -> Self {
self.path_secondary = Some(path);
self
}
/// Builder: set content hash (original content before operation)
pub fn with_content_hash(mut self, hash: ContentHash) -> Self {
self.content_hash = Some(hash);
self
}
/// Builder: set new content hash (content after operation)
pub fn with_new_content_hash(mut self, hash: ContentHash) -> Self {
self.new_content_hash = Some(hash);
self
}
/// Builder: set original file metadata
pub fn with_original_metadata(mut self, metadata: FileMetadata) -> Self {
self.original_metadata = Some(metadata);
self
}
/// Builder: set transaction ID
pub fn with_transaction_id(mut self, id: String) -> Self {
self.transaction_id = Some(id);
self
}
}
/// Serializable operation log (the append-only ledger)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationLog {
/// Version for format compatibility
pub version: String,
/// List of all operations (append-only)
pub operations: Vec<OperationMetadata>,
}
impl Default for OperationLog {
fn default() -> Self {
Self {
version: "1.0".to_string(),
operations: Vec::new(),
}
}
}
/// Metadata store for operation logging.
///
/// Wraps an `OperationLog` with filesystem persistence.
pub struct MetadataStore {
/// Path to the metadata file
path: PathBuf,
/// Cached operation log
log: OperationLog,
}
impl MetadataStore {
/// Create or open a metadata store
pub fn new(path: PathBuf) -> Result<Self> {
let log = if path.exists() {
let content = ({ use std::io::Read; std::fs::File::open(&path).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })?;
serde_json::from_str(&content)
.map_err(|e| ReversibleError::MetadataCorrupted(e.to_string()))?
} else {
OperationLog::default()
};
Ok(Self { path, log })
}
/// Append an operation to the log
pub fn append(&mut self, metadata: OperationMetadata) -> Result<()> {
self.log.operations.push(metadata);
self.save()
}
/// Save the log to disk
fn save(&self) -> Result<()> {
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent)?;
}
let content = serde_json::to_string_pretty(&self.log)?;
fs::write(&self.path, content)?;
Ok(())
}
/// Get all operations
pub fn operations(&self) -> &[OperationMetadata] {
&self.log.operations
}
/// Get operation by ID
pub fn get(&self, id: &str) -> Option<&OperationMetadata> {
self.log.operations.iter().find(|op| op.id == id)
}
/// Get mutable operation by ID
pub fn get_mut(&mut self, id: &str) -> Option<&mut OperationMetadata> {
self.log.operations.iter_mut().find(|op| op.id == id)
}
/// Get last N non-undone operations
pub fn last_n(&self, n: usize) -> Vec<&OperationMetadata> {
self.log
.operations
.iter()
.rev()
.filter(|op| !op.undone)
.take(n)
.collect()
}
/// Get last undoable operation
pub fn last_undoable(&self) -> Option<&OperationMetadata> {
self.log.operations.iter().rev().find(|op| !op.undone)
}
/// Get operations for a transaction
pub fn transaction_operations(&self, transaction_id: &str) -> Vec<&OperationMetadata> {
self.log
.operations
.iter()
.filter(|op| op.transaction_id.as_deref() == Some(transaction_id))
.collect()
}
/// Mark operation as undone
pub fn mark_undone(&mut self, id: &str, undo_op_id: &str) -> Result<()> {
if let Some(op) = self.get_mut(id) {
op.undone = true;
op.undo_operation_id = Some(undo_op_id.to_string());
self.save()?;
}
Ok(())
}
/// Filter operations by type
pub fn filter_by_type(&self, op_type: OperationType) -> Vec<&OperationMetadata> {
self.log
.operations
.iter()
.filter(|op| op.op_type == op_type)
.collect()
}
/// Filter operations by path pattern
pub fn filter_by_path(&self, pattern: &str) -> Result<Vec<&OperationMetadata>> {
let glob_pattern = glob::Pattern::new(pattern)?;
Ok(self
.log
.operations
.iter()
.filter(|op| glob_pattern.matches_path(&op.path))
.collect())
}
/// Get operation count
pub fn count(&self) -> usize {
self.log.operations.len()
}
/// Prune old operations (keep last N)
pub fn prune(&mut self, keep: usize) -> Result<usize> {
let original_count = self.log.operations.len();
if original_count <= keep {
return Ok(0);
}
let to_remove = original_count - keep;
self.log.operations.drain(0..to_remove);
self.save()?;
Ok(to_remove)
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_operation_metadata_creation() {
let meta =
OperationMetadata::new(OperationType::Delete, PathBuf::from("/test/file.txt"));
assert!(!meta.id.is_empty());
assert_eq!(meta.op_type, OperationType::Delete);
assert!(!meta.undone);
}
#[test]
fn test_operation_type_inverse() {
assert_eq!(OperationType::Delete.inverse(), OperationType::Create);
assert_eq!(OperationType::Create.inverse(), OperationType::Delete);
assert_eq!(OperationType::Move.inverse(), OperationType::Move);
assert_eq!(OperationType::Modify.inverse(), OperationType::Modify);
}
#[test]
fn test_metadata_store() {
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("metadata.json");
let mut store = MetadataStore::new(path.clone()).unwrap();
let meta =
OperationMetadata::new(OperationType::Delete, PathBuf::from("/test.txt"));
let id = meta.id.clone();
store.append(meta).unwrap();
assert_eq!(store.count(), 1);
assert!(store.get(&id).is_some());
// Reopen and verify persistence
let store2 = MetadataStore::new(path).unwrap();
assert_eq!(store2.count(), 1);
assert!(store2.get(&id).is_some());
}
}