Filesystem.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <iterator>
  5. #include <memory>
  6. #include <optional>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10. #include "Common/CommonTypes.h"
  11. #include "DiscIO/Volume.h"
  12. namespace DiscIO
  13. {
  14. // file info of an FST entry
  15. class FileInfo
  16. {
  17. friend class const_iterator;
  18. public:
  19. class const_iterator final
  20. {
  21. public:
  22. using iterator_category = std::forward_iterator_tag;
  23. using value_type = const FileInfo;
  24. using difference_type = std::ptrdiff_t;
  25. using pointer = value_type*;
  26. using reference = value_type&;
  27. const_iterator() : m_file_info(nullptr) {}
  28. const_iterator(std::unique_ptr<FileInfo> file_info) : m_file_info(std::move(file_info)) {}
  29. const_iterator(const const_iterator& it) : m_file_info(it.m_file_info->clone()) {}
  30. const_iterator(const_iterator&& it) noexcept : m_file_info(std::move(it.m_file_info)) {}
  31. ~const_iterator() = default;
  32. const_iterator& operator=(const const_iterator& it)
  33. {
  34. m_file_info = it.m_file_info ? it.m_file_info->clone() : nullptr;
  35. return *this;
  36. }
  37. const_iterator& operator=(const_iterator&& it) noexcept
  38. {
  39. m_file_info = std::move(it.m_file_info);
  40. return *this;
  41. }
  42. const_iterator& operator++()
  43. {
  44. ++*m_file_info;
  45. return *this;
  46. }
  47. const_iterator operator++(int)
  48. {
  49. const_iterator old = *this;
  50. ++*m_file_info;
  51. return old;
  52. }
  53. bool operator==(const const_iterator& it) const
  54. {
  55. return m_file_info ? (it.m_file_info && *m_file_info == *it.m_file_info) : (!it.m_file_info);
  56. }
  57. // Incrementing or destroying an iterator will invalidate its returned references and
  58. // pointers, but will not invalidate copies of the iterator or file info object.
  59. const FileInfo& operator*() const { return *m_file_info.get(); }
  60. const FileInfo* operator->() const { return m_file_info.get(); }
  61. private:
  62. std::unique_ptr<FileInfo> m_file_info;
  63. };
  64. virtual ~FileInfo();
  65. bool operator==(const FileInfo& other) const { return GetAddress() == other.GetAddress(); }
  66. virtual std::unique_ptr<FileInfo> clone() const = 0;
  67. virtual const_iterator cbegin() const { return begin(); }
  68. virtual const_iterator cend() const { return end(); }
  69. virtual const_iterator begin() const = 0;
  70. virtual const_iterator end() const = 0;
  71. // The offset of a file on the disc (inside the partition, if there is one).
  72. // Not guaranteed to return a meaningful value for directories.
  73. virtual u64 GetOffset() const = 0;
  74. // The size of a file.
  75. // Not guaranteed to return a meaningful value for directories.
  76. virtual u32 GetSize() const = 0;
  77. // For a file, returns its size. For a directory, returns the total size of its contents.
  78. u64 GetTotalSize() const;
  79. virtual bool IsRoot() const = 0;
  80. virtual bool IsDirectory() const = 0;
  81. // The number of files and directories in a directory, including those in subdirectories.
  82. // Not guaranteed to return a meaningful value for files.
  83. virtual u32 GetTotalChildren() const = 0;
  84. virtual std::string GetName() const = 0;
  85. virtual bool NameCaseInsensitiveEquals(std::string_view other) const = 0;
  86. // GetPath will find the parents of the current object and call GetName on them,
  87. // so it's slower than other functions. If you're traversing through folders
  88. // to get a file and its path, building the path while traversing is faster.
  89. virtual std::string GetPath() const = 0;
  90. protected:
  91. // Only used for comparisons with other file info objects
  92. virtual uintptr_t GetAddress() const = 0;
  93. // Called by iterators
  94. virtual FileInfo& operator++() = 0;
  95. };
  96. class FileSystem
  97. {
  98. public:
  99. virtual ~FileSystem();
  100. // If IsValid is false, GetRoot must not be called.
  101. virtual bool IsValid() const = 0;
  102. // The object returned by GetRoot and all objects created from it
  103. // are only valid for as long as the file system object is valid.
  104. virtual const FileInfo& GetRoot() const = 0;
  105. // Returns nullptr if not found
  106. virtual std::unique_ptr<FileInfo> FindFileInfo(std::string_view path) const = 0;
  107. // Returns nullptr if not found
  108. virtual std::unique_ptr<FileInfo> FindFileInfo(u64 disc_offset) const = 0;
  109. };
  110. // Calling Volume::GetFileSystem instead of manually constructing a filesystem is recommended,
  111. // because it will check IsValid for you, will automatically pick the right type of filesystem,
  112. // and will cache the filesystem in case it's needed again later.
  113. } // namespace DiscIO