summaryrefslogtreecommitdiff
path: root/Kernel/FileSystem/InodeIdentifier.h
blob: caeba928539858918edd7fccede4f229259892ea (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteBuffer.h>
#include <AK/DistinctNumeric.h>
#include <AK/String.h>
#include <AK/Types.h>

namespace Kernel {

class FS;
struct InodeMetadata;

TYPEDEF_DISTINCT_ORDERED_ID(unsigned, InodeIndex);

class InodeIdentifier {
public:
    InodeIdentifier() = default;
    InodeIdentifier(u32 fsid, InodeIndex inode)
        : m_fsid(fsid)
        , m_index(inode)
    {
    }

    bool is_valid() const { return m_fsid != 0 && m_index != 0; }

    u32 fsid() const { return m_fsid; }
    InodeIndex index() const { return m_index; }

    FS* fs();
    const FS* fs() const;

    bool operator==(const InodeIdentifier& other) const
    {
        return m_fsid == other.m_fsid && m_index == other.m_index;
    }

    bool operator!=(const InodeIdentifier& other) const
    {
        return m_fsid != other.m_fsid || m_index != other.m_index;
    }

    String to_string() const { return String::formatted("{}:{}", m_fsid, m_index); }

private:
    u32 m_fsid { 0 };
    InodeIndex m_index { 0 };
};

}

template<>
struct AK::Formatter<Kernel::InodeIdentifier> : AK::Formatter<FormatString> {
    void format(FormatBuilder& builder, Kernel::InodeIdentifier value)
    {
        return AK::Formatter<FormatString>::format(builder, "{}:{}", value.fsid(), value.index());
    }
};

template<>
struct AK::Formatter<Kernel::InodeIndex> : AK::Formatter<FormatString> {
    void format(FormatBuilder& builder, Kernel::InodeIndex value)
    {
        return AK::Formatter<FormatString>::format(builder, "{}", value.value());
    }
};