amaters-core 0.2.0

Core kernel for AmateRS - Fully Homomorphic Encrypted Database
Documentation
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
//! Backup and restore functionality for the storage engine
//!
//! Provides full backup creation, restoration, integrity verification,
//! and backup lifecycle management. Backups are stored as direct file
//! copies (no compression/archiving) with CRC32 checksums for integrity.

use crate::error::{AmateRSError, ErrorContext, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};

/// Backup metadata persisted alongside backup data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupMetadata {
    /// Unique identifier for this backup
    pub backup_id: String,
    /// Timestamp when the backup was created
    pub created_at: DateTime<Utc>,
    /// Source directory that was backed up
    pub source_dir: PathBuf,
    /// Total number of files in the backup
    pub total_files: usize,
    /// Total size in bytes of all backed-up files
    pub total_bytes: u64,
    /// CRC32 checksum of all files (deterministic ordering)
    pub checksum: u32,
    /// Type of backup (full or incremental)
    pub backup_type: BackupType,
    /// Software version at time of backup
    pub version: String,
}

/// Type of backup
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BackupType {
    /// Full backup containing all data
    Full,
    /// Incremental backup relative to a base backup
    Incremental {
        /// ID of the base backup this increment builds upon
        base_backup_id: String,
    },
}

/// Manages backup creation, restoration, and lifecycle
pub struct BackupManager {
    /// Root directory where all backups are stored
    backup_dir: PathBuf,
}

impl BackupManager {
    /// Create a new BackupManager with the given backup storage directory.
    ///
    /// Creates the directory if it does not exist.
    pub fn new(backup_dir: impl AsRef<Path>) -> Result<Self> {
        let backup_dir = backup_dir.as_ref().to_path_buf();
        fs::create_dir_all(&backup_dir).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to create backup directory '{}': {}",
                backup_dir.display(),
                e
            )))
        })?;
        Ok(Self { backup_dir })
    }

    /// Create a full backup of the source data directory.
    ///
    /// Copies all files recursively from `source_dir` into a new backup
    /// subdirectory, calculates a CRC32 checksum, and writes metadata.
    pub fn create_backup(&self, source_dir: &Path) -> Result<BackupMetadata> {
        if !source_dir.exists() {
            return Err(AmateRSError::ValidationError(ErrorContext::new(format!(
                "Source directory '{}' does not exist",
                source_dir.display()
            ))));
        }

        let backup_id = uuid::Uuid::new_v4().to_string();
        let backup_path = self.backup_dir.join(&backup_id);
        let data_path = backup_path.join("data");

        fs::create_dir_all(&data_path).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to create backup data directory: {}",
                e
            )))
        })?;

        let (total_files, total_bytes) = copy_dir_recursive(source_dir, &data_path)?;
        let checksum = calculate_dir_checksum(&data_path)?;

        let metadata = BackupMetadata {
            backup_id: backup_id.clone(),
            created_at: Utc::now(),
            source_dir: source_dir.to_path_buf(),
            total_files,
            total_bytes,
            checksum,
            backup_type: BackupType::Full,
            version: env!("CARGO_PKG_VERSION").to_string(),
        };

        let metadata_path = backup_path.join("metadata.json");
        let metadata_json = serde_json::to_string_pretty(&metadata).map_err(|e| {
            AmateRSError::SerializationError(ErrorContext::new(format!(
                "Failed to serialize backup metadata: {}",
                e
            )))
        })?;
        fs::write(&metadata_path, metadata_json).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to write backup metadata: {}",
                e
            )))
        })?;

        Ok(metadata)
    }

    /// Restore a backup to the given target directory.
    ///
    /// Verifies backup integrity before restoring. If `target_dir` already
    /// exists, it is cleared first.
    pub fn restore_backup(&self, backup_id: &str, target_dir: &Path) -> Result<BackupMetadata> {
        let backup_path = self.backup_dir.join(backup_id);
        if !backup_path.exists() {
            return Err(AmateRSError::ValidationError(ErrorContext::new(format!(
                "Backup '{}' does not exist",
                backup_id
            ))));
        }

        let metadata = self.load_metadata(backup_id)?;

        // Verify integrity before restoring
        if !self.verify_backup(backup_id)? {
            return Err(AmateRSError::StorageIntegrity(ErrorContext::new(format!(
                "Backup '{}' failed integrity check",
                backup_id
            ))));
        }

        // Clear target directory if it exists
        if target_dir.exists() {
            fs::remove_dir_all(target_dir).map_err(|e| {
                AmateRSError::IoError(ErrorContext::new(format!(
                    "Failed to clear target directory '{}': {}",
                    target_dir.display(),
                    e
                )))
            })?;
        }

        fs::create_dir_all(target_dir).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to create target directory '{}': {}",
                target_dir.display(),
                e
            )))
        })?;

        let data_path = backup_path.join("data");
        copy_dir_recursive(&data_path, target_dir)?;

        // Verify restored data matches backup checksum
        let restored_checksum = calculate_dir_checksum(target_dir)?;
        if restored_checksum != metadata.checksum {
            return Err(AmateRSError::StorageIntegrity(ErrorContext::new(format!(
                "Restored data checksum mismatch: expected {}, got {}",
                metadata.checksum, restored_checksum
            ))));
        }

        Ok(metadata)
    }

    /// List all available backups sorted by creation time (newest first).
    pub fn list_backups(&self) -> Result<Vec<BackupMetadata>> {
        let mut backups = Vec::new();

        let entries = fs::read_dir(&self.backup_dir).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to read backup directory: {}",
                e
            )))
        })?;

        for entry in entries {
            let entry = entry.map_err(|e| {
                AmateRSError::IoError(ErrorContext::new(format!(
                    "Failed to read directory entry: {}",
                    e
                )))
            })?;

            let path = entry.path();
            if path.is_dir() {
                let metadata_path = path.join("metadata.json");
                if metadata_path.exists() {
                    match self.load_metadata_from_path(&metadata_path) {
                        Ok(meta) => backups.push(meta),
                        Err(_) => {
                            // Skip directories without valid metadata
                            continue;
                        }
                    }
                }
            }
        }

        // Sort by creation time, newest first
        backups.sort_by_key(|b| std::cmp::Reverse(b.created_at));

        Ok(backups)
    }

    /// Delete a backup and all its data.
    pub fn delete_backup(&self, backup_id: &str) -> Result<()> {
        let backup_path = self.backup_dir.join(backup_id);
        if !backup_path.exists() {
            return Err(AmateRSError::ValidationError(ErrorContext::new(format!(
                "Backup '{}' does not exist",
                backup_id
            ))));
        }

        fs::remove_dir_all(&backup_path).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to delete backup '{}': {}",
                backup_id, e
            )))
        })?;

        Ok(())
    }

    /// Verify backup integrity by recalculating the CRC32 checksum.
    ///
    /// Returns `true` if the checksum matches, `false` otherwise.
    pub fn verify_backup(&self, backup_id: &str) -> Result<bool> {
        let backup_path = self.backup_dir.join(backup_id);
        if !backup_path.exists() {
            return Err(AmateRSError::ValidationError(ErrorContext::new(format!(
                "Backup '{}' does not exist",
                backup_id
            ))));
        }

        let metadata = self.load_metadata(backup_id)?;
        let data_path = backup_path.join("data");

        if !data_path.exists() {
            return Ok(metadata.total_files == 0 && metadata.checksum == 0);
        }

        let current_checksum = calculate_dir_checksum(&data_path)?;
        Ok(current_checksum == metadata.checksum)
    }

    /// Get the total size in bytes of a backup (data files only).
    pub fn backup_size(&self, backup_id: &str) -> Result<u64> {
        let backup_path = self.backup_dir.join(backup_id);
        if !backup_path.exists() {
            return Err(AmateRSError::ValidationError(ErrorContext::new(format!(
                "Backup '{}' does not exist",
                backup_id
            ))));
        }

        let data_path = backup_path.join("data");
        if !data_path.exists() {
            return Ok(0);
        }

        calculate_dir_size(&data_path)
    }

    /// Load backup metadata from the standard location.
    fn load_metadata(&self, backup_id: &str) -> Result<BackupMetadata> {
        let metadata_path = self.backup_dir.join(backup_id).join("metadata.json");
        self.load_metadata_from_path(&metadata_path)
    }

    /// Load backup metadata from an arbitrary path.
    fn load_metadata_from_path(&self, path: &Path) -> Result<BackupMetadata> {
        let content = fs::read_to_string(path).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to read metadata file '{}': {}",
                path.display(),
                e
            )))
        })?;

        serde_json::from_str(&content).map_err(|e| {
            AmateRSError::SerializationError(ErrorContext::new(format!(
                "Failed to deserialize backup metadata: {}",
                e
            )))
        })
    }
}

/// Copy a directory recursively from `src` to `dst`.
///
/// Returns `(file_count, total_bytes)` of all files copied.
fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<(usize, u64)> {
    let mut file_count = 0usize;
    let mut total_bytes = 0u64;

    if !src.exists() {
        return Ok((0, 0));
    }

    fs::create_dir_all(dst).map_err(|e| {
        AmateRSError::IoError(ErrorContext::new(format!(
            "Failed to create directory '{}': {}",
            dst.display(),
            e
        )))
    })?;

    let entries = fs::read_dir(src).map_err(|e| {
        AmateRSError::IoError(ErrorContext::new(format!(
            "Failed to read directory '{}': {}",
            src.display(),
            e
        )))
    })?;

    for entry in entries {
        let entry = entry.map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to read directory entry: {}",
                e
            )))
        })?;

        let src_path = entry.path();
        let file_name = entry.file_name();
        let dst_path = dst.join(&file_name);

        if src_path.is_dir() {
            let (sub_files, sub_bytes) = copy_dir_recursive(&src_path, &dst_path)?;
            file_count += sub_files;
            total_bytes += sub_bytes;
        } else if src_path.is_file() {
            let bytes = fs::copy(&src_path, &dst_path).map_err(|e| {
                AmateRSError::IoError(ErrorContext::new(format!(
                    "Failed to copy '{}' -> '{}': {}",
                    src_path.display(),
                    dst_path.display(),
                    e
                )))
            })?;
            file_count += 1;
            total_bytes += bytes;
        }
    }

    Ok((file_count, total_bytes))
}

/// Calculate CRC32 checksum of all files in a directory.
///
/// Files are processed in sorted order (by relative path) for determinism.
fn calculate_dir_checksum(dir: &Path) -> Result<u32> {
    let mut paths = collect_file_paths(dir, dir)?;
    paths.sort();

    let mut hasher = crc32fast::Hasher::new();

    for relative_path in &paths {
        let full_path = dir.join(relative_path);

        // Include the relative path in the checksum for structural integrity
        hasher.update(relative_path.to_string_lossy().as_bytes());

        let mut file = fs::File::open(&full_path).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to open file '{}' for checksum: {}",
                full_path.display(),
                e
            )))
        })?;

        let mut buffer = [0u8; 8192];
        loop {
            let bytes_read = file.read(&mut buffer).map_err(|e| {
                AmateRSError::IoError(ErrorContext::new(format!(
                    "Failed to read file '{}' for checksum: {}",
                    full_path.display(),
                    e
                )))
            })?;

            if bytes_read == 0 {
                break;
            }

            hasher.update(&buffer[..bytes_read]);
        }
    }

    Ok(hasher.finalize())
}

/// Collect all file paths relative to `base_dir` under `dir`.
fn collect_file_paths(dir: &Path, base_dir: &Path) -> Result<Vec<PathBuf>> {
    let mut paths = Vec::new();

    if !dir.exists() {
        return Ok(paths);
    }

    let entries = fs::read_dir(dir).map_err(|e| {
        AmateRSError::IoError(ErrorContext::new(format!(
            "Failed to read directory '{}': {}",
            dir.display(),
            e
        )))
    })?;

    for entry in entries {
        let entry = entry.map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to read directory entry: {}",
                e
            )))
        })?;

        let path = entry.path();

        if path.is_dir() {
            let sub_paths = collect_file_paths(&path, base_dir)?;
            paths.extend(sub_paths);
        } else if path.is_file() {
            let relative = path.strip_prefix(base_dir).map_err(|e| {
                AmateRSError::ValidationError(ErrorContext::new(format!(
                    "Failed to compute relative path: {}",
                    e
                )))
            })?;
            paths.push(relative.to_path_buf());
        }
    }

    Ok(paths)
}

/// Calculate total size of all files in a directory recursively.
fn calculate_dir_size(dir: &Path) -> Result<u64> {
    let mut total = 0u64;

    let entries = fs::read_dir(dir).map_err(|e| {
        AmateRSError::IoError(ErrorContext::new(format!(
            "Failed to read directory '{}': {}",
            dir.display(),
            e
        )))
    })?;

    for entry in entries {
        let entry = entry.map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!(
                "Failed to read directory entry: {}",
                e
            )))
        })?;

        let path = entry.path();
        if path.is_dir() {
            total += calculate_dir_size(&path)?;
        } else if path.is_file() {
            let meta = fs::metadata(&path).map_err(|e| {
                AmateRSError::IoError(ErrorContext::new(format!(
                    "Failed to get file metadata '{}': {}",
                    path.display(),
                    e
                )))
            })?;
            total += meta.len();
        }
    }

    Ok(total)
}

/// Verify that a directory's contents match an expected CRC32 checksum.
pub fn verify_directory(dir: &Path, expected_checksum: u32) -> Result<bool> {
    let actual = calculate_dir_checksum(dir)?;
    Ok(actual == expected_checksum)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Create a unique temp directory for a test
    fn test_dir(name: &str) -> PathBuf {
        let dir = std::env::temp_dir()
            .join("amaters_backup_tests")
            .join(name)
            .join(uuid::Uuid::new_v4().to_string());
        if dir.exists() {
            fs::remove_dir_all(&dir).ok();
        }
        fs::create_dir_all(&dir).ok();
        dir
    }

    /// Populate a directory with sample files for testing
    fn populate_source(dir: &Path) -> Result<()> {
        fs::create_dir_all(dir.join("subdir")).map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!("populate_source: {}", e)))
        })?;

        fs::write(dir.join("file1.dat"), b"hello world").map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!("populate_source: {}", e)))
        })?;

        fs::write(dir.join("file2.dat"), b"test data 1234567890").map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!("populate_source: {}", e)))
        })?;

        fs::write(dir.join("subdir").join("nested.dat"), b"nested content").map_err(|e| {
            AmateRSError::IoError(ErrorContext::new(format!("populate_source: {}", e)))
        })?;

        Ok(())
    }

    #[test]
    fn test_create_full_backup() -> Result<()> {
        let root = test_dir("create_full");
        let source = root.join("source");
        let backups = root.join("backups");

        populate_source(&source)?;
        let manager = BackupManager::new(&backups)?;
        let meta = manager.create_backup(&source)?;

        assert_eq!(meta.total_files, 3);
        assert!(meta.total_bytes > 0);
        assert!(matches!(meta.backup_type, BackupType::Full));

        // Verify backup directory exists with data and metadata
        let backup_path = backups.join(&meta.backup_id);
        assert!(backup_path.join("data").exists());
        assert!(backup_path.join("metadata.json").exists());
        assert!(backup_path.join("data").join("file1.dat").exists());
        assert!(
            backup_path
                .join("data")
                .join("subdir")
                .join("nested.dat")
                .exists()
        );

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_restore_backup() -> Result<()> {
        let root = test_dir("restore");
        let source = root.join("source");
        let backups = root.join("backups");
        let restored = root.join("restored");

        populate_source(&source)?;
        let manager = BackupManager::new(&backups)?;
        let meta = manager.create_backup(&source)?;

        let restored_meta = manager.restore_backup(&meta.backup_id, &restored)?;
        assert_eq!(restored_meta.backup_id, meta.backup_id);

        // Verify restored files match original
        let original_content = fs::read(source.join("file1.dat"))
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("read: {}", e))))?;
        let restored_content = fs::read(restored.join("file1.dat"))
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("read: {}", e))))?;
        assert_eq!(original_content, restored_content);

        let nested_original = fs::read(source.join("subdir").join("nested.dat"))
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("read: {}", e))))?;
        let nested_restored = fs::read(restored.join("subdir").join("nested.dat"))
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("read: {}", e))))?;
        assert_eq!(nested_original, nested_restored);

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_list_backups() -> Result<()> {
        let root = test_dir("list");
        let source = root.join("source");
        let backups = root.join("backups");

        populate_source(&source)?;
        let manager = BackupManager::new(&backups)?;

        // Create multiple backups
        let _meta1 = manager.create_backup(&source)?;
        let _meta2 = manager.create_backup(&source)?;
        let _meta3 = manager.create_backup(&source)?;

        let list = manager.list_backups()?;
        assert_eq!(list.len(), 3);

        // Should be sorted newest first
        assert!(list[0].created_at >= list[1].created_at);
        assert!(list[1].created_at >= list[2].created_at);

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_delete_backup() -> Result<()> {
        let root = test_dir("delete");
        let source = root.join("source");
        let backups = root.join("backups");

        populate_source(&source)?;
        let manager = BackupManager::new(&backups)?;
        let meta = manager.create_backup(&source)?;

        assert_eq!(manager.list_backups()?.len(), 1);

        manager.delete_backup(&meta.backup_id)?;

        assert_eq!(manager.list_backups()?.len(), 0);

        // Deleting non-existent backup should error
        let result = manager.delete_backup("nonexistent");
        assert!(result.is_err());

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_verify_backup() -> Result<()> {
        let root = test_dir("verify");
        let source = root.join("source");
        let backups = root.join("backups");

        populate_source(&source)?;
        let manager = BackupManager::new(&backups)?;
        let meta = manager.create_backup(&source)?;

        // Should pass verification
        assert!(manager.verify_backup(&meta.backup_id)?);

        // Corrupt a file and verify should fail
        let corrupt_path = backups.join(&meta.backup_id).join("data").join("file1.dat");
        fs::write(&corrupt_path, b"corrupted!")
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("write: {}", e))))?;

        assert!(!manager.verify_backup(&meta.backup_id)?);

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_backup_with_data() -> Result<()> {
        let root = test_dir("with_data");
        let source = root.join("source");
        let backups = root.join("backups");
        let restored = root.join("restored");

        // Create source with binary data simulating SSTable/WAL content
        fs::create_dir_all(source.join("wal"))
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("mkdir: {}", e))))?;
        fs::create_dir_all(source.join("sstables").join("L0"))
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("mkdir: {}", e))))?;

        let wal_data: Vec<u8> = (0..256).map(|i| (i % 256) as u8).collect();
        fs::write(source.join("wal").join("000001.wal"), &wal_data)
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("write: {}", e))))?;

        let sst_data: Vec<u8> = (0..1024).map(|i| ((i * 7) % 256) as u8).collect();
        fs::write(
            source.join("sstables").join("L0").join("table_001.sst"),
            &sst_data,
        )
        .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("write: {}", e))))?;

        let manager = BackupManager::new(&backups)?;
        let meta = manager.create_backup(&source)?;

        assert_eq!(meta.total_files, 2);

        // Restore and verify binary content
        manager.restore_backup(&meta.backup_id, &restored)?;

        let restored_wal = fs::read(restored.join("wal").join("000001.wal"))
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("read: {}", e))))?;
        assert_eq!(restored_wal, wal_data);

        let restored_sst = fs::read(restored.join("sstables").join("L0").join("table_001.sst"))
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("read: {}", e))))?;
        assert_eq!(restored_sst, sst_data);

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_backup_metadata_serialization() -> Result<()> {
        let meta = BackupMetadata {
            backup_id: "test-id-123".to_string(),
            created_at: Utc::now(),
            source_dir: PathBuf::from("/tmp/source"),
            total_files: 42,
            total_bytes: 123456,
            checksum: 0xDEAD_BEEF,
            backup_type: BackupType::Full,
            version: "0.2.0".to_string(),
        };

        let json = serde_json::to_string(&meta).map_err(|e| {
            AmateRSError::SerializationError(ErrorContext::new(format!("serialize: {}", e)))
        })?;

        let deserialized: BackupMetadata = serde_json::from_str(&json).map_err(|e| {
            AmateRSError::SerializationError(ErrorContext::new(format!("deserialize: {}", e)))
        })?;

        assert_eq!(deserialized.backup_id, meta.backup_id);
        assert_eq!(deserialized.total_files, meta.total_files);
        assert_eq!(deserialized.total_bytes, meta.total_bytes);
        assert_eq!(deserialized.checksum, meta.checksum);
        assert!(matches!(deserialized.backup_type, BackupType::Full));

        // Test incremental variant
        let incremental_meta = BackupMetadata {
            backup_type: BackupType::Incremental {
                base_backup_id: "base-123".to_string(),
            },
            ..meta
        };

        let json2 = serde_json::to_string(&incremental_meta).map_err(|e| {
            AmateRSError::SerializationError(ErrorContext::new(format!("serialize: {}", e)))
        })?;

        let deser2: BackupMetadata = serde_json::from_str(&json2).map_err(|e| {
            AmateRSError::SerializationError(ErrorContext::new(format!("deserialize: {}", e)))
        })?;

        if let BackupType::Incremental { base_backup_id } = &deser2.backup_type {
            assert_eq!(base_backup_id, "base-123");
        } else {
            return Err(AmateRSError::ValidationError(ErrorContext::new(
                "Expected Incremental backup type",
            )));
        }

        Ok(())
    }

    #[test]
    fn test_backup_empty_database() -> Result<()> {
        let root = test_dir("empty_db");
        let source = root.join("source");
        let backups = root.join("backups");
        let restored = root.join("restored");

        // Create an empty source directory
        fs::create_dir_all(&source)
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("mkdir: {}", e))))?;

        let manager = BackupManager::new(&backups)?;
        let meta = manager.create_backup(&source)?;

        assert_eq!(meta.total_files, 0);
        assert_eq!(meta.total_bytes, 0);

        // Verify and restore empty backup
        assert!(manager.verify_backup(&meta.backup_id)?);
        manager.restore_backup(&meta.backup_id, &restored)?;

        assert!(restored.exists());

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_restore_to_existing_directory() -> Result<()> {
        let root = test_dir("restore_existing");
        let source = root.join("source");
        let backups = root.join("backups");
        let target = root.join("target");

        populate_source(&source)?;

        // Pre-create target with different content
        fs::create_dir_all(&target)
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("mkdir: {}", e))))?;
        fs::write(target.join("old_file.txt"), b"old content")
            .map_err(|e| AmateRSError::IoError(ErrorContext::new(format!("write: {}", e))))?;

        let manager = BackupManager::new(&backups)?;
        let meta = manager.create_backup(&source)?;

        // Restore should clear existing content
        manager.restore_backup(&meta.backup_id, &target)?;

        // Old file should be gone
        assert!(!target.join("old_file.txt").exists());

        // New files should be present
        assert!(target.join("file1.dat").exists());
        assert!(target.join("file2.dat").exists());
        assert!(target.join("subdir").join("nested.dat").exists());

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_backup_size() -> Result<()> {
        let root = test_dir("backup_size");
        let source = root.join("source");
        let backups = root.join("backups");

        populate_source(&source)?;
        let manager = BackupManager::new(&backups)?;
        let meta = manager.create_backup(&source)?;

        let size = manager.backup_size(&meta.backup_id)?;
        assert_eq!(size, meta.total_bytes);

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_verify_directory_helper() -> Result<()> {
        let root = test_dir("verify_dir");
        let source = root.join("source");

        populate_source(&source)?;

        let checksum = calculate_dir_checksum(&source)?;
        assert!(verify_directory(&source, checksum)?);
        assert!(!verify_directory(&source, checksum.wrapping_add(1))?);

        fs::remove_dir_all(&root).ok();
        Ok(())
    }

    #[test]
    fn test_restore_nonexistent_backup() {
        let root = test_dir("restore_nonexistent");
        let backups = root.join("backups");
        let target = root.join("target");

        let manager = BackupManager::new(&backups).expect("BackupManager creation should succeed");
        let result = manager.restore_backup("does-not-exist", &target);
        assert!(result.is_err());

        fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn test_backup_nonexistent_source() {
        let root = test_dir("backup_nonexistent_source");
        let backups = root.join("backups");

        let manager = BackupManager::new(&backups).expect("BackupManager creation should succeed");
        let result = manager.create_backup(Path::new("/nonexistent/path/that/does/not/exist"));
        assert!(result.is_err());

        fs::remove_dir_all(&root).ok();
    }
}