-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle OS pretty name on old OS without OS release #35453
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,8 @@ | |
import java.lang.reflect.Method; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -567,22 +569,33 @@ private String getPrettyName() throws IOException { | |
} | ||
|
||
/** | ||
* The lines from {@code /etc/os-release} or {@code /usr/lib/os-release} as a fallback. These file represents identification of the | ||
* underlying operating system. The structure of the file is newlines of key-value pairs of shell-compatible variable assignments. | ||
* The lines from {@code /etc/os-release} or {@code /usr/lib/os-release} as a fallback, with an additional fallback to | ||
* {@code /etc/system-release}. These file represents identification of the underlying operating system. The structure of the file is | ||
* newlines of key-value pairs of shell-compatible variable assignments. | ||
* | ||
* @return the lines from {@code /etc/os-release} or {@code /usr/lib/os-release} | ||
* @throws IOException if an I/O exception occurs reading {@code /etc/os-release} or {@code /usr/lib/os-release} | ||
* @return the lines from {@code /etc/os-release} or {@code /usr/lib/os-release} or {@code /etc/system-release} | ||
* @throws IOException if an I/O exception occurs reading {@code /etc/os-release} or {@code /usr/lib/os-release} or | ||
* {@code /etc/system-release} | ||
*/ | ||
@SuppressForbidden(reason = "access /etc/os-release or /usr/lib/os-release") | ||
@SuppressForbidden(reason = "access /etc/os-release or /usr/lib/os-release or /etc/system-release") | ||
List<String> readOsRelease() throws IOException { | ||
final List<String> lines; | ||
if (Files.exists(PathUtils.get("/etc/os-release"))) { | ||
lines = Files.readAllLines(PathUtils.get("/etc/os-release")); | ||
} else { | ||
assert lines != null && lines.isEmpty() == false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to have this as an assertion or as an IllegalStateException or similar? If a user runs this on some weird Linux distribution that has an empty file here they might get odd errors later on, although I suppose you could argue that if they're not running on a supported OS then they're on their own anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly, we code for the platforms that we support and use these assertions to ensure that everything is as expected on those platforms. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ |
||
return lines; | ||
} else if (Files.exists(PathUtils.get("/usr/lib/os-release"))) { | ||
lines = Files.readAllLines(PathUtils.get("/usr/lib/os-release")); | ||
assert lines != null && lines.isEmpty() == false; | ||
return lines; | ||
} else if (Files.exists(PathUtils.get("/etc/system-release"))) { | ||
// fallback for older Red Hat-like OS | ||
lines = Files.readAllLines(PathUtils.get("/etc/system-release")); | ||
assert lines != null && lines.size() == 1; | ||
return Collections.singletonList("PRETTY_NAME=\"" + lines.get(0) + "\""); | ||
} else { | ||
return Collections.emptyList(); | ||
} | ||
assert lines != null && lines.isEmpty() == false; | ||
return lines; | ||
} | ||
|
||
public OsStats osStats() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: s/These file represents/These files represent/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed cac0f9f.