-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnapshot.rs
More file actions
515 lines (436 loc) · 14.1 KB
/
snapshot.rs
File metadata and controls
515 lines (436 loc) · 14.1 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Snapshot management for undo/recovery.
//!
//! Provides file system snapshots for reverting changes.
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use crate::error::{CortexError, Result};
/// Snapshot of file system state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Snapshot {
/// Snapshot ID.
pub id: String,
/// Description.
pub description: String,
/// Timestamp.
pub timestamp: u64,
/// Turn ID that created this snapshot.
pub turn_id: Option<String>,
/// File states.
pub files: HashMap<PathBuf, FileState>,
/// Directories created.
pub directories: Vec<PathBuf>,
}
impl Snapshot {
/// Create a new empty snapshot.
pub fn new(id: impl Into<String>, description: impl Into<String>) -> Self {
Self {
id: id.into(),
description: description.into(),
timestamp: timestamp_now(),
turn_id: None,
files: HashMap::new(),
directories: Vec::new(),
}
}
/// Set turn ID.
pub fn with_turn(mut self, turn_id: impl Into<String>) -> Self {
self.turn_id = Some(turn_id.into());
self
}
/// Add a file to the snapshot.
pub fn add_file(&mut self, path: impl AsRef<Path>, state: FileState) {
self.files.insert(path.as_ref().to_path_buf(), state);
}
/// Add a directory.
pub fn add_directory(&mut self, path: impl AsRef<Path>) {
self.directories.push(path.as_ref().to_path_buf());
}
/// Capture file state from disk.
pub fn capture_file(&mut self, path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
let state = if path.exists() {
let content = fs::read(path)?;
let metadata = fs::metadata(path)?;
FileState::Exists {
content,
permissions: get_permissions(&metadata),
modified: metadata
.modified()
.ok()
.and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok())
.map(|d| d.as_secs()),
}
} else {
FileState::NotExists
};
self.files.insert(path.to_path_buf(), state);
Ok(())
}
/// Capture multiple files.
pub fn capture_files<P: AsRef<Path>>(
&mut self,
paths: impl IntoIterator<Item = P>,
) -> Result<()> {
for path in paths {
self.capture_file(path)?;
}
Ok(())
}
/// Get files that would be modified.
pub fn files_to_modify(&self) -> Vec<&PathBuf> {
self.files.keys().collect()
}
/// Get number of files.
pub fn file_count(&self) -> usize {
self.files.len()
}
/// Check if snapshot is empty.
pub fn is_empty(&self) -> bool {
self.files.is_empty() && self.directories.is_empty()
}
}
/// File state in a snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FileState {
/// File exists with content.
Exists {
/// File content.
content: Vec<u8>,
/// File permissions (Unix mode).
permissions: Option<u32>,
/// Last modified timestamp.
modified: Option<u64>,
},
/// File does not exist.
NotExists,
}
impl FileState {
/// Check if file exists in this state.
pub fn exists(&self) -> bool {
matches!(self, Self::Exists { .. })
}
/// Get content if exists.
pub fn content(&self) -> Option<&[u8]> {
match self {
Self::Exists { content, .. } => Some(content),
Self::NotExists => None,
}
}
/// Get content as string if valid UTF-8.
pub fn content_str(&self) -> Option<&str> {
self.content().and_then(|c| std::str::from_utf8(c).ok())
}
}
/// Get file permissions.
#[cfg(unix)]
fn get_permissions(metadata: &std::fs::Metadata) -> Option<u32> {
use std::os::unix::fs::PermissionsExt;
Some(metadata.permissions().mode())
}
#[cfg(not(unix))]
fn get_permissions(_metadata: &std::fs::Metadata) -> Option<u32> {
None
}
/// Snapshot manager for maintaining snapshot history.
pub struct SnapshotManager {
/// Snapshots indexed by ID.
snapshots: RwLock<HashMap<String, Snapshot>>,
/// Snapshot order (newest first).
order: RwLock<Vec<String>>,
/// Maximum snapshots to keep.
max_snapshots: usize,
/// Storage directory.
storage_dir: Option<PathBuf>,
}
impl SnapshotManager {
/// Create a new snapshot manager.
pub fn new(max_snapshots: usize) -> Self {
Self {
snapshots: RwLock::new(HashMap::new()),
order: RwLock::new(Vec::new()),
max_snapshots,
storage_dir: None,
}
}
/// Set storage directory for persistence.
pub fn with_storage(mut self, dir: impl AsRef<Path>) -> Self {
self.storage_dir = Some(dir.as_ref().to_path_buf());
self
}
/// Create a new snapshot.
pub async fn create(&self, description: impl Into<String>) -> Snapshot {
let id = generate_snapshot_id();
Snapshot::new(id, description)
}
/// Save a snapshot.
pub async fn save(&self, snapshot: Snapshot) -> Result<()> {
let id = snapshot.id.clone();
// Add to storage
let mut snapshots = self.snapshots.write().await;
let mut order = self.order.write().await;
snapshots.insert(id.clone(), snapshot);
order.insert(0, id);
// Trim old snapshots
while order.len() > self.max_snapshots {
if let Some(old_id) = order.pop() {
snapshots.remove(&old_id);
}
}
// Persist if storage is configured
if let Some(ref dir) = self.storage_dir {
self.persist_to_disk(dir, &snapshots).await?;
}
Ok(())
}
/// Get a snapshot by ID.
pub async fn get(&self, id: &str) -> Option<Snapshot> {
self.snapshots.read().await.get(id).cloned()
}
/// Get the latest snapshot.
pub async fn latest(&self) -> Option<Snapshot> {
let order = self.order.read().await;
if let Some(id) = order.first() {
self.snapshots.read().await.get(id).cloned()
} else {
None
}
}
/// Get snapshot for a turn.
pub async fn for_turn(&self, turn_id: &str) -> Option<Snapshot> {
let snapshots = self.snapshots.read().await;
snapshots
.values()
.find(|s| s.turn_id.as_deref() == Some(turn_id))
.cloned()
}
/// List all snapshots.
pub async fn list(&self) -> Vec<SnapshotInfo> {
let snapshots = self.snapshots.read().await;
let order = self.order.read().await;
order
.iter()
.filter_map(|id| snapshots.get(id))
.map(|s| SnapshotInfo {
id: s.id.clone(),
description: s.description.clone(),
timestamp: s.timestamp,
file_count: s.files.len(),
})
.collect()
}
/// Delete a snapshot.
pub async fn delete(&self, id: &str) -> Result<()> {
let mut snapshots = self.snapshots.write().await;
let mut order = self.order.write().await;
snapshots.remove(id);
order.retain(|i| i != id);
Ok(())
}
/// Clear all snapshots.
pub async fn clear(&self) {
self.snapshots.write().await.clear();
self.order.write().await.clear();
}
/// Restore a snapshot to disk.
pub async fn restore(&self, id: &str) -> Result<RestoreResult> {
let snapshot = self
.get(id)
.await
.ok_or_else(|| CortexError::NotFound(format!("Snapshot not found: {id}")))?;
let mut result = RestoreResult::new();
// Restore files
for (path, state) in &snapshot.files {
match state {
FileState::Exists {
content,
#[cfg(unix)]
permissions,
#[cfg(not(unix))]
permissions: _,
..
} => {
// Create parent directories
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
// Write content
fs::write(path, content)?;
#[cfg(unix)]
if let Some(mode) = permissions {
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(path, std::fs::Permissions::from_mode(*mode))?;
}
result.restored.push(path.clone());
}
FileState::NotExists => {
// Delete file if it exists
if path.exists() {
fs::remove_file(path)?;
result.deleted.push(path.clone());
}
}
}
}
// Remove directories that were created after snapshot
for dir in snapshot.directories.iter().rev() {
if dir.exists() && dir.is_dir() {
// Only remove if empty
if fs::read_dir(dir)?.next().is_none() {
fs::remove_dir(dir)?;
result.directories_removed.push(dir.clone());
}
}
}
Ok(result)
}
/// Persist snapshots to disk.
async fn persist_to_disk(
&self,
dir: &Path,
snapshots: &HashMap<String, Snapshot>,
) -> Result<()> {
fs::create_dir_all(dir)?;
for (id, snapshot) in snapshots {
let path = dir.join(format!("{id}.json"));
let json = serde_json::to_string_pretty(snapshot)
.map_err(|e| CortexError::Serialization(e.to_string()))?;
fs::write(path, json)?;
}
Ok(())
}
/// Load snapshots from disk.
pub async fn load_from_disk(&self) -> Result<()> {
let dir = match &self.storage_dir {
Some(d) => d,
None => return Ok(()),
};
if !dir.exists() {
return Ok(());
}
let mut snapshots = self.snapshots.write().await;
let mut order = self.order.write().await;
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().map(|e| e == "json").unwrap_or(false) {
let content = fs::read_to_string(&path)?;
let snapshot: Snapshot = serde_json::from_str(&content)
.map_err(|e| CortexError::Serialization(e.to_string()))?;
order.push(snapshot.id.clone());
snapshots.insert(snapshot.id.clone(), snapshot);
}
}
// Sort by timestamp (newest first)
order.sort_by(|a, b| {
let ts_a = snapshots.get(a).map(|s| s.timestamp).unwrap_or(0);
let ts_b = snapshots.get(b).map(|s| s.timestamp).unwrap_or(0);
ts_b.cmp(&ts_a)
});
Ok(())
}
}
/// Snapshot info for listing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnapshotInfo {
/// Snapshot ID.
pub id: String,
/// Description.
pub description: String,
/// Timestamp.
pub timestamp: u64,
/// Number of files.
pub file_count: usize,
}
/// Result of a restore operation.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RestoreResult {
/// Files restored.
pub restored: Vec<PathBuf>,
/// Files deleted.
pub deleted: Vec<PathBuf>,
/// Directories removed.
pub directories_removed: Vec<PathBuf>,
}
impl RestoreResult {
/// Create a new restore result.
pub fn new() -> Self {
Self::default()
}
/// Get total changes.
pub fn total_changes(&self) -> usize {
self.restored.len() + self.deleted.len() + self.directories_removed.len()
}
/// Check if anything was changed.
pub fn has_changes(&self) -> bool {
self.total_changes() > 0
}
}
/// Generate a unique snapshot ID.
fn generate_snapshot_id() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let ts = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis())
.unwrap_or(0);
format!("snap_{ts:x}")
}
/// Get current timestamp.
fn timestamp_now() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[test]
fn test_snapshot_creation() {
let snapshot = Snapshot::new("test-1", "Test snapshot");
assert_eq!(snapshot.id, "test-1");
assert!(snapshot.is_empty());
}
#[test]
fn test_file_state() {
let state = FileState::Exists {
content: b"hello".to_vec(),
permissions: Some(0o644),
modified: Some(12345),
};
assert!(state.exists());
assert_eq!(state.content(), Some(b"hello".as_slice()));
assert_eq!(state.content_str(), Some("hello"));
}
#[tokio::test]
async fn test_snapshot_manager() {
let manager = SnapshotManager::new(10);
let mut snapshot = manager.create("Test").await;
snapshot.add_file(
"/test.txt",
FileState::Exists {
content: b"test".to_vec(),
permissions: None,
modified: None,
},
);
manager.save(snapshot).await.unwrap();
let latest = manager.latest().await.unwrap();
assert_eq!(latest.file_count(), 1);
}
#[test]
fn test_capture_file() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("test.txt");
fs::write(&file_path, "hello").unwrap();
let mut snapshot = Snapshot::new("test", "Test");
snapshot.capture_file(&file_path).unwrap();
assert_eq!(snapshot.file_count(), 1);
let state = snapshot.files.get(&file_path).unwrap();
assert_eq!(state.content_str(), Some("hello"));
}
}