2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-01 16:54:02 +00:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
2020-08-15 18:05:46 +00:00
|
|
|
#include <AK/NumberFormat.h>
|
2020-03-31 06:52:31 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2021-12-07 19:13:45 +00:00
|
|
|
#include <LibMain/Main.h>
|
2021-03-17 17:20:11 +00:00
|
|
|
#include <inttypes.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <stdlib.h>
|
2019-02-21 13:48:00 +00:00
|
|
|
|
|
|
|
struct FileSystem {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString fs;
|
2019-02-21 13:48:00 +00:00
|
|
|
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 };
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString mount_point;
|
2019-02-21 13:48:00 +00:00
|
|
|
};
|
|
|
|
|
2021-12-07 19:13:45 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-02-21 13:48:00 +00:00
|
|
|
{
|
2022-12-10 07:48:28 +00:00
|
|
|
bool flag_human_readable = false;
|
2023-01-10 12:07:17 +00:00
|
|
|
bool flag_human_readable_si = false;
|
2022-12-10 07:48:28 +00:00
|
|
|
bool flag_inode_info = false;
|
|
|
|
|
2020-03-31 06:52:31 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00: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');
|
2023-01-10 12:07:17 +00:00
|
|
|
args_parser.add_option(flag_human_readable_si, "Print human-readable sizes in SI units", "si", 'H');
|
2022-12-10 07:48:28 +00:00
|
|
|
args_parser.add_option(flag_inode_info, "Show inode information as well", "inodes", 'i');
|
2021-12-07 19:13:45 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-03-31 06:52:31 +00:00
|
|
|
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = TRY(Core::File::open("/sys/kernel/df"sv, Core::File::OpenMode::Read));
|
2020-03-31 06:52:31 +00:00
|
|
|
|
2022-12-10 07:48:28 +00:00
|
|
|
Vector<StringView> headers;
|
|
|
|
TRY(headers.try_append(flag_human_readable ? "Size"sv : "Blocks"sv));
|
|
|
|
TRY(headers.try_append("Used"sv));
|
|
|
|
TRY(headers.try_append("Available"sv));
|
|
|
|
TRY(headers.try_append("Used%"sv));
|
|
|
|
if (flag_inode_info) {
|
|
|
|
TRY(headers.try_append("Inodes"sv));
|
|
|
|
TRY(headers.try_append("IUsed"sv));
|
|
|
|
TRY(headers.try_append("IAvailable"sv));
|
|
|
|
TRY(headers.try_append("IUsed%"sv));
|
2020-03-31 06:52:31 +00:00
|
|
|
}
|
2022-12-10 07:48:28 +00:00
|
|
|
TRY(headers.try_append("Mount point"sv));
|
|
|
|
|
|
|
|
out("{:12} ", "Filesystem");
|
|
|
|
|
|
|
|
for (auto& header : headers)
|
|
|
|
out("{:>12} ", header);
|
|
|
|
outln();
|
2019-07-01 16:54:02 +00:00
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto file_contents = TRY(file->read_until_eof());
|
2021-12-07 19:13:45 +00:00
|
|
|
auto json_result = TRY(JsonValue::from_string(file_contents));
|
2021-11-15 00:46:51 +00:00
|
|
|
auto const& json = json_result.as_array();
|
2022-12-10 07:48:28 +00:00
|
|
|
json.for_each([&](auto& value) {
|
2021-05-31 16:59:02 +00:00
|
|
|
auto& fs_object = value.as_object();
|
2022-12-22 14:27:36 +00:00
|
|
|
auto fs = fs_object.get_deprecated_string("class_name"sv).value_or({});
|
|
|
|
auto total_block_count = fs_object.get_u64("total_block_count"sv).value_or(0);
|
|
|
|
auto free_block_count = fs_object.get_u64("free_block_count"sv).value_or(0);
|
2022-12-09 19:25:10 +00:00
|
|
|
auto used_block_count = total_block_count - free_block_count;
|
2022-12-22 14:27:36 +00:00
|
|
|
auto total_inode_count = fs_object.get_u64("total_inode_count"sv).value_or(0);
|
|
|
|
auto free_inode_count = fs_object.get_u64("free_inode_count"sv).value_or(0);
|
2022-12-10 07:48:28 +00:00
|
|
|
auto used_inode_count = total_inode_count - free_inode_count;
|
2022-12-22 14:27:36 +00:00
|
|
|
auto block_size = fs_object.get_u64("block_size"sv).value_or(0);
|
|
|
|
auto mount_point = fs_object.get_deprecated_string("mount_point"sv).value_or({});
|
2019-02-21 13:48:00 +00:00
|
|
|
|
2022-12-09 19:25:10 +00:00
|
|
|
auto used_percentage = 100;
|
|
|
|
if (total_block_count != 0)
|
|
|
|
used_percentage = (used_block_count * 100) / total_block_count;
|
|
|
|
|
2022-12-10 07:48:28 +00:00
|
|
|
auto used_inode_percentage = 100;
|
|
|
|
if (total_inode_count != 0)
|
|
|
|
used_inode_percentage = (used_inode_count * 100) / total_inode_count;
|
|
|
|
|
|
|
|
out("{:12} ", fs);
|
2020-03-31 06:52:31 +00:00
|
|
|
|
2023-01-10 12:07:17 +00:00
|
|
|
bool human_readable = flag_human_readable || flag_human_readable_si;
|
|
|
|
auto human_readable_based_on = flag_human_readable_si ? AK::HumanReadableBasedOn::Base10 : AK::HumanReadableBasedOn::Base2;
|
|
|
|
|
|
|
|
if (human_readable) {
|
|
|
|
out("{:>12} ", human_readable_size(total_block_count * block_size, human_readable_based_on));
|
|
|
|
out("{:>12} ", human_readable_size(used_block_count * block_size, human_readable_based_on));
|
|
|
|
out("{:>12} ", human_readable_size(free_block_count * block_size, human_readable_based_on));
|
2022-12-10 07:48:28 +00:00
|
|
|
out("{:>11}% ", used_percentage);
|
2020-03-31 06:52:31 +00:00
|
|
|
} else {
|
2022-12-10 07:48:28 +00:00
|
|
|
out("{:>12} ", (uint64_t)total_block_count);
|
|
|
|
out("{:>12} ", (uint64_t)used_block_count);
|
|
|
|
out("{:>12} ", (uint64_t)free_block_count);
|
|
|
|
out("{:>11}% ", used_percentage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag_inode_info) {
|
2023-01-10 12:07:17 +00:00
|
|
|
if (human_readable) {
|
|
|
|
out("{:>12} ", human_readable_quantity(total_inode_count, human_readable_based_on));
|
|
|
|
out("{:>12} ", human_readable_quantity(used_inode_count, human_readable_based_on));
|
|
|
|
out("{:>12} ", human_readable_quantity(free_inode_count, human_readable_based_on));
|
2022-12-10 07:48:28 +00:00
|
|
|
out("{:>11}% ", used_inode_percentage);
|
|
|
|
} else {
|
|
|
|
out("{:>12} ", (uint64_t)total_inode_count);
|
|
|
|
out("{:>12} ", (uint64_t)used_inode_count);
|
|
|
|
out("{:>12} ", (uint64_t)free_inode_count);
|
|
|
|
out("{:>11}% ", used_inode_percentage);
|
|
|
|
}
|
2020-03-31 06:52:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
out("{}", mount_point);
|
|
|
|
outln();
|
2019-07-01 16:54:02 +00:00
|
|
|
});
|
|
|
|
|
2019-02-21 13:48:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|