rustfs-storage-api 1.0.1

Storage API contracts for RustFS.
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
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt;
use std::sync::Arc;
use std::time::Duration;

use crate::replication::{
    ReplicationState, ReplicationStatusType, VersionPurgeStatusType, replication_statuses_map, version_purge_statuses_map,
};
use time::OffsetDateTime;
use uuid::Uuid;

const NULL_VERSION_MARKER: &str = "null";

#[derive(Debug, Default, Clone)]
pub struct HTTPPreconditions {
    pub if_match: Option<String>,
    pub if_none_match: Option<String>,
    pub if_modified_since: Option<OffsetDateTime>,
    pub if_unmodified_since: Option<OffsetDateTime>,
}

impl HTTPPreconditions {
    pub fn if_match_value(&self) -> Option<&str> {
        non_empty_condition_value(self.if_match.as_deref())
    }

    pub fn if_none_match_value(&self) -> Option<&str> {
        non_empty_condition_value(self.if_none_match.as_deref())
    }
}

#[derive(Debug, Default, Clone)]
pub struct ObjectLockRetentionOptions {
    pub mode: Option<String>,
    pub retain_until: Option<OffsetDateTime>,
    pub bypass_governance: bool,
}

#[derive(Debug, Clone, Default)]
pub struct ExpirationOptions {
    pub expire: bool,
}

#[derive(Debug, Default, Clone)]
pub struct ObjectLockDeleteOptions {
    pub bypass_governance: bool,
}

#[derive(Debug, Default, Clone)]
pub struct TransitionedObject {
    pub name: String,
    pub version_id: String,
    pub tier: String,
    pub free_version: bool,
    pub status: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ObjectPreconditionPart {
    pub number: usize,
    pub exists: bool,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ObjectPreconditionState<'a> {
    pub etag: Option<&'a str>,
    pub mod_time: Option<OffsetDateTime>,
    pub requested_part: Option<ObjectPreconditionPart>,
}

impl ObjectPreconditionState<'_> {
    pub fn check(self, preconditions: Option<&HTTPPreconditions>) -> Result<(), ObjectPreconditionError> {
        if let Some(part) = self.requested_part
            && part.number > 1
            && !part.exists
        {
            return Err(ObjectPreconditionError::InvalidPartNumber(part.number));
        }

        let Some(preconditions) = preconditions else {
            return Ok(());
        };

        let has_valid_mod_time = self.mod_time.is_some_and(|t| t != OffsetDateTime::UNIX_EPOCH);
        let if_none_match = preconditions.if_none_match_value();
        let if_match = preconditions.if_match_value();

        if let Some(if_none_match) = if_none_match
            && let Some(etag) = self.etag
            && etag_matches(etag, if_none_match)
        {
            return Err(ObjectPreconditionError::NotModified);
        }

        if has_valid_mod_time
            && let Some(if_modified_since) = &preconditions.if_modified_since
            && let Some(mod_time) = &self.mod_time
            && !is_modified_since(mod_time, if_modified_since)
        {
            return Err(ObjectPreconditionError::NotModified);
        }

        if let Some(if_match) = if_match {
            if let Some(etag) = self.etag {
                if !etag_matches(etag, if_match) {
                    return Err(ObjectPreconditionError::PreconditionFailed);
                }
            } else {
                return Err(ObjectPreconditionError::PreconditionFailed);
            }
        }

        if has_valid_mod_time
            && if_match.is_none()
            && let Some(if_unmodified_since) = &preconditions.if_unmodified_since
            && let Some(mod_time) = &self.mod_time
            && is_modified_since(mod_time, if_unmodified_since)
        {
            return Err(ObjectPreconditionError::PreconditionFailed);
        }

        Ok(())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ObjectPreconditionError {
    InvalidPartNumber(usize),
    NotModified,
    PreconditionFailed,
}

impl fmt::Display for ObjectPreconditionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidPartNumber(part_number) => write!(f, "invalid part number {part_number}"),
            Self::NotModified => f.write_str("object not modified"),
            Self::PreconditionFailed => f.write_str("object precondition failed"),
        }
    }
}

impl std::error::Error for ObjectPreconditionError {}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VersionMarker {
    Null,
    Version(Uuid),
}

impl VersionMarker {
    pub fn parse(marker: impl AsRef<str>) -> Result<Self, uuid::Error> {
        let marker = marker.as_ref();
        if marker == NULL_VERSION_MARKER {
            Ok(Self::Null)
        } else {
            let version = Uuid::parse_str(marker)?;
            // Older releases advertised the null version as a nil UUID
            // (issue #6745); a stored null version has no UUID, so resuming
            // by `Version(nil)` could never match. Fold it into `Null`.
            if version.is_nil() {
                Ok(Self::Null)
            } else {
                Ok(Self::Version(version))
            }
        }
    }
}

#[derive(Clone, Default, PartialEq, Eq)]
pub enum WalkVersionsSortOrder {
    #[default]
    Ascending,
    Descending,
}

/// In-memory compare-and-set identity for a queued exact-version delete.
///
/// `version_id` identifies the S3 version while these fields identify the
/// concrete generation observed before the delete was queued. This matters for
/// the reusable null version ID in versioning-suspended buckets.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ObjectToDeleteIdentity {
    pub data_dir: Option<Uuid>,
    pub mod_time: Option<OffsetDateTime>,
    pub delete_marker: bool,
}

#[derive(Debug, Default, Clone)]
pub struct ObjectToDelete {
    pub object_name: String,
    pub version_id: Option<Uuid>,
    /// RustFS-only precondition checked under the object write lock.
    #[doc(hidden)]
    pub expected_identity: Option<ObjectToDeleteIdentity>,
    pub synthetic_version_id: bool,
    pub delete_marker_replication_status: Option<String>,
    pub version_purge_status: Option<VersionPurgeStatusType>,
    pub version_purge_statuses: Option<String>,
    pub replicate_decision_str: Option<String>,
}

impl ObjectToDelete {
    pub fn with_expected_identity(
        mut self,
        data_dir: Option<Uuid>,
        mod_time: Option<OffsetDateTime>,
        delete_marker: bool,
    ) -> Self {
        self.expected_identity = Some(ObjectToDeleteIdentity {
            data_dir,
            mod_time,
            delete_marker,
        });
        self
    }

    pub fn replication_state(&self) -> ReplicationState {
        ReplicationState {
            replication_status_internal: self.delete_marker_replication_status.clone(),
            version_purge_status_internal: self.version_purge_statuses.clone(),
            replicate_decision_str: self.replicate_decision_str.clone().unwrap_or_default(),
            targets: replication_statuses_map(self.delete_marker_replication_status.as_deref().unwrap_or_default()),
            purge_targets: version_purge_statuses_map(self.version_purge_statuses.as_deref().unwrap_or_default()),
            ..Default::default()
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct DeletedObject {
    pub delete_marker: bool,
    /// True when the delete plan looked the target up and found no such
    /// object or version.
    ///
    /// The lookup only runs when the plan needs the source (Object Lock
    /// check, replication decision, tier journal, or an expected identity),
    /// so this proves absence and never proves presence: it stays false when
    /// no lookup ran. Callers that must not announce a delete that removed
    /// nothing need this, because the disk layer treats an absent version as
    /// an idempotent success and reports `found` regardless.
    pub source_missing: bool,
    pub delete_marker_version_id: Option<Uuid>,
    pub object_name: String,
    pub version_id: Option<Uuid>,
    pub delete_marker_mtime: Option<OffsetDateTime>,
    pub replication_state: Option<ReplicationState>,
    pub found: bool,
    pub force_delete: bool,
    pub force_delete_id: Option<Uuid>,
    pub force_delete_target_arns: Vec<String>,
    pub force_delete_generation: Option<i64>,
}

/// Accounting identity returned by the internal commit-time delete path.
///
/// This is carried separately from [`DeletedObject`] so adding quota details
/// does not change the source shape of the public S3 delete result contract.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DeleteAccounting {
    pub size: Option<u64>,
    pub version_id: Option<Uuid>,
    pub removed_current_object: bool,
}

impl DeletedObject {
    pub fn version_purge_status(&self) -> VersionPurgeStatusType {
        self.replication_state
            .as_ref()
            .map(|v| v.composite_version_purge_status())
            .unwrap_or(VersionPurgeStatusType::Empty)
    }

    pub fn delete_marker_replication_status(&self) -> ReplicationStatusType {
        self.replication_state
            .as_ref()
            .map(|v| v.composite_replication_status())
            .unwrap_or(ReplicationStatusType::Empty)
    }
}

#[derive(Clone)]
pub struct WalkOptions<Filter> {
    pub filter: Option<Filter>,
    pub marker: Option<String>,
    pub latest_only: bool,
    pub ask_disks: String,
    pub versions_sort: WalkVersionsSortOrder,
    pub limit: usize,
    pub include_free_versions: bool,
    pub walkdir_timeout: Option<Duration>,
    pub walkdir_stall_timeout: Option<Duration>,
}

impl<Filter> Default for WalkOptions<Filter> {
    fn default() -> Self {
        Self {
            filter: None,
            marker: None,
            latest_only: false,
            ask_disks: String::new(),
            versions_sort: WalkVersionsSortOrder::default(),
            limit: 0,
            include_free_versions: false,
            walkdir_timeout: None,
            walkdir_stall_timeout: None,
        }
    }
}

impl<Filter> WalkOptions<Filter> {
    pub fn with_walkdir_timeouts(mut self, timeout: Duration) -> Self {
        self.walkdir_timeout = Some(timeout);
        self.walkdir_stall_timeout = Some(timeout);
        self
    }
}

#[async_trait::async_trait]
pub trait ObjectIO: Send + Sync + fmt::Debug + 'static {
    type Error: std::error::Error + Send + Sync + 'static;
    type RangeSpec: Send + 'static;
    type HeaderMap: Send + 'static;
    type ObjectOptions: Send + Sync + 'static;
    type ObjectInfo: Send + 'static;
    type GetObjectReader: Send + 'static;
    type PutObjectReader: Send + 'static;

    async fn get_object_reader(
        &self,
        bucket: &str,
        object: &str,
        range: Option<Self::RangeSpec>,
        h: Self::HeaderMap,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::GetObjectReader, Self::Error>;

    async fn put_object(
        &self,
        bucket: &str,
        object: &str,
        data: &mut Self::PutObjectReader,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::ObjectInfo, Self::Error>;
}

#[async_trait::async_trait]
#[allow(clippy::too_many_arguments)]
pub trait ObjectOperations: Send + Sync + fmt::Debug {
    type Error: std::error::Error + Send + Sync + 'static;
    type ObjectInfo: Send + 'static;
    type ObjectOptions: Send + Sync + 'static;
    type FileInfo: Send + Sync + 'static;
    type ObjectToDelete: Send + 'static;
    type DeletedObject: Send + 'static;

    async fn get_object_info(
        &self,
        bucket: &str,
        object: &str,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::ObjectInfo, Self::Error>;
    async fn verify_object_integrity(&self, bucket: &str, object: &str, opts: &Self::ObjectOptions) -> Result<(), Self::Error>;
    async fn copy_object(
        &self,
        src_bucket: &str,
        src_object: &str,
        dst_bucket: &str,
        dst_object: &str,
        src_info: &mut Self::ObjectInfo,
        src_opts: &Self::ObjectOptions,
        dst_opts: &Self::ObjectOptions,
    ) -> Result<Self::ObjectInfo, Self::Error>;
    async fn delete_object_version(
        &self,
        bucket: &str,
        object: &str,
        fi: &Self::FileInfo,
        force_del_marker: bool,
    ) -> Result<(), Self::Error>;
    async fn delete_object(&self, bucket: &str, object: &str, opts: Self::ObjectOptions)
    -> Result<Self::ObjectInfo, Self::Error>;
    async fn delete_objects(
        &self,
        bucket: &str,
        objects: Vec<Self::ObjectToDelete>,
        opts: Self::ObjectOptions,
    ) -> (Vec<Self::DeletedObject>, Vec<Option<Self::Error>>);
    /// Delete objects and optionally return commit-time accounting identities.
    /// The default preserves the ordinary delete contract for implementations
    /// that do not expose storage-level accounting details.
    async fn delete_objects_with_accounting(
        &self,
        bucket: &str,
        objects: Vec<Self::ObjectToDelete>,
        opts: Self::ObjectOptions,
    ) -> (Vec<Self::DeletedObject>, Vec<Option<Self::Error>>, Vec<Option<DeleteAccounting>>) {
        let object_count = objects.len();
        let (deleted, errors) = self.delete_objects(bucket, objects, opts).await;
        (deleted, errors, vec![None; object_count])
    }
    async fn put_object_metadata(
        &self,
        bucket: &str,
        object: &str,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::ObjectInfo, Self::Error>;
    async fn get_object_tags(&self, bucket: &str, object: &str, opts: &Self::ObjectOptions) -> Result<String, Self::Error>;
    async fn put_object_tags(
        &self,
        bucket: &str,
        object: &str,
        tags: &str,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::ObjectInfo, Self::Error>;
    async fn delete_object_tags(
        &self,
        bucket: &str,
        object: &str,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::ObjectInfo, Self::Error>;
    async fn add_partial(&self, bucket: &str, object: &str, version_id: &str) -> Result<(), Self::Error>;
    async fn transition_object(&self, bucket: &str, object: &str, opts: &Self::ObjectOptions) -> Result<(), Self::Error>;
    async fn restore_transitioned_object(
        self: Arc<Self>,
        bucket: &str,
        object: &str,
        opts: &Self::ObjectOptions,
    ) -> Result<(), Self::Error>;
}

#[async_trait::async_trait]
#[allow(clippy::too_many_arguments)]
pub trait ListOperations: Send + Sync + fmt::Debug {
    type Error: std::error::Error + Send + Sync + 'static;
    type ListObjectsV2Info: Send + 'static;
    type ListObjectVersionsInfo: Send + 'static;
    type ObjectInfoOrErr: Send + 'static;
    type WalkOptions: Send + 'static;
    type WalkCancellation: Send + 'static;
    type WalkResultSender: Send + 'static;

    async fn list_objects_v2(
        self: Arc<Self>,
        bucket: &str,
        prefix: &str,
        continuation_token: Option<String>,
        delimiter: Option<String>,
        max_keys: i32,
        fetch_owner: bool,
        start_after: Option<String>,
        incl_deleted: bool,
    ) -> Result<Self::ListObjectsV2Info, Self::Error>;

    async fn list_object_versions(
        self: Arc<Self>,
        bucket: &str,
        prefix: &str,
        marker: Option<String>,
        version_marker: Option<String>,
        delimiter: Option<String>,
        max_keys: i32,
    ) -> Result<Self::ListObjectVersionsInfo, Self::Error>;

    async fn walk(
        self: Arc<Self>,
        rx: Self::WalkCancellation,
        bucket: &str,
        prefix: &str,
        result: Self::WalkResultSender,
        opts: Self::WalkOptions,
    ) -> Result<(), Self::Error>;
}

#[async_trait::async_trait]
#[allow(clippy::too_many_arguments)]
pub trait MultipartOperations: Send + Sync + fmt::Debug {
    type Error: std::error::Error + Send + Sync + 'static;
    type ObjectInfo: Send + 'static;
    type ObjectOptions: Send + Sync + 'static;
    type PutObjectReader: Send + 'static;
    type CompletePart: Send + 'static;
    type ListMultipartsInfo: Send + 'static;
    type MultipartUploadResult: Send + 'static;
    type PartInfo: Send + 'static;
    type MultipartInfo: Send + 'static;
    type ListPartsInfo: Send + 'static;

    async fn list_multipart_uploads(
        &self,
        bucket: &str,
        prefix: &str,
        key_marker: Option<String>,
        upload_id_marker: Option<String>,
        delimiter: Option<String>,
        max_uploads: usize,
    ) -> Result<Self::ListMultipartsInfo, Self::Error>;
    async fn new_multipart_upload(
        &self,
        bucket: &str,
        object: &str,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::MultipartUploadResult, Self::Error>;
    async fn copy_object_part(
        &self,
        src_bucket: &str,
        src_object: &str,
        dst_bucket: &str,
        dst_object: &str,
        upload_id: &str,
        part_id: usize,
        start_offset: i64,
        length: i64,
        src_info: &Self::ObjectInfo,
        src_opts: &Self::ObjectOptions,
        dst_opts: &Self::ObjectOptions,
    ) -> Result<(), Self::Error>;
    async fn put_object_part(
        &self,
        bucket: &str,
        object: &str,
        upload_id: &str,
        part_id: usize,
        data: &mut Self::PutObjectReader,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::PartInfo, Self::Error>;
    async fn get_multipart_info(
        &self,
        bucket: &str,
        object: &str,
        upload_id: &str,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::MultipartInfo, Self::Error>;
    async fn list_object_parts(
        &self,
        bucket: &str,
        object: &str,
        upload_id: &str,
        part_number_marker: Option<usize>,
        max_parts: usize,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::ListPartsInfo, Self::Error>;
    async fn abort_multipart_upload(
        &self,
        bucket: &str,
        object: &str,
        upload_id: &str,
        opts: &Self::ObjectOptions,
    ) -> Result<(), Self::Error>;
    async fn complete_multipart_upload(
        self: Arc<Self>,
        bucket: &str,
        object: &str,
        upload_id: &str,
        uploaded_parts: Vec<Self::CompletePart>,
        opts: &Self::ObjectOptions,
    ) -> Result<Self::ObjectInfo, Self::Error>;
}

#[async_trait::async_trait]
pub trait HealOperations: Send + Sync + fmt::Debug {
    type Error: std::error::Error + Send + Sync + 'static;
    type HealResultItem: Send + 'static;
    type HealOptions: Send + Sync + 'static;

    async fn heal_format(&self, dry_run: bool) -> Result<(Self::HealResultItem, Option<Self::Error>), Self::Error>;
    async fn heal_bucket(&self, bucket: &str, opts: &Self::HealOptions) -> Result<Self::HealResultItem, Self::Error>;
    async fn heal_object(
        &self,
        bucket: &str,
        object: &str,
        version_id: &str,
        opts: &Self::HealOptions,
    ) -> Result<(Self::HealResultItem, Option<Self::Error>), Self::Error>;
    async fn get_pool_and_set(&self, id: &str) -> Result<(Option<usize>, Option<usize>, Option<usize>), Self::Error>;
    async fn check_abandoned_parts(&self, bucket: &str, object: &str, opts: &Self::HealOptions) -> Result<(), Self::Error>;
}

#[async_trait::async_trait]
pub trait NamespaceLocking: Send + Sync + fmt::Debug + 'static {
    type Error: std::error::Error + Send + Sync + 'static;
    type NamespaceLock: Send + 'static;

    async fn new_ns_lock(&self, bucket: &str, object: &str) -> Result<Self::NamespaceLock, Self::Error>;
}

#[derive(Debug, Default)]
pub struct ListObjectsInfo<ObjectItem> {
    pub is_truncated: bool,
    pub next_marker: Option<String>,
    pub objects: Vec<ObjectItem>,
    pub prefixes: Vec<String>,
}

#[derive(Debug, Default)]
pub struct ListObjectsV2Info<ObjectItem> {
    pub is_truncated: bool,
    pub continuation_token: Option<String>,
    pub next_continuation_token: Option<String>,
    pub objects: Vec<ObjectItem>,
    pub prefixes: Vec<String>,
}

#[derive(Debug, Default, Clone)]
pub struct ListObjectVersionsInfo<ObjectItem> {
    pub is_truncated: bool,
    pub next_marker: Option<String>,
    pub next_version_idmarker: Option<String>,
    pub objects: Vec<ObjectItem>,
    pub prefixes: Vec<String>,
}

#[derive(Debug)]
pub struct ObjectInfoOrErr<ObjectItem, ListError> {
    pub item: Option<ObjectItem>,
    pub err: Option<ListError>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HTTPRangeError {
    InvalidRangeSpec(String),
}

impl fmt::Display for HTTPRangeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidRangeSpec(message) => f.write_str(message),
        }
    }
}

impl std::error::Error for HTTPRangeError {}

#[derive(Debug, Clone)]
pub struct HTTPRangeSpec {
    pub is_suffix_length: bool,
    pub start: i64,
    pub end: i64,
}

impl HTTPRangeSpec {
    pub fn from_part_sizes(object_size: i64, part_number: usize, part_sizes: impl IntoIterator<Item = i64>) -> Option<Self> {
        if object_size == 0 || part_number == 0 {
            return None;
        }

        let mut start = 0_i64;
        let mut end = -1_i64;
        let mut parts = part_sizes.into_iter();
        for _ in 0..part_number {
            let part_size = parts.next()?;
            start = end.checked_add(1)?;
            end = start.checked_add(part_size)?.checked_sub(1)?;
        }

        Some(Self {
            is_suffix_length: false,
            start,
            end,
        })
    }

    pub fn get_offset_length(&self, res_size: i64) -> Result<(usize, i64), HTTPRangeError> {
        let len = self.get_length(res_size)?;

        let mut start = self.start;
        if self.is_suffix_length {
            let suffix_len = if self.start < 0 {
                self.start
                    .checked_neg()
                    .ok_or_else(|| HTTPRangeError::InvalidRangeSpec("range value invalid: suffix length overflow".to_string()))?
            } else {
                self.start
            };
            start = res_size - suffix_len;
            if start < 0 {
                start = 0;
            }
        }
        let offset = usize::try_from(start)
            .map_err(|_| HTTPRangeError::InvalidRangeSpec("range value invalid: start offset overflow".to_string()))?;
        Ok((offset, len))
    }

    pub fn get_length(&self, res_size: i64) -> Result<i64, HTTPRangeError> {
        if res_size < 0 {
            return Err(HTTPRangeError::InvalidRangeSpec("The requested range is not satisfiable".to_string()));
        }

        if self.is_suffix_length {
            let specified_len = if self.start < 0 {
                self.start
                    .checked_neg()
                    .ok_or_else(|| HTTPRangeError::InvalidRangeSpec("range value invalid: suffix length overflow".to_string()))?
            } else {
                self.start
            };
            let mut range_length = specified_len;

            if specified_len > res_size {
                range_length = res_size;
            }

            return Ok(range_length);
        }

        if self.start >= res_size {
            return Err(HTTPRangeError::InvalidRangeSpec("The requested range is not satisfiable".to_string()));
        }

        if self.end > -1 {
            let mut end = self.end;
            if res_size <= end {
                end = res_size - 1;
            }

            let range_length = end - self.start + 1;
            return Ok(range_length);
        }

        if self.end == -1 {
            let range_length = res_size - self.start;
            return Ok(range_length);
        }

        Err(HTTPRangeError::InvalidRangeSpec(format!(
            "range value invalid: start={}, end={}, expected start <= end and end >= -1",
            self.start, self.end
        )))
    }
}

fn non_empty_condition_value(value: Option<&str>) -> Option<&str> {
    value.map(str::trim).filter(|value| !value.is_empty())
}

fn etag_matches(object_etag: &str, condition_etag: &str) -> bool {
    let object_etag = object_etag.trim_matches('"');
    let condition_etag = condition_etag.trim_matches('"');
    condition_etag == "*" || object_etag == condition_etag
}

fn is_modified_since(mod_time: &OffsetDateTime, given_time: &OffsetDateTime) -> bool {
    mod_time.unix_timestamp() > given_time.unix_timestamp()
}

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

    #[test]
    fn version_marker_parse_folds_null_and_nil_uuid_into_null() {
        assert_eq!(VersionMarker::parse("null"), Ok(VersionMarker::Null));
        // Older releases advertised the null version as a nil UUID
        // (issue #6745); it must resume as the null marker, not a UUID no
        // stored version carries.
        assert_eq!(VersionMarker::parse(Uuid::nil().to_string()), Ok(VersionMarker::Null));

        let version = Uuid::from_u128(7);
        assert_eq!(VersionMarker::parse(version.to_string()), Ok(VersionMarker::Version(version)));
        assert!(VersionMarker::parse("not-a-version").is_err());
    }

    #[test]
    fn http_preconditions_ignore_empty_etag_headers() {
        let opts = HTTPPreconditions {
            if_match: Some("  ".to_owned()),
            if_none_match: Some(" * ".to_owned()),
            ..Default::default()
        };

        assert_eq!(opts.if_match_value(), None);
        assert_eq!(opts.if_none_match_value(), Some("*"));
    }

    #[test]
    fn object_lock_retention_defaults_preserve_false_bypass() {
        let opts = ObjectLockRetentionOptions::default();

        assert!(opts.mode.is_none());
        assert!(opts.retain_until.is_none());
        assert!(!opts.bypass_governance);
    }

    #[test]
    fn object_precondition_state_rejects_missing_requested_part() {
        let state = ObjectPreconditionState {
            requested_part: Some(ObjectPreconditionPart {
                number: 3,
                exists: false,
            }),
            ..Default::default()
        };

        assert_eq!(state.check(None), Err(ObjectPreconditionError::InvalidPartNumber(3)));
    }

    #[test]
    fn object_precondition_state_keeps_existing_etag_priority() {
        let preconditions = HTTPPreconditions {
            if_match: Some("\"other\"".to_owned()),
            if_none_match: Some("\"abc\"".to_owned()),
            ..Default::default()
        };
        let state = ObjectPreconditionState {
            etag: Some("\"abc\""),
            ..Default::default()
        };

        assert_eq!(state.check(Some(&preconditions)), Err(ObjectPreconditionError::NotModified));
    }

    #[test]
    fn object_precondition_state_requires_etag_for_if_match() {
        let preconditions = HTTPPreconditions {
            if_match: Some("\"abc\"".to_owned()),
            ..Default::default()
        };

        assert_eq!(
            ObjectPreconditionState::default().check(Some(&preconditions)),
            Err(ObjectPreconditionError::PreconditionFailed)
        );
    }

    #[test]
    fn object_precondition_state_checks_modification_dates() {
        let mod_time = OffsetDateTime::UNIX_EPOCH + time::Duration::seconds(100);
        let preconditions = HTTPPreconditions {
            if_modified_since: Some(OffsetDateTime::UNIX_EPOCH + time::Duration::seconds(100)),
            ..Default::default()
        };
        let state = ObjectPreconditionState {
            mod_time: Some(mod_time),
            ..Default::default()
        };

        assert_eq!(state.check(Some(&preconditions)), Err(ObjectPreconditionError::NotModified));

        let preconditions = HTTPPreconditions {
            if_unmodified_since: Some(OffsetDateTime::UNIX_EPOCH + time::Duration::seconds(99)),
            ..Default::default()
        };

        assert_eq!(state.check(Some(&preconditions)), Err(ObjectPreconditionError::PreconditionFailed));
    }

    #[test]
    fn version_marker_parses_null_and_uuid_markers() {
        assert_eq!(VersionMarker::parse("null").expect("null marker should parse"), VersionMarker::Null);

        let marker = "550e8400-e29b-41d4-a716-446655440000";
        assert_eq!(
            VersionMarker::parse(marker).expect("uuid marker should parse"),
            VersionMarker::Version(Uuid::parse_str(marker).expect("test uuid should parse"))
        );
    }

    #[test]
    fn walk_versions_sort_order_defaults_to_ascending() {
        assert!(matches!(WalkVersionsSortOrder::default(), WalkVersionsSortOrder::Ascending));
    }

    #[test]
    fn walk_options_defaults_preserve_existing_contract() {
        type TestFilter = fn(&str) -> bool;

        let opts = WalkOptions::<TestFilter>::default();

        assert!(opts.filter.is_none());
        assert!(opts.marker.is_none());
        assert!(!opts.latest_only);
        assert!(opts.ask_disks.is_empty());
        assert!(matches!(opts.versions_sort, WalkVersionsSortOrder::Ascending));
        assert_eq!(opts.limit, 0);
        assert!(!opts.include_free_versions);
    }

    #[test]
    fn object_to_delete_builds_replication_state() {
        let object = ObjectToDelete {
            object_name: "photo.jpg".to_owned(),
            delete_marker_replication_status: Some("PENDING".to_owned()),
            version_purge_statuses: Some("arn:minio:replication:::target=PENDING".to_owned()),
            replicate_decision_str: Some("arn:minio:replication:::target=PENDING".to_owned()),
            ..Default::default()
        };

        let state = object.replication_state();

        assert_eq!(state.replication_status_internal.as_deref(), Some("PENDING"));
        assert_eq!(
            state.version_purge_status_internal.as_deref(),
            Some("arn:minio:replication:::target=PENDING")
        );
        assert_eq!(state.replicate_decision_str, "arn:minio:replication:::target=PENDING");
    }

    #[test]
    fn deleted_object_status_helpers_default_to_empty() {
        let object = DeletedObject::default();

        assert_eq!(object.version_purge_status(), VersionPurgeStatusType::Empty);
        assert_eq!(object.delete_marker_replication_status(), ReplicationStatusType::Empty);
    }

    #[test]
    fn lifecycle_helper_defaults_preserve_existing_contracts() {
        let expiration = ExpirationOptions::default();
        let transitioned = TransitionedObject::default();

        assert!(!expiration.expire);
        assert!(transitioned.name.is_empty());
        assert!(transitioned.version_id.is_empty());
        assert!(transitioned.tier.is_empty());
        assert!(!transitioned.free_version);
        assert!(transitioned.status.is_empty());
    }

    #[derive(Debug)]
    struct TestListBackend;

    #[derive(Debug)]
    struct TestListError;

    impl fmt::Display for TestListError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("test list error")
        }
    }

    impl std::error::Error for TestListError {}

    #[async_trait::async_trait]
    impl ListOperations for TestListBackend {
        type Error = TestListError;
        type ListObjectsV2Info = ListObjectsV2Info<&'static str>;
        type ListObjectVersionsInfo = ListObjectVersionsInfo<&'static str>;
        type ObjectInfoOrErr = ObjectInfoOrErr<&'static str, TestListError>;
        type WalkOptions = WalkOptions<fn(&str) -> bool>;
        type WalkCancellation = ();
        type WalkResultSender = ();

        async fn list_objects_v2(
            self: Arc<Self>,
            bucket: &str,
            prefix: &str,
            continuation_token: Option<String>,
            delimiter: Option<String>,
            max_keys: i32,
            fetch_owner: bool,
            start_after: Option<String>,
            incl_deleted: bool,
        ) -> Result<Self::ListObjectsV2Info, Self::Error> {
            assert_eq!(bucket, "bucket");
            assert_eq!(prefix, "photos/");
            assert_eq!(continuation_token.as_deref(), Some("token"));
            assert_eq!(delimiter.as_deref(), Some("/"));
            assert_eq!(max_keys, 10);
            assert!(fetch_owner);
            assert_eq!(start_after.as_deref(), Some("photos/0001.jpg"));
            assert!(!incl_deleted);

            Ok(ListObjectsV2Info {
                is_truncated: true,
                continuation_token,
                next_continuation_token: Some("next".to_owned()),
                objects: vec!["photos/0002.jpg"],
                prefixes: vec!["photos/2024/".to_owned()],
            })
        }

        async fn list_object_versions(
            self: Arc<Self>,
            bucket: &str,
            prefix: &str,
            marker: Option<String>,
            version_marker: Option<String>,
            delimiter: Option<String>,
            max_keys: i32,
        ) -> Result<Self::ListObjectVersionsInfo, Self::Error> {
            assert_eq!(bucket, "bucket");
            assert_eq!(prefix, "photos/");
            assert_eq!(marker.as_deref(), Some("photos/0001.jpg"));
            assert_eq!(version_marker.as_deref(), Some("version"));
            assert_eq!(delimiter.as_deref(), Some("/"));
            assert_eq!(max_keys, 10);

            Ok(ListObjectVersionsInfo {
                is_truncated: false,
                next_marker: None,
                next_version_idmarker: None,
                objects: vec!["photos/0001.jpg"],
                prefixes: Vec::new(),
            })
        }

        async fn walk(
            self: Arc<Self>,
            _rx: Self::WalkCancellation,
            bucket: &str,
            prefix: &str,
            _result: Self::WalkResultSender,
            opts: Self::WalkOptions,
        ) -> Result<(), Self::Error> {
            assert_eq!(bucket, "bucket");
            assert_eq!(prefix, "photos/");
            assert_eq!(opts.marker.as_deref(), Some("photos/0001.jpg"));
            Ok(())
        }
    }

    #[tokio::test]
    async fn list_operations_trait_exposes_generic_list_contract() -> Result<(), TestListError> {
        let backend = Arc::new(TestListBackend);

        let listed = backend
            .clone()
            .list_objects_v2(
                "bucket",
                "photos/",
                Some("token".to_owned()),
                Some("/".to_owned()),
                10,
                true,
                Some("photos/0001.jpg".to_owned()),
                false,
            )
            .await?;
        assert!(listed.is_truncated);
        assert_eq!(listed.objects, vec!["photos/0002.jpg"]);
        assert_eq!(listed.next_continuation_token.as_deref(), Some("next"));

        let versions = backend
            .clone()
            .list_object_versions(
                "bucket",
                "photos/",
                Some("photos/0001.jpg".to_owned()),
                Some("version".to_owned()),
                Some("/".to_owned()),
                10,
            )
            .await?;
        assert_eq!(versions.objects, vec!["photos/0001.jpg"]);

        let opts = WalkOptions::<fn(&str) -> bool> {
            marker: Some("photos/0001.jpg".to_owned()),
            ..Default::default()
        };
        backend.walk((), "bucket", "photos/", (), opts).await?;

        Ok(())
    }

    #[test]
    fn object_list_response_contracts_default_to_empty_collections() {
        let v1 = ListObjectsInfo::<()>::default();
        assert!(!v1.is_truncated);
        assert!(v1.next_marker.is_none());
        assert!(v1.objects.is_empty());
        assert!(v1.prefixes.is_empty());

        let v2 = ListObjectsV2Info::<()>::default();
        assert!(!v2.is_truncated);
        assert!(v2.continuation_token.is_none());
        assert!(v2.next_continuation_token.is_none());
        assert!(v2.objects.is_empty());
        assert!(v2.prefixes.is_empty());

        let versions = ListObjectVersionsInfo::<()>::default();
        assert!(!versions.is_truncated);
        assert!(versions.next_marker.is_none());
        assert!(versions.next_version_idmarker.is_none());
        assert!(versions.objects.is_empty());
        assert!(versions.prefixes.is_empty());
    }

    #[test]
    fn object_info_or_err_keeps_optional_item_and_error_slots() {
        let item = ObjectInfoOrErr::<usize, &str> {
            item: Some(7),
            err: None,
        };

        assert_eq!(item.item, Some(7));
        assert!(item.err.is_none());
    }

    #[test]
    fn http_range_spec_offset_length_handles_suffix_and_bounds() {
        let range = HTTPRangeSpec {
            is_suffix_length: false,
            start: 5,
            end: 10,
        };

        assert_eq!(range.get_offset_length(20).expect("range should fit"), (5, 6));

        let suffix = HTTPRangeSpec {
            is_suffix_length: true,
            start: -5,
            end: -1,
        };

        assert_eq!(suffix.get_offset_length(20).expect("suffix range should fit"), (15, 5));
    }

    #[test]
    fn http_range_spec_from_part_sizes_keeps_part_boundaries() {
        let spec = HTTPRangeSpec::from_part_sizes(100, 3, [10, 15, 20]).expect("third part should exist");

        assert_eq!(spec.start, 25);
        assert_eq!(spec.end, 44);
        assert!(HTTPRangeSpec::from_part_sizes(100, 0, [10]).is_none());
        assert!(HTTPRangeSpec::from_part_sizes(100, 2, [10]).is_none());
    }
}