disk_archive.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "common/common_types.h"
  6. #include "common/file_util.h"
  7. #include "core/file_sys/archive_backend.h"
  8. #include "core/loader/loader.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // FileSys namespace
  11. namespace FileSys {
  12. /**
  13. * Helper which implements a backend accessing the host machine's filesystem.
  14. * This should be subclassed by concrete archive types, which will provide the
  15. * base directory on the host filesystem and override any required functionality.
  16. */
  17. class DiskArchive : public ArchiveBackend {
  18. public:
  19. DiskArchive(const std::string& mount_point_) : mount_point(mount_point_) {}
  20. virtual std::string GetName() const = 0;
  21. std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const override;
  22. bool DeleteFile(const FileSys::Path& path) const override;
  23. bool RenameFile(const FileSys::Path& src_path, const FileSys::Path& dest_path) const override;
  24. bool DeleteDirectory(const FileSys::Path& path) const override;
  25. ResultCode CreateFile(const Path& path, u32 size) const override;
  26. bool CreateDirectory(const Path& path) const override;
  27. bool RenameDirectory(const FileSys::Path& src_path, const FileSys::Path& dest_path) const override;
  28. std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override;
  29. /**
  30. * Getter for the path used for this Archive
  31. * @return Mount point of that passthrough archive
  32. */
  33. const std::string& GetMountPoint() const {
  34. return mount_point;
  35. }
  36. protected:
  37. std::string mount_point;
  38. };
  39. class DiskFile : public FileBackend {
  40. public:
  41. DiskFile();
  42. DiskFile(const DiskArchive* archive, const Path& path, const Mode mode);
  43. ~DiskFile() override {
  44. Close();
  45. }
  46. bool Open() override;
  47. size_t Read(const u64 offset, const u32 length, u8* buffer) const override;
  48. size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override;
  49. size_t GetSize() const override;
  50. bool SetSize(const u64 size) const override;
  51. bool Close() const override;
  52. void Flush() const override {
  53. file->Flush();
  54. }
  55. protected:
  56. const DiskArchive* archive;
  57. std::string path;
  58. Mode mode;
  59. FileUtil::IOFile* file;
  60. };
  61. class DiskDirectory : public DirectoryBackend {
  62. public:
  63. DiskDirectory();
  64. DiskDirectory(const DiskArchive* archive, const Path& path);
  65. ~DiskDirectory() override {
  66. Close();
  67. }
  68. bool Open() override;
  69. u32 Read(const u32 count, Entry* entries) override;
  70. bool Close() const override {
  71. return true;
  72. }
  73. protected:
  74. const DiskArchive* archive;
  75. std::string path;
  76. u32 total_entries_in_directory;
  77. FileUtil::FSTEntry directory;
  78. // We need to remember the last entry we returned, so a subsequent call to Read will continue
  79. // from the next one. This iterator will always point to the next unread entry.
  80. std::vector<FileUtil::FSTEntry>::iterator children_iterator;
  81. };
  82. } // namespace FileSys