summaryrefslogtreecommitdiff
path: root/Userland/Utilities/lsblk.cpp
blob: eaaa1b34b7b312f402191953f0994039d8f45b6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/DeprecatedString.h>
#include <AK/Hex.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}"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, "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_filename = DeprecatedString::formatted("/sys/devices/storage/{}/command_set", dir);
        auto command_set_file = Core::File::open(command_set_filename, Core::File::OpenMode::Read);
        if (command_set_file.is_error()) {
            dbgln("Error: Could not open {}: {}", command_set_filename, command_set_file.error());
            continue;
        }
        auto last_lba_filename = DeprecatedString::formatted("/sys/devices/storage/{}/last_lba", dir);
        auto last_lba_file = Core::File::open(last_lba_filename, Core::File::OpenMode::Read);
        if (last_lba_file.is_error()) {
            dbgln("Error: Could not open {}: {}", last_lba_filename, last_lba_file.error());
            continue;
        }
        auto sector_size_filename = DeprecatedString::formatted("/sys/devices/storage/{}/sector_size", dir);
        auto sector_size_file = Core::File::open(sector_size_filename, Core::File::OpenMode::Read);
        if (sector_size_file.is_error()) {
            dbgln("Error: Could not open {}: {}", sector_size_filename, sector_size_file.error());
            continue;
        }

        auto maybe_command_set = command_set_file.value()->read_until_eof();
        if (maybe_command_set.is_error()) {
            dbgln("Error: Could not read {}: {}", command_set_filename, maybe_command_set.error());
            continue;
        }
        DeprecatedString command_set = StringView(maybe_command_set.value().bytes());

        auto maybe_last_lba = last_lba_file.value()->read_until_eof();
        if (maybe_last_lba.is_error()) {
            dbgln("Error: Could not read {}: {}", last_lba_filename, maybe_last_lba.error());
            continue;
        }
        DeprecatedString last_lba = StringView(maybe_last_lba.value().bytes());

        auto maybe_sector_size = sector_size_file.value()->read_until_eof();
        if (maybe_sector_size.is_error()) {
            dbgln("Error: Could not read {}: {}", sector_size_filename, maybe_sector_size.error());
            continue;
        }
        DeprecatedString sector_size = StringView(maybe_sector_size.value().bytes());

        outln(format_row, dir, command_set, sector_size, last_lba);
    }

    return 0;
}