IO-892: Add maximum traversal depth to FileAlterationObserver - #870
Conversation
Add an optional maximum traversal depth to the observer builder and apply the boundary before listing child entries during initialization and polling. Preserve unlimited recursive traversal by default and cover the new boundary semantics, serialization compatibility, and directory-listing behavior with tests. Generated-by: OpenAI Codex
There was a problem hiding this comment.
Pull request overview
Adds an optional maximum traversal depth to FileAlterationObserver to allow depth-bounded directory monitoring (primarily to reduce traversal cost and suppress events below a configured boundary) while preserving the default unlimited-recursion behavior.
Changes:
- Add
FileAlterationObserver.Builder#setMaxDepth(int)(defaultInteger.MAX_VALUE) and wire depth checks into initialization andcheckAndNotify()traversal. - Add tests covering max-depth defaults/validation, boundary behavior (including ensuring
listFiles()is not invoked past the boundary), and serialization round-trips. - Update release notes to document the new builder option (IO-892).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java | Introduces maxDepth configuration and uses it to prevent descending below the configured directory depth. |
| src/test/java/org/apache/commons/io/monitor/FileAlterationObserverTest.java | Adds unit tests validating depth-limited traversal behavior and serialization behavior for the new field. |
| src/changes/changes.xml | Adds a release note entry for Builder.setMaxDepth(int) (IO-892). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
garydgregory
left a comment
There was a problem hiding this comment.
Hello @zx8410-ops
Thank you for your PR. Please comments.
Use a primitive int for maxDepth, clarify the traversal-boundary Javadoc, and add the release-note developer metadata. Generated-by: OpenAI Codex
|
@garydgregory Thank you for the review. I addressed all four inline comments in 5cde511:
Local verification passed for the focused monitor tests, Checkstyle, Javadoc, package, and japicmp checks. Java CI, CodeQL, and Dependency Review are also all passing. Could you please take another look? |
|
Hello @zx8410-ops |
|
Thank you. I agree that reusing One detail I think we should verify before closing #870: The deep-event test in #871 currently uses With that boundary case covered, I am happy for #871 to replace #870 and for this PR to be closed. |
- Sort members - Update changes.xml
|
I went with this PR as the other one didn't do the file walking quite right (too deep). |
Thanks for your contribution to Apache Commons!
mvn.Summary
This PR adds an optional maximum traversal depth to
FileAlterationObserverthroughBuilder.setMaxDepth(int).The observed root has depth 0. Setting
maxDepthto 0 prevents listing entries below the root. At greater depths, entries atmaxDepthare monitored, but their children are not listed and do not generate events.The default remains
Integer.MAX_VALUE, so existing builder usage and deprecated constructors preserve full recursive monitoring. The depth limit applies during both initialization and everycheckAndNotify()cycle.Why this is not a FileFilter
A regular
FileFiltercannot provide the same traversal behavior.FileAlterationObservercallsdirectory.listFiles(fileFilter), so a depth-aware filter would still enumerate every child in a boundary directory. It also cannot express "monitor this directory but do not descend into it" with one boolean result.This implementation checks the depth boundary before invoking
listFiles().Tests
Tests cover default compatibility, invalid values, boundary directory events, ignored events below the boundary, repeated check cycles, verification that
listFiles()is not called below the boundary, custom root entries, and serialization round trips.Local Windows/NTFS results for 100,000 files below a depth-1 boundary:
maxDepth=1Verification completed successfully:
mvn -q "-Dtest=FileAlterationObserverTest,FileEntryTest,FileAlterationMonitorTest" testmvn -q -DskipTests checkstyle:check javadoc:javadoc package japicmp:cmpgit diff --check origin/master...HEADJIRA: https://issues.apache.org/jira/browse/IO-892