Skip to content

Commit

Permalink
Fix hidden files showing up in TransformationRegistry (openhab#3532)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
GitOrigin-RevId: 5ca849e
  • Loading branch information
J-N-K authored and splatch committed Jul 12, 2023
1 parent 41cc2ac commit 09f1043
Showing 1 changed file with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public FileTransformationProvider(
watchService.registerListener(this, transformationPath);
// read initial contents
try (Stream<Path> 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());
Expand All @@ -94,16 +95,18 @@ public Collection<Transformation> 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()) {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 09f1043

Please sign in to comment.