1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +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

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