chie-core 0.2.0

Core protocol logic for CHIE Protocol
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
//! Backup and restore functionality for CHIE storage.
//!
//! This module provides:
//! - Full backup creation
//! - Incremental backup support
//! - Backup restoration with integrity verification
//! - Progress tracking for long operations

use crate::storage::{ChunkStorage, PinnedContentInfo, StorageError};
use chie_crypto::hash;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use thiserror::Error;
use tokio::fs;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
use tracing::{debug, info, warn};

/// Backup-related errors.
#[derive(Debug, Error)]
pub enum BackupError {
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    #[error("Serialization error: {0}")]
    SerializationError(String),

    #[error("Storage error: {0}")]
    StorageError(#[from] StorageError),

    #[error("Backup not found: {path}")]
    BackupNotFound { path: String },

    #[error("Invalid backup format: {0}")]
    InvalidFormat(String),

    #[error("Checksum mismatch: expected {expected}, got {actual}")]
    ChecksumMismatch { expected: String, actual: String },

    #[error("Backup cancelled")]
    Cancelled,

    #[error("Incompatible backup version: {version}")]
    IncompatibleVersion { version: u32 },
}

/// Current backup format version.
const BACKUP_VERSION: u32 = 1;

/// Backup configuration.
#[derive(Debug, Clone)]
pub struct BackupConfig {
    /// Enable compression for backup files.
    pub compress: bool,
    /// Chunk size for backup archive (default 4MB).
    pub archive_chunk_size: usize,
    /// Verify checksums during backup.
    pub verify_on_backup: bool,
    /// Verify checksums during restore.
    pub verify_on_restore: bool,
    /// Include metadata in backups.
    pub include_metadata: bool,
}

impl Default for BackupConfig {
    fn default() -> Self {
        Self {
            compress: true,
            archive_chunk_size: 4 * 1024 * 1024, // 4MB
            verify_on_backup: true,
            verify_on_restore: true,
            include_metadata: true,
        }
    }
}

/// Backup manifest describing the backup contents.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupManifest {
    /// Backup format version.
    pub version: u32,
    /// When the backup was created.
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Type of backup.
    pub backup_type: BackupType,
    /// Previous backup ID for incremental backups.
    pub parent_backup_id: Option<String>,
    /// Unique backup identifier.
    pub backup_id: String,
    /// Content items included.
    pub content_items: Vec<BackupContentEntry>,
    /// Total size of backup data.
    pub total_size: u64,
    /// Checksum of the backup data.
    pub checksum: String,
    /// Source storage path.
    pub source_path: String,
}

/// Type of backup.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BackupType {
    /// Full backup of all content.
    Full,
    /// Incremental backup (changes since last backup).
    Incremental,
}

/// Entry for each content item in the backup.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupContentEntry {
    /// Content CID.
    pub cid: String,
    /// Number of chunks.
    pub chunk_count: u64,
    /// Total size.
    pub total_size: u64,
    /// Chunk checksums for verification.
    pub chunk_checksums: Vec<String>,
    /// Offset in the backup archive.
    pub archive_offset: u64,
}

/// Progress tracking for backup/restore operations.
#[derive(Debug, Clone)]
pub struct BackupProgress {
    /// Total bytes to process.
    pub total_bytes: Arc<AtomicU64>,
    /// Bytes processed so far.
    pub processed_bytes: Arc<AtomicU64>,
    /// Total items to process.
    pub total_items: Arc<AtomicU64>,
    /// Items processed so far.
    pub processed_items: Arc<AtomicU64>,
    /// Current operation description.
    pub current_operation: Arc<std::sync::RwLock<String>>,
    /// Cancellation flag.
    pub cancelled: Arc<AtomicBool>,
}

impl Default for BackupProgress {
    fn default() -> Self {
        Self::new()
    }
}

impl BackupProgress {
    /// Create a new progress tracker.
    #[must_use]
    #[inline]
    pub fn new() -> Self {
        Self {
            total_bytes: Arc::new(AtomicU64::new(0)),
            processed_bytes: Arc::new(AtomicU64::new(0)),
            total_items: Arc::new(AtomicU64::new(0)),
            processed_items: Arc::new(AtomicU64::new(0)),
            current_operation: Arc::new(std::sync::RwLock::new(String::new())),
            cancelled: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Get progress percentage (0-100).
    #[must_use]
    #[inline]
    pub fn percentage(&self) -> f64 {
        let total = self.total_bytes.load(Ordering::Relaxed);
        if total == 0 {
            return 0.0;
        }
        let processed = self.processed_bytes.load(Ordering::Relaxed);
        (processed as f64 / total as f64) * 100.0
    }

    /// Check if operation is cancelled.
    #[must_use]
    #[inline]
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::Relaxed)
    }

    /// Cancel the operation.
    #[inline]
    pub fn cancel(&self) {
        self.cancelled.store(true, Ordering::Relaxed);
    }

    /// Set current operation description.
    #[inline]
    pub fn set_operation(&self, op: &str) {
        if let Ok(mut guard) = self.current_operation.write() {
            *guard = op.to_string();
        }
    }

    /// Add processed bytes.
    #[inline]
    pub fn add_bytes(&self, bytes: u64) {
        self.processed_bytes.fetch_add(bytes, Ordering::Relaxed);
    }

    /// Increment processed items.
    #[inline]
    pub fn increment_items(&self) {
        self.processed_items.fetch_add(1, Ordering::Relaxed);
    }
}

/// Result of a backup operation.
#[derive(Debug, Clone)]
pub struct BackupResult {
    /// Backup manifest.
    pub manifest: BackupManifest,
    /// Path to the backup file.
    pub backup_path: PathBuf,
    /// Duration of the backup.
    pub duration_secs: f64,
    /// Number of content items backed up.
    pub items_backed_up: usize,
}

/// Result of a restore operation.
#[derive(Debug, Clone)]
pub struct RestoreResult {
    /// Number of content items restored.
    pub items_restored: usize,
    /// Number of chunks restored.
    pub chunks_restored: u64,
    /// Total bytes restored.
    pub bytes_restored: u64,
    /// Duration of the restore.
    pub duration_secs: f64,
    /// Items that failed to restore.
    pub failed_items: Vec<String>,
}

/// Backup manager for creating and restoring backups.
pub struct BackupManager {
    /// Backup configuration.
    config: BackupConfig,
}

impl BackupManager {
    /// Create a new backup manager.
    #[must_use]
    #[inline]
    pub fn new(config: BackupConfig) -> Self {
        Self { config }
    }

    /// Create a full backup of the storage.
    pub async fn create_full_backup(
        &self,
        storage: &ChunkStorage,
        backup_dir: &Path,
        progress: Option<&BackupProgress>,
    ) -> Result<BackupResult, BackupError> {
        let start = std::time::Instant::now();

        // Create backup directory
        fs::create_dir_all(backup_dir).await?;

        let backup_id = uuid::Uuid::new_v4().to_string();
        let backup_path = backup_dir.join(format!("backup_{}.chie", backup_id));

        info!("Creating full backup: {}", backup_id);

        if let Some(p) = progress {
            p.set_operation("Preparing backup");
        }

        // Collect content to backup
        let pinned_cids = storage.list_pinned();
        let mut content_entries = Vec::new();
        let mut total_size = 0u64;

        if let Some(p) = progress {
            p.total_items
                .store(pinned_cids.len() as u64, Ordering::Relaxed);
        }

        // Calculate total size for progress
        for cid in &pinned_cids {
            if let Some(info) = storage.get_pinned_info(cid) {
                total_size += info.total_size;
            }
        }

        if let Some(p) = progress {
            p.total_bytes.store(total_size, Ordering::Relaxed);
        }

        // Create backup file
        let mut backup_file = fs::File::create(&backup_path).await?;
        let mut archive_offset = 0u64;

        // Write header placeholder (will update later)
        let header_placeholder = vec![0u8; 1024];
        backup_file.write_all(&header_placeholder).await?;
        archive_offset += header_placeholder.len() as u64;

        // Backup each content item
        for cid in pinned_cids {
            if let Some(p) = progress {
                if p.is_cancelled() {
                    // Clean up partial backup
                    let _ = fs::remove_file(&backup_path).await;
                    return Err(BackupError::Cancelled);
                }
                p.set_operation(&format!("Backing up {}", cid));
            }

            let entry = self
                .backup_content(
                    storage,
                    cid,
                    &mut backup_file,
                    &mut archive_offset,
                    progress,
                )
                .await?;

            content_entries.push(entry);

            if let Some(p) = progress {
                p.increment_items();
            }
        }

        // Create manifest
        let manifest_data = self.create_manifest_data(&content_entries)?;
        let checksum = hex::encode(hash(&manifest_data));

        let manifest = BackupManifest {
            version: BACKUP_VERSION,
            created_at: chrono::Utc::now(),
            backup_type: BackupType::Full,
            parent_backup_id: None,
            backup_id: backup_id.clone(),
            content_items: content_entries.clone(),
            total_size,
            checksum,
            source_path: storage.base_path().to_string_lossy().to_string(),
        };

        // Write manifest at the end
        let manifest_json = serde_json::to_vec_pretty(&manifest)
            .map_err(|e| BackupError::SerializationError(e.to_string()))?;

        backup_file.write_all(&manifest_json).await?;

        // Write manifest length at the end
        let manifest_len = manifest_json.len() as u64;
        backup_file.write_all(&manifest_len.to_le_bytes()).await?;

        // Flush and sync
        backup_file.flush().await?;
        backup_file.sync_all().await?;

        let duration = start.elapsed().as_secs_f64();

        info!(
            "Backup complete: {} items in {:.2}s",
            content_entries.len(),
            duration
        );

        Ok(BackupResult {
            manifest,
            backup_path,
            duration_secs: duration,
            items_backed_up: content_entries.len(),
        })
    }

    /// Create an incremental backup.
    pub async fn create_incremental_backup(
        &self,
        storage: &ChunkStorage,
        backup_dir: &Path,
        parent_manifest: &BackupManifest,
        progress: Option<&BackupProgress>,
    ) -> Result<BackupResult, BackupError> {
        let start = std::time::Instant::now();

        let backup_id = uuid::Uuid::new_v4().to_string();
        let backup_path = backup_dir.join(format!("backup_{}_incr.chie", backup_id));

        info!(
            "Creating incremental backup: {} (parent: {})",
            backup_id, parent_manifest.backup_id
        );

        if let Some(p) = progress {
            p.set_operation("Analyzing changes");
        }

        // Build set of existing content from parent
        let parent_cids: HashSet<_> = parent_manifest
            .content_items
            .iter()
            .map(|e| e.cid.clone())
            .collect();

        // Find new/changed content
        let current_cids: HashSet<_> = storage
            .list_pinned()
            .into_iter()
            .map(String::from)
            .collect();
        let new_cids: Vec<_> = current_cids.difference(&parent_cids).cloned().collect();

        if new_cids.is_empty() {
            info!("No changes detected, skipping backup");
            return Ok(BackupResult {
                manifest: BackupManifest {
                    version: BACKUP_VERSION,
                    created_at: chrono::Utc::now(),
                    backup_type: BackupType::Incremental,
                    parent_backup_id: Some(parent_manifest.backup_id.clone()),
                    backup_id,
                    content_items: vec![],
                    total_size: 0,
                    checksum: String::new(),
                    source_path: storage.base_path().to_string_lossy().to_string(),
                },
                backup_path,
                duration_secs: start.elapsed().as_secs_f64(),
                items_backed_up: 0,
            });
        }

        // Create backup file
        fs::create_dir_all(backup_dir).await?;
        let mut backup_file = fs::File::create(&backup_path).await?;
        let mut archive_offset = 0u64;

        // Write header placeholder
        let header_placeholder = vec![0u8; 1024];
        backup_file.write_all(&header_placeholder).await?;
        archive_offset += header_placeholder.len() as u64;

        let mut content_entries = Vec::new();
        let mut total_size = 0u64;

        if let Some(p) = progress {
            p.total_items
                .store(new_cids.len() as u64, Ordering::Relaxed);
        }

        // Backup only new content
        for cid in &new_cids {
            if let Some(p) = progress {
                if p.is_cancelled() {
                    let _ = fs::remove_file(&backup_path).await;
                    return Err(BackupError::Cancelled);
                }
                p.set_operation(&format!("Backing up {}", cid));
            }

            if let Some(info) = storage.get_pinned_info(cid) {
                total_size += info.total_size;
            }

            let entry = self
                .backup_content(
                    storage,
                    cid,
                    &mut backup_file,
                    &mut archive_offset,
                    progress,
                )
                .await?;

            content_entries.push(entry);

            if let Some(p) = progress {
                p.increment_items();
            }
        }

        // Create manifest
        let manifest_data = self.create_manifest_data(&content_entries)?;
        let checksum = hex::encode(hash(&manifest_data));

        let manifest = BackupManifest {
            version: BACKUP_VERSION,
            created_at: chrono::Utc::now(),
            backup_type: BackupType::Incremental,
            parent_backup_id: Some(parent_manifest.backup_id.clone()),
            backup_id: backup_id.clone(),
            content_items: content_entries.clone(),
            total_size,
            checksum,
            source_path: storage.base_path().to_string_lossy().to_string(),
        };

        // Write manifest
        let manifest_json = serde_json::to_vec_pretty(&manifest)
            .map_err(|e| BackupError::SerializationError(e.to_string()))?;
        backup_file.write_all(&manifest_json).await?;

        let manifest_len = manifest_json.len() as u64;
        backup_file.write_all(&manifest_len.to_le_bytes()).await?;

        backup_file.flush().await?;
        backup_file.sync_all().await?;

        let duration = start.elapsed().as_secs_f64();

        info!(
            "Incremental backup complete: {} items in {:.2}s",
            content_entries.len(),
            duration
        );

        Ok(BackupResult {
            manifest,
            backup_path,
            duration_secs: duration,
            items_backed_up: content_entries.len(),
        })
    }

    /// Restore from a backup file.
    pub async fn restore_backup(
        &self,
        backup_path: &Path,
        storage: &mut ChunkStorage,
        progress: Option<&BackupProgress>,
    ) -> Result<RestoreResult, BackupError> {
        let start = std::time::Instant::now();

        if !backup_path.exists() {
            return Err(BackupError::BackupNotFound {
                path: backup_path.to_string_lossy().to_string(),
            });
        }

        info!("Restoring from backup: {:?}", backup_path);

        if let Some(p) = progress {
            p.set_operation("Reading backup manifest");
        }

        // Read manifest from end of file
        let manifest = self.read_manifest(backup_path).await?;

        if manifest.version != BACKUP_VERSION {
            return Err(BackupError::IncompatibleVersion {
                version: manifest.version,
            });
        }

        if let Some(p) = progress {
            p.total_items
                .store(manifest.content_items.len() as u64, Ordering::Relaxed);
            p.total_bytes.store(manifest.total_size, Ordering::Relaxed);
        }

        let mut items_restored = 0;
        let mut chunks_restored = 0u64;
        let mut bytes_restored = 0u64;
        let mut failed_items = Vec::new();

        // Open backup file for reading
        let mut backup_file = fs::File::open(backup_path).await?;

        // Restore each content item
        for entry in &manifest.content_items {
            if let Some(p) = progress {
                if p.is_cancelled() {
                    return Err(BackupError::Cancelled);
                }
                p.set_operation(&format!("Restoring {}", entry.cid));
            }

            match self
                .restore_content(entry, &mut backup_file, storage, progress)
                .await
            {
                Ok((chunks, bytes)) => {
                    items_restored += 1;
                    chunks_restored += chunks;
                    bytes_restored += bytes;
                }
                Err(e) => {
                    warn!("Failed to restore {}: {}", entry.cid, e);
                    failed_items.push(entry.cid.clone());
                }
            }

            if let Some(p) = progress {
                p.increment_items();
            }
        }

        let duration = start.elapsed().as_secs_f64();

        info!(
            "Restore complete: {} items, {} chunks, {} bytes in {:.2}s",
            items_restored, chunks_restored, bytes_restored, duration
        );

        Ok(RestoreResult {
            items_restored,
            chunks_restored,
            bytes_restored,
            duration_secs: duration,
            failed_items,
        })
    }

    /// Read backup manifest from a backup file.
    pub async fn read_manifest(&self, backup_path: &Path) -> Result<BackupManifest, BackupError> {
        let mut file = fs::File::open(backup_path).await?;

        // Read manifest length from end
        let file_size = file.metadata().await?.len();
        file.seek(std::io::SeekFrom::End(-8)).await?;

        let mut len_bytes = [0u8; 8];
        file.read_exact(&mut len_bytes).await?;
        let manifest_len = u64::from_le_bytes(len_bytes) as usize;

        // Read manifest
        let manifest_start = file_size - 8 - manifest_len as u64;
        file.seek(std::io::SeekFrom::Start(manifest_start)).await?;

        let mut manifest_data = vec![0u8; manifest_len];
        file.read_exact(&mut manifest_data).await?;

        let manifest: BackupManifest = serde_json::from_slice(&manifest_data)
            .map_err(|e| BackupError::SerializationError(e.to_string()))?;

        Ok(manifest)
    }

    /// List available backups in a directory.
    pub async fn list_backups(
        &self,
        backup_dir: &Path,
    ) -> Result<Vec<BackupManifest>, BackupError> {
        let mut manifests = Vec::new();

        if !backup_dir.exists() {
            return Ok(manifests);
        }

        let mut entries = fs::read_dir(backup_dir).await?;
        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            if path.extension().is_some_and(|ext| ext == "chie") {
                match self.read_manifest(&path).await {
                    Ok(manifest) => manifests.push(manifest),
                    Err(e) => {
                        debug!("Skipping invalid backup {:?}: {}", path, e);
                    }
                }
            }
        }

        // Sort by creation time (newest first)
        manifests.sort_by(|a, b| b.created_at.cmp(&a.created_at));

        Ok(manifests)
    }

    /// Verify a backup file integrity.
    pub async fn verify_backup(
        &self,
        backup_path: &Path,
        progress: Option<&BackupProgress>,
    ) -> Result<bool, BackupError> {
        if let Some(p) = progress {
            p.set_operation("Verifying backup integrity");
        }

        let manifest = self.read_manifest(backup_path).await?;

        // Verify manifest checksum
        let manifest_data = self.create_manifest_data(&manifest.content_items)?;
        let computed_checksum = hex::encode(hash(&manifest_data));

        if computed_checksum != manifest.checksum {
            return Ok(false);
        }

        // Verify each content entry exists and has correct checksums
        let mut file = fs::File::open(backup_path).await?;

        if let Some(p) = progress {
            p.total_items
                .store(manifest.content_items.len() as u64, Ordering::Relaxed);
        }

        for entry in &manifest.content_items {
            if let Some(p) = progress {
                if p.is_cancelled() {
                    return Err(BackupError::Cancelled);
                }
                p.set_operation(&format!("Verifying {}", entry.cid));
            }

            // Seek to entry offset and verify chunks exist
            file.seek(std::io::SeekFrom::Start(entry.archive_offset))
                .await?;

            // Read and verify chunk count
            let mut count_bytes = [0u8; 8];
            file.read_exact(&mut count_bytes).await?;
            let stored_count = u64::from_le_bytes(count_bytes);

            if stored_count != entry.chunk_count {
                return Ok(false);
            }

            if let Some(p) = progress {
                p.increment_items();
            }
        }

        Ok(true)
    }

    // Helper methods

    async fn backup_content(
        &self,
        storage: &ChunkStorage,
        cid: &str,
        backup_file: &mut fs::File,
        archive_offset: &mut u64,
        progress: Option<&BackupProgress>,
    ) -> Result<BackupContentEntry, BackupError> {
        let info = storage
            .get_pinned_info(cid)
            .ok_or(StorageError::ContentNotFound {
                cid: cid.to_string(),
            })?;

        let entry_offset = *archive_offset;
        let mut chunk_checksums = Vec::new();

        // Write chunk count
        let count_bytes = info.chunk_count.to_le_bytes();
        backup_file.write_all(&count_bytes).await?;
        *archive_offset += 8;

        // Write content metadata
        let meta_json =
            serde_json::to_vec(info).map_err(|e| BackupError::SerializationError(e.to_string()))?;
        let meta_len = meta_json.len() as u32;
        backup_file.write_all(&meta_len.to_le_bytes()).await?;
        backup_file.write_all(&meta_json).await?;
        *archive_offset += 4 + meta_json.len() as u64;

        // Write each chunk
        for chunk_idx in 0..info.chunk_count {
            let chunk_data = storage.get_chunk(cid, chunk_idx).await?;
            let chunk_hash = hash(&chunk_data);
            chunk_checksums.push(hex::encode(chunk_hash));

            // Write chunk length and data
            let chunk_len = chunk_data.len() as u32;
            backup_file.write_all(&chunk_len.to_le_bytes()).await?;
            backup_file.write_all(&chunk_data).await?;
            *archive_offset += 4 + chunk_data.len() as u64;

            if let Some(p) = progress {
                p.add_bytes(chunk_data.len() as u64);
            }
        }

        Ok(BackupContentEntry {
            cid: cid.to_string(),
            chunk_count: info.chunk_count,
            total_size: info.total_size,
            chunk_checksums,
            archive_offset: entry_offset,
        })
    }

    async fn restore_content(
        &self,
        entry: &BackupContentEntry,
        backup_file: &mut fs::File,
        storage: &mut ChunkStorage,
        progress: Option<&BackupProgress>,
    ) -> Result<(u64, u64), BackupError> {
        // Seek to entry offset
        backup_file
            .seek(std::io::SeekFrom::Start(entry.archive_offset))
            .await?;

        // Read chunk count
        let mut count_bytes = [0u8; 8];
        backup_file.read_exact(&mut count_bytes).await?;
        let chunk_count = u64::from_le_bytes(count_bytes);

        if chunk_count != entry.chunk_count {
            return Err(BackupError::InvalidFormat(format!(
                "Chunk count mismatch for {}: expected {}, got {}",
                entry.cid, entry.chunk_count, chunk_count
            )));
        }

        // Read content metadata
        let mut meta_len_bytes = [0u8; 4];
        backup_file.read_exact(&mut meta_len_bytes).await?;
        let meta_len = u32::from_le_bytes(meta_len_bytes) as usize;

        let mut meta_data = vec![0u8; meta_len];
        backup_file.read_exact(&mut meta_data).await?;

        let content_info: PinnedContentInfo = serde_json::from_slice(&meta_data)
            .map_err(|e| BackupError::SerializationError(e.to_string()))?;

        // Read all chunks
        let mut chunks = Vec::new();
        let mut total_bytes = 0u64;

        for (idx, expected_checksum) in entry.chunk_checksums.iter().enumerate() {
            let mut chunk_len_bytes = [0u8; 4];
            backup_file.read_exact(&mut chunk_len_bytes).await?;
            let chunk_len = u32::from_le_bytes(chunk_len_bytes) as usize;

            let mut chunk_data = vec![0u8; chunk_len];
            backup_file.read_exact(&mut chunk_data).await?;

            // Verify checksum if enabled
            if self.config.verify_on_restore {
                let actual_checksum = hex::encode(hash(&chunk_data));
                if &actual_checksum != expected_checksum {
                    return Err(BackupError::ChecksumMismatch {
                        expected: expected_checksum.clone(),
                        actual: actual_checksum,
                    });
                }
            }

            total_bytes += chunk_data.len() as u64;
            chunks.push(chunk_data);

            if let Some(p) = progress {
                p.add_bytes(chunk_len as u64);
            }

            debug!(
                "Restored chunk {}/{} for {}",
                idx + 1,
                chunk_count,
                entry.cid
            );
        }

        // Pin the content in storage
        storage
            .pin_content(
                &entry.cid,
                &chunks,
                &content_info.encryption_key,
                &content_info.base_nonce,
            )
            .await?;

        Ok((chunk_count, total_bytes))
    }

    fn create_manifest_data(&self, entries: &[BackupContentEntry]) -> Result<Vec<u8>, BackupError> {
        // Create a deterministic representation for checksum
        let mut data = Vec::new();
        for entry in entries {
            data.extend_from_slice(entry.cid.as_bytes());
            data.extend_from_slice(&entry.chunk_count.to_le_bytes());
            data.extend_from_slice(&entry.total_size.to_le_bytes());
            for checksum in &entry.chunk_checksums {
                data.extend_from_slice(checksum.as_bytes());
            }
        }
        Ok(data)
    }
}

/// Retention policy for managing backup history.
#[derive(Debug, Clone)]
pub struct RetentionPolicy {
    /// Keep backups for at least this many days.
    pub min_retention_days: u32,
    /// Maximum number of full backups to keep.
    pub max_full_backups: usize,
    /// Maximum number of incremental backups per full backup.
    pub max_incremental_per_full: usize,
}

impl Default for RetentionPolicy {
    fn default() -> Self {
        Self {
            min_retention_days: 30,
            max_full_backups: 5,
            max_incremental_per_full: 10,
        }
    }
}

/// Apply retention policy to backup directory.
pub async fn apply_retention_policy(
    backup_dir: &Path,
    policy: &RetentionPolicy,
) -> Result<Vec<PathBuf>, BackupError> {
    let manager = BackupManager::new(BackupConfig::default());
    let manifests = manager.list_backups(backup_dir).await?;

    let mut to_delete = Vec::new();
    let now = chrono::Utc::now();
    let min_age = chrono::Duration::days(policy.min_retention_days as i64);

    // Group by full backup
    let mut full_backups: Vec<_> = manifests
        .iter()
        .filter(|m| m.backup_type == BackupType::Full)
        .collect();

    // Keep only max_full_backups
    if full_backups.len() > policy.max_full_backups {
        for manifest in full_backups.drain(policy.max_full_backups..) {
            if now - manifest.created_at > min_age {
                to_delete.push(backup_dir.join(format!("backup_{}.chie", manifest.backup_id)));
            }
        }
    }

    // For each remaining full backup, limit incrementals
    for full_manifest in &full_backups {
        let incrementals: Vec<_> = manifests
            .iter()
            .filter(|m| {
                m.backup_type == BackupType::Incremental
                    && m.parent_backup_id.as_ref() == Some(&full_manifest.backup_id)
            })
            .collect();

        if incrementals.len() > policy.max_incremental_per_full {
            for manifest in incrementals.iter().skip(policy.max_incremental_per_full) {
                if now - manifest.created_at > min_age {
                    to_delete
                        .push(backup_dir.join(format!("backup_{}_incr.chie", manifest.backup_id)));
                }
            }
        }
    }

    // Delete old backups
    for path in &to_delete {
        if path.exists() {
            fs::remove_file(path).await?;
            info!("Deleted old backup: {:?}", path);
        }
    }

    Ok(to_delete)
}

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

    #[tokio::test]
    async fn test_backup_config_default() {
        let config = BackupConfig::default();
        assert!(config.compress);
        assert!(config.verify_on_backup);
        assert!(config.verify_on_restore);
    }

    #[tokio::test]
    async fn test_progress_tracking() {
        let progress = BackupProgress::new();
        progress.total_bytes.store(100, Ordering::Relaxed);
        progress.processed_bytes.store(50, Ordering::Relaxed);

        assert!((progress.percentage() - 50.0).abs() < 0.01);

        progress.cancel();
        assert!(progress.is_cancelled());
    }

    #[tokio::test]
    async fn test_retention_policy_default() {
        let policy = RetentionPolicy::default();
        assert_eq!(policy.min_retention_days, 30);
        assert_eq!(policy.max_full_backups, 5);
        assert_eq!(policy.max_incremental_per_full, 10);
    }

    #[tokio::test]
    async fn test_list_empty_backups() {
        let tmp = tempdir().unwrap();
        let manager = BackupManager::new(BackupConfig::default());
        let backups = manager.list_backups(tmp.path()).await.unwrap();
        assert!(backups.is_empty());
    }
}