disk_archive.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. #include "common/file_util.h"
  11. #include "core/file_sys/archive_backend.h"
  12. #include "core/file_sys/directory_backend.h"
  13. #include "core/file_sys/file_backend.h"
  14. #include "core/hle/result.h"
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // FileSys namespace
  17. namespace FileSys {
  18. /**
  19. * Helper which implements a backend accessing the host machine's filesystem.
  20. * This should be subclassed by concrete archive types, which will provide the
  21. * base directory on the host filesystem and override any required functionality.
  22. */
  23. class DiskArchive : public ArchiveBackend {
  24. public:
  25. DiskArchive(const std::string& mount_point_) : mount_point(mount_point_) {}
  26. virtual std::string GetName() const override {
  27. return "DiskArchive: " + mount_point;
  28. }
  29. ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
  30. const Mode mode) const override;
  31. ResultCode DeleteFile(const Path& path) const override;
  32. bool RenameFile(const Path& src_path, const Path& dest_path) const override;
  33. bool DeleteDirectory(const Path& path) const override;
  34. ResultCode CreateFile(const Path& path, u64 size) const override;
  35. bool CreateDirectory(const Path& path) const override;
  36. bool RenameDirectory(const Path& src_path, const Path& dest_path) const override;
  37. std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override;
  38. u64 GetFreeBytes() const override;
  39. protected:
  40. friend class DiskFile;
  41. friend class DiskDirectory;
  42. std::string mount_point;
  43. };
  44. class DiskFile : public FileBackend {
  45. public:
  46. DiskFile(const DiskArchive& archive, const Path& path, const Mode mode);
  47. ResultCode Open() override;
  48. ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
  49. ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
  50. u64 GetSize() const override;
  51. bool SetSize(u64 size) const override;
  52. bool Close() const override;
  53. void Flush() const override {
  54. file->Flush();
  55. }
  56. protected:
  57. std::string path;
  58. Mode mode;
  59. std::unique_ptr<FileUtil::IOFile> file;
  60. };
  61. class DiskDirectory : public DirectoryBackend {
  62. public:
  63. DiskDirectory(const DiskArchive& archive, const Path& path);
  64. ~DiskDirectory() override {
  65. Close();
  66. }
  67. bool Open() override;
  68. u32 Read(const u32 count, Entry* entries) override;
  69. bool Close() const override {
  70. return true;
  71. }
  72. protected:
  73. std::string path;
  74. u32 total_entries_in_directory;
  75. FileUtil::FSTEntry directory;
  76. // We need to remember the last entry we returned, so a subsequent call to Read will continue
  77. // from the next one. This iterator will always point to the next unread entry.
  78. std::vector<FileUtil::FSTEntry>::iterator children_iterator;
  79. };
  80. } // namespace FileSys