~aleteoryx/muditaos

ref: e35fca8524dfe78c3076073d20bedad897e876a7 muditaos/module-vfs/include/internal/purefs/blkdev/disk_handle.hpp -rw-r--r-- 2.3 KiB
e35fca85 — Lefucjusz [MOS-1064] Fix no input language selected for French/Spanish 1 year, 9 months ago
                                                                                
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <memory>
#include <string>
#include <limits>
#include <purefs/blkdev/defs.hpp>

namespace purefs::blkdev
{
    class disk;
}

namespace purefs::blkdev::internal
{

    class disk_handle
    {
        static constexpr part_t no_partition     = -1;
        static constexpr auto hw_partition_first = std::numeric_limits<part_t>::max() / 2;

      public:
        static constexpr auto to_syspart_num(part_t part) -> part_t
        {
            return part + hw_partition_first;
        }
        static auto is_any_partition(part_t part) noexcept -> bool
        {
            return part >= 0;
        }
        static auto is_system_partition(part_t part) noexcept -> bool
        {
            return part >= hw_partition_first;
        }
        static auto is_user_partition(part_t part) noexcept -> bool
        {
            return is_any_partition(part) && !is_system_partition(part);
        }
        explicit disk_handle(std::weak_ptr<blkdev::disk> disk, std::string_view name, part_t partition = no_partition)
            : m_disk(disk), m_partition(partition), m_name(name)
        {}
        auto is_any_partition() const noexcept -> bool
        {
            return is_any_partition(m_partition);
        }
        auto is_system_partition() const noexcept -> bool
        {
            return is_system_partition(m_partition);
        }
        auto is_user_partition() const noexcept -> bool
        {
            return is_user_partition(m_partition);
        }
        auto disk() const noexcept
        {
            return m_disk.lock();
        }
        auto partition() const noexcept
        {
            return m_partition;
        }
        auto system_partition() const noexcept -> int
        {
            return (m_partition >= hw_partition_first) ? (m_partition - hw_partition_first + 1) : (0);
        }
        auto sectors() const noexcept -> sector_t;
        auto name() const noexcept
        {
            return m_name;
        }

      private:
        const std::weak_ptr<blkdev::disk> m_disk;
        const part_t m_partition{no_partition};
        mutable sector_t m_sectors{0};
        const std::string m_name;
    };
} // namespace purefs::blkdev::internal