df.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <AK/JsonArray.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/NumberFormat.h>
  29. #include <AK/String.h>
  30. #include <AK/Vector.h>
  31. #include <LibCore/ArgsParser.h>
  32. #include <LibCore/File.h>
  33. #include <fcntl.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. static bool flag_human_readable = false;
  39. struct FileSystem {
  40. String fs;
  41. size_t total_block_count { 0 };
  42. size_t free_block_count { 0 };
  43. size_t total_inode_count { 0 };
  44. size_t free_inode_count { 0 };
  45. size_t block_size { 0 };
  46. String mount_point;
  47. };
  48. int main(int argc, char** argv)
  49. {
  50. Core::ArgsParser args_parser;
  51. args_parser.add_option(flag_human_readable, "Print human-readable sizes", "human-readable", 'h');
  52. args_parser.parse(argc, argv);
  53. auto file = Core::File::construct("/proc/df");
  54. if (!file->open(Core::IODevice::ReadOnly)) {
  55. fprintf(stderr, "Failed to open /proc/df: %s\n", file->error_string());
  56. return 1;
  57. }
  58. if (flag_human_readable) {
  59. printf("Filesystem Size Used Available Mount point\n");
  60. } else {
  61. printf("Filesystem Blocks Used Available Mount point\n");
  62. }
  63. auto file_contents = file->read_all();
  64. auto json_result = JsonValue::from_string(file_contents);
  65. ASSERT(json_result.has_value());
  66. auto json = json_result.value().as_array();
  67. json.for_each([](auto& value) {
  68. auto fs_object = value.as_object();
  69. auto fs = fs_object.get("class_name").to_string();
  70. auto total_block_count = fs_object.get("total_block_count").to_u32();
  71. auto free_block_count = fs_object.get("free_block_count").to_u32();
  72. auto total_inode_count = fs_object.get("total_inode_count").to_u32();
  73. auto free_inode_count = fs_object.get("free_inode_count").to_u32();
  74. auto block_size = fs_object.get("block_size").to_u32();
  75. auto mount_point = fs_object.get("mount_point").to_string();
  76. (void)total_inode_count;
  77. (void)free_inode_count;
  78. printf("%-10s", fs.characters());
  79. if (flag_human_readable) {
  80. printf("%10s ", human_readable_size(total_block_count * block_size).characters());
  81. printf("%10s ", human_readable_size((total_block_count - free_block_count) * block_size).characters());
  82. printf("%10s ", human_readable_size(free_block_count * block_size).characters());
  83. } else {
  84. printf("%10u ", total_block_count);
  85. printf("%10u ", total_block_count - free_block_count);
  86. printf("%10u ", free_block_count);
  87. }
  88. printf("%s", mount_point.characters());
  89. printf("\n");
  90. });
  91. return 0;
  92. }