171171
172172import java .io .IOException ;
173173import java .nio .ByteBuffer ;
174+ import java .nio .file .DirectoryNotEmptyException ;
175+ import java .nio .file .FileVisitResult ;
174176import java .nio .file .Files ;
175177import java .nio .file .Path ;
178+ import java .nio .file .SimpleFileVisitor ;
179+ import java .nio .file .attribute .BasicFileAttributes ;
176180import java .util .Collection ;
177181import java .util .Collections ;
178182import java .util .Comparator ;
@@ -538,6 +542,7 @@ private void assertNoStaleRepositoryData() throws IOException {
538542 repos = reposDir .filter (s -> s .getFileName ().toString ().startsWith ("extra" ) == false ).collect (Collectors .toList ());
539543 }
540544 for (Path repoRoot : repos ) {
545+ cleanupEmptyTrees (repoRoot );
541546 final Path latestIndexGenBlob = repoRoot .resolve ("index.latest" );
542547 assertTrue ("Could not find index.latest blob for repo at [" + repoRoot + ']' , Files .exists (latestIndexGenBlob ));
543548 final long latestGen = ByteBuffer .wrap (Files .readAllBytes (latestIndexGenBlob )).getLong (0 );
@@ -553,6 +558,35 @@ private void assertNoStaleRepositoryData() throws IOException {
553558 }
554559 }
555560
561+ // Lucene's mock file system randomly generates empty `extra0` files that break the deletion of blob-store directories.
562+ // We clean those up here before checking a blob-store for stale files in this test.
563+ private void cleanupEmptyTrees (Path repoPath ) {
564+ try {
565+ Files .walkFileTree (repoPath , new SimpleFileVisitor <>() {
566+
567+ @ Override
568+ public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
569+ if (file .getFileName ().toString ().startsWith ("extra" )) {
570+ Files .delete (file );
571+ }
572+ return FileVisitResult .CONTINUE ;
573+ }
574+
575+ @ Override
576+ public FileVisitResult postVisitDirectory (Path dir , IOException exc ) throws IOException {
577+ try {
578+ Files .delete (dir );
579+ } catch (DirectoryNotEmptyException e ) {
580+ // We're only interested in deleting empty trees here, just ignore directories with content
581+ }
582+ return FileVisitResult .CONTINUE ;
583+ }
584+ });
585+ } catch (IOException e ) {
586+ throw new AssertionError (e );
587+ }
588+ }
589+
556590 private static void assertIndexGenerations (Path repoRoot , long latestGen ) throws IOException {
557591 try (Stream <Path > repoRootBlobs = Files .list (repoRoot )) {
558592 final long [] indexGenerations = repoRootBlobs .filter (p -> p .getFileName ().toString ().startsWith ("index-" ))
0 commit comments