From cf2b28beca6c91ecad49f0c76ddc06c76681d96f Mon Sep 17 00:00:00 2001 From: chatlanin Date: Wed, 19 Apr 2023 21:04:10 +0300 Subject: [PATCH] initial commit --- .gitignore | 2 ++ bin/main.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ bin/meson.build | 6 +++++ meson.build | 37 +++++++++++++++++++++++++++++++ props.json | 2 ++ run | 13 +++++++++++ 6 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 bin/main.cpp create mode 100644 bin/meson.build create mode 100644 meson.build create mode 100644 props.json create mode 100755 run diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9785597 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +.cache diff --git a/bin/main.cpp b/bin/main.cpp new file mode 100644 index 0000000..3f20491 --- /dev/null +++ b/bin/main.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include + +template +void print_in_color(const T& text, const std::string& color) +{ + std::cout << "\033[" << color << "m" << text << "\033[0m"; +} + +template +std::string get_size_string(const T& size) { + if (size < 1024) { + return std::to_string(size) + " B"; + } else if (size < 1024 * 1024) { + return std::to_string(size / 1024) + " KB"; + } else if (size < 1024 * 1024 * 1024) { + return std::to_string(size / 1024 / 1024) + " MB"; + } else { + return std::to_string(size / 1024 / 1024 / 1024) + " GB"; + } +} + + +auto main() -> int +{ + std::ifstream fs("/etc/mtab"); + std::FILE* file = std::fopen("/etc/fstab", "r"); + struct mntent* mount_entry = nullptr; + + std::cout << "\n\n"; + while ((mount_entry = getmntent(file))) + { + struct statvfs stat; + if (statvfs(mount_entry->mnt_dir, &stat) != 0) { + std::cerr << "Ошибка при получении информации о файловой системе: " << mount_entry->mnt_dir << "\n"; + continue; + } + + unsigned long long total_size = (unsigned long long) stat.f_frsize * stat.f_blocks; + unsigned long long free_size = (unsigned long long) stat.f_frsize * stat.f_bfree; + unsigned long long used_size = total_size - free_size; + + std::cout << "Точка монтирования: " << mount_entry->mnt_dir << "\n"; + std::cout << "Тип файловой системы: " << mount_entry->mnt_type << "\n"; + std::cout << "Общий объем: "; + print_in_color(get_size_string(total_size), "0;33"); + std::cout << "\nСвободное место: "; + print_in_color(get_size_string(free_size), "0;32"); + std::cout << "\nИспользовано: "; + print_in_color(get_size_string(used_size), "0;35"); + std::cout << "\n\n"; + } + + std::fclose(file); + fs.close(); +} diff --git a/bin/meson.build b/bin/meson.build new file mode 100644 index 0000000..43a6d81 --- /dev/null +++ b/bin/meson.build @@ -0,0 +1,6 @@ +executable( + 'disk-info', + 'main.cpp', + dependencies : deps, + cpp_args: args +) diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..d1fc5fa --- /dev/null +++ b/meson.build @@ -0,0 +1,37 @@ +project( + 'disk-info', + 'cpp', + version : run_command('jq', '-r', '.version', join_paths(meson.source_root(), 'props.json'), check: true).stdout().strip(), + default_options : [ + 'warning_level=1', + 'optimization=3', + 'default_library=static', + 'cpp_std=c++20', +]) + +add_project_arguments ( + '-Wpedantic', + '-Wno-shadow', + '-Wno-unused-but-set-variable', + '-Wno-comment', + '-Wno-unused-parameter', + '-Wno-unused-value', + '-Wno-missing-field-initializers', + '-Wno-narrowing', + '-Wno-deprecated-enum-enum-conversion', + '-Wno-volatile', + '-Wno-deprecated-declarations', + language: 'cpp' +) + +############################################################# + +args = [] +deps = [] +inc = [] + +############################################################# + +#subdir('src') +subdir('bin') +#subdir('tests') diff --git a/props.json b/props.json new file mode 100644 index 0000000..4342cdc --- /dev/null +++ b/props.json @@ -0,0 +1,2 @@ +{"version": "0.1.0"} + diff --git a/run b/run new file mode 100755 index 0000000..4de5442 --- /dev/null +++ b/run @@ -0,0 +1,13 @@ +#!/bin/zsh + +PROJECT_NAME="disk-info" + +command meson compile -C build + +if [[ -z "$1" ]]; then + cd build + ./bin/$PROJECT_NAME + cd .. +else + meson test $1 -C build +fi