Skip to content

Commit

Permalink
Avoid null hash warning in WatchServiceImpl when possible (#3691)
Browse files Browse the repository at this point in the history
Fix #3680

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored Jul 7, 2023
1 parent abcfe54 commit 591b16f
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public void unregisterListener(WatchEventListener watchEventListener) {

@Override
public void onEvent(@Nullable DirectoryChangeEvent directoryChangeEvent) throws IOException {
logger.trace("onEvent {}", directoryChangeEvent);
if (directoryChangeEvent == null || directoryChangeEvent.isDirectory()
|| directoryChangeEvent.eventType() == DirectoryChangeEvent.EventType.OVERFLOW) {
// exit early, we are neither interested in directory events nor in OVERFLOW events
Expand All @@ -223,12 +224,6 @@ public void onEvent(@Nullable DirectoryChangeEvent directoryChangeEvent) throws

Path path = directoryChangeEvent.path();

if (directoryChangeEvent.eventType() != DirectoryChangeEvent.EventType.DELETE
&& directoryChangeEvent.hash() == null) {
logger.warn("Detected invalid event (hash must not be null for CREATE/MODIFY): {}", directoryChangeEvent);
return;
}

synchronized (scheduledEvents) {
ScheduledFuture<?> future = scheduledEvents.remove(path);
if (future != null && !future.isDone()) {
Expand Down Expand Up @@ -259,9 +254,17 @@ private void notifyListeners(Path path) {
hashCache.remove(lastElement.path());
doNotify(path, Kind.DELETE);
} else if (firstElement.eventType() == DirectoryChangeEvent.EventType.CREATE) {
if (lastElement.hash() == null) {
logger.warn("Detected invalid event (hash must not be null for CREATE/MODIFY): {}", lastElement);
return;
}
hashCache.put(lastElement.path(), lastElement.hash());
doNotify(path, Kind.CREATE);
} else {
if (lastElement.hash() == null) {
logger.warn("Detected invalid event (hash must not be null for CREATE/MODIFY): {}", lastElement);
return;
}
FileHash oldHash = hashCache.put(lastElement.path(), lastElement.hash());
if (!Objects.equals(oldHash, lastElement.hash())) {
// only notify if hashes are different, otherwise the file content did not chnge
Expand Down

0 comments on commit 591b16f

Please sign in to comment.