summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibCore/ProcessStatisticsReader.cpp')
-rw-r--r--Userland/Libraries/LibCore/ProcessStatisticsReader.cpp30
1 files changed, 7 insertions, 23 deletions
diff --git a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp
index c5f79f6d2e..8f2ea44cc0 100644
--- a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp
+++ b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp
@@ -8,7 +8,6 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
-#include <LibCore/File.h>
#include <LibCore/ProcessStatisticsReader.h>
#include <pwd.h>
@@ -16,29 +15,14 @@ namespace Core {
HashMap<uid_t, DeprecatedString> ProcessStatisticsReader::s_usernames;
-Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file, bool include_usernames)
+ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(Core::Stream::SeekableStream& proc_all_file, bool include_usernames)
{
- if (proc_all_file) {
- if (!proc_all_file->seek(0, Core::SeekMode::SetPosition)) {
- warnln("ProcessStatisticsReader: Failed to refresh /sys/kernel/processes: {}", proc_all_file->error_string());
- return {};
- }
- } else {
- proc_all_file = Core::File::construct("/sys/kernel/processes");
- if (!proc_all_file->open(Core::OpenMode::ReadOnly)) {
- warnln("ProcessStatisticsReader: Failed to open /sys/kernel/processes: {}", proc_all_file->error_string());
- return {};
- }
- }
+ TRY(proc_all_file.seek(0, Core::Stream::SeekMode::SetPosition));
AllProcessesStatistics all_processes_statistics;
- auto file_contents = proc_all_file->read_all();
- auto json = JsonValue::from_string(file_contents);
- if (json.is_error())
- return {};
-
- auto& json_obj = json.value().as_object();
+ auto file_contents = TRY(proc_all_file.read_all());
+ auto json_obj = TRY(JsonValue::from_string(file_contents)).as_object();
json_obj.get("processes"sv).as_array().for_each([&](auto& value) {
const JsonObject& process_object = value.as_object();
Core::ProcessStatistics process;
@@ -104,10 +88,10 @@ Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all(RefPtr<Core::F
return all_processes_statistics;
}
-Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all(bool include_usernames)
+ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(bool include_usernames)
{
- RefPtr<Core::File> proc_all_file;
- return get_all(proc_all_file, include_usernames);
+ auto proc_all_file = TRY(Core::Stream::File::open("/sys/kernel/processes"sv, Core::Stream::OpenMode::Read));
+ return get_all(*proc_all_file, include_usernames);
}
DeprecatedString ProcessStatisticsReader::username_from_uid(uid_t uid)