Conversation
|
in general, to test such things, you either try to mock up |
|
@sylvestre Thanks for the review, I'll implement your suggestions a bit later. Since we're on the topic of deduping: there's some code here that's copy-pasted from my work on |
yes, we try to publish content in uucore |
There was a problem hiding this comment.
PR Overview
This PR implements the new chcpu utility for managing CPU states via enabling, disabling, configuring, and dispatching mode management. Key changes include:
- Adding package configuration and build targets for chcpu.
- Implementing core CPU management functions with system file interaction and error handling placeholders.
- Providing documentation and updating the global Cargo.toml feature flags.
Reviewed Changes
| File | Description |
|---|---|
| src/uu/chcpu/Cargo.toml | Defines package configuration for chcpu. |
| src/uu/chcpu/src/chcpu.rs | Implements CPU management functionality (enable, disable, etc.). |
| src/uu/chcpu/chcpu.md | Adds documentation for the chcpu command. |
| src/uu/chcpu/src/main.rs | Defines the main entry point for chcpu. |
| Cargo.toml | Updates feature flags to include chcpu. |
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Comments suppressed due to low confidence (2)
src/uu/chcpu/src/chcpu.rs:186
- [nitpick] On failure to trigger a CPU rescan, handling the error with a proper exit code would improve reliability instead of only printing a message.
Err(e) => println!("Failed to trigger rescan of CPUs: {}", e.kind()),
src/uu/chcpu/src/chcpu.rs:230
- [nitpick] Consider returning an appropriate error with a non-zero exit code when the dispatching mode is unsupported, following the TODO guidance.
if !path.exists() {
| fn is_online(&self) -> bool { | ||
| fs::read_to_string(self.get_path().join("online")) | ||
| .map(|content| match content.trim() { | ||
| "0" => false, | ||
| "1" => true, | ||
| other => panic!("Unrecognized CPU online state: {}", other), | ||
| }) | ||
| // Just in case the caller forgot to check `is_hotpluggable` first, | ||
| // instead of panicing that the file doesn't exist, return true | ||
| // This is because a non-hotpluggable CPU is assumed to be always online | ||
| .unwrap_or(true) |
There was a problem hiding this comment.
Calling panic! on an unexpected CPU online state can cause abrupt termination. Consider returning an error or handling the unexpected state more gracefully.
| fn is_online(&self) -> bool { | |
| fs::read_to_string(self.get_path().join("online")) | |
| .map(|content| match content.trim() { | |
| "0" => false, | |
| "1" => true, | |
| other => panic!("Unrecognized CPU online state: {}", other), | |
| }) | |
| // Just in case the caller forgot to check `is_hotpluggable` first, | |
| // instead of panicing that the file doesn't exist, return true | |
| // This is because a non-hotpluggable CPU is assumed to be always online | |
| .unwrap_or(true) | |
| fn is_online(&self) -> Result<bool, String> { | |
| fs::read_to_string(self.get_path().join("online")) | |
| .map(|content| match content.trim() { | |
| "0" => Ok(false), | |
| "1" => Ok(true), | |
| other => Err(format!("Unrecognized CPU online state: {}", other)), | |
| }) | |
| // Just in case the caller forgot to check `is_hotpluggable` first, | |
| // instead of panicing that the file doesn't exist, return Ok(true) | |
| // This is because a non-hotpluggable CPU is assumed to be always online | |
| .unwrap_or(Ok(true)) |
| fn read_cpu_file(&self, file: &str) -> String { | ||
| fs::read_to_string(self.get_path().join(file)).unwrap() |
There was a problem hiding this comment.
Using unwrap here may cause an unexpected panic on I/O error. It would be better to propagate the error using Result to handle read failures gracefully.
| fn read_cpu_file(&self, file: &str) -> String { | |
| fs::read_to_string(self.get_path().join(file)).unwrap() | |
| fn read_cpu_file(&self, file: &str) -> std::io::Result<String> { | |
| fs::read_to_string(self.get_path().join(file)) |
0ce313e to
fbc1e6e
Compare
|
Updated the implementation to make use of |
In regards to this, good to know. I can look into adding the shared CPU list parsing functionality to that later, and then replace the implementations present in |
|
I am really really sorry but I took this #234 instead. it was more complete :( sorry again :( |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #226 +/- ##
===========================
===========================
☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This PR implements pretty much the entirety of
chcpu.Some things still missing:
chcpushould return a non-zero exit code in some edge cases. This implementation does check for most such cases, but pretty much always returns a success exit-code. I'm guessing thatuucore::error::UResultcan be used to solve this somehow without having to manually callstd::process::exit().