|
32 | 32 |
|
33 | 33 | import java.io.Closeable; |
34 | 34 | import java.io.IOException; |
35 | | -import java.io.UncheckedIOException; |
36 | 35 | import java.nio.file.FileAlreadyExistsException; |
| 36 | +import java.nio.file.FileVisitResult; |
| 37 | +import java.nio.file.FileVisitor; |
37 | 38 | import java.nio.file.Files; |
| 39 | +import java.nio.file.NoSuchFileException; |
38 | 40 | import java.nio.file.Path; |
39 | 41 | import java.nio.file.StandardCopyOption; |
40 | 42 | import java.nio.file.StandardOpenOption; |
| 43 | +import java.nio.file.attribute.BasicFileAttributes; |
41 | 44 | import java.security.MessageDigest; |
42 | 45 | import java.util.ArrayList; |
43 | 46 | import java.util.Collection; |
@@ -104,18 +107,37 @@ final class DatabaseRegistry implements Closeable { |
104 | 107 |
|
105 | 108 | public void initialize(ResourceWatcherService resourceWatcher, IngestService ingestService) throws IOException { |
106 | 109 | localDatabases.initialize(resourceWatcher); |
107 | | - if (Files.exists(geoipTmpDirectory)) { |
108 | | - Files.walk(geoipTmpDirectory) |
109 | | - .filter(Files::isRegularFile) |
110 | | - .peek(path -> LOGGER.info("deleting stale file [{}]", path)) |
111 | | - .forEach(path -> { |
112 | | - try { |
113 | | - Files.delete(path); |
114 | | - } catch (IOException e) { |
115 | | - throw new UncheckedIOException(e); |
116 | | - } |
117 | | - }); |
118 | | - } else { |
| 110 | + Files.walkFileTree(geoipTmpDirectory, new FileVisitor<Path>() { |
| 111 | + @Override |
| 112 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { |
| 113 | + return FileVisitResult.CONTINUE; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 118 | + try { |
| 119 | + LOGGER.info("deleting stale file [{}]", file); |
| 120 | + Files.deleteIfExists(file); |
| 121 | + } catch (IOException e) { |
| 122 | + LOGGER.warn("can't delete stale file [" + file + "]", e); |
| 123 | + } |
| 124 | + return FileVisitResult.CONTINUE; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public FileVisitResult visitFileFailed(Path file, IOException e) { |
| 129 | + if(e instanceof NoSuchFileException == false) { |
| 130 | + LOGGER.warn("can't delete stale file [" + file + "]", e); |
| 131 | + } |
| 132 | + return FileVisitResult.CONTINUE; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) { |
| 137 | + return FileVisitResult.CONTINUE; |
| 138 | + } |
| 139 | + }); |
| 140 | + if (Files.exists(geoipTmpDirectory) == false) { |
119 | 141 | Files.createDirectory(geoipTmpDirectory); |
120 | 142 | } |
121 | 143 | LOGGER.info("initialized database registry, using geoip-databases directory [{}]", geoipTmpDirectory); |
|
0 commit comments