/
snapshot.rs
1121 lines (1024 loc) · 36.3 KB
/
snapshot.rs
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
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
use crate::Store;
use bazel_protos;
use boxfuture::{try_future, BoxFuture, Boxable};
use fs::{Dir, File, GlobMatching, PathGlobs, PathStat, PosixFS, SymlinkBehavior};
use futures::future::{self, join_all};
use futures::Future;
use hashing::{Digest, EMPTY_DIGEST};
use indexmap::{self, IndexMap};
use itertools::Itertools;
use protobuf;
use std::ffi::OsString;
use std::fmt;
use std::iter::Iterator;
use std::path::{Path, PathBuf};
use std::sync::Arc;
#[derive(Eq, Hash, PartialEq)]
pub struct Snapshot {
pub digest: Digest,
pub path_stats: Vec<PathStat>,
}
impl Snapshot {
pub fn empty() -> Snapshot {
Snapshot {
digest: EMPTY_DIGEST,
path_stats: vec![],
}
}
pub fn from_path_stats<
S: StoreFileByDigest<Error> + Sized + Clone,
Error: fmt::Debug + 'static + Send,
>(
store: Store,
file_digester: &S,
mut path_stats: Vec<PathStat>,
) -> BoxFuture<Snapshot, String> {
path_stats.sort_by(|a, b| a.path().cmp(b.path()));
// The helper assumes that if a Path has multiple children, it must be a directory.
// Proactively error if we run into identically named files, because otherwise we will treat
// them like empty directories.
let pre_dedupe_len = path_stats.len();
path_stats.dedup_by(|a, b| a.path() == b.path());
if path_stats.len() != pre_dedupe_len {
return future::err(format!(
"Snapshots must be constructed from unique path stats; got duplicates in {:?}",
path_stats
))
.to_boxed();
}
Snapshot::ingest_directory_from_sorted_path_stats(store, file_digester, &path_stats)
.map(|digest| Snapshot { digest, path_stats })
.to_boxed()
}
pub fn from_digest(store: Store, digest: Digest) -> BoxFuture<Snapshot, String> {
store
.walk(digest, |_, path_so_far, _, directory| {
let mut path_stats = Vec::new();
path_stats.extend(directory.get_directories().iter().map(move |dir_node| {
let path = path_so_far.join(dir_node.get_name());
PathStat::dir(path.clone(), Dir(path))
}));
path_stats.extend(directory.get_files().iter().map(move |file_node| {
let path = path_so_far.join(file_node.get_name());
PathStat::file(
path.clone(),
File {
path,
is_executable: file_node.is_executable,
},
)
}));
future::ok(path_stats).to_boxed()
})
.map(move |path_stats_per_directory| {
let mut path_stats =
Iterator::flatten(path_stats_per_directory.into_iter().map(Vec::into_iter))
.collect::<Vec<_>>();
path_stats.sort_by(|l, r| l.path().cmp(&r.path()));
Snapshot { digest, path_stats }
})
.to_boxed()
}
pub fn digest_from_path_stats<
S: StoreFileByDigest<Error> + Sized + Clone,
Error: fmt::Debug + 'static + Send,
>(
store: Store,
file_digester: &S,
path_stats: &[PathStat],
) -> BoxFuture<Digest, String> {
let mut sorted_path_stats = path_stats.to_owned();
sorted_path_stats.sort_by(|a, b| a.path().cmp(b.path()));
Snapshot::ingest_directory_from_sorted_path_stats(store, file_digester, &sorted_path_stats)
}
fn ingest_directory_from_sorted_path_stats<
S: StoreFileByDigest<Error> + Sized + Clone,
Error: fmt::Debug + 'static + Send,
>(
store: Store,
file_digester: &S,
path_stats: &[PathStat],
) -> BoxFuture<Digest, String> {
let mut file_futures: Vec<BoxFuture<bazel_protos::remote_execution::FileNode, String>> =
Vec::new();
let mut dir_futures: Vec<BoxFuture<bazel_protos::remote_execution::DirectoryNode, String>> =
Vec::new();
for (first_component, group) in &path_stats
.iter()
.cloned()
.group_by(|s| s.path().components().next().unwrap().as_os_str().to_owned())
{
let mut path_group: Vec<PathStat> = group.collect();
if path_group.len() == 1 && path_group[0].path().components().count() == 1 {
// Exactly one entry with exactly one component indicates either a file in this directory,
// or an empty directory.
// If the child is a non-empty directory, or a file therein, there must be multiple
// PathStats with that prefix component, and we will handle that in the recursive
// save_directory call.
match path_group.pop().unwrap() {
PathStat::File { ref stat, .. } => {
let is_executable = stat.is_executable;
file_futures.push(
file_digester
.clone()
.store_by_digest(stat.clone())
.map_err(|e| format!("{:?}", e))
.and_then(move |digest| {
let mut file_node = bazel_protos::remote_execution::FileNode::new();
file_node.set_name(osstring_as_utf8(first_component)?);
file_node.set_digest((&digest).into());
file_node.set_is_executable(is_executable);
Ok(file_node)
})
.to_boxed(),
);
}
PathStat::Dir { .. } => {
// Because there are no children of this Dir, it must be empty.
dir_futures.push(
store
.record_directory(&bazel_protos::remote_execution::Directory::new(), true)
.map(move |digest| {
let mut directory_node = bazel_protos::remote_execution::DirectoryNode::new();
directory_node.set_name(osstring_as_utf8(first_component).unwrap());
directory_node.set_digest((&digest).into());
directory_node
})
.to_boxed(),
);
}
}
} else {
dir_futures.push(
// TODO: Memoize this in the graph
Snapshot::ingest_directory_from_sorted_path_stats(
store.clone(),
file_digester,
&paths_of_child_dir(path_group),
)
.and_then(move |digest| {
let mut dir_node = bazel_protos::remote_execution::DirectoryNode::new();
dir_node.set_name(osstring_as_utf8(first_component)?);
dir_node.set_digest((&digest).into());
Ok(dir_node)
})
.to_boxed(),
);
}
}
join_all(dir_futures)
.join(join_all(file_futures))
.and_then(move |(dirs, files)| {
let mut directory = bazel_protos::remote_execution::Directory::new();
directory.set_directories(protobuf::RepeatedField::from_vec(dirs));
directory.set_files(protobuf::RepeatedField::from_vec(files));
store.record_directory(&directory, true)
})
.to_boxed()
}
///
/// Given N Snapshots, returns a new Snapshot that merges them.
///
/// Any files that exist in multiple Snapshots will cause this method to fail: the assumption
/// behind this behaviour is that almost any colliding file would represent a Rule implementation
/// error, and in cases where overwriting a file is desirable, explicitly removing a duplicated
/// copy should be straightforward.
///
pub fn merge(store: Store, snapshots: &[Snapshot]) -> BoxFuture<Snapshot, String> {
// We dedupe PathStats by their symbolic names, as those will be their names within the
// `Directory` structure. Only `Dir+Dir` collisions are legal.
let path_stats = {
let mut uniq_paths: IndexMap<PathBuf, PathStat> = IndexMap::new();
for path_stat in Iterator::flatten(snapshots.iter().map(|s| s.path_stats.iter().cloned())) {
match uniq_paths.entry(path_stat.path().to_owned()) {
indexmap::map::Entry::Occupied(e) => match (&path_stat, e.get()) {
(&PathStat::Dir { .. }, &PathStat::Dir { .. }) => (),
(x, y) => {
return future::err(format!(
"Snapshots contained duplicate path: {:?} vs {:?}",
x, y
))
.to_boxed();
}
},
indexmap::map::Entry::Vacant(v) => {
v.insert(path_stat);
}
}
}
uniq_paths.into_iter().map(|(_, v)| v).collect()
};
// Recursively merge the Digests in the Snapshots.
Self::merge_directories(store, snapshots.iter().map(|s| s.digest).collect())
.map(move |root_digest| Snapshot {
digest: root_digest,
path_stats: path_stats,
})
.to_boxed()
}
///
/// Given Digest(s) representing Directory instances, merge them recursively into a single
/// output Directory Digest.
///
/// If a file is present with the same name and contents multiple times, it will appear once.
/// If a file is present with the same name, but different contents, an error will be returned.
///
pub fn merge_directories(store: Store, dir_digests: Vec<Digest>) -> BoxFuture<Digest, String> {
if dir_digests.is_empty() {
return future::ok(EMPTY_DIGEST).to_boxed();
} else if dir_digests.len() == 1 {
let mut dir_digests = dir_digests;
return future::ok(dir_digests.pop().unwrap()).to_boxed();
}
let directories = dir_digests
.into_iter()
.map(|digest| {
store
.load_directory(digest)
.and_then(move |maybe_directory| {
maybe_directory
.map(|(dir, _metadata)| dir)
.ok_or_else(|| format!("Digest {:?} did not exist in the Store.", digest))
})
})
.collect::<Vec<_>>();
join_all(directories)
.and_then(move |mut directories| {
let mut out_dir = bazel_protos::remote_execution::Directory::new();
// Merge FileNodes.
let file_nodes = Iterator::flatten(
directories
.iter_mut()
.map(|directory| directory.take_files().into_iter()),
)
.sorted_by(|a, b| a.name.cmp(&b.name));
out_dir.set_files(protobuf::RepeatedField::from_vec(
file_nodes.into_iter().dedup().collect(),
));
let unique_count = out_dir
.get_files()
.iter()
.map(bazel_protos::remote_execution::FileNode::get_name)
.dedup()
.count();
if unique_count != out_dir.get_files().len() {
let groups = out_dir
.get_files()
.iter()
.group_by(|f| f.get_name().to_owned());
for (file_name, group) in &groups {
if group.count() > 1 {
return future::err(format!(
"Can only merge Directories with no duplicates, but found duplicate files: {}",
file_name
))
.to_boxed();
}
}
}
// Group and recurse for DirectoryNodes.
let sorted_child_directories = {
let mut directories_to_merge = Iterator::flatten(
directories
.iter_mut()
.map(|directory| directory.take_directories().into_iter()),
)
.collect::<Vec<_>>();
directories_to_merge.sort_by(|a, b| a.name.cmp(&b.name));
directories_to_merge
};
let store2 = store.clone();
join_all(
sorted_child_directories
.into_iter()
.group_by(|d| d.name.clone())
.into_iter()
.map(move |(child_name, group)| {
let store2 = store2.clone();
let digests_result = group
.map(|d| d.get_digest().into())
.collect::<Result<Vec<_>, String>>();
future::done(digests_result)
.and_then(move |digests| Self::merge_directories(store2.clone(), digests))
.map(move |merged_digest| {
let mut child_dir = bazel_protos::remote_execution::DirectoryNode::new();
child_dir.set_name(child_name);
child_dir.set_digest((&merged_digest).into());
child_dir
})
})
.collect::<Vec<_>>(),
)
.and_then(move |child_directories| {
out_dir.set_directories(protobuf::RepeatedField::from_vec(child_directories));
store.record_directory(&out_dir, true)
})
.to_boxed()
})
.to_boxed()
}
pub fn strip_prefix(
store: Store,
root_digest: Digest,
prefix: PathBuf,
) -> impl Future<Item = Digest, Error = String> {
let store2 = store.clone();
Self::get_directory_or_err(store.clone(), root_digest)
.and_then(move |dir| {
futures::future::loop_fn(
(dir, PathBuf::new(), prefix),
move |(dir, already_stripped, prefix)| {
let has_already_stripped_any = already_stripped.components().next().is_some();
let mut components = prefix.components();
let component_to_strip = components.next();
if let Some(component_to_strip) = component_to_strip {
let remaining_prefix = components.collect();
let component_to_strip_str = component_to_strip.as_os_str().to_string_lossy();
let mut saw_matching_dir = false;
let extra_directories: Vec<_> = dir.get_directories().iter().filter_map(|subdir| {
if subdir.get_name() == component_to_strip_str {
saw_matching_dir = true;
None
} else {
Some(subdir.get_name().to_owned())
}
}).collect();
let files: Vec<_> = dir.get_files().iter().map(|file| file.get_name().to_owned()).collect();
match (saw_matching_dir, extra_directories.is_empty() && files.is_empty()) {
(false, true) => return futures::future::ok(futures::future::Loop::Break(bazel_protos::remote_execution::Directory::new())).to_boxed(),
(false, false) => {
// Prefer "No subdirectory found" error to "had extra files" error.
return futures::future::err(format!(
"Cannot strip prefix {} from root directory {:?} - {}directory{} didn't contain a directory named {}{}",
already_stripped.join(&prefix).display(),
root_digest,
if has_already_stripped_any { "sub" } else { "root " },
if has_already_stripped_any { format!(" {}", already_stripped.display()) } else { String::new() },
component_to_strip_str,
if !extra_directories.is_empty() || !files.is_empty() { format!(" but did contain {}", Self::directories_and_files(&extra_directories, &files)) } else { String::new() },
)).to_boxed()
},
(true, false) => {
return futures::future::err(format!(
"Cannot strip prefix {} from root directory {:?} - {}directory{} contained non-matching {}",
already_stripped.join(&prefix).display(),
root_digest,
if has_already_stripped_any { "sub" } else { "root " },
if has_already_stripped_any { format!(" {}", already_stripped.display()) } else { String::new() },
Self::directories_and_files(&extra_directories, &files),
)).to_boxed();
},
(true, true) => {
// Must be 0th index, because we've checked that we saw a matching directory, and no others.
let maybe_digest: Result<Digest, String> = dir.get_directories()[0]
.get_digest()
.into();
let next_already_stripped = already_stripped.join(component_to_strip);
let dir = Self::get_directory_or_err(store.clone(), try_future!(maybe_digest));
dir
.map(|dir| {
futures::future::Loop::Continue((dir, next_already_stripped, remaining_prefix))
})
.to_boxed()
}
}
} else {
futures::future::ok(futures::future::Loop::Break(dir)).to_boxed()
}
},
)
})
.and_then(move |dir| store2.record_directory(&dir, true))
}
fn directories_and_files(directories: &[String], files: &[String]) -> String {
format!(
"{}{}{}",
if directories.is_empty() {
String::new()
} else {
format!(
"director{} named: {}",
if directories.len() == 1 { "y" } else { "ies" },
directories.join(", ")
)
},
if !directories.is_empty() && !files.is_empty() {
" and "
} else {
""
},
if files.is_empty() {
String::new()
} else {
format!(
"file{} named: {}",
if files.len() == 1 { "" } else { "s" },
files.join(", ")
)
},
)
}
fn get_directory_or_err(
store: Store,
digest: Digest,
) -> impl Future<Item = bazel_protos::remote_execution::Directory, Error = String> {
store.load_directory(digest).and_then(move |maybe_dir| {
maybe_dir
.map(|(dir, _metadata)| dir)
.ok_or_else(|| format!("{:?} was not known", digest))
})
}
///
/// Capture a Snapshot of a presumed-immutable piece of the filesystem.
///
/// Note that we don't use a Graph here, and don't cache any intermediate steps, we just place
/// the resultant Snapshot into the store and return it. This is important, because we're reading
/// things from arbitrary filepaths which we don't want to cache in the graph, as we don't watch
/// them for changes. Because we're not caching things, we can safely configure the virtual
/// filesystem to be symlink-oblivious.
///
/// If the `digest_hint` is given, first attempt to load the Snapshot using that Digest, and only
/// fall back to actually walking the filesystem if we don't have it (either due to garbage
/// collection or Digest-oblivious legacy caching).
///
pub fn capture_snapshot_from_arbitrary_root<P: AsRef<Path> + Send + 'static>(
store: Store,
executor: task_executor::Executor,
root_path: P,
path_globs: PathGlobs,
digest_hint: Option<Digest>,
) -> BoxFuture<Snapshot, String> {
// Attempt to use the digest hint to load a Snapshot without expanding the globs; otherwise,
// expand the globs to capture a Snapshot.
let store2 = store.clone();
future::result(digest_hint.ok_or_else(|| "No digest hint provided.".to_string()))
.and_then(move |digest| Snapshot::from_digest(store, digest))
.or_else(|_| {
let posix_fs = Arc::new(try_future!(PosixFS::new_with_symlink_behavior(
root_path,
&[],
executor,
SymlinkBehavior::Oblivious
)));
posix_fs
.expand(path_globs)
.map_err(|err| format!("Error expanding globs: {}", err))
.and_then(|path_stats| {
Snapshot::from_path_stats(
store2.clone(),
&OneOffStoreFileByDigest::new(store2, posix_fs),
path_stats,
)
})
.to_boxed()
})
.to_boxed()
}
}
impl fmt::Debug for Snapshot {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Snapshot(digest={:?}, entries={})",
self.digest,
self.path_stats.len()
)
}
}
fn paths_of_child_dir(paths: Vec<PathStat>) -> Vec<PathStat> {
paths
.into_iter()
.filter_map(|s| {
if s.path().components().count() == 1 {
return None;
}
Some(match s {
PathStat::File { path, stat } => PathStat::File {
path: path.iter().skip(1).collect(),
stat: stat,
},
PathStat::Dir { path, stat } => PathStat::Dir {
path: path.iter().skip(1).collect(),
stat: stat,
},
})
})
.collect()
}
fn osstring_as_utf8(path: OsString) -> Result<String, String> {
path
.into_string()
.map_err(|p| format!("{:?}'s file_name is not representable in UTF8", p))
}
// StoreFileByDigest allows a File to be saved to an underlying Store, in such a way that it can be
// looked up by the Digest produced by the store_by_digest method.
// It is a separate trait so that caching implementations can be written which wrap the Store (used
// to store the bytes) and VFS (used to read the files off disk if needed).
pub trait StoreFileByDigest<Error> {
fn store_by_digest(&self, file: File) -> BoxFuture<Digest, Error>;
}
///
/// A StoreFileByDigest which reads with a PosixFS and writes to a Store, with no caching.
///
#[derive(Clone)]
pub struct OneOffStoreFileByDigest {
store: Store,
posix_fs: Arc<PosixFS>,
}
impl OneOffStoreFileByDigest {
pub fn new(store: Store, posix_fs: Arc<PosixFS>) -> OneOffStoreFileByDigest {
OneOffStoreFileByDigest { store, posix_fs }
}
}
impl StoreFileByDigest<String> for OneOffStoreFileByDigest {
fn store_by_digest(&self, file: File) -> BoxFuture<Digest, String> {
let store = self.store.clone();
self
.posix_fs
.read_file(&file)
.map_err(move |err| format!("Error reading file {:?}: {:?}", file, err))
.and_then(move |content| store.store_file_bytes(content.content, true))
.to_boxed()
}
}
#[cfg(test)]
mod tests {
use futures::future::Future;
use hashing::{Digest, Fingerprint};
use tempfile;
use testutil::data::TestDirectory;
use testutil::make_file;
use super::{OneOffStoreFileByDigest, Snapshot};
use crate::Store;
use fs::{
Dir, File, GlobExpansionConjunction, GlobMatching, PathGlobs, PathStat, PosixFS,
StrictGlobMatching,
};
use std;
use std::path::{Path, PathBuf};
use std::sync::Arc;
const STR: &str = "European Burmese";
fn setup() -> (
Store,
tempfile::TempDir,
Arc<PosixFS>,
OneOffStoreFileByDigest,
task_executor::Executor,
) {
let executor = task_executor::Executor::new();
// TODO: Pass a remote CAS address through.
let store = Store::local_only(
executor.clone(),
tempfile::Builder::new()
.prefix("lmdb_store")
.tempdir()
.unwrap(),
)
.unwrap();
let dir = tempfile::Builder::new().prefix("root").tempdir().unwrap();
let posix_fs = Arc::new(PosixFS::new(dir.path(), &[], task_executor::Executor::new()).unwrap());
let file_saver = OneOffStoreFileByDigest::new(store.clone(), posix_fs.clone());
(store, dir, posix_fs, file_saver, executor)
}
#[test]
fn snapshot_one_file() {
let (store, dir, posix_fs, digester, runtime) = setup();
let file_name = PathBuf::from("roland");
make_file(&dir.path().join(&file_name), STR.as_bytes(), 0o600);
let path_stats = expand_all_sorted(posix_fs, &runtime);
let snapshot = runtime
.block_on(Snapshot::from_path_stats(
store,
&digester,
path_stats.clone(),
))
.unwrap();
assert_eq!(
snapshot,
Snapshot {
digest: Digest(
Fingerprint::from_hex_string(
"63949aa823baf765eff07b946050d76ec0033144c785a94d3ebd82baa931cd16",
)
.unwrap(),
80,
),
path_stats: path_stats,
}
);
}
#[test]
fn snapshot_recursive_directories() {
let (store, dir, posix_fs, digester, runtime) = setup();
let cats = PathBuf::from("cats");
let roland = cats.join("roland");
std::fs::create_dir_all(&dir.path().join(cats)).unwrap();
make_file(&dir.path().join(&roland), STR.as_bytes(), 0o600);
let path_stats = expand_all_sorted(posix_fs, &runtime);
let snapshot = runtime
.block_on(Snapshot::from_path_stats(
store,
&digester,
path_stats.clone(),
))
.unwrap();
assert_eq!(
snapshot,
Snapshot {
digest: Digest(
Fingerprint::from_hex_string(
"8b1a7ea04eaa2527b35683edac088bc826117b53b7ec6601740b55e20bce3deb",
)
.unwrap(),
78,
),
path_stats: path_stats,
}
);
}
#[test]
fn snapshot_from_digest() {
let (store, dir, posix_fs, digester, runtime) = setup();
let cats = PathBuf::from("cats");
let roland = cats.join("roland");
std::fs::create_dir_all(&dir.path().join(cats)).unwrap();
make_file(&dir.path().join(&roland), STR.as_bytes(), 0o600);
let path_stats = expand_all_sorted(posix_fs, &runtime);
let expected_snapshot = runtime
.block_on(Snapshot::from_path_stats(
store.clone(),
&digester,
path_stats.clone(),
))
.unwrap();
assert_eq!(
expected_snapshot,
runtime
.block_on(Snapshot::from_digest(store, expected_snapshot.digest))
.unwrap()
);
}
#[test]
fn snapshot_recursive_directories_including_empty() {
let (store, dir, posix_fs, digester, runtime) = setup();
let cats = PathBuf::from("cats");
let roland = cats.join("roland");
let dogs = PathBuf::from("dogs");
let llamas = PathBuf::from("llamas");
std::fs::create_dir_all(&dir.path().join(&cats)).unwrap();
std::fs::create_dir_all(&dir.path().join(&dogs)).unwrap();
std::fs::create_dir_all(&dir.path().join(&llamas)).unwrap();
make_file(&dir.path().join(&roland), STR.as_bytes(), 0o600);
let sorted_path_stats = expand_all_sorted(posix_fs, &runtime);
let mut unsorted_path_stats = sorted_path_stats.clone();
unsorted_path_stats.reverse();
assert_eq!(
runtime
.block_on(Snapshot::from_path_stats(
store,
&digester,
unsorted_path_stats.clone()
))
.unwrap(),
Snapshot {
digest: Digest(
Fingerprint::from_hex_string(
"fbff703bdaac62accf2ea5083bcfed89292073bf710ef9ad14d9298c637e777b",
)
.unwrap(),
232,
),
path_stats: sorted_path_stats,
}
);
}
#[test]
fn merge_directories_two_files() {
let (store, _, _, _, runtime) = setup();
let containing_roland = TestDirectory::containing_roland();
let containing_treats = TestDirectory::containing_treats();
runtime
.block_on(store.record_directory(&containing_roland.directory(), false))
.expect("Storing roland directory");
runtime
.block_on(store.record_directory(&containing_treats.directory(), false))
.expect("Storing treats directory");
let result = runtime.block_on(Snapshot::merge_directories(
store,
vec![containing_treats.digest(), containing_roland.digest()],
));
assert_eq!(
result,
Ok(TestDirectory::containing_roland_and_treats().digest())
);
}
#[test]
fn merge_directories_clashing_files() {
let (store, _, _, _, runtime) = setup();
let containing_roland = TestDirectory::containing_roland();
let containing_wrong_roland = TestDirectory::containing_wrong_roland();
runtime
.block_on(store.record_directory(&containing_roland.directory(), false))
.expect("Storing roland directory");
runtime
.block_on(store.record_directory(&containing_wrong_roland.directory(), false))
.expect("Storing wrong roland directory");
let err = runtime
.block_on(Snapshot::merge_directories(
store,
vec![containing_roland.digest(), containing_wrong_roland.digest()],
))
.expect_err("Want error merging");
assert!(
err.contains("roland"),
"Want error message to contain roland but was: {}",
err
);
}
#[test]
fn merge_directories_same_files() {
let (store, _, _, _, runtime) = setup();
let containing_roland = TestDirectory::containing_roland();
let containing_roland_and_treats = TestDirectory::containing_roland_and_treats();
runtime
.block_on(store.record_directory(&containing_roland.directory(), false))
.expect("Storing roland directory");
runtime
.block_on(store.record_directory(&containing_roland_and_treats.directory(), false))
.expect("Storing treats directory");
let result = runtime.block_on(Snapshot::merge_directories(
store,
vec![
containing_roland.digest(),
containing_roland_and_treats.digest(),
],
));
assert_eq!(
result,
Ok(TestDirectory::containing_roland_and_treats().digest())
);
}
#[test]
fn snapshot_merge_two_files() {
let (store, tempdir, _, digester, runtime) = setup();
let common_dir_name = "tower";
let common_dir = PathBuf::from(common_dir_name);
let dir = make_dir_stat(tempdir.path(), &common_dir);
let file1 = make_file_stat(
tempdir.path(),
&common_dir.join("roland"),
STR.as_bytes(),
false,
);
let file2 = make_file_stat(
tempdir.path(),
&common_dir.join("susannah"),
STR.as_bytes(),
true,
);
let snapshot1 = runtime
.block_on(Snapshot::from_path_stats(
store.clone(),
&digester,
vec![dir.clone(), file1.clone()],
))
.unwrap();
let snapshot2 = runtime
.block_on(Snapshot::from_path_stats(
store.clone(),
&digester,
vec![dir.clone(), file2.clone()],
))
.unwrap();
let merged = runtime
.block_on(Snapshot::merge(store.clone(), &[snapshot1, snapshot2]))
.unwrap();
let merged_root_directory = runtime
.block_on(store.load_directory(merged.digest))
.unwrap()
.unwrap()
.0;
assert_eq!(merged.path_stats, vec![dir, file1, file2]);
assert_eq!(merged_root_directory.files.len(), 0);
assert_eq!(merged_root_directory.directories.len(), 1);
let merged_child_dirnode = merged_root_directory.directories[0].clone();
let merged_child_dirnode_digest: Result<Digest, String> =
merged_child_dirnode.get_digest().into();
let merged_child_directory = runtime
.block_on(store.load_directory(merged_child_dirnode_digest.unwrap()))
.unwrap()
.unwrap()
.0;
assert_eq!(merged_child_dirnode.name, common_dir_name);
assert_eq!(
merged_child_directory
.files
.iter()
.map(|filenode| filenode.name.clone())
.collect::<Vec<_>>(),
vec!["roland".to_string(), "susannah".to_string()],
);
}
#[test]
fn snapshot_merge_colliding() {
let (store, tempdir, _, digester, runtime) = setup();
let file = make_file_stat(
tempdir.path(),
&PathBuf::from("roland"),
STR.as_bytes(),
false,
);
let snapshot1 = runtime
.block_on(Snapshot::from_path_stats(
store.clone(),
&digester,
vec![file.clone()],
))
.unwrap();
let snapshot2 = runtime
.block_on(Snapshot::from_path_stats(
store.clone(),
&digester,
vec![file],
))
.unwrap();
let merged_res = Snapshot::merge(store.clone(), &[snapshot1, snapshot2]).wait();
match merged_res {
Err(ref msg) if msg.contains("contained duplicate path") && msg.contains("roland") => (),
x => panic!(
"Snapshot::merge should have failed with a useful message; got: {:?}",
x
),
}
}
#[test]
fn strip_empty_prefix() {
let (store, _, _, _, runtime) = setup();
let dir = TestDirectory::nested();
runtime
.block_on(store.record_directory(&dir.directory(), false))
.expect("Error storing directory");
let result = runtime.block_on(super::Snapshot::strip_prefix(
store,
dir.digest(),
PathBuf::from(""),
));
assert_eq!(result, Ok(dir.digest()));
}
#[test]
fn strip_non_empty_prefix() {
let (store, _, _, _, runtime) = setup();
let dir = TestDirectory::nested();
runtime
.block_on(store.record_directory(&dir.directory(), false))
.expect("Error storing directory");
runtime
.block_on(store.record_directory(&TestDirectory::containing_roland().directory(), false))
.expect("Error storing directory");
let result = runtime.block_on(super::Snapshot::strip_prefix(
store,
dir.digest(),
PathBuf::from("cats"),
));
assert_eq!(result, Ok(TestDirectory::containing_roland().digest()));
}
#[test]
fn strip_prefix_empty_subdir() {
let (store, _, _, _, runtime) = setup();
let dir = TestDirectory::containing_falcons_dir();
runtime
.block_on(store.record_directory(&dir.directory(), false))
.expect("Error storing directory");
let result = runtime.block_on(super::Snapshot::strip_prefix(
store,
dir.digest(),
PathBuf::from("falcons/peregrine"),
));
assert_eq!(result, Ok(TestDirectory::empty().digest()));
}
#[test]
fn strip_dir_not_in_store() {
let (store, _, _, _, runtime) = setup();
let digest = TestDirectory::nested().digest();
let result = runtime.block_on(super::Snapshot::strip_prefix(
store,
digest,
PathBuf::from("cats"),
));
assert_eq!(result, Err(format!("{:?} was not known", digest)));
}