Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/String.h>
28#include <AK/JsonArray.h>
29#include <AK/JsonObject.h>
30#include <AK/Vector.h>
31#include <LibCore/ArgsParser.h>
32#include <LibCore/File.h>
33#include <cstring>
34#include <fcntl.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <unistd.h>
38
39static bool flag_human_readable = false;
40
41struct FileSystem {
42 String fs;
43 size_t total_block_count { 0 };
44 size_t free_block_count { 0 };
45 size_t total_inode_count { 0 };
46 size_t free_inode_count { 0 };
47 size_t block_size { 0 };
48 String mount_point;
49};
50
51// FIXME: Remove this hackery once printf() supports floats.
52// FIXME: Also, we should probably round the sizes in df -h output.
53static String number_string_with_one_decimal(float number, const char* suffix)
54{
55 float decimals = number - (int)number;
56 return String::format("%d.%d%s", (int)number, (int)(decimals * 10), suffix);
57}
58
59static String human_readable_size(size_t size)
60{
61 if (size < 1 * KB)
62 return String::number(size);
63 if (size < 1 * MB)
64 return number_string_with_one_decimal((float)size / (float)KB, "K");
65 if (size < 1 * GB)
66 return number_string_with_one_decimal((float)size / (float)MB, "M");
67 return number_string_with_one_decimal((float)size / (float)GB, "G");
68}
69
70int main(int argc, char** argv)
71{
72 Core::ArgsParser args_parser;
73 args_parser.add_option(flag_human_readable, "Print human-readable sizes", "human-readable", 'h');
74 args_parser.parse(argc, argv);
75
76 auto file = Core::File::construct("/proc/df");
77 if (!file->open(Core::IODevice::ReadOnly)) {
78 fprintf(stderr, "Failed to open /proc/df: %s\n", file->error_string());
79 return 1;
80 }
81
82 if (flag_human_readable) {
83 printf("Filesystem Size Used Available Mount point\n");
84 } else {
85 printf("Filesystem Blocks Used Available Mount point\n");
86 }
87
88 auto file_contents = file->read_all();
89 auto json = JsonValue::from_string(file_contents).as_array();
90 json.for_each([](auto& value) {
91 auto fs_object = value.as_object();
92 auto fs = fs_object.get("class_name").to_string();
93 auto total_block_count = fs_object.get("total_block_count").to_u32();
94 auto free_block_count = fs_object.get("free_block_count").to_u32();
95 auto total_inode_count = fs_object.get("total_inode_count").to_u32();
96 auto free_inode_count = fs_object.get("free_inode_count").to_u32();
97 auto block_size = fs_object.get("block_size").to_u32();
98 auto mount_point = fs_object.get("mount_point").to_string();
99
100 (void)total_inode_count;
101 (void)free_inode_count;
102
103 printf("%-10s", fs.characters());
104
105 if (flag_human_readable) {
106 printf("%10s ", human_readable_size(total_block_count * block_size).characters());
107 printf("%10s ", human_readable_size((total_block_count - free_block_count) * block_size).characters());
108 printf("%10s ", human_readable_size(free_block_count * block_size).characters());
109 } else {
110 printf("%10u ", total_block_count);
111 printf("%10u ", total_block_count - free_block_count);
112 printf("%10u ", free_block_count);
113 }
114
115 printf("%s", mount_point.characters());
116 printf("\n");
117 });
118
119 return 0;
120}