Skip to content

Commit

Permalink
Handle delete events on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasenko committed Mar 13, 2016
1 parent 02a7f5b commit ecf8493
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/takari/watcher/DirectoryWatcherJdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public void processEventsJdk() throws IOException {
} else if (kind == ENTRY_MODIFY) {
HashCode existingHash = pathHashes.get(child);
HashCode newHash = hash(child);
if (existingHash != null && !existingHash.equals(newHash)) {
// newHash can be null when using File#delete() on windows - it generates MODIFY and DELETE in succession
// in this case the MODIFY event can be safely ignored
if (existingHash != null && newHash != null && !existingHash.equals(newHash)) {
pathHashes.put(child, newHash);
listener.onModify(child);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/takari/watcher/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class PathUtils {

public static HashCode hash(Path file) {
try {
if (!file.toFile().isDirectory()) {
File f = file.toFile();
if (!f.isDirectory()) {
if(!f.exists()) return null;
return Files.hash(file.toFile(), HASH_FUNCTION);
} else {
return HASH_FUNCTION.newHasher().putString(file.toString(), Charsets.UTF_8).hash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,16 @@ protected void runWatcher(Path directory, Watchable watchable, WatchService watc
// the order of the played actions to match the order of the events emitted.
//
List<WatchEvent.Kind<Path>> one = events.get("one.txt");
assertEquals(2, one.size());
assertEquals(one.get(0), actions.get(0).kind);
assertEquals(one.get(1), actions.get(5).kind);

List<WatchEvent.Kind<Path>> two = events.get("two.txt");
assertEquals(1, two.size());
assertEquals(two.get(0), actions.get(1).kind);

List<WatchEvent.Kind<Path>> three = events.get("three.txt");
assertEquals(3, three.size());
assertEquals(three.get(0), actions.get(2).kind);
assertEquals(three.get(1), actions.get(3).kind);
assertEquals(three.get(2), actions.get(4).kind);
Expand All @@ -117,6 +121,7 @@ public void run() {
try {
watcher.watch();
} catch (Exception e) {
e.printStackTrace();
}
}
};
Expand Down

0 comments on commit ecf8493

Please sign in to comment.