vfs_real.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <iterator>
  7. #include <utility>
  8. #include "common/assert.h"
  9. #include "common/common_paths.h"
  10. #include "common/file_util.h"
  11. #include "common/logging/log.h"
  12. #include "core/file_sys/vfs_real.h"
  13. namespace FileSys {
  14. static std::string ModeFlagsToString(Mode mode) {
  15. std::string mode_str;
  16. // Calculate the correct open mode for the file.
  17. if (mode & Mode::Read && mode & Mode::Write) {
  18. if (mode & Mode::Append)
  19. mode_str = "a+";
  20. else
  21. mode_str = "r+";
  22. } else {
  23. if (mode & Mode::Read)
  24. mode_str = "r";
  25. else if (mode & Mode::Append)
  26. mode_str = "a";
  27. else if (mode & Mode::Write)
  28. mode_str = "w";
  29. else
  30. UNREACHABLE_MSG("Invalid file open mode: {:02X}", static_cast<u8>(mode));
  31. }
  32. mode_str += "b";
  33. return mode_str;
  34. }
  35. RealVfsFilesystem::RealVfsFilesystem() : VfsFilesystem(nullptr) {}
  36. RealVfsFilesystem::~RealVfsFilesystem() = default;
  37. std::string RealVfsFilesystem::GetName() const {
  38. return "Real";
  39. }
  40. bool RealVfsFilesystem::IsReadable() const {
  41. return true;
  42. }
  43. bool RealVfsFilesystem::IsWritable() const {
  44. return true;
  45. }
  46. VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const {
  47. const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
  48. if (!FileUtil::Exists(path))
  49. return VfsEntryType::None;
  50. if (FileUtil::IsDirectory(path))
  51. return VfsEntryType::Directory;
  52. return VfsEntryType::File;
  53. }
  54. VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
  55. const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
  56. if (cache.find(path) != cache.end()) {
  57. auto weak = cache[path];
  58. if (!weak.expired()) {
  59. return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, weak.lock(), path, perms));
  60. }
  61. }
  62. if (!FileUtil::Exists(path) && (perms & Mode::WriteAppend) != 0)
  63. FileUtil::CreateEmptyFile(path);
  64. auto backing = std::make_shared<FileUtil::IOFile>(path, ModeFlagsToString(perms).c_str());
  65. cache[path] = backing;
  66. // Cannot use make_shared as RealVfsFile constructor is private
  67. return std::shared_ptr<RealVfsFile>(new RealVfsFile(*this, backing, path, perms));
  68. }
  69. VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) {
  70. const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
  71. const auto path_fwd = FileUtil::SanitizePath(path, FileUtil::DirectorySeparator::ForwardSlash);
  72. if (!FileUtil::Exists(path)) {
  73. FileUtil::CreateFullPath(path_fwd);
  74. if (!FileUtil::CreateEmptyFile(path))
  75. return nullptr;
  76. }
  77. return OpenFile(path, perms);
  78. }
  79. VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) {
  80. const auto old_path =
  81. FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
  82. const auto new_path =
  83. FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
  84. if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
  85. FileUtil::IsDirectory(old_path) || !FileUtil::Copy(old_path, new_path))
  86. return nullptr;
  87. return OpenFile(new_path, Mode::ReadWrite);
  88. }
  89. VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) {
  90. const auto old_path =
  91. FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
  92. const auto new_path =
  93. FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
  94. if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
  95. FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path))
  96. return nullptr;
  97. if (cache.find(old_path) != cache.end()) {
  98. auto cached = cache[old_path];
  99. if (!cached.expired()) {
  100. auto file = cached.lock();
  101. file->Open(new_path, "r+b");
  102. cache.erase(old_path);
  103. cache[new_path] = file;
  104. }
  105. }
  106. return OpenFile(new_path, Mode::ReadWrite);
  107. }
  108. bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
  109. const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
  110. if (cache.find(path) != cache.end()) {
  111. if (!cache[path].expired())
  112. cache[path].lock()->Close();
  113. cache.erase(path);
  114. }
  115. return FileUtil::Delete(path);
  116. }
  117. VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
  118. const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
  119. // Cannot use make_shared as RealVfsDirectory constructor is private
  120. return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
  121. }
  122. VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) {
  123. const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
  124. const auto path_fwd = FileUtil::SanitizePath(path, FileUtil::DirectorySeparator::ForwardSlash);
  125. if (!FileUtil::Exists(path)) {
  126. FileUtil::CreateFullPath(path_fwd);
  127. if (!FileUtil::CreateDir(path))
  128. return nullptr;
  129. }
  130. // Cannot use make_shared as RealVfsDirectory constructor is private
  131. return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
  132. }
  133. VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_,
  134. std::string_view new_path_) {
  135. const auto old_path =
  136. FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
  137. const auto new_path =
  138. FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
  139. if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
  140. !FileUtil::IsDirectory(old_path))
  141. return nullptr;
  142. FileUtil::CopyDir(old_path, new_path);
  143. return OpenDirectory(new_path, Mode::ReadWrite);
  144. }
  145. VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
  146. std::string_view new_path_) {
  147. const auto old_path =
  148. FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
  149. const auto new_path =
  150. FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
  151. if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
  152. FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path))
  153. return nullptr;
  154. for (auto& kv : cache) {
  155. // Path in cache starts with old_path
  156. if (kv.first.rfind(old_path, 0) == 0) {
  157. const auto file_old_path =
  158. FileUtil::SanitizePath(kv.first, FileUtil::DirectorySeparator::PlatformDefault);
  159. const auto file_new_path =
  160. FileUtil::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()),
  161. FileUtil::DirectorySeparator::PlatformDefault);
  162. auto cached = cache[file_old_path];
  163. if (!cached.expired()) {
  164. auto file = cached.lock();
  165. file->Open(file_new_path, "r+b");
  166. cache.erase(file_old_path);
  167. cache[file_new_path] = file;
  168. }
  169. }
  170. }
  171. return OpenDirectory(new_path, Mode::ReadWrite);
  172. }
  173. bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
  174. const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
  175. for (auto& kv : cache) {
  176. // Path in cache starts with old_path
  177. if (kv.first.rfind(path, 0) == 0) {
  178. if (!cache[kv.first].expired())
  179. cache[kv.first].lock()->Close();
  180. cache.erase(kv.first);
  181. }
  182. }
  183. return FileUtil::DeleteDirRecursively(path);
  184. }
  185. RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::shared_ptr<FileUtil::IOFile> backing_,
  186. const std::string& path_, Mode perms_)
  187. : base(base_), backing(std::move(backing_)), path(path_),
  188. parent_path(FileUtil::GetParentPath(path_)),
  189. path_components(FileUtil::SplitPathComponents(path_)),
  190. parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)),
  191. perms(perms_) {}
  192. RealVfsFile::~RealVfsFile() = default;
  193. std::string RealVfsFile::GetName() const {
  194. return path_components.back();
  195. }
  196. std::size_t RealVfsFile::GetSize() const {
  197. return backing->GetSize();
  198. }
  199. bool RealVfsFile::Resize(std::size_t new_size) {
  200. return backing->Resize(new_size);
  201. }
  202. std::shared_ptr<VfsDirectory> RealVfsFile::GetContainingDirectory() const {
  203. return base.OpenDirectory(parent_path, perms);
  204. }
  205. bool RealVfsFile::IsWritable() const {
  206. return (perms & Mode::WriteAppend) != 0;
  207. }
  208. bool RealVfsFile::IsReadable() const {
  209. return (perms & Mode::ReadWrite) != 0;
  210. }
  211. std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
  212. if (!backing->Seek(offset, SEEK_SET))
  213. return 0;
  214. return backing->ReadBytes(data, length);
  215. }
  216. std::size_t RealVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
  217. if (!backing->Seek(offset, SEEK_SET))
  218. return 0;
  219. return backing->WriteBytes(data, length);
  220. }
  221. bool RealVfsFile::Rename(std::string_view name) {
  222. return base.MoveFile(path, parent_path + DIR_SEP + std::string(name)) != nullptr;
  223. }
  224. bool RealVfsFile::Close() {
  225. return backing->Close();
  226. }
  227. // TODO(DarkLordZach): MSVC would not let me combine the following two functions using 'if
  228. // constexpr' because there is a compile error in the branch not used.
  229. template <>
  230. std::vector<VirtualFile> RealVfsDirectory::IterateEntries<RealVfsFile, VfsFile>() const {
  231. if (perms == Mode::Append)
  232. return {};
  233. std::vector<VirtualFile> out;
  234. FileUtil::ForeachDirectoryEntry(
  235. nullptr, path,
  236. [&out, this](u64* entries_out, const std::string& directory, const std::string& filename) {
  237. const std::string full_path = directory + DIR_SEP + filename;
  238. if (!FileUtil::IsDirectory(full_path))
  239. out.emplace_back(base.OpenFile(full_path, perms));
  240. return true;
  241. });
  242. return out;
  243. }
  244. template <>
  245. std::vector<VirtualDir> RealVfsDirectory::IterateEntries<RealVfsDirectory, VfsDirectory>() const {
  246. if (perms == Mode::Append)
  247. return {};
  248. std::vector<VirtualDir> out;
  249. FileUtil::ForeachDirectoryEntry(
  250. nullptr, path,
  251. [&out, this](u64* entries_out, const std::string& directory, const std::string& filename) {
  252. const std::string full_path = directory + DIR_SEP + filename;
  253. if (FileUtil::IsDirectory(full_path))
  254. out.emplace_back(base.OpenDirectory(full_path, perms));
  255. return true;
  256. });
  257. return out;
  258. }
  259. RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string& path_, Mode perms_)
  260. : base(base_), path(FileUtil::RemoveTrailingSlash(path_)),
  261. parent_path(FileUtil::GetParentPath(path)),
  262. path_components(FileUtil::SplitPathComponents(path)),
  263. parent_components(FileUtil::SliceVector(path_components, 0, path_components.size() - 1)),
  264. perms(perms_) {
  265. if (!FileUtil::Exists(path) && perms & Mode::WriteAppend)
  266. FileUtil::CreateDir(path);
  267. }
  268. RealVfsDirectory::~RealVfsDirectory() = default;
  269. std::shared_ptr<VfsFile> RealVfsDirectory::GetFileRelative(std::string_view path) const {
  270. const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
  271. if (!FileUtil::Exists(full_path) || FileUtil::IsDirectory(full_path))
  272. return nullptr;
  273. return base.OpenFile(full_path, perms);
  274. }
  275. std::shared_ptr<VfsDirectory> RealVfsDirectory::GetDirectoryRelative(std::string_view path) const {
  276. const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
  277. if (!FileUtil::Exists(full_path) || !FileUtil::IsDirectory(full_path))
  278. return nullptr;
  279. return base.OpenDirectory(full_path, perms);
  280. }
  281. std::shared_ptr<VfsFile> RealVfsDirectory::GetFile(std::string_view name) const {
  282. return GetFileRelative(name);
  283. }
  284. std::shared_ptr<VfsDirectory> RealVfsDirectory::GetSubdirectory(std::string_view name) const {
  285. return GetDirectoryRelative(name);
  286. }
  287. std::shared_ptr<VfsFile> RealVfsDirectory::CreateFileRelative(std::string_view path) {
  288. const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
  289. return base.CreateFile(full_path, perms);
  290. }
  291. std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateDirectoryRelative(std::string_view path) {
  292. const auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(path));
  293. return base.CreateDirectory(full_path, perms);
  294. }
  295. bool RealVfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) {
  296. auto full_path = FileUtil::SanitizePath(this->path + DIR_SEP + std::string(name));
  297. return base.DeleteDirectory(full_path);
  298. }
  299. std::vector<std::shared_ptr<VfsFile>> RealVfsDirectory::GetFiles() const {
  300. return IterateEntries<RealVfsFile, VfsFile>();
  301. }
  302. std::vector<std::shared_ptr<VfsDirectory>> RealVfsDirectory::GetSubdirectories() const {
  303. return IterateEntries<RealVfsDirectory, VfsDirectory>();
  304. }
  305. bool RealVfsDirectory::IsWritable() const {
  306. return (perms & Mode::WriteAppend) != 0;
  307. }
  308. bool RealVfsDirectory::IsReadable() const {
  309. return (perms & Mode::ReadWrite) != 0;
  310. }
  311. std::string RealVfsDirectory::GetName() const {
  312. return path_components.back();
  313. }
  314. std::shared_ptr<VfsDirectory> RealVfsDirectory::GetParentDirectory() const {
  315. if (path_components.size() <= 1)
  316. return nullptr;
  317. return base.OpenDirectory(parent_path, perms);
  318. }
  319. std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateSubdirectory(std::string_view name) {
  320. const std::string subdir_path = (path + DIR_SEP).append(name);
  321. return base.CreateDirectory(subdir_path, perms);
  322. }
  323. std::shared_ptr<VfsFile> RealVfsDirectory::CreateFile(std::string_view name) {
  324. const std::string file_path = (path + DIR_SEP).append(name);
  325. return base.CreateFile(file_path, perms);
  326. }
  327. bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) {
  328. const std::string subdir_path = (path + DIR_SEP).append(name);
  329. return base.DeleteDirectory(subdir_path);
  330. }
  331. bool RealVfsDirectory::DeleteFile(std::string_view name) {
  332. const std::string file_path = (path + DIR_SEP).append(name);
  333. return base.DeleteFile(file_path);
  334. }
  335. bool RealVfsDirectory::Rename(std::string_view name) {
  336. const std::string new_name = (parent_path + DIR_SEP).append(name);
  337. return base.MoveFile(path, new_name) != nullptr;
  338. }
  339. std::string RealVfsDirectory::GetFullPath() const {
  340. auto out = path;
  341. std::replace(out.begin(), out.end(), '\\', '/');
  342. return out;
  343. }
  344. std::map<std::string, VfsEntryType, std::less<>> RealVfsDirectory::GetEntries() const {
  345. if (perms == Mode::Append)
  346. return {};
  347. std::map<std::string, VfsEntryType, std::less<>> out;
  348. FileUtil::ForeachDirectoryEntry(
  349. nullptr, path,
  350. [&out](u64* entries_out, const std::string& directory, const std::string& filename) {
  351. const std::string full_path = directory + DIR_SEP + filename;
  352. out.emplace(filename, FileUtil::IsDirectory(full_path) ? VfsEntryType::Directory
  353. : VfsEntryType::File);
  354. return true;
  355. });
  356. return out;
  357. }
  358. } // namespace FileSys