From 09f10432dd634cff13b813e5f3d1b699f4b2ea84 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sat, 15 Apr 2023 09:11:03 +0200 Subject: [PATCH] Fix hidden files showing up in TransformationRegistry (#3532) Signed-off-by: Jan N. Klug GitOrigin-RevId: 5ca849ed883c602ed7c8cfc09243082e83573688 --- .../transform/FileTransformationProvider.java | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/bundles/org.opensmarthouse.core.transform/src/main/java/org/openhab/core/transform/FileTransformationProvider.java b/bundles/org.opensmarthouse.core.transform/src/main/java/org/openhab/core/transform/FileTransformationProvider.java index a5df5441d90..4fd1213ef8b 100644 --- a/bundles/org.opensmarthouse.core.transform/src/main/java/org/openhab/core/transform/FileTransformationProvider.java +++ b/bundles/org.opensmarthouse.core.transform/src/main/java/org/openhab/core/transform/FileTransformationProvider.java @@ -67,7 +67,8 @@ public FileTransformationProvider( watchService.registerListener(this, transformationPath); // read initial contents try (Stream files = Files.walk(transformationPath)) { - files.filter(Files::isRegularFile).map(transformationPath::relativize).forEach(f -> processPath(CREATE, f)); + files.filter(Files::isRegularFile).map(transformationPath::relativize) + .forEach(f -> processWatchEvent(CREATE, f)); } catch (IOException e) { logger.warn("Could not list files in '{}', transformation configurations might be missing: {}", transformationPath, e.getMessage()); @@ -94,16 +95,18 @@ public Collection getAll() { return transformationConfigurations.values(); } - private void processPath(WatchService.Kind kind, Path path) { + @Override + public void processWatchEvent(WatchService.Kind kind, Path path) { Path finalPath = transformationPath.resolve(path); - if (kind == DELETE) { - Transformation oldElement = transformationConfigurations.remove(path); - if (oldElement != null) { - logger.trace("Removed configuration from file '{}", path); - listeners.forEach(listener -> listener.removed(this, oldElement)); - } - } else if (Files.isRegularFile(finalPath) && ((kind == CREATE) || (kind == MODIFY))) { - try { + try { + if (kind == DELETE) { + Transformation oldElement = transformationConfigurations.remove(path); + if (oldElement != null) { + logger.trace("Removed configuration from file '{}", path); + listeners.forEach(listener -> listener.removed(this, oldElement)); + } + } else if (Files.isRegularFile(finalPath) && !Files.isHidden(finalPath) + && ((kind == CREATE) || (kind == MODIFY))) { String fileName = path.getFileName().toString(); Matcher m = FILENAME_PATTERN.matcher(fileName); if (!m.matches()) { @@ -131,16 +134,11 @@ private void processPath(WatchService.Kind kind, Path path) { logger.trace("Updated new configuration from file '{}'", path); listeners.forEach(listener -> listener.updated(this, oldElement, newElement)); } - } catch (IOException e) { - logger.warn("Skipping {} event for '{}' - failed to read content: {}", kind, path, e.getMessage()); + } else { + logger.trace("Skipping {} event for '{}' - not a regular file", kind, path); } - } else { - logger.trace("Skipping {} event for '{}' - not a regular file", kind, path); + } catch (IOException e) { + logger.warn("Skipping {} event for '{}' - failed to process it: {}", kind, path, e.getMessage()); } } - - @Override - public void processWatchEvent(WatchService.Kind kind, Path path) { - processPath(kind, path); - } }