1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:48:11 +00:00

LibCore: Use Core::Stream for ProcessStatisticsReader

This commit is contained in:
Tim Schumacher 2022-12-08 14:50:31 +01:00 committed by Linus Groh
parent e338a0656d
commit 8940f2da7f
18 changed files with 55 additions and 95 deletions

View file

@ -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)