2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-07-01 18:54:02 +02:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
2020-08-15 14:05:46 -04:00
|
|
|
#include <AK/NumberFormat.h>
|
|
|
|
#include <AK/String.h>
|
2020-03-31 06:52:31 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2021-12-07 20:13:45 +01:00
|
|
|
#include <LibMain/Main.h>
|
2021-03-17 18:20:11 +01:00
|
|
|
#include <inttypes.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <stdlib.h>
|
2019-02-21 14:48:00 +01:00
|
|
|
|
2020-03-31 06:52:31 +00:00
|
|
|
static bool flag_human_readable = false;
|
|
|
|
|
2019-02-21 14:48:00 +01:00
|
|
|
struct FileSystem {
|
|
|
|
String fs;
|
|
|
|
size_t total_block_count { 0 };
|
|
|
|
size_t free_block_count { 0 };
|
|
|
|
size_t total_inode_count { 0 };
|
|
|
|
size_t free_inode_count { 0 };
|
2020-03-31 06:52:31 +00:00
|
|
|
size_t block_size { 0 };
|
2019-02-21 14:48:00 +01:00
|
|
|
String mount_point;
|
|
|
|
};
|
|
|
|
|
2021-12-07 20:13:45 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-02-21 14:48:00 +01:00
|
|
|
{
|
2020-03-31 06:52:31 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 16:22:58 +01:00
|
|
|
args_parser.set_general_help("Display free disk space of each partition.");
|
2020-03-31 06:52:31 +00:00
|
|
|
args_parser.add_option(flag_human_readable, "Print human-readable sizes", "human-readable", 'h');
|
2021-12-07 20:13:45 +01:00
|
|
|
args_parser.parse(arguments);
|
2020-03-31 06:52:31 +00:00
|
|
|
|
2022-10-14 21:56:19 +03:00
|
|
|
auto file = TRY(Core::File::open("/sys/kernel/df", Core::OpenMode::ReadOnly));
|
2020-03-31 06:52:31 +00:00
|
|
|
|
|
|
|
if (flag_human_readable) {
|
2021-05-31 15:43:25 +01:00
|
|
|
outln("Filesystem Size Used Available Mount point");
|
2020-03-31 06:52:31 +00:00
|
|
|
} else {
|
2021-05-31 15:43:25 +01:00
|
|
|
outln("Filesystem Blocks Used Available Mount point");
|
2020-03-31 06:52:31 +00:00
|
|
|
}
|
2019-07-01 18:54:02 +02:00
|
|
|
|
2019-09-21 20:50:06 +02:00
|
|
|
auto file_contents = file->read_all();
|
2021-12-07 20:13:45 +01:00
|
|
|
auto json_result = TRY(JsonValue::from_string(file_contents));
|
2021-11-15 01:46:51 +01:00
|
|
|
auto const& json = json_result.as_array();
|
2019-07-01 18:54:02 +02:00
|
|
|
json.for_each([](auto& value) {
|
2021-05-31 17:59:02 +01:00
|
|
|
auto& fs_object = value.as_object();
|
2022-07-11 17:32:29 +00:00
|
|
|
auto fs = fs_object.get("class_name"sv).to_string();
|
|
|
|
auto total_block_count = fs_object.get("total_block_count"sv).to_u64();
|
|
|
|
auto free_block_count = fs_object.get("free_block_count"sv).to_u64();
|
|
|
|
[[maybe_unused]] auto total_inode_count = fs_object.get("total_inode_count"sv).to_u64();
|
|
|
|
[[maybe_unused]] auto free_inode_count = fs_object.get("free_inode_count"sv).to_u64();
|
|
|
|
auto block_size = fs_object.get("block_size"sv).to_u64();
|
|
|
|
auto mount_point = fs_object.get("mount_point"sv).to_string();
|
2019-02-21 14:48:00 +01:00
|
|
|
|
2021-05-31 15:43:25 +01:00
|
|
|
out("{:10}", fs);
|
2020-03-31 06:52:31 +00:00
|
|
|
|
|
|
|
if (flag_human_readable) {
|
2021-05-31 15:43:25 +01:00
|
|
|
out("{:>10} ", human_readable_size(total_block_count * block_size));
|
|
|
|
out("{:>10} ", human_readable_size((total_block_count - free_block_count) * block_size));
|
|
|
|
out("{:>10} ", human_readable_size(free_block_count * block_size));
|
2020-03-31 06:52:31 +00:00
|
|
|
} else {
|
2021-05-31 15:43:25 +01:00
|
|
|
out("{:>10} ", (uint64_t)total_block_count);
|
|
|
|
out("{:>10} ", (uint64_t)(total_block_count - free_block_count));
|
|
|
|
out("{:>10} ", (uint64_t)free_block_count);
|
2020-03-31 06:52:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 15:43:25 +01:00
|
|
|
out("{}", mount_point);
|
|
|
|
outln();
|
2019-07-01 18:54:02 +02:00
|
|
|
});
|
|
|
|
|
2019-02-21 14:48:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|