InodeIdentifier.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/DistinctNumeric.h>
  9. #include <AK/String.h>
  10. #include <AK/Types.h>
  11. namespace Kernel {
  12. class FileSystem;
  13. struct InodeMetadata;
  14. TYPEDEF_DISTINCT_ORDERED_ID(u64, InodeIndex);
  15. class InodeIdentifier {
  16. public:
  17. InodeIdentifier() = default;
  18. InodeIdentifier(u32 fsid, InodeIndex inode)
  19. : m_fsid(fsid)
  20. , m_index(inode)
  21. {
  22. }
  23. bool is_valid() const { return m_fsid != 0 && m_index != 0; }
  24. u32 fsid() const { return m_fsid; }
  25. InodeIndex index() const { return m_index; }
  26. FileSystem* fs();
  27. const FileSystem* fs() const;
  28. bool operator==(const InodeIdentifier& other) const
  29. {
  30. return m_fsid == other.m_fsid && m_index == other.m_index;
  31. }
  32. bool operator!=(const InodeIdentifier& other) const
  33. {
  34. return m_fsid != other.m_fsid || m_index != other.m_index;
  35. }
  36. String to_string() const { return String::formatted("{}:{}", m_fsid, m_index); }
  37. private:
  38. u32 m_fsid { 0 };
  39. InodeIndex m_index { 0 };
  40. };
  41. }
  42. template<>
  43. struct AK::Formatter<Kernel::InodeIdentifier> : AK::Formatter<FormatString> {
  44. void format(FormatBuilder& builder, Kernel::InodeIdentifier value)
  45. {
  46. return AK::Formatter<FormatString>::format(builder, "{}:{}", value.fsid(), value.index());
  47. }
  48. };
  49. template<>
  50. struct AK::Formatter<Kernel::InodeIndex> : AK::Formatter<FormatString> {
  51. void format(FormatBuilder& builder, Kernel::InodeIndex value)
  52. {
  53. return AK::Formatter<FormatString>::format(builder, "{}", value.value());
  54. }
  55. };