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
|
use std::{
collections::{HashMap, VecDeque},
fmt::Display,
fs::File,
io::{BufWriter, Read as _, Write},
os::unix::fs::MetadataExt,
path::{Path, PathBuf},
str::FromStr,
sync::{Arc, Mutex},
time::SystemTime,
};
use clap::{Args, Parser, Subcommand, ValueEnum};
use sha2::Digest;
use slotmap::SlotMap;
const BLOB_STORE_HIERARCHY_DEPTH: usize = 4;
#[derive(Debug)]
pub struct InvalidBlobId;
impl Display for InvalidBlobId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "invalid blob id")
}
}
impl std::error::Error for InvalidBlobId {}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BlobId([u8; 32]);
impl BlobId {
pub fn hash(&self, data: &[u8]) -> BlobId {
let mut hasher = sha2::Sha256::default();
hasher.write_all(data).unwrap();
Self(hasher.finalize().try_into().unwrap())
}
pub fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
}
impl Display for BlobId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
hex::display(&self.0).fmt(f)
}
}
impl FromStr for BlobId {
type Err = InvalidBlobId;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let decoded = hex::decode(s).map_err(|_| InvalidBlobId)?;
Ok(Self(decoded.try_into().map_err(|_| InvalidBlobId)?))
}
}
#[derive(Debug, Clone)]
pub struct BlobStore(PathBuf);
impl BlobStore {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self(path.into())
}
}
pub fn blob_path(store: &BlobStore, blob_id: &BlobId) -> PathBuf {
let encoded = hex::encode(blob_id.as_bytes());
let mut path = store.0.clone();
for depth in 0..BLOB_STORE_HIERARCHY_DEPTH {
let base_idx = depth * 2;
path.push(&encoded[base_idx..base_idx + 2]);
}
path.push(encoded);
path
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
pub enum ImportMode {
Move,
Copy,
HardLink,
}
pub fn blob_import_file(
store: &BlobStore,
mode: ImportMode,
blob_id: &BlobId,
file_path: &Path,
) -> std::io::Result<()> {
let blob_path = blob_path(store, blob_id);
if blob_path.exists() {
return Ok(());
}
if let Some(parent) = blob_path.parent() {
std::fs::create_dir_all(parent)?;
}
match mode {
ImportMode::Move => match std::fs::rename(&file_path, &blob_path) {
Ok(()) => {}
Err(err) if err.kind() == std::io::ErrorKind::CrossesDevices => {
let blob_tmp_path = {
let mut p = blob_path.clone();
p.set_file_name(format!("{blob_id}.tmp"));
p
};
std::fs::copy(&file_path, &blob_tmp_path)?;
std::fs::rename(&blob_tmp_path, blob_path)?;
std::fs::remove_file(&file_path)?;
}
Err(err) => return Err(err),
},
ImportMode::Copy => {
let blob_tmp_path = {
let mut p = blob_path.clone();
p.set_file_name(format!("{blob_id}.tmp"));
p
};
std::fs::copy(file_path, &blob_tmp_path)?;
std::fs::rename(&blob_tmp_path, blob_path)?;
}
ImportMode::HardLink => match std::fs::hard_link(file_path, blob_path) {
Ok(()) => {}
Err(err) => {
if err.kind() != std::io::ErrorKind::AlreadyExists {
panic!("{err}");
}
}
},
}
Ok(())
}
pub fn blob_hash_file(file_path: &Path) -> std::io::Result<BlobId> {
let mut file = std::fs::File::open(file_path)?;
let mut buf = vec![0u8; 1 * 1024 * 1024];
let mut hasher = sha2::Sha256::default();
loop {
let n = file.read(&mut buf)?;
if n == 0 {
break;
}
hasher.write_all(&buf[..n])?;
}
Ok(BlobId(hasher.finalize().try_into().unwrap()))
}
pub fn blob_size(store: &BlobStore, blob_id: &BlobId) -> std::io::Result<u64> {
let blob_path = blob_path(store, blob_id);
Ok(blob_path.metadata()?.size())
}
const OPERATION_KIND_CREATE_FILE: &'static str = "create_file";
const OPERATION_KIND_CREATE_DIR: &'static str = "create_dir";
const OPERATION_KIND_REMOVE: &'static str = "remove";
const OPERATION_KIND_RENAME: &'static str = "rename";
#[derive(Debug)]
pub struct InvalidOperation;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Operation {
pub header: OperationHeader,
pub data: OperationData,
}
impl FromStr for Operation {
type Err = InvalidOperation;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let err = || InvalidOperation;
let mut iter = s.split('\t');
let timestamp_str = iter.next().ok_or_else(err)?;
let revision_str = iter.next().ok_or_else(err)?;
let email_str = iter.next().ok_or_else(err)?;
let kind_str = iter.next().ok_or_else(err)?;
let timestamp = timestamp_str.parse().map_err(|_| err())?;
let revision = revision_str.parse().map_err(|_| err())?;
let email = email_str.to_string();
let data = match kind_str {
OPERATION_KIND_CREATE_FILE => {
let path_str = iter.next().ok_or_else(err)?;
let blob_str = iter.next().ok_or_else(err)?;
let size_str = iter.next().ok_or_else(err)?;
let path = path_str.parse().map_err(|_| err())?;
let blob = blob_str.parse().map_err(|_| err())?;
let size = size_str.parse().map_err(|_| err())?;
OperationData::CreateFile(OperationCreateFile { path, blob, size })
}
OPERATION_KIND_CREATE_DIR => {
let path_str = iter.next().ok_or_else(err)?;
let path = path_str.parse().map_err(|_| err())?;
OperationData::MkDir(OperationMkDir { path })
}
OPERATION_KIND_RENAME => {
let old_str = iter.next().ok_or_else(err)?;
let new_str = iter.next().ok_or_else(err)?;
let old = old_str.parse().map_err(|_| err())?;
let new = new_str.parse().map_err(|_| err())?;
OperationData::Rename(OperationRename { old, new })
}
OPERATION_KIND_REMOVE => {
let path_str = iter.next().ok_or_else(err)?;
let path = path_str.parse().map_err(|_| err())?;
OperationData::Remove(OperationRemove { path })
}
_ => return Err(err()),
};
Ok(Self {
header: OperationHeader {
timestamp,
revision,
email,
},
data,
})
}
}
impl Display for Operation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}\t{}\t{}\t",
self.header.timestamp, self.header.revision, self.header.email
)?;
match &self.data {
OperationData::CreateFile(data) => write!(
f,
"{}\t{}\t{}\t{}",
OPERATION_KIND_CREATE_FILE, data.path, data.blob, data.size
),
OperationData::MkDir(data) => {
write!(f, "{}\t{}", OPERATION_KIND_CREATE_DIR, data.path)
}
OperationData::Remove(data) => write!(f, "{}\t{}", OPERATION_KIND_REMOVE, data.path),
OperationData::Rename(data) => {
write!(f, "{}\t{}\t{}", OPERATION_KIND_RENAME, data.old, data.new)
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OperationHeader {
pub timestamp: u64,
pub revision: u64,
pub email: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum OperationData {
CreateFile(OperationCreateFile),
MkDir(OperationMkDir),
Remove(OperationRemove),
Rename(OperationRename),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OperationCreateFile {
pub path: DrivePath,
pub blob: BlobId,
pub size: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OperationMkDir {
pub path: DrivePath,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OperationRemove {
pub path: DrivePath,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OperationRename {
pub old: DrivePath,
pub new: DrivePath,
}
#[derive(Debug)]
pub struct InvalidDrivePath(String);
impl std::fmt::Display for InvalidDrivePath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "invalid path: {}", self.0)
}
}
impl std::error::Error for InvalidDrivePath {}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct DrivePath(Vec<DrivePathComponent>);
impl Display for DrivePath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.is_root() {
f.write_str("/")?;
} else {
for comp in self.components() {
f.write_str("/")?;
comp.fmt(f)?;
}
}
Ok(())
}
}
impl DrivePath {
pub fn is_root(&self) -> bool {
self.0.is_empty()
}
pub fn push(&self, component: DrivePathComponent) -> DrivePath {
let mut components = self.0.clone();
components.push(component);
Self(components)
}
pub fn join(&self, path: DrivePath) -> DrivePath {
let mut components = self.0.clone();
components.extend(path.0);
Self(components)
}
pub fn components(&self) -> &[DrivePathComponent] {
self.0.as_slice()
}
pub fn parent(&self) -> DrivePath {
if self.0.is_empty() {
Self(Default::default())
} else {
let slice = self.0.as_slice();
Self(slice[..slice.len() - 1].to_owned())
}
}
pub fn last(&self) -> Option<DrivePathComponent> {
self.0.last().cloned()
}
pub fn split(&self) -> Option<(DrivePath, DrivePathComponent)> {
if self.0.is_empty() {
None
} else {
Some((
Self(self.0[..self.0.len() - 1].to_owned()),
self.0[self.0.len() - 1].clone(),
))
}
}
}
impl FromStr for DrivePath {
type Err = InvalidDrivePath;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut components = Vec::default();
for unchecked_component in s.trim_matches('/').split('/') {
if unchecked_component.is_empty() {
continue;
}
match unchecked_component.parse() {
Ok(component) => components.push(component),
Err(err) => {
return Err(InvalidDrivePath(format!(
"path contained invalid component '{unchecked_component}': {err}"
)));
}
};
}
Ok(Self(components))
}
}
#[derive(Debug)]
pub struct InvalidDrivePathComponent(&'static str);
impl Display for InvalidDrivePathComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl InvalidDrivePathComponent {
pub const fn new(msg: &'static str) -> Self {
Self(msg)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DrivePathComponent(String);
impl Display for DrivePathComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl DrivePathComponent {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl FromStr for DrivePathComponent {
type Err = InvalidDrivePathComponent;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Err(InvalidDrivePathComponent::new(
"path component cannot be empty",
));
}
if s.contains('\t') {
return Err(InvalidDrivePathComponent::new(
"path component cannot contain tabs",
));
}
if s == "." || s == ".." {
return Err(InvalidDrivePathComponent::new(
"path component cannot be '.' or '..'",
));
}
if s.contains('/') {
return Err(InvalidDrivePathComponent::new(
"path component cannot contain '/'",
));
}
if s.len() > 256 {
return Err(InvalidDrivePathComponent::new("path component is too long"));
}
Ok(Self(s.to_string()))
}
}
#[derive(Debug)]
pub struct FsError(String);
impl From<String> for FsError {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for FsError {
fn from(value: &str) -> Self {
Self(value.to_string())
}
}
#[derive(Debug)]
pub struct InvalidFsNodeKind;
impl Display for InvalidFsNodeKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("invalid filesystem node type, must be one of 'directory' or 'file'")
}
}
impl std::error::Error for InvalidFsNodeKind {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FsNodeKind {
File,
Directory,
}
impl FromStr for FsNodeKind {
type Err = InvalidFsNodeKind;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"file" => Ok(Self::File),
"dir" | "directory" => Ok(Self::Directory),
_ => Err(InvalidFsNodeKind),
}
}
}
pub struct FsNode {
pub kind: FsNodeKind,
pub author: String,
pub lastmod: u64,
pub children: HashMap<DrivePathComponent, FsNodeId>,
pub blob: BlobId,
pub size: u64,
}
slotmap::new_key_type! { pub struct FsNodeId; }
pub struct Fs {
pub root: FsNodeId,
pub nodes: SlotMap<FsNodeId, FsNode>,
}
impl Default for Fs {
fn default() -> Self {
let mut nodes: SlotMap<FsNodeId, FsNode> = Default::default();
let root = nodes.insert(FsNode {
kind: FsNodeKind::Directory,
author: "system".to_string(),
lastmod: 0,
children: Default::default(),
blob: Default::default(),
size: 0,
});
Self { root, nodes }
}
}
pub fn compute_directory_sizes(fs: &mut Fs) {
fn compute(fs: &mut Fs, node_id: FsNodeId) {
let node = &fs.nodes[node_id];
if node.kind == FsNodeKind::File {
return;
}
let children = node.children.values().copied().collect::<Vec<_>>();
children.into_iter().for_each(|id| compute(fs, id));
let mut size = 0;
for child in fs.nodes[node_id].children.values() {
let child = &fs.nodes[*child];
size += child.size;
}
fs.nodes[node_id].size = size;
}
compute(fs, fs.root);
}
pub fn apply(fs: &mut Fs, op: &Operation) -> Result<(), FsError> {
match &op.data {
OperationData::CreateFile(data) => apply_create_file(fs, &op.header, &data),
OperationData::MkDir(data) => apply_create_dir(fs, &op.header, &data),
OperationData::Remove(data) => apply_remove(fs, &op.header, &data),
OperationData::Rename(data) => apply_rename(fs, &op.header, &data),
}
}
pub fn apply_create_file(
fs: &mut Fs,
header: &OperationHeader,
data: &OperationCreateFile,
) -> Result<(), FsError> {
if data.path.is_root() {
return Err(FsError::from("cannot create file at root"));
}
let mut parent_id = fs.root;
for comp in data.path.parent().components() {
let node = &mut fs.nodes[parent_id];
if node.kind != FsNodeKind::Directory {
return Err(FsError::from("cannot create file under another file"));
}
parent_id = match node.children.get(comp) {
Some(id) => *id,
None => {
let id = fs.nodes.insert(FsNode {
kind: FsNodeKind::Directory,
author: header.email.clone(),
lastmod: header.timestamp,
children: Default::default(),
blob: Default::default(),
size: 0,
});
fs.nodes[parent_id].children.insert(comp.clone(), id);
id
}
};
}
let name = data.path.last().unwrap();
let parent = &mut fs.nodes[parent_id];
if parent.kind != FsNodeKind::Directory {
return Err(FsError::from("cannot create file under another file"));
}
match parent.children.get(&name).copied() {
Some(node_id) => {
let node = &mut fs.nodes[node_id];
if node.kind == FsNodeKind::Directory {
return Err(FsError::from(
"node at path already exists but is a directory",
));
}
node.author = header.email.clone();
node.lastmod = header.timestamp;
node.blob = data.blob.clone();
}
None => {
let id = fs.nodes.insert(FsNode {
kind: FsNodeKind::File,
author: header.email.clone(),
lastmod: header.timestamp,
children: Default::default(),
blob: data.blob.clone(),
size: data.size,
});
fs.nodes[parent_id].children.insert(name, id);
}
}
Ok(())
}
pub fn apply_create_dir(
fs: &mut Fs,
header: &OperationHeader,
data: &OperationMkDir,
) -> Result<(), FsError> {
let (parent, name) = data
.path
.split()
.ok_or_else(|| FsError::from("cannot recreate root directory"))?;
let mut parent_id = fs.root;
for comp in parent.components() {
let node = &fs.nodes[parent_id];
parent_id = match node.children.get(comp) {
Some(&child_id) => {
let child = &fs.nodes[child_id];
if child.kind == FsNodeKind::File {
return Err(FsError::from("cannot create directory under file"));
}
child_id
}
None => {
let id = fs.nodes.insert(FsNode {
kind: FsNodeKind::Directory,
author: header.email.clone(),
lastmod: header.timestamp,
children: Default::default(),
blob: Default::default(),
size: 0,
});
fs.nodes[parent_id].children.insert(comp.clone(), id);
id
}
};
}
let parent = &fs.nodes[parent_id];
match parent.children.get(&name).copied() {
Some(child_id) => {
let child = &fs.nodes[child_id];
if child.kind != FsNodeKind::Directory {
return Err(FsError::from(
"cannot create directory, the given path is already a file",
));
}
}
None => {
let id = fs.nodes.insert(FsNode {
kind: FsNodeKind::Directory,
author: header.email.clone(),
lastmod: header.timestamp,
children: Default::default(),
blob: Default::default(),
size: 0,
});
let parent = &mut fs.nodes[parent_id];
parent.children.insert(name, id);
}
}
Ok(())
}
pub fn apply_remove(
fs: &mut Fs,
_header: &OperationHeader,
data: &OperationRemove,
) -> Result<(), FsError> {
let (parent, name) = data
.path
.split()
.ok_or_else(|| FsError::from("cannot remove root directory"))?;
let parent_id = match find_node(fs, &parent) {
Some(id) => id,
None => return Ok(()),
};
let parent = &mut fs.nodes[parent_id];
parent.children.remove(&name);
Ok(())
}
fn find_node(fs: &Fs, path: &DrivePath) -> Option<FsNodeId> {
let mut node_id = fs.root;
for comp in path.components() {
let node = &fs.nodes[node_id];
node_id = *node.children.get(comp)?;
}
Some(node_id)
}
pub fn apply_rename(
fs: &mut Fs,
_header: &OperationHeader,
data: &OperationRename,
) -> Result<(), FsError> {
let (old_parent, old_name) = data
.old
.split()
.ok_or_else(|| FsError::from("cannot move root directory"))?;
let (new_parent, new_name) = data
.new
.split()
.ok_or_else(|| FsError::from("move destination cannot be root directory"))?;
let old_parent_id = find_node(fs, &old_parent).unwrap();
let old_parent = &mut fs.nodes[old_parent_id];
let node_id = old_parent.children.remove(&old_name).unwrap();
let new_parent_id = find_node(fs, &new_parent).unwrap();
let new_parent = &mut fs.nodes[new_parent_id];
new_parent.children.insert(new_name, node_id);
Ok(())
}
#[derive(Debug, Parser)]
struct Cli {
#[clap(subcommand)]
cmd: Cmd,
}
#[derive(Debug, Parser)]
struct CliCommon {
#[clap(long, default_value = "./", env = "FCTDRIVE_PATH")]
drive: PathBuf,
}
#[derive(Debug, Subcommand)]
enum Cmd {
Mkdir(MkDirArgs),
Remove(RemoveArgs),
Rename(RenameArgs),
Ls(LsArgs),
Import(ImportArgs),
Blob(BlobArgs),
Log(LogArgs),
Stat(StatArgs),
DriveSize(DriveSizeArgs),
}
#[derive(Debug, Args)]
struct MkDirArgs {
#[clap(flatten)]
common: CliCommon,
#[clap(long)]
timestamp: Option<u64>,
#[clap(long)]
email: String,
#[clap(long)]
path: DrivePath,
}
#[derive(Debug, Args)]
struct RemoveArgs {
#[clap(flatten)]
common: CliCommon,
#[clap(long)]
timestamp: Option<u64>,
#[clap(long)]
email: String,
path: Vec<DrivePath>,
}
#[derive(Debug, Args)]
struct RenameArgs {
#[clap(flatten)]
common: CliCommon,
#[clap(long)]
timestamp: Option<u64>,
#[clap(long)]
email: String,
#[clap(long)]
old: DrivePath,
#[clap(long)]
new: DrivePath,
}
#[derive(Debug, Args)]
struct LsArgs {
#[clap(flatten)]
common: CliCommon,
#[clap(short, long)]
recursive: bool,
#[clap(long)]
relative: bool,
#[clap(short = 't', long = "type")]
type_: Option<FsNodeKind>,
path: Option<DrivePath>,
}
#[derive(Debug, Args)]
struct ImportArgs {
#[clap(flatten)]
common: CliCommon,
#[clap(long)]
mode: ImportMode,
#[clap(long)]
timestamp: Option<u64>,
#[clap(long)]
email: String,
#[clap(long, default_value = "/")]
destination: DrivePath,
path: PathBuf,
}
#[derive(Debug, Args)]
struct BlobArgs {
#[clap(flatten)]
common: CliCommon,
blob_id: BlobId,
}
#[derive(Debug, Args)]
struct LogArgs {
#[clap(flatten)]
common: CliCommon,
}
#[derive(Debug, Args)]
struct StatArgs {
#[clap(flatten)]
common: CliCommon,
path: DrivePath,
}
#[derive(Debug, Args)]
struct DriveSizeArgs {
#[clap(flatten)]
common: CliCommon,
}
fn main() {
let cli = Cli::parse();
match cli.cmd {
Cmd::Mkdir(args) => cmd_mkdir(args),
Cmd::Remove(args) => cmd_remove(args),
Cmd::Rename(args) => cmd_rename(args),
Cmd::Ls(args) => cmd_ls(args),
Cmd::Import(args) => cmd_import(args),
Cmd::Blob(args) => cmd_blob(args),
Cmd::Log(args) => cmd_log(args),
Cmd::Stat(args) => cmd_stat(args),
Cmd::DriveSize(args) => cmd_drive_size(args),
}
}
fn cmd_mkdir(args: MkDirArgs) {
let _lock = common_write_lock(&args.common);
let mut fs = Fs::default();
let mut ops = common_read_log_file(&args.common);
ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
let timestamp = args.timestamp.unwrap_or_else(get_timestamp);
let revision = get_next_revision(&ops);
let new_op = Operation {
header: OperationHeader {
timestamp,
revision,
email: args.email,
},
data: OperationData::MkDir(OperationMkDir { path: args.path }),
};
apply(&mut fs, &new_op).unwrap();
ops.push(new_op);
common_write_log_file(&args.common, &ops);
}
fn cmd_remove(args: RemoveArgs) {
let _lock = common_write_lock(&args.common);
let mut fs = Fs::default();
let mut ops = common_read_log_file(&args.common);
ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
let timestamp = args.timestamp.unwrap_or_else(get_timestamp);
let revision = get_next_revision(&ops);
for path in args.path {
let new_op = Operation {
header: OperationHeader {
timestamp,
revision,
email: args.email.clone(),
},
data: OperationData::Remove(OperationRemove { path }),
};
apply(&mut fs, &new_op).unwrap();
ops.push(new_op);
}
common_write_log_file(&args.common, &ops);
}
fn cmd_rename(args: RenameArgs) {
let _lock = common_write_lock(&args.common);
let mut fs = Fs::default();
let mut ops = common_read_log_file(&args.common);
ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
let timestamp = args.timestamp.unwrap_or_else(get_timestamp);
let revision = get_next_revision(&ops);
let new_op = Operation {
header: OperationHeader {
timestamp,
revision,
email: args.email,
},
data: OperationData::Rename(OperationRename {
old: args.old,
new: args.new,
}),
};
apply(&mut fs, &new_op).unwrap();
ops.push(new_op);
common_write_log_file(&args.common, &ops);
}
type FsNodeWriter<'a> = std::io::BufWriter<std::io::StdoutLock<'a>>;
fn cmd_ls(args: LsArgs) {
let stdout = std::io::stdout().lock();
let mut writer = BufWriter::new(stdout);
let mut fs = Fs::default();
let ops = common_read_log_file(&args.common);
ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
compute_directory_sizes(&mut fs);
let node_id = match &args.path {
Some(path) => find_node(&fs, path),
None => Some(fs.root),
};
if let Some(node_id) = node_id {
write_node(
&mut writer,
&fs,
node_id,
if args.relative {
Default::default()
} else {
args.path.unwrap_or_default()
},
args.recursive,
true,
args.type_,
);
}
writer.flush().unwrap();
std::process::exit(0);
}
fn write_node(
writer: &mut FsNodeWriter,
fs: &Fs,
node_id: FsNodeId,
path: DrivePath,
recursive: bool,
recurse: bool,
filter_type: Option<FsNodeKind>,
) {
let node = &fs.nodes[node_id];
match node.kind {
FsNodeKind::File => {
if filter_type == None || filter_type == Some(FsNodeKind::File) {
writeln!(
writer,
"{}\tfile\t{}\t{}\t{}\t{}",
path, node.lastmod, node.blob, node.size, node.author
)
.unwrap();
}
}
FsNodeKind::Directory => {
if !recurse && (filter_type == None || filter_type == Some(FsNodeKind::Directory)) {
writeln!(
writer,
"{}\tdir\t{}\t-\t{}\t{}",
path, node.lastmod, node.size, node.author
)
.unwrap();
}
if recursive || recurse {
for (child_comp, child_id) in node.children.iter() {
let child_path = path.push(child_comp.clone());
write_node(
writer,
fs,
*child_id,
child_path,
recursive,
false,
filter_type,
);
}
}
}
}
}
#[derive(Debug, Clone)]
struct Queue<T>(Arc<Mutex<VecDeque<T>>>);
impl<T> Default for Queue<T> {
fn default() -> Self {
Self(Arc::new(Mutex::new(Default::default())))
}
}
impl<T> From<Vec<T>> for Queue<T> {
fn from(value: Vec<T>) -> Self {
Self(Arc::new(Mutex::new(value.into())))
}
}
impl<T> Queue<T> {
pub fn push(&self, v: T) {
self.0.lock().unwrap().push_back(v);
}
pub fn pop(&self) -> Option<T> {
self.0.lock().unwrap().pop_front()
}
}
fn cmd_import(args: ImportArgs) {
let _lock = common_write_lock(&args.common);
let mut ops = common_read_log_file(&args.common);
let store = common_create_blob_store(&args.common);
let timestamp = args.timestamp.unwrap_or_else(get_timestamp);
let root = args.path.canonicalize().unwrap();
let import_mode = args.mode;
let files = Queue::from(collect_all_file_paths(&root));
let num_threads = std::thread::available_parallelism().unwrap().get();
let mut handles = Vec::default();
for _ in 0..num_threads {
let root = root.clone();
let email = args.email.clone();
let files = files.clone();
let store = store.clone();
let destination = args.destination.clone();
let handle = std::thread::spawn(move || {
let mut ops = Vec::default();
while let Some(file) = files.pop() {
let rel = file.strip_prefix(&root).unwrap();
let drive_path =
destination.join(rel.to_str().unwrap().parse::<DrivePath>().unwrap());
let blob_id = blob_hash_file(&file).unwrap();
blob_import_file(&store, import_mode, &blob_id, &file).unwrap();
let blob_size = blob_size(&store, &blob_id).unwrap();
let op = Operation {
header: OperationHeader {
timestamp,
revision: 0,
email: email.clone(),
},
data: OperationData::CreateFile(OperationCreateFile {
path: drive_path,
blob: blob_id,
size: blob_size,
}),
};
ops.push(op);
}
ops
});
handles.push(handle);
}
let mut fs = Fs::default();
let mut next_rev = get_next_revision(&ops);
for handle in handles {
let mut task_ops = handle.join().unwrap();
for op in &mut task_ops {
op.header.revision = next_rev;
next_rev += 1;
}
ops.extend(task_ops);
}
ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
common_write_log_file(&args.common, &ops);
}
fn cmd_blob(args: BlobArgs) {
let store = common_create_blob_store(&args.common);
let path = blob_path(&store, &args.blob_id);
println!("{}", path.display());
}
fn cmd_log(args: LogArgs) {
let ops = common_read_log_file(&args.common);
for op in ops {
println!("{op}");
}
}
fn cmd_stat(args: StatArgs) {
let ops = common_read_log_file(&args.common);
let mut fs = Fs::default();
ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
let node_id = find_node(&fs, &args.path).unwrap();
let node = &fs.nodes[node_id];
println!("{}", node.blob);
}
fn cmd_drive_size(args: DriveSizeArgs) {
let ops = common_read_log_file(&args.common);
let mut fs = Fs::default();
ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
compute_directory_sizes(&mut fs);
println!("{}", fs.nodes[fs.root].size);
}
fn collect_all_file_paths(root: &Path) -> Vec<PathBuf> {
let mut queue = vec![root.to_path_buf()];
let mut files = vec![];
while let Some(path) = queue.pop() {
if path.is_dir() {
let mut read = path.read_dir().unwrap();
while let Some(entry) = read.next() {
let entry = entry.unwrap();
queue.push(entry.path());
}
} else {
files.push(path);
}
}
files
}
fn common_create_blob_store(common: &CliCommon) -> BlobStore {
BlobStore::new(common.drive.join("blobs"))
}
fn common_log_file_path(common: &CliCommon) -> PathBuf {
common.drive.join("log.txt")
}
fn common_log_temp_file_path(common: &CliCommon) -> PathBuf {
common.drive.join("log.txt.tmp")
}
fn common_read_log_file(common: &CliCommon) -> Vec<Operation> {
let log_file_path = common_log_file_path(common);
let mut operations = Vec::default();
if std::fs::exists(&log_file_path).unwrap() {
let contents = std::fs::read_to_string(&log_file_path).unwrap();
for line in contents.lines() {
let operation = line.parse().unwrap();
operations.push(operation);
}
}
operations
}
fn common_write_log_file(common: &CliCommon, ops: &[Operation]) {
let log_file_path = common_log_file_path(common);
let log_temp_file_path = common_log_temp_file_path(common);
{
let file = File::options()
.create(true)
.write(true)
.truncate(true)
.open(&log_temp_file_path)
.unwrap();
let mut writer = BufWriter::new(file);
for op in ops {
writeln!(writer, "{op}").unwrap();
}
writer.flush().unwrap();
}
std::fs::rename(&log_temp_file_path, &log_file_path).unwrap();
}
fn common_write_lock_path(common: &CliCommon) -> PathBuf {
common.drive.join("write.lock")
}
fn common_write_lock(common: &CliCommon) -> File {
let lock_path = common_write_lock_path(common);
let file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(lock_path)
.unwrap();
file.lock().unwrap();
file
}
fn get_timestamp() -> u64 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
}
fn get_next_revision(ops: &[Operation]) -> u64 {
match ops.last() {
Some(op) => op.header.revision + 1,
None => 0,
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_drive_path() {
let p = "/".parse::<DrivePath>().unwrap();
}
}
|