add error exception
This commit is contained in:
parent
dea68d7b33
commit
a7ec673c10
60
bin/main.cpp
60
bin/main.cpp
@ -33,34 +33,42 @@ std::string get_size_string(const T& size)
|
|||||||
|
|
||||||
auto main() -> int
|
auto main() -> int
|
||||||
{
|
{
|
||||||
std::ifstream fs("/etc/mtab");
|
try
|
||||||
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;
|
std::FILE* file = std::fopen("/etc/fstab", "r");
|
||||||
if (statvfs(mount_entry->mnt_dir, &stat) != 0) {
|
if (file == nullptr)
|
||||||
std::cerr << "Ошибка при получении информации о файловой системе: " << mount_entry->mnt_dir << "\n";
|
throw std::runtime_error("File not found");
|
||||||
continue;
|
|
||||||
|
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";
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long long total_size = (unsigned long long)stat.f_frsize * stat.f_blocks;
|
std::fclose(file);
|
||||||
unsigned long long free_size = (unsigned long long)stat.f_frsize * stat.f_bfree;
|
}
|
||||||
unsigned long long used_size = total_size - free_size;
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
std::cout << "Точка монтирования: " << mount_entry->mnt_dir << "\n";
|
std::cerr << e.what() << std::endl;
|
||||||
std::cout << "Тип файловой системы: " << mount_entry->mnt_type << "\n";
|
return 1;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user