Skip to content

Commit 9167055

Browse files
committed
Polish "Add support for symlinks in FileWatcher"
See gh-43586
1 parent 26ca379 commit 9167055

File tree

2 files changed

+26
-11
lines changed
  • spring-boot-project/spring-boot-autoconfigure/src

2 files changed

+26
-11
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/FileWatcher.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -86,23 +86,24 @@ void watch(Set<Path> paths, Runnable action) {
8686
this.thread = new WatcherThread();
8787
this.thread.start();
8888
}
89-
this.thread.register(new Registration(resolveSymlinks(paths), action));
89+
Set<Path> actualPaths = new HashSet<>();
90+
for (Path path : paths) {
91+
actualPaths.add(resolveSymlinkIfNecessary(path));
92+
}
93+
this.thread.register(new Registration(actualPaths, action));
9094
}
9195
catch (IOException ex) {
9296
throw new UncheckedIOException("Failed to register paths for watching: " + paths, ex);
9397
}
9498
}
9599
}
96100

97-
private Set<Path> resolveSymlinks(Set<Path> paths) throws IOException {
98-
Set<Path> result = new HashSet<>();
99-
for (Path path : paths) {
100-
result.add(path);
101-
if (Files.isSymbolicLink(path)) {
102-
result.add(Files.readSymbolicLink(path));
103-
}
101+
private static Path resolveSymlinkIfNecessary(Path path) throws IOException {
102+
if (Files.isSymbolicLink(path)) {
103+
Path target = Files.readSymbolicLink(path);
104+
return resolveSymlinkIfNecessary(target);
104105
}
105-
return result;
106+
return path;
106107
}
107108

108109
@Override

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ssl/FileWatcherTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -106,6 +106,20 @@ void shouldFollowSymlink(@TempDir Path tempDir) throws Exception {
106106
callback.expectChanges();
107107
}
108108

109+
@Test
110+
void shouldFollowSymlinkRecursively(@TempDir Path tempDir) throws Exception {
111+
Path realFile = tempDir.resolve("realFile.txt");
112+
Path symLink = tempDir.resolve("symlink.txt");
113+
Path symLink2 = tempDir.resolve("symlink2.txt");
114+
Files.createFile(realFile);
115+
Files.createSymbolicLink(symLink, symLink2);
116+
Files.createSymbolicLink(symLink2, realFile);
117+
WaitingCallback callback = new WaitingCallback();
118+
this.fileWatcher.watch(Set.of(symLink), callback);
119+
Files.writeString(realFile, "Some content");
120+
callback.expectChanges();
121+
}
122+
109123
@Test
110124
void shouldIgnoreNotWatchedFiles(@TempDir Path tempDir) throws Exception {
111125
Path watchedFile = tempDir.resolve("watched.txt");

0 commit comments

Comments
 (0)