Repository navigation
Consolidate Linux UsbDevice udev logic into a shared tree builder - #3448
Conversation
The JNA, FFM, and native-free (sysfs) Linux USB backends each carried a full copy of the map-building, parent/child hub graph, sorting, and recursive tree-construction logic, differing only in how they enumerated devices. Split that boundary cleanly: - New UdevUsbDevice carrier holds the raw per-device attributes a backend extracts (syspath, product, manufacturer, vendorId, productId, serial, parentSyspath; null parent = root controller). - Each backend now only enumerates once and returns List<UdevUsbDevice>: LinuxUsbDeviceJNA (JNA libudev), LinuxUsbDeviceFFM (FFM libudev), and LinuxUsbDeviceNF (sysfs reader). - LinuxUsbDevice.getUsbDevices(List<UdevUsbDevice>) holds the single copy of the lookup-map/hub-graph/sort/tree logic; the three HALs call it with their backend's carrier list. No user-facing change: same device tree, same map fallbacks, same sort order. LinuxUsbDeviceNFTest routes through getUsbDevices to keep asserting on the built tree; LinuxUsbDeviceTest still unit-tests getDeviceAndChildren.
📝 WalkthroughWalkthroughUSB enumeration now returns raw ChangesLinux USB tree construction
Native-free enumeration
JNA and FFM enumeration
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3448 +/- ##
============================================
+ Coverage 74.46% 74.48% +0.01%
Complexity 76 76
============================================
Files 582 583 +1
Lines 21913 21934 +21
Branches 3669 3671 +2
============================================
+ Hits 16318 16338 +20
- Misses 3558 3559 +1
Partials 2037 2037
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
oshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.java (1)
16-97: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider converting
UdevUsbDeviceto a record. It’s a pure immutable carrier, and the existing call sites in this PR can switch to record accessors with minimal churn.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@oshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.java` around lines 16 - 97, Convert UdevUsbDevice from an immutable class to a Java record using its seven existing components: syspath, product, manufacturer, vendorId, productId, serial, and parentSyspath. Remove the explicit fields, constructor, and get* accessors, then update all call sites to use record accessors such as syspath() and parentSyspath().
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In
`@oshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.java`:
- Around line 16-97: Convert UdevUsbDevice from an immutable class to a Java
record using its seven existing components: syspath, product, manufacturer,
vendorId, productId, serial, and parentSyspath. Remove the explicit fields,
constructor, and get* accessors, then update all call sites to use record
accessors such as syspath() and parentSyspath().
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 63529975-66b1-41f3-ad0b-ecc66f519445
📒 Files selected for processing (9)
oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxHardwareAbstractionLayerNF.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.javaoshi-common/src/test/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNFTest.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
📜 Review details
⏰ Context from checks skipped due to timeout. (15)
- GitHub Check: CodeRabbit
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 26, ubuntu-latest
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
- GitHub Check: Test JDK 26, windows-latest
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: Test JDK 26, macos-latest
- GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
- GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
- GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (4)
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.
Applied to files:
oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxHardwareAbstractionLayerNF.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.
Applied to files:
oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxHardwareAbstractionLayerNF.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/UdevUsbDevice.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-06T04:45:47.644Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3121
File: oshi-core/src/main/java/oshi/software/os/linux/LinuxOperatingSystem.java:53-53
Timestamp: 2026-04-06T04:45:47.644Z
Learning: In the oshi/oshi project, only interfaces and classes in the root packages `oshi`, `oshi.hardware`, and `oshi.software.os` are considered the public API. Code in subpackages that represent specific implementations/platforms (e.g., `oshi.software.os.linux`, `oshi.hardware.platform.linux`, and similar subpackages) is implementation detail. When reviewing, do not treat changes to classes in these platform-specific subpackages as public API breaks (for example: making `LinuxOperatingSystem` abstract, narrowing constructor visibility, or adding/removing abstract methods should not be flagged as breaking public API).
Applied to files:
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.javaoshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
📚 Learning: 2026-04-05T15:37:16.042Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java:59-65
Timestamp: 2026-04-05T15:37:16.042Z
Learning: For OSHI Linux USB device implementations, the behavior of `getUsbDevices(boolean tree)` with `tree=false` is intentional: the returned flat list includes controller nodes (it is not meant to exclude them). In code review, do not flag the implementation for including controllers in flat mode; instead, only flag/update documentation/JavaDoc that incorrectly claims controllers are excluded (e.g., Javadoc text like "returns a flat list excluding controllers" should be corrected).
Applied to files:
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
🔇 Additional comments (8)
oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerFFM.java (1)
21-21: LGTM!Also applies to: 74-74
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java (1)
21-27: LGTM!Also applies to: 43-52, 63-76, 89-89
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java (1)
21-21: LGTM!Also applies to: 59-59
oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java (1)
2-10: LGTM!Also applies to: 19-77, 79-95
oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNF.java (1)
7-82: LGTM!oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxHardwareAbstractionLayerNF.java (1)
20-20: LGTM!Also applies to: 57-57
oshi-common/src/test/java/oshi/hardware/common/platform/linux/nativefree/LinuxUsbDeviceNFTest.java (1)
24-41: LGTM!Also applies to: 50-51, 67-68, 96-97, 127-128
oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java (1)
21-27: LGTM!Also applies to: 43-56, 68-68, 85-97
Summary
Part of the ongoing cross-OS consistency/dedup audit. The three Linux USB backends (JNA libudev, FFM libudev, and the native-free sysfs reader) each carried a full copy of the same map-building, parent/child hub-graph, sorting, and recursive device-tree construction — differing only in how they enumerated devices from the OS.
This splits at the carrier boundary: each backend enumerates once and returns a list of raw attributes; a single shared builder assembles the tree.
Changes
UdevUsbDevice(oshi-common) — immutable carrier for one device's raw attributes:syspath,product,manufacturer,vendorId,productId,serial,parentSyspath(nullparent = root controller). Any field may benullif the backend didn't report it.LinuxUsbDevice— nowfinal; holds the single copy ofgetUsbDevices(List<UdevUsbDevice>)(lookup maps + hub graph + sort + recursive tree).getDeviceAndChildrenkept package-visible for its existing unit test.List<UdevUsbDevice>:LinuxUsbDeviceJNA— JNA libudev (unref()finally chain)LinuxUsbDeviceFFM— FFM libudev (callInArenaOrDefault+NativeHandle)LinuxUsbDeviceNF— sysfs/sys/bus/usb/devices/readercreateUsbDevices()→LinuxUsbDevice.getUsbDevices(XxxDriver.queryUsbDevices()).Net −82 LOC of hand-written logic (three copies collapsed to one).
Behavior
No user-facing change: same device tree, same map fallbacks, same sort order.
LinuxUsbDeviceNFTestnow routes its sysfs fixtures throughgetUsbDevicesso it keeps asserting on the built tree;LinuxUsbDeviceTeststill unit-testsgetDeviceAndChildrendirectly.Testing
installwith tests: BUILD SUCCESS, 0 tests failedSummary by CodeRabbit
New Features
Bug Fixes