vfs_real.cpp 15 KB

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