asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
//! Sparse Writer Implementation for Out-of-Order Chunk Writing

use super::{
    ChunkRange, CommitPolicy, PlatformCapabilities, RangeTracker, SparseRange, TempPathManager,
};
use crate::atp::manifest::{Manifest, MerkleRoot};
use crate::atp::object::ObjectId;
use crate::cx::Cx;
use crate::types::outcome::Outcome;
use parking_lot::{Mutex, MutexGuard};
use sha2::{Digest, Sha256};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;

const CONTENT_ID_DOMAIN: &[u8] = b"asupersync.atp.content-id.v1\0";
const DIGEST_BUFFER_SIZE: usize = 8192;

/// Configuration for sparse writer behavior
#[derive(Debug, Clone)]
pub struct SparseWriterConfig {
    /// Enable preallocation when supported by platform
    pub enable_preallocation: bool,
    /// Preferred chunk size for preallocation hints
    pub chunk_size_hint: u64,
    /// Maximum number of concurrent temp files
    pub max_temp_files: usize,
    /// Fsync policy for durability guarantees
    pub fsync_policy: super::FsyncPolicy,
    /// Atomic commit policy
    pub commit_policy: CommitPolicy,
    /// Enable quarantine for failed writes
    pub enable_quarantine: bool,
    /// Maximum age for temp files before cleanup
    pub temp_file_max_age: std::time::Duration,
}

impl Default for SparseWriterConfig {
    fn default() -> Self {
        Self {
            enable_preallocation: true,
            chunk_size_hint: 1024 * 1024, // 1MB
            max_temp_files: 64,
            fsync_policy: super::FsyncPolicy::VerifiedChunks,
            commit_policy: CommitPolicy::AtomicRename,
            enable_quarantine: true,
            temp_file_max_age: std::time::Duration::from_hours(24),
        }
    }
}

/// Options for individual write operations
#[derive(Debug, Clone)]
pub struct WriteOptions {
    /// Priority for this write operation
    pub priority: WritePriority,
    /// Whether to fsync after this specific write
    pub force_sync: bool,
    /// Expected final size hint for preallocation
    pub size_hint: Option<u64>,
}

impl Default for WriteOptions {
    fn default() -> Self {
        Self {
            priority: WritePriority::Normal,
            force_sync: false,
            size_hint: None,
        }
    }
}

/// Priority levels for write operations
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum WritePriority {
    /// Low priority, can be deferred
    Low = 0,
    /// Normal priority
    Normal = 1,
    /// High priority, process quickly
    High = 2,
    /// Critical priority, process immediately
    Critical = 3,
}

/// Sparse writer state for tracking progress and consistency
#[derive(Debug)]
struct SparseWriterState {
    /// Object being written
    object_id: ObjectId,
    /// Final destination path
    final_path: PathBuf,
    /// Temporary file handle
    temp_file: Option<File>,
    /// Current temp path
    temp_path: Option<PathBuf>,
    /// Range tracker for written chunks
    range_tracker: RangeTracker,
    /// Written chunks metadata
    written_chunks: BTreeMap<u64, ChunkMetadata>,
    /// Total expected size if known
    expected_size: Option<u64>,
    /// Current allocated size
    allocated_size: u64,
    /// Whether file is preallocated
    is_preallocated: bool,
    /// Creation timestamp
    created_at: Instant,
    /// Last write timestamp
    last_write_at: Instant,
    /// Verification state
    verification_state: VerificationState,
}

/// Metadata for individual chunks
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct ChunkMetadata {
    /// Offset in final file
    offset: u64,
    /// Size in bytes
    size: u64,
    /// Hash of chunk data
    hash: [u8; 32],
    /// Write timestamp
    written_at: Instant,
    /// Whether chunk was fsynced
    synced: bool,
}

/// Verification state tracking
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum VerificationState {
    /// Not verified yet
    Pending,
    /// Verification in progress
    InProgress,
    /// Successfully verified
    Verified { manifest_root: MerkleRoot },
    /// Verification failed
    Failed { reason: String },
}

/// Main sparse writer implementation
pub struct SparseWriter {
    /// Writer configuration
    config: SparseWriterConfig,
    /// Platform capabilities
    platform: Arc<PlatformCapabilities>,
    /// Path manager for temp files
    path_manager: Arc<Mutex<TempPathManager>>,
    /// Current writer state
    state: Arc<Mutex<SparseWriterState>>,
}

impl SparseWriter {
    /// Create a new sparse writer for the given object
    pub async fn new(
        cx: &Cx,
        object_id: ObjectId,
        final_path: impl AsRef<Path>,
        config: SparseWriterConfig,
    ) -> Outcome<Self, SparseWriterError> {
        let final_path = final_path.as_ref().to_path_buf();

        let destination_dir = final_path
            .parent()
            .filter(|parent| !parent.as_os_str().is_empty())
            .unwrap_or(Path::new("."));

        // Detect capabilities for the destination filesystem. ATP transfers may
        // target a mounted volume whose sparse/preallocation/rename behavior
        // differs from the process working directory.
        let platform = match PlatformCapabilities::detect_for_path(cx, destination_dir).await {
            crate::types::outcome::Outcome::Ok(caps) => Arc::new(caps),
            crate::types::outcome::Outcome::Err(e) => {
                return crate::types::outcome::Outcome::Err(SparseWriterError::PlatformDetection(
                    e.to_string(),
                ));
            }
            crate::types::outcome::Outcome::Cancelled(reason) => {
                return crate::types::outcome::Outcome::Cancelled(reason);
            }
            crate::types::outcome::Outcome::Panicked(payload) => {
                return crate::types::outcome::Outcome::Panicked(payload);
            }
        };

        // Initialize path manager
        let path_manager = Arc::new(Mutex::new(TempPathManager::new(destination_dir)));

        // Create initial state
        let state = Arc::new(Mutex::new(SparseWriterState {
            object_id,
            final_path,
            temp_file: None,
            temp_path: None,
            range_tracker: RangeTracker::new(),
            written_chunks: BTreeMap::new(),
            expected_size: None,
            allocated_size: 0,
            is_preallocated: false,
            created_at: Instant::now(),
            last_write_at: Instant::now(),
            verification_state: VerificationState::Pending,
        }));

        crate::types::outcome::Outcome::Ok(Self {
            config,
            platform,
            path_manager,
            state,
        })
    }

    fn lock_state(&self) -> MutexGuard<'_, SparseWriterState> {
        self.state.lock()
    }

    /// Set expected final size for preallocation
    pub fn set_expected_size(&self, size: u64) -> Result<(), SparseWriterError> {
        let mut state = self.lock_state();
        state.expected_size = Some(size);

        // Trigger preallocation if enabled and file is open
        if self.config.enable_preallocation && state.temp_file.is_some() {
            self.preallocate_internal(&mut state, size)?;
        }

        Ok(())
    }

    /// Write a chunk at the specified offset
    pub async fn write_chunk(
        &self,
        _cx: &Cx,
        offset: u64,
        data: &[u8],
        options: WriteOptions,
    ) -> Outcome<ChunkRange, SparseWriterError> {
        if data.is_empty() {
            return crate::types::outcome::Outcome::Err(SparseWriterError::EmptyChunk);
        }

        let chunk_size = data.len() as u64;
        let chunk_range = ChunkRange {
            offset,
            size: chunk_size,
        };

        let end = match offset.checked_add(chunk_size) {
            Some(end) => end,
            None => {
                return Outcome::Err(SparseWriterError::InvalidRange {
                    offset,
                    size: chunk_size,
                });
            }
        };
        let sparse_range = SparseRange { start: offset, end };

        let mut state = self.lock_state();
        if let Err(error) = self.ensure_temp_file_open_locked(&mut state) {
            return Outcome::Err(error);
        }
        if state.range_tracker.overlaps(&sparse_range) {
            return Outcome::Err(SparseWriterError::OverlappingWrite {
                offset,
                size: chunk_size,
            });
        }
        let (hash, synced) = match self.write_chunk_locked(&mut state, offset, data, &options) {
            Ok(result) => result,
            Err(e) => return Outcome::Err(e),
        };
        let written_at = Instant::now();
        state.range_tracker.add_range(sparse_range);
        state.written_chunks.insert(
            offset,
            ChunkMetadata {
                offset,
                size: chunk_size,
                hash,
                written_at,
                synced,
            },
        );
        state.last_write_at = written_at;

        Outcome::ok(chunk_range)
    }

    fn is_complete_locked(state: &SparseWriterState) -> bool {
        if let Some(expected_size) = state.expected_size {
            state.range_tracker.is_contiguous_to(expected_size)
        } else {
            false
        }
    }

    /// Check if all expected ranges have been written
    pub fn is_complete(&self) -> bool {
        let state = self.lock_state();
        Self::is_complete_locked(&state)
    }

    /// Verify written data against expected manifest
    pub async fn verify(
        &self,
        _cx: &Cx,
        expected_manifest: &Manifest,
    ) -> Outcome<(), SparseWriterError> {
        if let Err(error) = expected_manifest.validate() {
            return self.fail_verification(format!("manifest validation failed: {error}"));
        }

        let verification_input = {
            let mut state = self.lock_state();
            state.verification_state = VerificationState::InProgress;

            if !Self::is_complete_locked(&state) {
                return Self::fail_verification_locked(
                    &mut state,
                    "writer is incomplete; cannot verify sparse output".to_owned(),
                );
            }

            let Some(temp_path) = state.temp_path.clone() else {
                return Self::fail_verification_locked(
                    &mut state,
                    "writer has no temporary file to verify".to_owned(),
                );
            };

            if let Some(file) = state.temp_file.as_mut() {
                if let Err(error) = file.flush() {
                    return Self::fail_verification_locked(
                        &mut state,
                        format!("flush before verification failed: {error}"),
                    );
                }
            }

            (
                state.object_id.clone(),
                temp_path,
                state.expected_size,
                state.written_chunks.clone(),
            )
        };

        let (object_id, temp_path, expected_size, written_chunks) = verification_input;
        let Some(manifest_object) = expected_manifest.objects.get(&object_id) else {
            return self.fail_verification(format!("manifest does not contain object {object_id}"));
        };

        let mut file = match File::open(&temp_path) {
            Ok(file) => file,
            Err(error) => {
                return self.fail_verification(format!("open for verification failed: {error}"));
            }
        };
        let file_len = match file.metadata() {
            Ok(metadata) => metadata.len(),
            Err(error) => {
                return self
                    .fail_verification(format!("metadata for verification failed: {error}"));
            }
        };

        if let Some(expected_size) = expected_size {
            if file_len != expected_size {
                return self.fail_verification(format!(
                    "writer expected size {expected_size}, found {file_len} bytes"
                ));
            }
        }
        if let Some(manifest_size) = manifest_object.size_bytes {
            if file_len != manifest_size {
                return self.fail_verification(format!(
                    "manifest expected size {manifest_size}, found {file_len} bytes"
                ));
            }
        }

        for (offset, metadata) in &written_chunks {
            let end = match offset.checked_add(metadata.size) {
                Some(end) => end,
                None => {
                    return self.fail_verification(format!(
                        "written chunk at {offset} overflows with size {}",
                        metadata.size
                    ));
                }
            };
            if end > file_len {
                return self.fail_verification(format!(
                    "written chunk at {offset}..{end} is outside verified file"
                ));
            }
            let actual_hash = match Self::hash_file_range(&mut file, *offset, metadata.size) {
                Ok(hash) => hash,
                Err(error) => return self.fail_verification(error.to_string()),
            };
            if actual_hash != metadata.hash {
                return self
                    .fail_verification(format!("written chunk hash mismatch at offset {offset}"));
            }
        }

        if let Some(expected_hash) = manifest_object.content_hash {
            let actual_hash = if object_id.is_content_addressed() {
                Self::hash_file_with_prefix(&mut file, Some(CONTENT_ID_DOMAIN))
            } else {
                Self::hash_file_with_prefix(&mut file, None)
            };
            let actual_hash = match actual_hash {
                Ok(hash) => hash,
                Err(error) => {
                    return self
                        .fail_verification(format!("content hash verification failed: {error}"));
                }
            };
            if actual_hash != expected_hash {
                return self.fail_verification("manifest content hash mismatch".to_owned());
            }
        } else if manifest_object.chunk_boundaries.is_empty() {
            return self.fail_verification(
                "manifest object has no content hash or chunk boundaries".to_owned(),
            );
        }

        for boundary in &manifest_object.chunk_boundaries {
            let end = match boundary.byte_offset.checked_add(boundary.size_bytes) {
                Some(end) => end,
                None => {
                    return self.fail_verification(format!(
                        "manifest chunk {} overflows at offset {} with size {}",
                        boundary.index, boundary.byte_offset, boundary.size_bytes
                    ));
                }
            };
            if end > file_len {
                return self.fail_verification(format!(
                    "manifest chunk {} range {}..{} is outside verified file",
                    boundary.index, boundary.byte_offset, end
                ));
            }
            let actual_hash =
                match Self::hash_file_range(&mut file, boundary.byte_offset, boundary.size_bytes) {
                    Ok(hash) => hash,
                    Err(error) => return self.fail_verification(error.to_string()),
                };
            if actual_hash != boundary.content_hash {
                return self
                    .fail_verification(format!("manifest chunk {} hash mismatch", boundary.index));
            }
        }

        let mut state = self.lock_state();
        state.verification_state = VerificationState::Verified {
            manifest_root: expected_manifest.merkle_root.clone(),
        };

        Outcome::ok(())
    }

    /// Commit the written data atomically to final destination
    pub async fn commit(&self, _cx: &Cx) -> Outcome<PathBuf, SparseWriterError> {
        {
            let state = self.lock_state();
            if !Self::is_complete_locked(&state) {
                return Outcome::Err(SparseWriterError::IncompleteData);
            }
            if !matches!(state.verification_state, VerificationState::Verified { .. }) {
                return Outcome::Err(SparseWriterError::NotVerified);
            }
        }

        // Apply fsync policy before commit
        match self.apply_fsync_policy().await {
            Outcome::Ok(()) => {}
            Outcome::Err(e) => return Outcome::Err(e),
            Outcome::Cancelled(reason) => return Outcome::cancelled(reason),
            Outcome::Panicked(payload) => return Outcome::panicked(payload),
        }

        // Perform atomic commit based on policy
        let final_path = match self.atomic_commit().await {
            Outcome::Ok(path) => path,
            Outcome::Err(e) => return Outcome::Err(e),
            Outcome::Cancelled(reason) => return Outcome::cancelled(reason),
            Outcome::Panicked(payload) => return Outcome::panicked(payload),
        };

        // Clean up temp file
        match self.cleanup_temp_file().await {
            Ok(()) => {}
            Err(e) => return Outcome::Err(e),
        }

        Outcome::ok(final_path)
    }

    /// Cancel the write operation and clean up
    pub async fn cancel(&self, _cx: &Cx) -> Outcome<(), SparseWriterError> {
        // Move temp file to quarantine if configured
        if self.config.enable_quarantine {
            match self.quarantine_temp_file("cancelled").await {
                Ok(_) => {}
                Err(e) => return Outcome::Err(e),
            }
        } else {
            match self.cleanup_temp_file().await {
                Ok(_) => {}
                Err(e) => return Outcome::Err(e),
            }
        }

        Outcome::ok(())
    }

    /// Get current write statistics
    pub fn get_stats(&self) -> SparseWriterStats {
        let state = self.lock_state();
        let total_written = state.range_tracker.total_bytes();
        let chunk_count = state.written_chunks.len();
        let completion_ratio = if let Some(expected) = state.expected_size {
            if expected == 0 {
                1.0
            } else {
                total_written as f64 / expected as f64
            }
        } else {
            0.0
        };

        SparseWriterStats {
            object_id: state.object_id.clone(),
            total_bytes_written: total_written,
            chunk_count,
            allocated_size: state.allocated_size,
            completion_ratio,
            is_preallocated: state.is_preallocated,
            created_at: state.created_at,
            last_write_at: state.last_write_at,
            verification_state: state.verification_state.clone(),
        }
    }

    // Internal implementation methods

    fn ensure_temp_file_open_locked(
        &self,
        state: &mut SparseWriterState,
    ) -> Result<(), SparseWriterError> {
        if state.temp_file.is_some() {
            return Ok(());
        }

        // Generate temp path
        let temp_path = {
            let mut path_mgr = self.path_manager.lock();
            path_mgr
                .create_temp_path(&state.object_id.to_string())
                .map_err(|e| SparseWriterError::TempPathCreation(e.to_string()))?
        };

        // Create and open temp file
        let file = std::fs::OpenOptions::new()
            .create(true)
            .write(true)
            .read(true)
            .truncate(false)
            .open(&temp_path)
            .map_err(|e| SparseWriterError::FileOpen(e.to_string()))?;

        state.temp_file = Some(file);
        state.temp_path = Some(temp_path);

        // Apply preallocation if size is known
        if let Some(size) = state.expected_size {
            if self.config.enable_preallocation {
                match self.preallocate_internal(state, size) {
                    Ok(()) => (),
                    Err(e) => return Err(e),
                }
            }
        }

        Ok(())
    }

    #[allow(unsafe_code)]
    fn preallocate_internal(
        &self,
        state: &mut SparseWriterState,
        size: u64,
    ) -> Result<(), SparseWriterError> {
        if let Some(ref mut file) = state.temp_file {
            if self.platform.filesystem.supports_preallocation {
                // Platform-specific preallocation
                #[cfg(target_os = "linux")]
                {
                    use std::os::unix::io::AsRawFd;
                    let size_i64 = i64::try_from(size)
                        .map_err(|_| SparseWriterError::PreallocationTooLarge { size })?;
                    unsafe {
                        let fd = file.as_raw_fd();
                        let result = libc::fallocate(fd, 0, 0, size_i64);
                        if result == 0 {
                            state.allocated_size = size;
                            state.is_preallocated = true;
                        }
                    }
                }

                #[cfg(not(target_os = "linux"))]
                {
                    // Fallback: seek and write zero
                    match file.seek(SeekFrom::Start(size.saturating_sub(1))) {
                        Ok(_) => {
                            if file.write_all(&[0]).is_ok() {
                                file.seek(SeekFrom::Start(0)).ok();
                                state.allocated_size = size;
                                state.is_preallocated = true;
                            }
                        }
                        Err(_) => {
                            // Preallocation failed, continue without it
                        }
                    }
                }
            }
        }
        Ok(())
    }

    fn write_chunk_locked(
        &self,
        state: &mut SparseWriterState,
        offset: u64,
        data: &[u8],
        options: &WriteOptions,
    ) -> Result<([u8; 32], bool), SparseWriterError> {
        let file = state
            .temp_file
            .as_mut()
            .ok_or(SparseWriterError::NoTempFile)?;

        // Seek to offset
        file.seek(SeekFrom::Start(offset))
            .map_err(|e| SparseWriterError::SeekFailed(e.to_string()))?;

        // Write data
        file.write_all(data)
            .map_err(|e| SparseWriterError::WriteFailed(e.to_string()))?;

        // Compute a stable content digest for later manifest verification.
        let hash = Sha256::digest(data).into();

        // Apply fsync if required
        let synced = if options.force_sync
            || matches!(self.config.fsync_policy, super::FsyncPolicy::EveryWrite)
        {
            file.sync_data()
                .map_err(|e| SparseWriterError::SyncFailed(e.to_string()))?;
            true
        } else {
            false
        };

        Ok((hash, synced))
    }

    async fn apply_fsync_policy(&self) -> Outcome<(), SparseWriterError> {
        let mut state = self.lock_state();
        let verified = matches!(state.verification_state, VerificationState::Verified { .. });

        // Check sync requirements before borrowing file mutably
        let needs_sync_for_every_write =
            matches!(self.config.fsync_policy, super::FsyncPolicy::EveryWrite)
                && state.written_chunks.values().any(|chunk| !chunk.synced);

        if let Some(ref mut file) = state.temp_file {
            match self.config.fsync_policy {
                super::FsyncPolicy::Never => {
                    // No sync required
                }
                super::FsyncPolicy::EveryWrite => {
                    if needs_sync_for_every_write {
                        // Force sync for any unsynced chunks
                        match file.sync_data() {
                            Ok(_) => {
                                // Mark all chunks as synced
                                for chunk in state.written_chunks.values_mut() {
                                    chunk.synced = true;
                                }
                            }
                            Err(e) => {
                                return Outcome::Err(SparseWriterError::SyncFailed(e.to_string()));
                            }
                        }
                    }
                }
                super::FsyncPolicy::VerifiedChunks => {
                    // Sync only if verification passed
                    if verified {
                        match file.sync_data() {
                            Ok(_) => {}
                            Err(e) => {
                                return Outcome::Err(SparseWriterError::SyncFailed(e.to_string()));
                            }
                        }
                    }
                }
                super::FsyncPolicy::BeforeCommit => match file.sync_data() {
                    Ok(_) => {}
                    Err(e) => return Outcome::Err(SparseWriterError::SyncFailed(e.to_string())),
                },
            }
        }

        Outcome::ok(())
    }

    async fn atomic_commit(&self) -> Outcome<PathBuf, SparseWriterError> {
        let (temp_path, final_path) = {
            let mut state = self.lock_state();
            let temp_path = match state.temp_path.clone() {
                Some(path) => path,
                None => return Outcome::Err(SparseWriterError::NoTempFile),
            };
            let final_path = state.final_path.clone();
            if let Some(file) = state.temp_file.as_mut() {
                if let Err(error) = file.flush() {
                    return Outcome::Err(SparseWriterError::CommitFailed(format!(
                        "flush before commit failed: {error}"
                    )));
                }
            }
            state.temp_file = None;
            (temp_path, final_path)
        };

        match self.config.commit_policy {
            CommitPolicy::AtomicRename => match std::fs::rename(&temp_path, &final_path) {
                Ok(_) => {}
                Err(e) => return Outcome::Err(SparseWriterError::CommitFailed(e.to_string())),
            },
            CommitPolicy::CopyAndVerify => {
                match std::fs::copy(&temp_path, &final_path) {
                    Ok(_) => {}
                    Err(e) => return Outcome::Err(SparseWriterError::CommitFailed(e.to_string())),
                }
                if let Err(error) = Self::verify_copied_file(&temp_path, &final_path) {
                    return Outcome::Err(error);
                }
            }
            CommitPolicy::LinkAndUnlink => {
                #[cfg(unix)]
                {
                    match std::fs::hard_link(&temp_path, &final_path) {
                        Ok(_) => {}
                        Err(e) => {
                            return Outcome::Err(SparseWriterError::CommitFailed(e.to_string()));
                        }
                    }
                    match std::fs::remove_file(&temp_path) {
                        Ok(_) => {}
                        Err(e) => {
                            return Outcome::Err(SparseWriterError::CommitFailed(e.to_string()));
                        }
                    }
                }
                #[cfg(not(unix))]
                {
                    // Fallback to copy on non-Unix systems
                    match std::fs::copy(&temp_path, &final_path) {
                        Ok(_) => {}
                        Err(e) => {
                            return Outcome::Err(SparseWriterError::CommitFailed(e.to_string()));
                        }
                    }
                }
            }
        }

        Outcome::ok(final_path)
    }

    async fn cleanup_temp_file(&self) -> Result<(), SparseWriterError> {
        let mut state = self.lock_state();

        if let Some(temp_path) = state.temp_path.take() {
            std::fs::remove_file(&temp_path).ok(); // Ignore errors
        }

        state.temp_file = None;
        Ok(())
    }

    async fn quarantine_temp_file(&self, reason: &str) -> Result<(), SparseWriterError> {
        let mut state = self.lock_state();

        if let Some(temp_path) = state.temp_path.take() {
            let mut path_mgr = self.path_manager.lock();
            path_mgr
                .quarantine_file(&temp_path, reason)
                .map_err(|e| SparseWriterError::TempPathCreation(e.to_string()))?;
        }

        state.temp_file = None;
        Ok(())
    }

    fn fail_verification(&self, reason: String) -> Outcome<(), SparseWriterError> {
        let mut state = self.lock_state();
        Self::fail_verification_locked(&mut state, reason)
    }

    fn fail_verification_locked(
        state: &mut SparseWriterState,
        reason: String,
    ) -> Outcome<(), SparseWriterError> {
        state.verification_state = VerificationState::Failed {
            reason: reason.clone(),
        };
        Outcome::Err(SparseWriterError::VerificationFailed(reason))
    }

    fn verify_copied_file(source: &Path, destination: &Path) -> Result<(), SparseWriterError> {
        let source_digest = Self::file_digest(source)?;
        let destination_digest = Self::file_digest(destination)?;
        if source_digest != destination_digest {
            return Err(SparseWriterError::CommitFailed(
                "copy verification failed: destination digest differs from temp file".to_owned(),
            ));
        }
        Ok(())
    }

    fn file_digest(path: &Path) -> Result<[u8; 32], SparseWriterError> {
        let mut file =
            File::open(path).map_err(|error| SparseWriterError::CommitFailed(error.to_string()))?;
        let mut hasher = Sha256::new();
        let mut buffer = [0u8; 8192];
        loop {
            let read = file
                .read(&mut buffer)
                .map_err(|error| SparseWriterError::CommitFailed(error.to_string()))?;
            if read == 0 {
                break;
            }
            hasher.update(&buffer[..read]);
        }
        Ok(hasher.finalize().into())
    }

    fn hash_file_with_prefix(
        file: &mut File,
        prefix: Option<&[u8]>,
    ) -> Result<[u8; 32], SparseWriterError> {
        file.seek(SeekFrom::Start(0))
            .map_err(|error| SparseWriterError::VerificationFailed(error.to_string()))?;
        let mut hasher = Sha256::new();
        if let Some(prefix) = prefix {
            hasher.update(prefix);
        }
        let mut buffer = [0u8; DIGEST_BUFFER_SIZE];
        loop {
            let read = file
                .read(&mut buffer)
                .map_err(|error| SparseWriterError::VerificationFailed(error.to_string()))?;
            if read == 0 {
                break;
            }
            hasher.update(&buffer[..read]);
        }
        Ok(hasher.finalize().into())
    }

    fn hash_file_range(
        file: &mut File,
        offset: u64,
        size: u64,
    ) -> Result<[u8; 32], SparseWriterError> {
        file.seek(SeekFrom::Start(offset))
            .map_err(|error| SparseWriterError::VerificationFailed(error.to_string()))?;
        let mut remaining = size;
        let mut hasher = Sha256::new();
        let mut buffer = [0u8; DIGEST_BUFFER_SIZE];

        while remaining > 0 {
            let to_read = remaining.min(buffer.len() as u64) as usize;
            file.read_exact(&mut buffer[..to_read])
                .map_err(|error| SparseWriterError::VerificationFailed(error.to_string()))?;
            hasher.update(&buffer[..to_read]);
            remaining -= to_read as u64;
        }

        Ok(hasher.finalize().into())
    }
}

/// Statistics for sparse writer operations
#[derive(Debug, Clone)]
pub struct SparseWriterStats {
    pub object_id: ObjectId,
    pub total_bytes_written: u64,
    pub chunk_count: usize,
    pub allocated_size: u64,
    pub completion_ratio: f64,
    pub is_preallocated: bool,
    pub created_at: Instant,
    pub last_write_at: Instant,
    pub verification_state: VerificationState,
}

/// Errors that can occur during sparse writing
#[derive(Debug, thiserror::Error)]
pub enum SparseWriterError {
    #[error("Platform detection failed: {0}")]
    PlatformDetection(String),

    #[error("Failed to create temp path: {0}")]
    TempPathCreation(String),

    #[error("Failed to open file: {0}")]
    FileOpen(String),

    #[error("No temp file available")]
    NoTempFile,

    #[error("Empty chunk not allowed")]
    EmptyChunk,

    #[error("Overlapping write at offset {offset}, size {size}")]
    OverlappingWrite { offset: u64, size: u64 },

    #[error("Invalid chunk range at offset {offset}, size {size}")]
    InvalidRange { offset: u64, size: u64 },

    #[error("Preallocation size is too large: {size}")]
    PreallocationTooLarge { size: u64 },

    #[error("Seek failed: {0}")]
    SeekFailed(String),

    #[error("Write failed: {0}")]
    WriteFailed(String),

    #[error("Sync failed: {0}")]
    SyncFailed(String),

    #[error("Data incomplete, cannot commit")]
    IncompleteData,

    #[error("Data not verified")]
    NotVerified,

    #[error("Verification failed: {0}")]
    VerificationFailed(String),

    #[error("Commit failed: {0}")]
    CommitFailed(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::atp::manifest::Manifest;
    use crate::atp::object::ContentId;
    use crate::atp::object::{MetadataPolicy, Object, ObjectGraph};

    fn create_test_cx() -> Cx {
        Cx::for_testing()
    }

    fn test_object_id(label: &str) -> ObjectId {
        ObjectId::content(ContentId::from_bytes(label.as_bytes()))
    }

    fn unique_temp_path(label: &str) -> PathBuf {
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("test clock should be after epoch")
            .as_nanos();
        std::env::temp_dir().join(format!(
            "asupersync_sparse_writer_{label}_{}_{}",
            std::process::id(),
            nanos
        ))
    }

    fn manifest_for_content(content: &[u8]) -> (ObjectId, Manifest) {
        let object = Object::file(content.to_vec());
        let object_id = object.id.clone();
        let mut graph = ObjectGraph::new();
        graph
            .add_root(object)
            .expect("test object graph should build");
        let manifest = Manifest::from_graph(&graph, MetadataPolicy::portable())
            .expect("manifest should build");
        (object_id, manifest)
    }

    #[test]
    fn test_sparse_writer_basic() {
        futures_lite::future::block_on(async {
            let cx = create_test_cx();
            let object_id = test_object_id("test-object");
            let temp_dir = std::env::temp_dir();
            let final_path = temp_dir.join("test_sparse_output");

            let config = SparseWriterConfig::default();
            let writer = SparseWriter::new(&cx, object_id, final_path, config)
                .await
                .unwrap();

            // Set expected size
            writer.set_expected_size(1000).unwrap();

            // Write some chunks out of order
            let options = WriteOptions::default();
            writer
                .write_chunk(&cx, 500, b"middle", options.clone())
                .await
                .unwrap();
            writer
                .write_chunk(&cx, 0, b"start", options.clone())
                .await
                .unwrap();
            writer.write_chunk(&cx, 994, b"end", options).await.unwrap();

            // Check completion status
            assert!(!writer.is_complete()); // Still has gaps

            // Fill remaining gaps
            let fill_data = vec![0u8; 495];
            writer
                .write_chunk(&cx, 5, &fill_data, WriteOptions::default())
                .await
                .unwrap();
            let middle_fill = vec![0u8; 488];
            writer
                .write_chunk(&cx, 506, &middle_fill, WriteOptions::default())
                .await
                .unwrap();
            let end_fill = vec![0u8; 3];
            writer
                .write_chunk(&cx, 997, &end_fill, WriteOptions::default())
                .await
                .unwrap();

            assert!(writer.is_complete());
        });
    }

    #[test]
    fn test_overlapping_write_detection() {
        futures_lite::future::block_on(async {
            let cx = create_test_cx();
            let object_id = test_object_id("test-overlap");
            let temp_dir = std::env::temp_dir();
            let final_path = temp_dir.join("test_overlap_output");

            let config = SparseWriterConfig::default();
            let writer = SparseWriter::new(&cx, object_id, final_path, config)
                .await
                .unwrap();

            let options = WriteOptions::default();

            // First write
            writer
                .write_chunk(&cx, 0, b"hello", options.clone())
                .await
                .unwrap();

            // Overlapping write should fail
            let result = writer.write_chunk(&cx, 2, b"world", options).await;
            assert!(matches!(
                result,
                Outcome::Err(SparseWriterError::OverlappingWrite { .. })
            ));
        });
    }

    #[test]
    fn test_preallocation() {
        futures_lite::future::block_on(async {
            let cx = create_test_cx();
            let object_id = test_object_id("test-prealloc");
            let temp_dir = std::env::temp_dir();
            let final_path = temp_dir.join("test_prealloc_output");

            let mut config = SparseWriterConfig::default();
            config.enable_preallocation = true;

            let writer = SparseWriter::new(&cx, object_id, final_path, config)
                .await
                .unwrap();

            // Set expected size - should trigger preallocation
            writer.set_expected_size(1024 * 1024).unwrap();

            let stats = writer.get_stats();
            // Note: actual preallocation depends on platform support
            assert!(stats.allocated_size <= 1024 * 1024);
        });
    }

    #[test]
    fn streaming_content_id_hash_matches_object_content_id_domain() {
        let content = b"content id domain regression guard";
        let path = unique_temp_path("content_id_domain");
        std::fs::write(&path, content).unwrap();
        let mut file = File::open(&path).unwrap();

        let streaming_hash =
            SparseWriter::hash_file_with_prefix(&mut file, Some(CONTENT_ID_DOMAIN)).unwrap();

        assert_eq!(streaming_hash, *ContentId::from_bytes(content).hash());
        std::fs::remove_file(path).ok();
    }

    #[test]
    fn stats_report_complete_ratio_for_zero_expected_size() {
        futures_lite::future::block_on(async {
            let cx = create_test_cx();
            let object_id = test_object_id("test-zero-stats");
            let writer = SparseWriter::new(
                &cx,
                object_id,
                unique_temp_path("zero_stats"),
                SparseWriterConfig::default(),
            )
            .await
            .unwrap();

            writer.set_expected_size(0).unwrap();

            let stats = writer.get_stats();
            assert_eq!(stats.completion_ratio, 1.0);
            assert!(writer.is_complete());
        });
    }

    #[test]
    fn verify_checks_manifest_content_hash_and_records_real_root() {
        futures_lite::future::block_on(async {
            let cx = create_test_cx();
            let content = b"verified sparse writer content";
            let (object_id, manifest) = manifest_for_content(content);
            let writer = SparseWriter::new(
                &cx,
                object_id,
                unique_temp_path("verify_success"),
                SparseWriterConfig::default(),
            )
            .await
            .unwrap();

            writer.set_expected_size(content.len() as u64).unwrap();
            writer
                .write_chunk(&cx, 0, content, WriteOptions::default())
                .await
                .unwrap();

            writer.verify(&cx, &manifest).await.unwrap();

            let stats = writer.get_stats();
            assert_eq!(
                stats.verification_state,
                VerificationState::Verified {
                    manifest_root: manifest.merkle_root
                }
            );
        });
    }

    #[test]
    fn commit_atomic_rename_closes_temp_handle_before_filesystem_move() {
        futures_lite::future::block_on(async {
            let cx = create_test_cx();
            let content = b"verified sparse writer commit content";
            let (object_id, manifest) = manifest_for_content(content);
            let final_path = unique_temp_path("commit_atomic_final");
            let writer =
                SparseWriter::new(&cx, object_id, &final_path, SparseWriterConfig::default())
                    .await
                    .unwrap();

            writer.set_expected_size(content.len() as u64).unwrap();
            writer
                .write_chunk(&cx, 0, content, WriteOptions::default())
                .await
                .unwrap();
            writer.verify(&cx, &manifest).await.unwrap();

            let committed_path = writer.commit(&cx).await.unwrap();

            assert_eq!(committed_path, final_path);
            assert_eq!(std::fs::read(&committed_path).unwrap(), content);
            std::fs::remove_file(&committed_path).ok();
        });
    }

    #[test]
    fn verify_fails_closed_on_manifest_content_mismatch() {
        futures_lite::future::block_on(async {
            let cx = create_test_cx();
            let expected = b"expected bytes";
            let actual = b"tampered bytes";
            assert_eq!(expected.len(), actual.len());
            let (object_id, manifest) = manifest_for_content(expected);
            let writer = SparseWriter::new(
                &cx,
                object_id,
                unique_temp_path("verify_mismatch"),
                SparseWriterConfig::default(),
            )
            .await
            .unwrap();

            writer.set_expected_size(actual.len() as u64).unwrap();
            writer
                .write_chunk(&cx, 0, actual, WriteOptions::default())
                .await
                .unwrap();

            let result = writer.verify(&cx, &manifest).await;

            assert!(matches!(
                result,
                Outcome::Err(SparseWriterError::VerificationFailed(_))
            ));
            assert!(matches!(
                writer.get_stats().verification_state,
                VerificationState::Failed { .. }
            ));
        });
    }
}