2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-05-15 13:34:40 +03:00
|
|
|
#include <AK/Assertions.h>
|
2020-11-15 15:11:21 +03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-07-01 19:54:02 +03:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
2020-08-15 21:05:46 +03:00
|
|
|
#include <AK/NumberFormat.h>
|
|
|
|
#include <AK/String.h>
|
2020-03-31 09:52:31 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-03-17 20:20:11 +03:00
|
|
|
#include <inttypes.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <stdlib.h>
|
2019-02-21 16:48:00 +03:00
|
|
|
|
2020-03-31 09:52:31 +03:00
|
|
|
static bool flag_human_readable = false;
|
|
|
|
|
2019-02-21 16:48:00 +03: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 09:52:31 +03:00
|
|
|
size_t block_size { 0 };
|
2019-02-21 16:48:00 +03:00
|
|
|
String mount_point;
|
|
|
|
};
|
|
|
|
|
2020-03-31 09:52:31 +03:00
|
|
|
int main(int argc, char** argv)
|
2019-02-21 16:48:00 +03:00
|
|
|
{
|
2020-03-31 09:52:31 +03:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 18:22:58 +03:00
|
|
|
args_parser.set_general_help("Display free disk space of each partition.");
|
2020-03-31 09:52:31 +03:00
|
|
|
args_parser.add_option(flag_human_readable, "Print human-readable sizes", "human-readable", 'h');
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
auto file = Core::File::construct("/proc/df");
|
2021-05-12 12:26:43 +03:00
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("Failed to open {}: {}", file->name(), file->error_string());
|
2019-02-21 16:48:00 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-31 09:52:31 +03:00
|
|
|
|
|
|
|
if (flag_human_readable) {
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("Filesystem Size Used Available Mount point");
|
2020-03-31 09:52:31 +03:00
|
|
|
} else {
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("Filesystem Blocks Used Available Mount point");
|
2020-03-31 09:52:31 +03:00
|
|
|
}
|
2019-07-01 19:54:02 +03:00
|
|
|
|
2019-09-21 21:50:06 +03:00
|
|
|
auto file_contents = file->read_all();
|
2020-06-11 07:40:27 +03:00
|
|
|
auto json_result = JsonValue::from_string(file_contents);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(json_result.has_value());
|
2020-06-11 07:40:27 +03:00
|
|
|
auto json = json_result.value().as_array();
|
2019-07-01 19:54:02 +03:00
|
|
|
json.for_each([](auto& value) {
|
2021-05-31 19:59:02 +03:00
|
|
|
auto& fs_object = value.as_object();
|
2019-07-01 19:54:02 +03:00
|
|
|
auto fs = fs_object.get("class_name").to_string();
|
2021-03-17 20:20:11 +03:00
|
|
|
auto total_block_count = fs_object.get("total_block_count").to_u64();
|
|
|
|
auto free_block_count = fs_object.get("free_block_count").to_u64();
|
|
|
|
[[maybe_unused]] auto total_inode_count = fs_object.get("total_inode_count").to_u64();
|
|
|
|
[[maybe_unused]] auto free_inode_count = fs_object.get("free_inode_count").to_u64();
|
|
|
|
auto block_size = fs_object.get("block_size").to_u64();
|
2019-07-01 19:54:02 +03:00
|
|
|
auto mount_point = fs_object.get("mount_point").to_string();
|
2019-02-21 16:48:00 +03:00
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{:10}", fs);
|
2020-03-31 09:52:31 +03:00
|
|
|
|
|
|
|
if (flag_human_readable) {
|
2021-05-31 17:43:25 +03: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 09:52:31 +03:00
|
|
|
} else {
|
2021-05-31 17:43:25 +03: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 09:52:31 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{}", mount_point);
|
|
|
|
outln();
|
2019-07-01 19:54:02 +03:00
|
|
|
});
|
|
|
|
|
2019-02-21 16:48:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|