1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 12:54:58 +00:00

Kernel+Userland: Convert /proc/df to JSON.

This commit is contained in:
Andreas Kling 2019-07-01 18:54:02 +02:00
parent aaedc24f15
commit 54d7670fc3
2 changed files with 30 additions and 35 deletions

View file

@ -450,18 +450,19 @@ ByteBuffer procfs$mounts(InodeIdentifier)
ByteBuffer procfs$df(InodeIdentifier) ByteBuffer procfs$df(InodeIdentifier)
{ {
// FIXME: This is obviously racy against the VFS mounts changing. // FIXME: This is obviously racy against the VFS mounts changing.
StringBuilder builder; JsonArray json;
VFS::the().for_each_mount([&builder](auto& mount) { VFS::the().for_each_mount([&json](auto& mount) {
auto& fs = mount.guest_fs(); auto& fs = mount.guest_fs();
builder.appendf("%s,", fs.class_name()); JsonObject fs_object;
builder.appendf("%u,", fs.total_block_count()); fs_object.set("class_name", fs.class_name());
builder.appendf("%u,", fs.free_block_count()); fs_object.set("total_block_count", fs.total_block_count());
builder.appendf("%u,", fs.total_inode_count()); fs_object.set("free_block_count", fs.free_block_count());
builder.appendf("%u,", fs.free_inode_count()); fs_object.set("total_inode_count", fs.total_inode_count());
builder.append(mount.absolute_path()); fs_object.set("free_inode_count", fs.free_inode_count());
builder.append('\n'); fs_object.set("mount_point", mount.absolute_path());
json.append(fs_object);
}); });
return builder.to_byte_buffer(); return json.serialized().to_byte_buffer();
} }
ByteBuffer procfs$cpuinfo(InodeIdentifier) ByteBuffer procfs$cpuinfo(InodeIdentifier)

View file

@ -1,5 +1,8 @@
#include <AK/AKString.h> #include <AK/AKString.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/CFile.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -16,31 +19,23 @@ struct FileSystem {
int main(int, char**) int main(int, char**)
{ {
FILE* fp = fopen("/proc/df", "r"); CFile file("/proc/df");
if (!fp) { if (!file.open(CIODevice::ReadOnly)) {
perror("failed to open /proc/df"); fprintf(stderr, "Failed to open /proc/df: %s\n", file.error_string());
return 1; return 1;
} }
printf("Filesystem Blocks Used Available Mount point\n"); printf("Filesystem Blocks Used Available Mount point\n");
for (;;) {
char buf[4096]; auto file_contents = file.read_all();
char* ptr = fgets(buf, sizeof(buf), fp); auto json = JsonValue::from_string(file_contents).as_array();
if (!ptr) json.for_each([](auto& value) {
break; auto fs_object = value.as_object();
auto parts = String(buf, Chomp).split(','); auto fs = fs_object.get("class_name").to_string();
if (parts.size() < 6) auto total_block_count = fs_object.get("total_block_count").to_dword();
break; auto free_block_count = fs_object.get("free_block_count").to_dword();
bool ok; auto total_inode_count = fs_object.get("total_inode_count").to_dword();
String fs = parts[0]; auto free_inode_count = fs_object.get("free_inode_count").to_dword();
unsigned total_block_count = parts[1].to_uint(ok); auto mount_point = fs_object.get("mount_point").to_string();
ASSERT(ok);
unsigned free_block_count = parts[2].to_uint(ok);
ASSERT(ok);
unsigned total_inode_count = parts[3].to_uint(ok);
ASSERT(ok);
unsigned free_inode_count = parts[4].to_uint(ok);
ASSERT(ok);
String mount_point = parts[5];
(void)total_inode_count; (void)total_inode_count;
(void)free_inode_count; (void)free_inode_count;
@ -51,8 +46,7 @@ int main(int, char**)
printf("%10u ", free_block_count); printf("%10u ", free_block_count);
printf("%s", mount_point.characters()); printf("%s", mount_point.characters());
printf("\n"); printf("\n");
} });
int rc = fclose(fp);
ASSERT(rc == 0);
return 0; return 0;
} }