file_sys.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2012- PPSSPP Project.
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, version 2.0 or later versions.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License 2.0 for more details.
  9. // A copy of the GPL 2.0 should have been included with the program.
  10. // If not, see http://www.gnu.org/licenses/
  11. // Official git repository and contact information can be found at
  12. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
  13. #pragma once
  14. #include "common/common.h"
  15. #include "common/chunk_file.h"
  16. enum FileAccess {
  17. FILEACCESS_NONE=0,
  18. FILEACCESS_READ=1,
  19. FILEACCESS_WRITE=2,
  20. FILEACCESS_APPEND=4,
  21. FILEACCESS_CREATE=8
  22. };
  23. enum FileMove {
  24. FILEMOVE_BEGIN=0,
  25. FILEMOVE_CURRENT=1,
  26. FILEMOVE_END=2
  27. };
  28. enum FileType {
  29. FILETYPE_NORMAL=1,
  30. FILETYPE_DIRECTORY=2
  31. };
  32. class IHandleAllocator {
  33. public:
  34. virtual ~IHandleAllocator() {}
  35. virtual u32 GetNewHandle() = 0;
  36. virtual void FreeHandle(u32 handle) = 0;
  37. };
  38. class SequentialHandleAllocator : public IHandleAllocator {
  39. public:
  40. SequentialHandleAllocator() : handle_(1) {}
  41. virtual u32 GetNewHandle() { return handle_++; }
  42. virtual void FreeHandle(u32 handle) {}
  43. private:
  44. int handle_;
  45. };
  46. struct FileInfo {
  47. FileInfo()
  48. : size(0), access(0), exists(false), type(FILETYPE_NORMAL), isOnSectorSystem(false), startSector(0), numSectors(0) {}
  49. void DoState(PointerWrap &p) {
  50. auto s = p.Section("FileInfo", 1);
  51. if (!s)
  52. return;
  53. p.Do(name);
  54. p.Do(size);
  55. p.Do(access);
  56. p.Do(exists);
  57. p.Do(type);
  58. p.Do(atime);
  59. p.Do(ctime);
  60. p.Do(mtime);
  61. p.Do(isOnSectorSystem);
  62. p.Do(startSector);
  63. p.Do(numSectors);
  64. p.Do(sectorSize);
  65. }
  66. std::string name;
  67. s64 size;
  68. u32 access; //unix 777
  69. bool exists;
  70. FileType type;
  71. tm atime;
  72. tm ctime;
  73. tm mtime;
  74. bool isOnSectorSystem;
  75. u32 startSector;
  76. u32 numSectors;
  77. u32 sectorSize;
  78. };
  79. class IFileSystem {
  80. public:
  81. virtual ~IFileSystem() {}
  82. virtual void DoState(PointerWrap &p) = 0;
  83. virtual std::vector<FileInfo> GetDirListing(std::string path) = 0;
  84. virtual u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) = 0;
  85. virtual void CloseFile(u32 handle) = 0;
  86. virtual size_t ReadFile(u32 handle, u8 *pointer, s64 size) = 0;
  87. virtual size_t WriteFile(u32 handle, const u8 *pointer, s64 size) = 0;
  88. virtual size_t SeekFile(u32 handle, s32 position, FileMove type) = 0;
  89. virtual FileInfo GetFileInfo(std::string filename) = 0;
  90. virtual bool OwnsHandle(u32 handle) = 0;
  91. virtual bool MkDir(const std::string &dirname) = 0;
  92. virtual bool RmDir(const std::string &dirname) = 0;
  93. virtual int RenameFile(const std::string &from, const std::string &to) = 0;
  94. virtual bool RemoveFile(const std::string &filename) = 0;
  95. virtual bool GetHostPath(const std::string &inpath, std::string &outpath) = 0;
  96. };
  97. class EmptyFileSystem : public IFileSystem {
  98. public:
  99. virtual void DoState(PointerWrap &p) {}
  100. std::vector<FileInfo> GetDirListing(std::string path) {std::vector<FileInfo> vec; return vec;}
  101. u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) {return 0;}
  102. void CloseFile(u32 handle) {}
  103. size_t ReadFile(u32 handle, u8 *pointer, s64 size) {return 0;}
  104. size_t WriteFile(u32 handle, const u8 *pointer, s64 size) {return 0;}
  105. size_t SeekFile(u32 handle, s32 position, FileMove type) {return 0;}
  106. FileInfo GetFileInfo(std::string filename) {FileInfo f; return f;}
  107. bool OwnsHandle(u32 handle) {return false;}
  108. virtual bool MkDir(const std::string &dirname) {return false;}
  109. virtual bool RmDir(const std::string &dirname) {return false;}
  110. virtual int RenameFile(const std::string &from, const std::string &to) {return -1;}
  111. virtual bool RemoveFile(const std::string &filename) {return false;}
  112. virtual bool GetHostPath(const std::string &inpath, std::string &outpath) {return false;}
  113. };