mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
Userland: Introduce the lsblk utility to show list of storage devices
This commit is contained in:
parent
1dbd32488f
commit
a660040806
Notes:
sideshowbarker
2024-07-17 18:49:10 +09:00
Author: https://github.com/supercomputer7 Commit: https://github.com/SerenityOS/serenity/commit/a660040806 Pull-request: https://github.com/SerenityOS/serenity/pull/13779
3 changed files with 95 additions and 0 deletions
24
Base/usr/share/man/man8/lsblk.md
Normal file
24
Base/usr/share/man/man8/lsblk.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
## Name
|
||||
|
||||
lsblk - list connected storage devices
|
||||
|
||||
## Synopsis
|
||||
|
||||
```**sh
|
||||
$ lsblk
|
||||
```
|
||||
|
||||
## Description
|
||||
|
||||
lsblk is a utility for displaying information about storage devices in the system.
|
||||
It shows a brief list of these devices.
|
||||
|
||||
## Files
|
||||
|
||||
* `/sys/devices/storage` - source of the storage devices list.
|
||||
|
||||
## Examples
|
||||
|
||||
```sh
|
||||
$ lsblk
|
||||
```
|
|
@ -149,6 +149,7 @@ target_link_libraries(ls LibMain)
|
|||
target_link_libraries(lscpu LibMain)
|
||||
target_link_libraries(lsirq LibMain)
|
||||
target_link_libraries(lsof LibMain)
|
||||
target_link_libraries(lsblk LibMain)
|
||||
target_link_libraries(lspci LibPCIDB LibMain)
|
||||
target_link_libraries(lsusb LibUSBDB LibMain)
|
||||
target_link_libraries(man LibMarkdown LibMain)
|
||||
|
|
70
Userland/Utilities/lsblk.cpp
Normal file
70
Userland/Utilities/lsblk.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Hex.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
static constexpr StringView format_row = "{:10s}\t{:10s}\t{:10s}\t{:10s}\t{:10s}"sv;
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio rpath"));
|
||||
TRY(Core::System::unveil("/sys/devices/storage", "r"));
|
||||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.set_general_help("List Storage (Block) devices.");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
Core::DirIterator di("/sys/devices/storage/", Core::DirIterator::SkipParentAndBaseDir);
|
||||
if (di.has_error()) {
|
||||
warnln("Failed to open /sys/devices/storage - {}", di.error());
|
||||
return 1;
|
||||
}
|
||||
|
||||
outln(format_row, "LUN"sv, "Interface type"sv, "Command set"sv, "Block Size"sv, "Last LBA"sv);
|
||||
|
||||
TRY(Core::System::pledge("stdio rpath"));
|
||||
|
||||
while (di.has_next()) {
|
||||
auto dir = di.next_path();
|
||||
auto command_set_file = Core::File::construct(String::formatted("/sys/devices/storage/{}/command_set", dir));
|
||||
if (!command_set_file->open(Core::OpenMode::ReadOnly)) {
|
||||
dbgln("Error: Could not open {}: {}", command_set_file->name(), command_set_file->error_string());
|
||||
continue;
|
||||
}
|
||||
auto interface_type_file = Core::File::construct(String::formatted("/sys/devices/storage/{}/interface_type", dir));
|
||||
if (!interface_type_file->open(Core::OpenMode::ReadOnly)) {
|
||||
dbgln("Error: Could not open {}: {}", interface_type_file->name(), interface_type_file->error_string());
|
||||
continue;
|
||||
}
|
||||
auto last_lba_file = Core::File::construct(String::formatted("/sys/devices/storage/{}/last_lba", dir));
|
||||
if (!last_lba_file->open(Core::OpenMode::ReadOnly)) {
|
||||
dbgln("Error: Could not open {}: {}", last_lba_file->name(), last_lba_file->error_string());
|
||||
continue;
|
||||
}
|
||||
auto sector_size_file = Core::File::construct(String::formatted("/sys/devices/storage/{}/sector_size", dir));
|
||||
if (!sector_size_file->open(Core::OpenMode::ReadOnly)) {
|
||||
dbgln("Error: Could not open {}: {}", sector_size_file->name(), sector_size_file->error_string());
|
||||
continue;
|
||||
}
|
||||
|
||||
String command_set = StringView(command_set_file->read_all().bytes());
|
||||
String interface_type = StringView(interface_type_file->read_all().bytes());
|
||||
String last_lba = StringView(last_lba_file->read_all().bytes());
|
||||
String sector_size = StringView(sector_size_file->read_all().bytes());
|
||||
|
||||
outln(format_row, dir, interface_type, command_set, sector_size, last_lba);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue