rocfl 1.7.0

A CLI for OCFL repositories
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
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
//! S3 OCFL storage implementation.

use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
use std::vec::IntoIter;

use bytes::Bytes;
use const_format::concatcp;
use futures::{FutureExt, TryStreamExt};
use globset::GlobBuilder;
use log::{debug, error, info, warn};
use rusoto_core::credential::{AutoRefreshingProvider, ChainProvider, ProfileProvider};
use rusoto_core::{ByteStream, Client, HttpClient, Region, RusotoError};
use rusoto_s3::{
    AbortMultipartUploadRequest, CompleteMultipartUploadRequest, CompletedMultipartUpload,
    CompletedPart, CreateMultipartUploadRequest, DeleteObjectRequest, GetObjectError,
    GetObjectRequest, ListObjectsV2Output, ListObjectsV2Request, PutObjectRequest,
    S3Client as RusotoS3Client, StreamingBody, UploadPartRequest, S3,
};
use serde::de::DeserializeOwned;
use tokio::io::AsyncReadExt;
use tokio::runtime;
use tokio::runtime::Runtime;
use walkdir::WalkDir;

use super::layout::StorageLayout;
use super::{OcflLayout, OcflStore};
use crate::ocfl::consts::*;
use crate::ocfl::error::{not_found, Result, RocflError};
use crate::ocfl::inventory::Inventory;
use crate::ocfl::paths::{join, join_with_trailing_slash};
use crate::ocfl::store::{Listing, OcflLayoutLenient, Storage};
use crate::ocfl::validate::{IncrementalValidator, ObjectValidationResult, Validator};
use crate::ocfl::Knowable::{Known, Unknown};
use crate::ocfl::{
    paths, specs, util, DigestAlgorithm, InventoryPath, Knowable, LayoutExtensionName, LogicalPath,
    ObjectInfo, RepoInfo, SpecVersion, VersionRef,
};

const TYPE_PLAIN: &str = "text/plain; charset=UTF-8";
const TYPE_MARKDOWN: &str = "text/markdown; charset=UTF-8";
const TYPE_JSON: &str = "application/json; charset=UTF-8";

const PART_SIZE: u64 = 1024 * 1024 * 5;

const EXTENSIONS_DIR_SUFFIX: &str = concatcp!("/", EXTENSIONS_DIR);

pub struct S3OcflStore {
    s3_client: Arc<S3Client>,
    /// Maps object IDs to paths within the storage root
    storage_layout: Option<StorageLayout>,
    validator: Validator<S3Storage>,
    // TODO this never expires entries and is only intended to be useful within the scope of the cli
    /// Caches object ID to path mappings
    id_path_cache: RwLock<HashMap<String, String>>,
    prefix: Option<String>,
    closed: Arc<AtomicBool>,
}

impl S3OcflStore {
    /// Creates a new S3OcflStore
    pub fn new(
        region: Region,
        bucket: &str,
        prefix: Option<&str>,
        profile: Option<&str>,
    ) -> Result<Self> {
        let s3_client = S3Client::new(region, bucket, prefix, profile)?;

        check_extensions(&s3_client);
        let storage_layout = load_storage_layout(&s3_client);

        let s3_client = Arc::new(s3_client);

        Ok(Self {
            validator: Validator::new(S3Storage::new(s3_client.clone())),
            s3_client,
            storage_layout,
            id_path_cache: RwLock::new(HashMap::new()),
            prefix: prefix.map(|p| util::trim_trailing_slashes(p).to_string()),
            closed: Arc::new(AtomicBool::new(false)),
        })
    }

    /// Initializes a new OCFL repository at the specified location
    pub fn init(
        region: Region,
        bucket: &str,
        prefix: Option<&str>,
        profile: Option<&str>,
        version: SpecVersion,
        layout: Option<StorageLayout>,
    ) -> Result<Self> {
        let s3_client = S3Client::new(region, bucket, prefix, profile)?;

        init_new_repo(&s3_client, version, layout.as_ref())?;

        let s3_client = Arc::new(s3_client);

        Ok(Self {
            validator: Validator::new(S3Storage::new(s3_client.clone())),
            s3_client,
            storage_layout: layout,
            id_path_cache: RwLock::new(HashMap::new()),
            prefix: prefix.map(|p| util::trim_trailing_slashes(p).to_string()),
            closed: Arc::new(AtomicBool::new(false)),
        })
    }

    /// This method first attempts to locate the path to the object using the storage layout.
    /// If it is not able to, then it scans the repository looking for the object.
    fn lookup_or_find_object_root_path(&self, object_id: &str) -> Result<String> {
        match self.get_object_root_path(object_id) {
            Some(path) => Ok(path),
            None => match self.scan_for_inventory(object_id) {
                Ok(inventory) => Ok(inventory.object_root),
                Err(e) => Err(e),
            },
        }
    }

    /// Returns the storage root relative path to the object by doing a cache look up. If
    /// the mapping was not found in the cache, then it is computed using the configured
    /// storage layout. If there is no storage layout, then `None` is returned.
    fn get_object_root_path(&self, object_id: &str) -> Option<String> {
        if let Ok(cache) = self.id_path_cache.read() {
            if let Some(object_root) = cache.get(object_id) {
                return Some(object_root.clone());
            }
        }

        if let Some(storage_layout) = &self.storage_layout {
            let object_root = storage_layout.map_object_id(object_id);

            if let Ok(mut cache) = self.id_path_cache.write() {
                cache.insert(object_id.to_string(), object_root.clone());
                return Some(object_root);
            }
        }

        None
    }

    fn scan_for_inventory(&self, object_id: &str) -> Result<Inventory> {
        info!(
            "Storage layout not configured, scanning repository to locate object {}",
            &object_id
        );

        let mut iter = InventoryIter::new_id_matching(self, object_id, self.closed.clone());

        loop {
            return match iter.next() {
                Some(Ok(inventory)) => Ok(inventory),
                Some(Err(e)) => {
                    error!("{:#}", e);
                    continue;
                }
                None => Err(not_found(object_id, None)),
            };
        }
    }

    fn parse_inventory_required(&self, object_id: &str, object_root: &str) -> Result<Inventory> {
        match self.parse_inventory(object_root)? {
            Some(inventory) => {
                if inventory.id != object_id {
                    Err(RocflError::CorruptObject {
                        object_id: object_id.to_string(),
                        message: format!(
                            "Expected object to exist at {} but found object {} instead.",
                            object_root, inventory.id
                        ),
                    })
                } else {
                    Ok(inventory)
                }
            }
            None => Err(not_found(object_id, None)),
        }
    }

    /// Parses the HEAD inventory of the OCFL object that's rooted in the specified directory.
    /// This is normally the `inventory.json` file in the object's root, but it could also be
    /// the inventory file in an extension directory, such as the mutable HEAD extension.
    fn parse_inventory(&self, object_root: &str) -> Result<Option<Inventory>> {
        let bytes = self.get_inventory_bytes(object_root)?;
        // TODO should validate hash

        if let Some((bytes, mutable_head)) = bytes {
            let mut inventory = match self.parse_inventory_bytes(&bytes) {
                Ok(inventory) => inventory,
                Err(e) => {
                    return Err(RocflError::General(format!(
                        "Failed to parse inventory in object at {}: {}",
                        object_root, e
                    )))
                }
            };
            inventory.object_root = util::trim_slashes(object_root).to_string();

            inventory.storage_path = match &self.prefix {
                Some(prefix) => join(prefix, &inventory.object_root),
                None => inventory.object_root.clone(),
            };
            inventory.mutable_head = mutable_head;

            Ok(Some(inventory))
        } else {
            Ok(None)
        }
    }

    fn parse_inventory_bytes(&self, bytes: &[u8]) -> Result<Inventory> {
        let inventory: Inventory = serde_json::from_slice(bytes)?;
        Ok(inventory)
    }

    fn get_inventory_bytes(&self, object_root: &str) -> Result<Option<(Vec<u8>, bool)>> {
        let mutable_head_inv = join(object_root, MUTABLE_HEAD_INVENTORY_FILE);

        match self.s3_client.get_object(&mutable_head_inv)? {
            Some(bytes) => {
                info!("Found mutable HEAD at {}", &mutable_head_inv);
                Ok(Some((bytes, true)))
            }
            None => {
                let inv_path = join(object_root, INVENTORY_FILE);
                match self.s3_client.get_object(&inv_path)? {
                    Some(bytes) => Ok(Some((bytes, false))),
                    None => Ok(None),
                }
            }
        }
    }

    fn upload_all_files_with_rollback(
        &self,
        dst_path: &str,
        src_dir: impl AsRef<Path>,
    ) -> Result<Vec<String>> {
        self.do_with_rollback(Vec::new(), |done: &mut Vec<String>| -> Result<()> {
            for file in WalkDir::new(src_dir.as_ref()) {
                // Want an error returned here so that we rollback
                self.ensure_open()?;

                let file = file?;
                if file.file_type().is_dir() {
                    continue;
                }

                let relative_path = pathdiff::diff_paths(file.path(), src_dir.as_ref())
                    .unwrap()
                    .to_string_lossy()
                    .to_string();
                let content_path = util::convert_backslash_to_forward(relative_path.as_ref());
                let storage_path = join(dst_path, content_path.as_ref());
                self.s3_client
                    .put_object_file(&storage_path, file.path(), None)?;
                done.push(storage_path);
            }
            Ok(())
        })
    }

    fn install_inventory_in_root_with_rollback(
        &self,
        object_root: &str,
        digest_algorithm: DigestAlgorithm,
        version_path: impl AsRef<Path>,
        uploaded: Vec<String>,
    ) -> Result<()> {
        let inventory_src = paths::inventory_path(&version_path);
        let sidecar_src = paths::sidecar_path(&version_path, digest_algorithm);
        let inventory_dst = join(object_root, INVENTORY_FILE);
        let sidecar_dst = join(
            object_root,
            &sidecar_src.file_name().unwrap().to_string_lossy(),
        );

        self.do_with_rollback(uploaded, |done: &mut Vec<String>| -> Result<()> {
            self.s3_client
                .put_object_file(&inventory_dst, &inventory_src, Some(TYPE_JSON))?;
            done.push(inventory_dst.clone());
            self.s3_client
                .put_object_file(&sidecar_dst, &sidecar_src, Some(TYPE_PLAIN))?;
            Ok(())
        })?;

        Ok(())
    }

    fn do_with_rollback(
        &self,
        mut done: Vec<String>,
        mut callable: impl FnMut(&mut Vec<String>) -> Result<()>,
    ) -> Result<Vec<String>> {
        if let Err(e) = callable(&mut done) {
            for path in &done {
                if let Err(e2) = self.s3_client.delete_object(path) {
                    error!("Failed to rollback file {}: {}", path, e2);
                }
            }
            return Err(RocflError::General(
                format!("Failed to upload all files to S3. Successfully uploaded files were rolled back. Error: {}", e)));
        }
        Ok(done)
    }

    fn write_object_namaste(&self, object_root: &str, version: SpecVersion) -> Result<()> {
        let object_namaste = version.object_namaste();
        self.s3_client.put_object_bytes(
            &join(object_root, object_namaste.filename),
            Bytes::from(object_namaste.content.as_bytes()),
            Some(TYPE_PLAIN),
        )?;
        Ok(())
    }

    /// Pass through to S3 to list the contents of a path in S3
    fn list_dir(&self, path: &str) -> Result<ListResult> {
        self.s3_client.list_dir(path)
    }

    /// Lists all extension names in the `extensions` directory under the specified `base_dir`
    fn list_extensions(&self, base_dir: &str) -> Result<Vec<String>> {
        let extensions_dir = join(base_dir, EXTENSIONS_DIR);
        let list_result = self.list_dir(&extensions_dir)?;

        let mut extensions = Vec::with_capacity(list_result.directories.len());

        for path in list_result.directories {
            extensions.push(path[extensions_dir.len() + 1..].to_string());
        }

        Ok(extensions)
    }

    /// Identifies the first version declaration file in the directory and returns the portion of the
    /// filename that's after the prefix, which should be the OCFL spec version
    fn find_files(&self, dir: &str, prefix: &str) -> Result<Vec<String>> {
        let prefix = join(dir, prefix);
        Ok(self
            .list_dir(dir)?
            .objects
            .into_iter()
            .filter(|entry| entry.len() > prefix.len())
            .filter(|entry| entry.starts_with(&prefix))
            .collect())
    }

    /// Identifies the first version declaration file in the directory and returns the portion of the
    /// filename that's after the prefix, which should be the OCFL spec version
    fn find_first_version_declaration(&self, prefix: &str, dir: &str) -> Result<String> {
        let prefix = join(dir, prefix);
        for entry in self.list_dir(dir)?.objects {
            if let Some(stripped) = entry.strip_prefix(&prefix) {
                return Ok(stripped.to_string());
            }
        }

        Err(RocflError::NotFound("Version declaration file".to_string()))
    }

    /// Returns an error if the store is closed
    fn ensure_open(&self) -> Result<()> {
        if self.is_closed() {
            Err(RocflError::Closed)
        } else {
            Ok(())
        }
    }

    fn is_closed(&self) -> bool {
        self.closed.load(Ordering::Acquire)
    }
}

impl OcflStore for S3OcflStore {
    /// Returns the OCFL spec version that the repository root adheres to
    fn repo_spec_version(&self) -> Result<Option<Knowable<SpecVersion, String>>> {
        self.ensure_open()?;

        match self.find_first_version_declaration(ROOT_NAMASTE_FILE_PREFIX, "") {
            Ok(version) => match SpecVersion::try_from_num(&version) {
                Ok(version) => Ok(Some(Known(version))),
                Err(_) => Ok(Some(Unknown(version))),
            },
            Err(RocflError::NotFound(_)) => Ok(None),
            Err(e) => Err(e),
        }
    }

    /// Returns the most recent inventory version for the specified object, or an a
    /// `RocflError::NotFound` if it does not exist.
    fn get_inventory(&self, object_id: &str) -> Result<Inventory> {
        self.ensure_open()?;

        match self.get_object_root_path(object_id) {
            Some(object_root) => self.parse_inventory_required(object_id, &object_root),
            None => self.scan_for_inventory(object_id),
        }
    }

    /// Returns an iterator that iterates over every object in an OCFL repository, returning
    /// the most recent inventory of each. Optionally, a glob pattern may be provided that filters
    /// the objects that are returned by OCFL ID.
    fn iter_inventories<'a>(
        &'a self,
        filter_glob: Option<&str>,
    ) -> Result<Box<dyn Iterator<Item = Result<Inventory>> + 'a>> {
        self.ensure_open()?;

        Ok(Box::new(match filter_glob {
            Some(glob) => InventoryIter::new_glob_matching(self, glob, self.closed.clone())?,
            None => InventoryIter::new(self, None, self.closed.clone()),
        }))
    }

    /// Writes the specified file to the sink.
    ///
    /// If the file cannot be found, then a `RocflError::NotFound` error is returned.
    fn get_object_file(
        &self,
        object_id: &str,
        path: &LogicalPath,
        version_num: VersionRef,
        sink: &mut dyn Write,
    ) -> Result<()> {
        self.ensure_open()?;

        let inventory = self.get_inventory(object_id)?;

        let content_path = inventory.content_path_for_logical_path(path, version_num)?;
        let storage_path = join(&inventory.object_root, content_path.as_str());

        self.s3_client.stream_object(&storage_path, sink)
    }

    /// Writes a new OCFL object. The contents at `object_path` must be a fully formed OCFL
    /// object that is able to be moved into place with no additional modifications.
    ///
    /// The object must not already exist.
    fn write_new_object(
        &self,
        inventory: &mut Inventory,
        src_object_path: &Path,
        object_root: Option<&str>,
    ) -> Result<()> {
        self.ensure_open()?;

        let object_root = match self.get_object_root_path(&inventory.id) {
            Some(object_root) => object_root,
            None => {
                if let Some(root) = object_root {
                    util::trim_slashes(root).to_string()
                } else {
                    return Err(RocflError::IllegalState(
                            "Cannot create object because the repository does not have a defined storage layout, and an object root path was not specified."
                                .to_string(),
                        ));
                }
            }
        };

        if !self.s3_client.list_dir(&object_root)?.is_empty() {
            return Err(RocflError::IllegalState(format!(
                "Cannot create object {} because there are existing files at {}",
                inventory.id, object_root
            )));
        }

        info!("Creating new object {}", inventory.id);

        self.upload_all_files_with_rollback(&object_root, src_object_path)?;

        inventory.storage_path = match &self.prefix {
            Some(prefix) => join(prefix, &inventory.object_root),
            None => object_root,
        };

        Ok(())
    }

    /// Writes a new version to the OCFL object. The contents at `version_path` must be a fully
    /// formed OCFL version that is able to be moved into place within the object, requiring
    /// no additional modifications.
    ///
    /// The object must already exist, and the new version must not exist.
    fn write_new_version(&self, inventory: &mut Inventory, version_path: &Path) -> Result<()> {
        self.ensure_open()?;

        if inventory.is_new() {
            return Err(RocflError::IllegalState(format!(
                "Object {} must be created before adding new versions to it.",
                inventory.id
            )));
        }

        let existing_inventory = self.get_inventory(&inventory.id)?;
        let version_str = inventory.head.to_string();

        if existing_inventory.head != inventory.head.previous().unwrap() {
            return Err(RocflError::IllegalState(format!(
                "Cannot create version {} in object {} because the current version is at {}",
                version_str, inventory.id, existing_inventory.head
            )));
        }

        let version_dst_path = join(&existing_inventory.object_root, &version_str);

        if !self.s3_client.list_dir(&version_dst_path)?.is_empty() {
            return Err(RocflError::IllegalState(
                format!("Cannot create version {} in object {} because the version directory already exists.",
                        version_str, inventory.id)));
        }

        info!(
            "Creating version {} of object {}",
            version_str, inventory.id
        );

        let uploaded = self.upload_all_files_with_rollback(&version_dst_path, version_path)?;
        self.install_inventory_in_root_with_rollback(
            &existing_inventory.object_root,
            inventory.digest_algorithm,
            version_path,
            uploaded,
        )?;

        inventory.storage_path = existing_inventory.storage_path;

        if inventory.type_declaration != existing_inventory.type_declaration {
            // This is a version upgrade
            let old_namastes =
                self.find_files(&existing_inventory.object_root, OBJECT_NAMASTE_FILE_PREFIX)?;
            self.write_object_namaste(
                &existing_inventory.object_root,
                inventory.spec_version().unwrap(),
            )?;
            for old in old_namastes {
                self.s3_client.delete_object(&old)?;
            }
        }

        Ok(())
    }

    /// Purges the specified object from the repository, if it exists. If it does not exist,
    /// nothing happens. Any dangling directories that were created as a result of purging
    /// the object are also removed.
    fn purge_object(&self, object_id: &str) -> Result<()> {
        self.ensure_open()?;

        let object_root = match self.lookup_or_find_object_root_path(object_id) {
            Err(RocflError::NotFound(_)) => return Ok(()),
            Err(e) => return Err(e),
            Ok(object_root) => object_root,
        };

        info!("Purging object {} at {}", object_id, object_root);

        let mut failed = false;

        for file in self.s3_client.list_objects(&object_root)? {
            if self.is_closed() {
                error!("Terminating purge of object {} at {}. This object will need to be cleaned up manually.",
                       object_id, object_root);
                failed = true;
                break;
            }
            if let Err(e) = self.s3_client.delete_object(&file) {
                error!("Failed to delete file {}: {}", file, e);
                failed = true;
            }
        }

        if failed {
            return Err(RocflError::CorruptObject {
                object_id: object_id.to_string(),
                message: format!(
                    "Failed to purge object at {}. This object may need to be removed manually.",
                    object_root
                ),
            });
        }

        Ok(())
    }

    /// Returns a list of all of the extension names that are associated with the object
    fn list_object_extensions(&self, object_id: &str) -> Result<Vec<String>> {
        self.ensure_open()?;

        let object_root = self.lookup_or_find_object_root_path(object_id)?;

        self.list_extensions(&object_root)
    }

    /// Validates the specified object and returns any problems found. Err will only be returned
    /// if a non-validation problem was encountered.
    fn validate_object(
        &self,
        object_id: &str,
        fixity_check: bool,
    ) -> Result<ObjectValidationResult> {
        let object_root = self.lookup_or_find_object_root_path(object_id)?;

        self.validator
            .validate_object(Some(object_id), &object_root, None, fixity_check)
    }

    /// Validates the specified object at the specified path, relative the storage root, and
    /// returns any problems found. Err will only be returned if a non-validation problem was
    /// encountered.
    fn validate_object_at(
        &self,
        object_root: &str,
        fixity_check: bool,
    ) -> Result<ObjectValidationResult> {
        self.validator
            .validate_object(None, object_root, None, fixity_check)
    }

    /// Validates the structure of an OCFL repository as well as all of the objects in the repository
    /// When `fixity_check` is `false`, then the digests of object content files are not validated.
    ///
    /// The storage root is validated immediately, and an incremental validator is returned that
    /// is used to lazily validate the rest of the repository.
    fn validate_repo<'a>(
        &'a self,
        fixity_check: bool,
    ) -> Result<Box<dyn IncrementalValidator + 'a>> {
        Ok(Box::new(self.validator.validate_repo(fixity_check)?))
    }

    /// Returns details about an OCFL repository
    fn describe_repo(&self) -> Result<RepoInfo> {
        self.ensure_open()?;

        let version = self.find_first_version_declaration(ROOT_NAMASTE_FILE_PREFIX, "")?;

        let layout =
            load_ocfl_layout::<OcflLayoutLenient>(&self.s3_client).map(|layout| layout.extension);

        let extensions = self.list_extensions("")?;

        Ok(RepoInfo::new(version, layout, extensions))
    }

    /// Returns details about an OCFL object
    fn describe_object(&self, object_id: &str) -> Result<ObjectInfo> {
        self.ensure_open()?;

        let object_root = self
            .lookup_or_find_object_root_path(object_id)
            .map_err(|_| not_found(object_id, None))?;
        let version = self
            .find_first_version_declaration(OBJECT_NAMASTE_FILE_PREFIX, &object_root)
            .map_err(|_| not_found(object_id, None))?;
        let extensions = self.list_object_extensions(object_id)?;

        let algorithm = if SUPPORTED_VERSIONS.contains(&version.as_str()) {
            Some(
                self.parse_inventory_required(object_id, &object_root)?
                    .digest_algorithm
                    .to_string(),
            )
        } else {
            None
        };

        Ok(ObjectInfo::new(version, algorithm, extensions))
    }

    /// Upgrades the repository to the specified version
    fn upgrade_repo(&self, version: SpecVersion) -> Result<()> {
        self.ensure_open()?;

        let old_namastes = self.find_files("", ROOT_NAMASTE_FILE_PREFIX)?;

        write_namaste_and_spec(&self.s3_client, version)?;

        for old in old_namastes {
            self.s3_client.delete_object(&old)?;
        }

        Ok(())
    }

    /// Instructs the store to gracefully stop any in-flight work and not accept any additional
    /// requests.
    fn close(&self) {
        self.closed.store(true, Ordering::Release);
        self.validator.close();
    }
}

struct S3Client {
    s3_client: RusotoS3Client,
    bucket: String,
    prefix: String,
    // TODO this should ideally be externalized, but wait for new aws rust client
    runtime: Runtime,
}

struct ListResult {
    objects: Vec<String>,
    directories: Vec<String>,
}

type IdMatcher = Box<dyn Fn(&str) -> bool>;

struct InventoryIter<'a> {
    store: &'a S3OcflStore,
    dir_iters: Vec<IntoIter<String>>,
    current: RefCell<Option<IntoIter<String>>>,
    id_matcher: Option<IdMatcher>,
    closed: Arc<AtomicBool>,
}

pub struct S3Storage {
    s3_client: Arc<S3Client>,
}

impl S3Client {
    fn new(
        region: Region,
        bucket: &str,
        prefix: Option<&str>,
        profile: Option<&str>,
    ) -> Result<Self> {
        Ok(S3Client {
            s3_client: create_rusoto_client(region, profile),
            bucket: bucket.to_owned(),
            prefix: prefix.unwrap_or_default().to_owned(),
            runtime: runtime::Builder::new_multi_thread().enable_all().build()?,
        })
    }

    /// Returns all of the object keys or logical directories that are under the specified prefix.
    /// All returned keys and key parts are relative the repository prefix; not the search prefix.
    fn list_dir(&self, path: &str) -> Result<ListResult> {
        self.list_prefix(path, Some("/".to_string()))
    }

    /// Returns all of the object keys under the specified prefix. All returned keys and key parts
    /// are relative the repository prefix; not the search prefix.
    fn list_objects(&self, path: &str) -> Result<Vec<String>> {
        Ok(self.list_prefix(path, None)?.objects)
    }

    /// Returns all of the object keys or logical directories that are under the specified prefix.
    /// All returned keys and key parts are relative the repository prefix; not the search prefix.
    fn list_prefix(&self, path: &str, delimiter: Option<String>) -> Result<ListResult> {
        let prefix = join_with_trailing_slash(&self.prefix, path);

        info!("Listing S3 prefix: {}", prefix);

        let mut objects = Vec::new();
        let mut directories = Vec::new();
        let mut continuation = None;

        loop {
            let result: ListObjectsV2Output =
                self.runtime
                    .block_on(self.s3_client.list_objects_v2(ListObjectsV2Request {
                        bucket: self.bucket.clone(),
                        prefix: Some(prefix.clone()),
                        delimiter: delimiter.clone(),
                        continuation_token: continuation.clone(),
                        ..Default::default()
                    }))?;

            let prefix_offset = if self.prefix.is_empty() {
                0
            } else {
                self.prefix.len() + 1
            };

            if let Some(contents) = &result.contents {
                for object in contents {
                    objects.push(object.key.as_ref().unwrap()[prefix_offset..].to_owned());
                }
            }

            if let Some(prefixes) = &result.common_prefixes {
                for prefix in prefixes {
                    let length = prefix.prefix.as_ref().unwrap().len() - 1;
                    directories
                        .push(prefix.prefix.as_ref().unwrap()[prefix_offset..length].to_owned());
                }
            }

            if result.is_truncated.unwrap() {
                continuation = result.next_continuation_token.clone();
            } else {
                break;
            }
        }

        Ok(ListResult {
            objects,
            directories,
        })
    }

    fn get_object(&self, path: &str) -> Result<Option<Vec<u8>>> {
        let key = join(&self.prefix, path);

        info!("Getting object from S3: {}", key);

        let result = self
            .runtime
            .block_on(self.s3_client.get_object(GetObjectRequest {
                bucket: self.bucket.clone(),
                key,
                ..Default::default()
            }));

        match result {
            Ok(result) => self.runtime.block_on(async move {
                let mut buffer = Vec::new();
                result
                    .body
                    .unwrap()
                    .into_async_read()
                    .read_to_end(&mut buffer)
                    .await?;
                Ok(Some(buffer))
            }),
            Err(RusotoError::Service(GetObjectError::NoSuchKey(_e))) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }

    fn stream_object(&self, path: &str, sink: &mut dyn Write) -> Result<()> {
        let key = join(&self.prefix, path);

        info!("Streaming object from S3: {}", key);

        let result = self
            .runtime
            .block_on(self.s3_client.get_object(GetObjectRequest {
                bucket: self.bucket.clone(),
                key,
                ..Default::default()
            }));

        match result {
            Ok(result) => self.runtime.block_on(async move {
                let mut reader = result.body.unwrap().into_async_read();
                let mut buf = [0; 8192];
                loop {
                    let read = reader.read(&mut buf).await?;
                    if read == 0 {
                        break;
                    }
                    sink.write_all(&buf[..read])?;
                }
                Ok(())
            }),
            Err(e) => Err(e.into()),
        }
    }

    fn delete_object(&self, path: &str) -> Result<()> {
        let key = join(&self.prefix, path);

        info!("Deleting object in S3: {}", key);

        self.runtime
            .block_on(self.s3_client.delete_object(DeleteObjectRequest {
                bucket: self.bucket.clone(),
                key,
                ..Default::default()
            }))?;

        Ok(())
    }

    fn put_object_bytes(
        &self,
        path: &str,
        content: Bytes,
        content_type: Option<&str>,
    ) -> Result<()> {
        let key = join(&self.prefix, path);

        info!("Putting object in S3: {}", key);

        self.runtime
            .block_on(self.s3_client.put_object(PutObjectRequest {
                key,
                bucket: self.bucket.clone(),
                content_length: Some(content.len() as i64),
                body: Some(ByteStream::new(futures::stream::once(async move {
                    Ok(content)
                }))),
                content_type: content_type.map(|s| s.to_string()),
                ..Default::default()
            }))?;

        Ok(())
    }

    fn put_object_file(
        &self,
        path: &str,
        file_path: impl AsRef<Path>,
        content_type: Option<&str>,
    ) -> Result<()> {
        let content_length = std::fs::metadata(&file_path)?.len();

        if content_length > PART_SIZE {
            self.multipart_put_file(path, file_path, content_length, content_type)?;
        } else {
            let key = join(&self.prefix, path);
            info!("Putting {} in S3 at {}", file_path.as_ref().display(), key);

            #[allow(clippy::unnecessary_to_owned)]
            let stream = tokio::fs::read(file_path.as_ref().to_path_buf())
                .into_stream()
                .map_ok(Bytes::from);

            self.runtime
                .block_on(self.s3_client.put_object(PutObjectRequest {
                    key,
                    bucket: self.bucket.clone(),
                    content_length: Some(content_length as i64),
                    body: Some(StreamingBody::new(stream)),
                    content_type: content_type.map(|s| s.to_string()),
                    ..Default::default()
                }))?;
        }

        Ok(())
    }

    fn multipart_put_file(
        &self,
        path: &str,
        file_path: impl AsRef<Path>,
        content_length: u64,
        content_type: Option<&str>,
    ) -> Result<()> {
        let key = join(&self.prefix, path);

        info!(
            "Initiating S3 multipart upload of {} to {}",
            file_path.as_ref().to_string_lossy(),
            key
        );

        let mut i = 1;
        let mut reader = File::open(file_path)?;
        let mut buffer = [b'a'; PART_SIZE as usize];
        let mut parts = Vec::with_capacity(((content_length / PART_SIZE) + 1) as usize);

        let upload_id = self
            .runtime
            .block_on(
                self.s3_client
                    .create_multipart_upload(CreateMultipartUploadRequest {
                        bucket: self.bucket.clone(),
                        content_type: content_type.map(|s| s.to_string()),
                        key: key.clone(),
                        ..Default::default()
                    }),
            )?
            .upload_id
            .unwrap();

        let create_upload_part = |content: Vec<u8>, part_number: i64| -> UploadPartRequest {
            UploadPartRequest {
                upload_id: upload_id.clone(),
                part_number,
                bucket: self.bucket.clone(),
                key: key.clone(),
                body: Some(content.into()),
                ..Default::default()
            }
        };

        loop {
            let read = match reader.read(&mut buffer) {
                Ok(read) => read,
                Err(e) => {
                    self.abort_multipart(&key, &upload_id);
                    return Err(e.into());
                }
            };

            if read == 0 {
                break;
            }

            debug!("Upload part {} for {}", i, read);

            let e_tag = match self.runtime.block_on(
                self.s3_client
                    .upload_part(create_upload_part(buffer[..read].to_vec(), i)),
            ) {
                Ok(result) => result.e_tag,
                Err(e) => {
                    self.abort_multipart(&key, &upload_id);
                    return Err(e.into());
                }
            };

            parts.push(CompletedPart {
                e_tag,
                part_number: Some(i),
            });

            i += 1;
        }

        debug!("Finish multipart upload for {}", key);

        self.runtime
            .block_on(
                self.s3_client
                    .complete_multipart_upload(CompleteMultipartUploadRequest {
                        bucket: self.bucket.clone(),
                        key: key.clone(),
                        multipart_upload: Some(CompletedMultipartUpload { parts: Some(parts) }),
                        upload_id: upload_id.clone(),
                        ..Default::default()
                    }),
            )?;

        Ok(())
    }

    fn abort_multipart(&self, key: &str, upload_id: &str) {
        info!("Aborting multipart upload to {}", key);
        if let Err(e) = self.runtime.block_on(self.s3_client.abort_multipart_upload(
            AbortMultipartUploadRequest {
                bucket: self.bucket.clone(),
                key: key.to_string(),
                upload_id: upload_id.to_string(),
                ..Default::default()
            },
        )) {
            error!("Failed to abort multipart upload to {}: {}", key, e);
        };
    }
}

impl ListResult {
    fn is_empty(&self) -> bool {
        self.objects.is_empty() && self.directories.is_empty()
    }
}

impl<'a> InventoryIter<'a> {
    /// Creates a new iterator that only returns objects that match the given object ID.
    fn new_id_matching(store: &'a S3OcflStore, object_id: &str, closed: Arc<AtomicBool>) -> Self {
        let o = object_id.to_string();
        InventoryIter::new(store, Some(Box::new(move |id| id == o)), closed)
    }

    /// Creates a new iterator that only returns objects with IDs that match the specified glob
    /// pattern.
    fn new_glob_matching(
        store: &'a S3OcflStore,
        glob: &str,
        closed: Arc<AtomicBool>,
    ) -> Result<Self> {
        let matcher = GlobBuilder::new(glob)
            .backslash_escape(true)
            .build()?
            .compile_matcher();
        Ok(InventoryIter::new(
            store,
            Some(Box::new(move |id| matcher.is_match(id))),
            closed,
        ))
    }

    /// Creates a new iterator that returns all objects if no `id_matcher` is provided, or only
    /// the objects the `id_matcher` returns `true` for if one is provided.
    fn new(store: &'a S3OcflStore, id_matcher: Option<IdMatcher>, closed: Arc<AtomicBool>) -> Self {
        Self {
            store,
            dir_iters: Vec::new(),
            current: RefCell::new(Some(vec!["".to_string()].into_iter())),
            id_matcher,
            closed,
        }
    }

    fn create_if_matches(&self, object_root: &str) -> Option<Result<Inventory>> {
        match self.store.parse_inventory(object_root) {
            Ok(Some(inventory)) => {
                if let Some(id_matcher) = &self.id_matcher {
                    if id_matcher(&inventory.id) {
                        Some(Ok(inventory))
                    } else {
                        None
                    }
                } else {
                    Some(Ok(inventory))
                }
            }
            Ok(None) => Some(Err(RocflError::NotFound(format!(
                "Expected object to exist at {}, but none found.",
                object_root
            )))),
            Err(e) => Some(Err(e)),
        }
    }
}

impl<'a> Iterator for InventoryIter<'a> {
    type Item = Result<Inventory>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.closed.load(Ordering::Acquire) {
                info!("Terminating object search");
                return None;
            }

            if self.current.borrow().is_none() && self.dir_iters.is_empty() {
                return None;
            } else if self.current.borrow().is_none() {
                self.current.replace(self.dir_iters.pop());
            }

            let entry = self.current.borrow_mut().as_mut().unwrap().next();

            match entry {
                None => {
                    self.current.replace(None);
                }
                Some(entry) => {
                    if entry.ends_with(EXTENSIONS_DIR_SUFFIX) {
                        continue;
                    }

                    let listing = match self.store.list_dir(&entry) {
                        Ok(listing) => listing,
                        Err(e) => return Some(Err(e)),
                    };

                    if is_object_dir(&listing.objects) {
                        match self.create_if_matches(&entry) {
                            Some(Ok(inventory)) => return Some(Ok(inventory)),
                            Some(Err(e)) => return Some(Err(e)),
                            _ => (),
                        }
                    } else {
                        self.dir_iters.push(self.current.replace(None).unwrap());
                        self.current.replace(Some(listing.directories.into_iter()));
                    }
                }
            }
        }
    }
}

impl S3Storage {
    fn new(s3_client: Arc<S3Client>) -> Self {
        Self { s3_client }
    }
}

impl Storage for S3Storage {
    /// Reads the file at the specified path and writes its contents to the provided sink.
    fn read<W: Write>(&self, path: &str, sink: &mut W) -> Result<()> {
        self.s3_client.stream_object(path, sink)
    }

    /// Lists the contents of the specified directory. If `recursive` is `true`, then all leaf-nodes
    /// are returned. If the directory does not exist, or is empty, then an empty vector is returned.
    /// The returned paths are all relative the directory that was listed.
    fn list(&self, path: &str, recursive: bool) -> Result<Vec<Listing>> {
        let prefix_len = if path.is_empty() || path.ends_with('/') {
            path.len()
        } else {
            path.len() + 1
        };

        if recursive {
            let key_parts = self.s3_client.list_objects(path)?;
            Ok(key_parts
                .iter()
                .map(|entry| Listing::file_owned(entry[prefix_len..].to_string()))
                .collect::<Vec<Listing>>())
        } else {
            let s3_result = self.s3_client.list_dir(path)?;
            let mut result =
                Vec::with_capacity(s3_result.directories.len() + s3_result.objects.len());

            s3_result
                .objects
                .iter()
                .map(|entry| Listing::file_owned(entry[prefix_len..].to_string()))
                .for_each(|entry| result.push(entry));
            s3_result
                .directories
                .iter()
                .map(|entry| Listing::dir_owned(entry[prefix_len..].to_string()))
                .for_each(|entry| result.push(entry));

            Ok(result)
        }
    }

    /// Returns the native path separator used by the store.
    fn path_separator(&self) -> char {
        '/'
    }
}

fn check_extensions(s3_client: &S3Client) {
    match s3_client.list_dir(EXTENSIONS_DIR) {
        Ok(result) => {
            for entry in result.directories {
                let ext_name = &entry[EXTENSIONS_DIR.len() + 1..];
                if !SUPPORTED_EXTENSIONS.contains(ext_name) {
                    warn!(
                        "Storage root extension {} is not supported at this time",
                        ext_name
                    );
                }
            }
        }
        Err(e) => error!("Failed to list storage root extensions: {}", e),
    }
}

fn create_rusoto_client(region: Region, profile: Option<&str>) -> RusotoS3Client {
    match profile {
        Some(profile) => {
            // Client setup code copied from Rusoto -- they don't make it easy to set the profile
            let credentials_provider =
                AutoRefreshingProvider::new(ChainProvider::with_profile_provider(
                    ProfileProvider::with_default_credentials(profile)
                        .expect("failed to create profile provider"),
                ))
                .expect("failed to create credentials provider");
            let dispatcher = HttpClient::new().expect("failed to create request dispatcher");
            let client = Client::new_with(credentials_provider, dispatcher);
            RusotoS3Client::new_with_client(client, region)
        }
        None => RusotoS3Client::new(region),
    }
}

fn init_new_repo(
    s3_client: &S3Client,
    version: SpecVersion,
    layout: Option<&StorageLayout>,
) -> Result<()> {
    if !s3_client.list_dir("")?.is_empty() {
        return Err(RocflError::IllegalState(
            "Cannot create new repository. Storage root must be empty".to_string(),
        ));
    }

    info!(
        "Initializing OCFL storage root in bucket {} under prefix {}",
        s3_client.bucket, s3_client.prefix
    );

    write_namaste_and_spec(s3_client, version)?;

    if let Some(layout) = layout {
        write_layout_config(s3_client, layout)?;
    }

    Ok(())
}

fn write_namaste_and_spec(s3_client: &S3Client, version: SpecVersion) -> Result<()> {
    let root_namaste = version.root_namaste();

    s3_client.put_object_bytes(
        root_namaste.filename,
        Bytes::from(root_namaste.content.as_bytes()),
        Some(TYPE_PLAIN),
    )?;

    let spec = match version {
        SpecVersion::Ocfl1_0 => specs::OCFL_1_0_SPEC,
        SpecVersion::Ocfl1_1 => specs::OCFL_1_1_SPEC,
    };

    s3_client.put_object_bytes(
        version.spec_filename(),
        Bytes::from(spec.as_bytes()),
        Some(TYPE_PLAIN),
    )?;

    Ok(())
}

fn write_layout_config(s3_client: &S3Client, layout: &StorageLayout) -> Result<()> {
    let extension_name = layout.extension_name().to_string();

    let ocfl_layout = OcflLayout {
        extension: layout.extension_name(),
        description: format!("See specification document {}.md", extension_name),
    };

    let mut ocfl_layout_bytes = Vec::new();

    serde_json::to_writer_pretty(&mut ocfl_layout_bytes, &ocfl_layout)?;

    s3_client.put_object_bytes(
        OCFL_LAYOUT_FILE,
        Bytes::from(ocfl_layout_bytes),
        Some(TYPE_JSON),
    )?;

    s3_client.put_object_bytes(
        &format!(
            "{}/{}/{}",
            EXTENSIONS_DIR, extension_name, EXTENSIONS_CONFIG_FILE
        ),
        Bytes::from(layout.serialize()?),
        Some(TYPE_JSON),
    )?;

    let extension_spec = match layout.extension_name() {
        LayoutExtensionName::FlatDirectLayout => specs::EXT_0002_SPEC,
        LayoutExtensionName::HashedNTupleObjectIdLayout => specs::EXT_0003_SPEC,
        LayoutExtensionName::HashedNTupleLayout => specs::EXT_0004_SPEC,
        LayoutExtensionName::FlatOmitPrefixLayout => specs::EXT_0006_SPEC,
        LayoutExtensionName::NTupleOmitPrefixLayout => specs::EXT_0007_SPEC,
    };

    s3_client.put_object_bytes(
        &format!("{}.md", extension_name),
        Bytes::from(extension_spec),
        Some(TYPE_MARKDOWN),
    )?;

    Ok(())
}

/// Attempts to read `ocfl_layout.json` and returns it if able
fn load_ocfl_layout<T: DeserializeOwned>(s3_client: &S3Client) -> Option<T> {
    match s3_client.get_object(OCFL_LAYOUT_FILE) {
        Ok(Some(layout)) => match serde_json::from_slice::<T>(layout.as_slice()) {
            Ok(layout) => Some(layout),
            Err(e) => {
                error!("Failed to load OCFL layout: {:#}", e);
                None
            }
        },
        Ok(None) => {
            info!(
                "The OCFL repository at {}/{} does not contain an ocfl_layout.json file.",
                s3_client.bucket, s3_client.prefix
            );
            None
        }
        Err(e) => {
            error!("Failed to load OCFL layout: {:#}", e);
            None
        }
    }
}

/// Reads `ocfl_layout.json` and attempts to load the specified storage layout extension
fn load_storage_layout(s3_client: &S3Client) -> Option<StorageLayout> {
    load_ocfl_layout::<OcflLayout>(s3_client)
        .and_then(|layout| load_layout_extension(layout, s3_client))
}

/// Attempts to read a storage layout extension config and return configured `StorageLayout`
fn load_layout_extension(layout: OcflLayout, s3_client: &S3Client) -> Option<StorageLayout> {
    let config_path = join(
        &join(EXTENSIONS_DIR, &layout.extension.to_string()),
        EXTENSIONS_CONFIG_FILE,
    );

    match s3_client.get_object(&config_path) {
        Ok(config) => match StorageLayout::new(layout.extension, config.as_deref()) {
            Ok(storage_layout) => {
                info!(
                    "Loaded storage layout extension {}",
                    layout.extension.to_string()
                );
                Some(storage_layout)
            }
            Err(e) => {
                error!(
                    "Failed to load storage layout extension {}: {:#}",
                    layout.extension.to_string(),
                    e
                );
                None
            }
        },
        Err(e) => {
            error!(
                "Failed to load storage layout extension {}: {:#}",
                layout.extension.to_string(),
                e
            );
            None
        }
    }
}

fn is_object_dir(objects: &[String]) -> bool {
    for object in objects {
        if object.ends_with(OBJECT_NAMASTE_FILE_1_0) || object.ends_with(OBJECT_NAMASTE_FILE_1_1) {
            return true;
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::{is_object_dir, join, join_with_trailing_slash};

    #[test]
    fn join_path_when_both_empty() {
        assert_eq!(join("", ""), "");
        assert_eq!(join_with_trailing_slash("", ""), "");
    }

    #[test]
    fn join_path_when_first_empty() {
        assert_eq!(join("", "foo"), "foo");
        assert_eq!(join_with_trailing_slash("", "foo"), "foo/");
    }

    #[test]
    fn join_path_when_second_empty() {
        assert_eq!(join("foo", ""), "foo");
        assert_eq!(join_with_trailing_slash("foo", ""), "foo/");
    }

    #[test]
    fn join_path_when_first_is_only_slash() {
        assert_eq!(join("/", "foo"), "/foo");
        assert_eq!(join_with_trailing_slash("/", "foo"), "/foo/");
    }

    #[test]
    fn join_path_when_first_has_slash() {
        assert_eq!(join("foo/", "bar"), "foo/bar");
        assert_eq!(join_with_trailing_slash("foo/", "bar"), "foo/bar/");
    }

    #[test]
    fn join_path_when_both_have_slashes() {
        assert_eq!(join("/foo/", "/bar/"), "/foo/bar/");
        assert_eq!(join_with_trailing_slash("/foo/", "/bar/"), "/foo/bar/");
    }

    #[test]
    fn join_path_when_both_no_slashes() {
        assert_eq!(join("foo", "bar"), "foo/bar");
        assert_eq!(join_with_trailing_slash("foo", "bar"), "foo/bar/");
    }

    #[test]
    fn is_root_when_has_object_marker_key() {
        let objects = vec![
            "foo/bar.txt".to_string(),
            "foo/0=ocfl_object_1.0".to_string(),
        ];
        assert!(is_object_dir(&objects));
    }

    #[test]
    fn is_root_when_not_has_object_marker_key() {
        let objects = vec!["foo/bar.txt".to_string()];
        assert!(!is_object_dir(&objects));
    }
}