vfs_real.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <cstddef>
  5. #include <iterator>
  6. #include <utility>
  7. #include "common/assert.h"
  8. #include "common/fs/file.h"
  9. #include "common/fs/fs.h"
  10. #include "common/fs/path_util.h"
  11. #include "common/logging/log.h"
  12. #include "core/file_sys/vfs.h"
  13. #include "core/file_sys/vfs_real.h"
  14. // For FileTimeStampRaw
  15. #include <sys/stat.h>
  16. #ifdef _MSC_VER
  17. #define stat _stat64
  18. #endif
  19. namespace FileSys {
  20. namespace FS = Common::FS;
  21. namespace {
  22. constexpr size_t MaxOpenFiles = 512;
  23. constexpr FS::FileAccessMode ModeFlagsToFileAccessMode(Mode mode) {
  24. switch (mode) {
  25. case Mode::Read:
  26. return FS::FileAccessMode::Read;
  27. case Mode::Write:
  28. case Mode::ReadWrite:
  29. case Mode::Append:
  30. case Mode::ReadAppend:
  31. case Mode::WriteAppend:
  32. case Mode::All:
  33. return FS::FileAccessMode::ReadWrite;
  34. default:
  35. return {};
  36. }
  37. }
  38. } // Anonymous namespace
  39. RealVfsFilesystem::RealVfsFilesystem() : VfsFilesystem(nullptr) {}
  40. RealVfsFilesystem::~RealVfsFilesystem() = default;
  41. std::string RealVfsFilesystem::GetName() const {
  42. return "Real";
  43. }
  44. bool RealVfsFilesystem::IsReadable() const {
  45. return true;
  46. }
  47. bool RealVfsFilesystem::IsWritable() const {
  48. return true;
  49. }
  50. VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const {
  51. const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
  52. if (!FS::Exists(path)) {
  53. return VfsEntryType::None;
  54. }
  55. if (FS::IsDir(path)) {
  56. return VfsEntryType::Directory;
  57. }
  58. return VfsEntryType::File;
  59. }
  60. VirtualFile RealVfsFilesystem::OpenFileFromEntry(std::string_view path_, std::optional<u64> size,
  61. Mode perms) {
  62. const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
  63. if (auto it = cache.find(path); it != cache.end()) {
  64. if (auto file = it->second.lock(); file) {
  65. return file;
  66. }
  67. }
  68. if (!size && !FS::IsFile(path)) {
  69. return nullptr;
  70. }
  71. auto reference = std::make_unique<FileReference>();
  72. this->InsertReferenceIntoList(*reference);
  73. auto file = std::shared_ptr<RealVfsFile>(
  74. new RealVfsFile(*this, std::move(reference), path, perms, size));
  75. cache[path] = file;
  76. return file;
  77. }
  78. VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
  79. return OpenFileFromEntry(path_, {}, perms);
  80. }
  81. VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) {
  82. const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
  83. cache.erase(path);
  84. // Current usages of CreateFile expect to delete the contents of an existing file.
  85. if (FS::IsFile(path)) {
  86. FS::IOFile temp{path, FS::FileAccessMode::Write, FS::FileType::BinaryFile};
  87. if (!temp.IsOpen()) {
  88. return nullptr;
  89. }
  90. temp.Close();
  91. return OpenFile(path, perms);
  92. }
  93. if (!FS::NewFile(path)) {
  94. return nullptr;
  95. }
  96. return OpenFile(path, perms);
  97. }
  98. VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) {
  99. // Unused
  100. return nullptr;
  101. }
  102. VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) {
  103. const auto old_path = FS::SanitizePath(old_path_, FS::DirectorySeparator::PlatformDefault);
  104. const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault);
  105. cache.erase(old_path);
  106. cache.erase(new_path);
  107. if (!FS::RenameFile(old_path, new_path)) {
  108. return nullptr;
  109. }
  110. return OpenFile(new_path, Mode::ReadWrite);
  111. }
  112. bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
  113. const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
  114. cache.erase(path);
  115. return FS::RemoveFile(path);
  116. }
  117. VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
  118. const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
  119. return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
  120. }
  121. VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) {
  122. const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
  123. if (!FS::CreateDirs(path)) {
  124. return nullptr;
  125. }
  126. return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
  127. }
  128. VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_,
  129. std::string_view new_path_) {
  130. // Unused
  131. return nullptr;
  132. }
  133. VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
  134. std::string_view new_path_) {
  135. const auto old_path = FS::SanitizePath(old_path_, FS::DirectorySeparator::PlatformDefault);
  136. const auto new_path = FS::SanitizePath(new_path_, FS::DirectorySeparator::PlatformDefault);
  137. if (!FS::RenameDir(old_path, new_path)) {
  138. return nullptr;
  139. }
  140. return OpenDirectory(new_path, Mode::ReadWrite);
  141. }
  142. bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
  143. const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
  144. return FS::RemoveDirRecursively(path);
  145. }
  146. void RealVfsFilesystem::RefreshReference(const std::string& path, Mode perms,
  147. FileReference& reference) {
  148. // Temporarily remove from list.
  149. this->RemoveReferenceFromList(reference);
  150. // Restore file if needed.
  151. if (!reference.file) {
  152. this->EvictSingleReference();
  153. reference.file =
  154. FS::FileOpen(path, ModeFlagsToFileAccessMode(perms), FS::FileType::BinaryFile);
  155. if (reference.file) {
  156. num_open_files++;
  157. }
  158. }
  159. // Reinsert into list.
  160. this->InsertReferenceIntoList(reference);
  161. }
  162. void RealVfsFilesystem::DropReference(std::unique_ptr<FileReference>&& reference) {
  163. // Remove from list.
  164. this->RemoveReferenceFromList(*reference);
  165. // Close the file.
  166. if (reference->file) {
  167. reference->file.reset();
  168. num_open_files--;
  169. }
  170. }
  171. void RealVfsFilesystem::EvictSingleReference() {
  172. if (num_open_files < MaxOpenFiles || open_references.empty()) {
  173. return;
  174. }
  175. // Get and remove from list.
  176. auto& reference = open_references.back();
  177. this->RemoveReferenceFromList(reference);
  178. // Close the file.
  179. if (reference.file) {
  180. reference.file.reset();
  181. num_open_files--;
  182. }
  183. // Reinsert into closed list.
  184. this->InsertReferenceIntoList(reference);
  185. }
  186. void RealVfsFilesystem::InsertReferenceIntoList(FileReference& reference) {
  187. if (reference.file) {
  188. open_references.push_front(reference);
  189. } else {
  190. closed_references.push_front(reference);
  191. }
  192. }
  193. void RealVfsFilesystem::RemoveReferenceFromList(FileReference& reference) {
  194. if (reference.file) {
  195. open_references.erase(open_references.iterator_to(reference));
  196. } else {
  197. closed_references.erase(closed_references.iterator_to(reference));
  198. }
  199. }
  200. RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::unique_ptr<FileReference> reference_,
  201. const std::string& path_, Mode perms_, std::optional<u64> size_)
  202. : base(base_), reference(std::move(reference_)), path(path_),
  203. parent_path(FS::GetParentPath(path_)), path_components(FS::SplitPathComponents(path_)),
  204. size(size_), perms(perms_) {}
  205. RealVfsFile::~RealVfsFile() {
  206. base.DropReference(std::move(reference));
  207. }
  208. std::string RealVfsFile::GetName() const {
  209. return path_components.back();
  210. }
  211. std::size_t RealVfsFile::GetSize() const {
  212. if (size) {
  213. return *size;
  214. }
  215. return FS::GetSize(path);
  216. }
  217. bool RealVfsFile::Resize(std::size_t new_size) {
  218. size.reset();
  219. base.RefreshReference(path, perms, *reference);
  220. return reference->file ? reference->file->SetSize(new_size) : false;
  221. }
  222. VirtualDir RealVfsFile::GetContainingDirectory() const {
  223. return base.OpenDirectory(parent_path, perms);
  224. }
  225. bool RealVfsFile::IsWritable() const {
  226. return True(perms & Mode::Write);
  227. }
  228. bool RealVfsFile::IsReadable() const {
  229. return True(perms & Mode::Read);
  230. }
  231. std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
  232. base.RefreshReference(path, perms, *reference);
  233. if (!reference->file || !reference->file->Seek(static_cast<s64>(offset))) {
  234. return 0;
  235. }
  236. return reference->file->ReadSpan(std::span{data, length});
  237. }
  238. std::size_t RealVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
  239. size.reset();
  240. base.RefreshReference(path, perms, *reference);
  241. if (!reference->file || !reference->file->Seek(static_cast<s64>(offset))) {
  242. return 0;
  243. }
  244. return reference->file->WriteSpan(std::span{data, length});
  245. }
  246. bool RealVfsFile::Rename(std::string_view name) {
  247. return base.MoveFile(path, parent_path + '/' + std::string(name)) != nullptr;
  248. }
  249. // TODO(DarkLordZach): MSVC would not let me combine the following two functions using 'if
  250. // constexpr' because there is a compile error in the branch not used.
  251. template <>
  252. std::vector<VirtualFile> RealVfsDirectory::IterateEntries<RealVfsFile, VfsFile>() const {
  253. if (perms == Mode::Append) {
  254. return {};
  255. }
  256. std::vector<VirtualFile> out;
  257. const FS::DirEntryCallable callback = [this,
  258. &out](const std::filesystem::directory_entry& entry) {
  259. const auto full_path_string = FS::PathToUTF8String(entry.path());
  260. out.emplace_back(base.OpenFileFromEntry(full_path_string, entry.file_size(), perms));
  261. return true;
  262. };
  263. FS::IterateDirEntries(path, callback, FS::DirEntryFilter::File);
  264. return out;
  265. }
  266. template <>
  267. std::vector<VirtualDir> RealVfsDirectory::IterateEntries<RealVfsDirectory, VfsDirectory>() const {
  268. if (perms == Mode::Append) {
  269. return {};
  270. }
  271. std::vector<VirtualDir> out;
  272. const FS::DirEntryCallable callback = [this,
  273. &out](const std::filesystem::directory_entry& entry) {
  274. const auto full_path_string = FS::PathToUTF8String(entry.path());
  275. out.emplace_back(base.OpenDirectory(full_path_string, perms));
  276. return true;
  277. };
  278. FS::IterateDirEntries(path, callback, FS::DirEntryFilter::Directory);
  279. return out;
  280. }
  281. RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string& path_, Mode perms_)
  282. : base(base_), path(FS::RemoveTrailingSlash(path_)), parent_path(FS::GetParentPath(path)),
  283. path_components(FS::SplitPathComponents(path)), perms(perms_) {
  284. if (!FS::Exists(path) && True(perms & Mode::Write)) {
  285. void(FS::CreateDirs(path));
  286. }
  287. }
  288. RealVfsDirectory::~RealVfsDirectory() = default;
  289. VirtualFile RealVfsDirectory::GetFileRelative(std::string_view relative_path) const {
  290. const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
  291. if (!FS::Exists(full_path) || FS::IsDir(full_path)) {
  292. return nullptr;
  293. }
  294. return base.OpenFile(full_path, perms);
  295. }
  296. VirtualDir RealVfsDirectory::GetDirectoryRelative(std::string_view relative_path) const {
  297. const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
  298. if (!FS::Exists(full_path) || !FS::IsDir(full_path)) {
  299. return nullptr;
  300. }
  301. return base.OpenDirectory(full_path, perms);
  302. }
  303. VirtualFile RealVfsDirectory::GetFile(std::string_view name) const {
  304. return GetFileRelative(name);
  305. }
  306. VirtualDir RealVfsDirectory::GetSubdirectory(std::string_view name) const {
  307. return GetDirectoryRelative(name);
  308. }
  309. VirtualFile RealVfsDirectory::CreateFileRelative(std::string_view relative_path) {
  310. const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
  311. if (!FS::CreateParentDirs(full_path)) {
  312. return nullptr;
  313. }
  314. return base.CreateFile(full_path, perms);
  315. }
  316. VirtualDir RealVfsDirectory::CreateDirectoryRelative(std::string_view relative_path) {
  317. const auto full_path = FS::SanitizePath(path + '/' + std::string(relative_path));
  318. return base.CreateDirectory(full_path, perms);
  319. }
  320. bool RealVfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) {
  321. const auto full_path = FS::SanitizePath(this->path + '/' + std::string(name));
  322. return base.DeleteDirectory(full_path);
  323. }
  324. std::vector<VirtualFile> RealVfsDirectory::GetFiles() const {
  325. return IterateEntries<RealVfsFile, VfsFile>();
  326. }
  327. FileTimeStampRaw RealVfsDirectory::GetFileTimeStamp(std::string_view path_) const {
  328. const auto full_path = FS::SanitizePath(path + '/' + std::string(path_));
  329. const auto fs_path = std::filesystem::path{FS::ToU8String(full_path)};
  330. struct stat file_status;
  331. #ifdef _WIN32
  332. const auto stat_result = _wstat64(fs_path.c_str(), &file_status);
  333. #else
  334. const auto stat_result = stat(fs_path.c_str(), &file_status);
  335. #endif
  336. if (stat_result != 0) {
  337. return {};
  338. }
  339. return {
  340. .created{static_cast<u64>(file_status.st_ctime)},
  341. .accessed{static_cast<u64>(file_status.st_atime)},
  342. .modified{static_cast<u64>(file_status.st_mtime)},
  343. };
  344. }
  345. std::vector<VirtualDir> RealVfsDirectory::GetSubdirectories() const {
  346. return IterateEntries<RealVfsDirectory, VfsDirectory>();
  347. }
  348. bool RealVfsDirectory::IsWritable() const {
  349. return True(perms & Mode::Write);
  350. }
  351. bool RealVfsDirectory::IsReadable() const {
  352. return True(perms & Mode::Read);
  353. }
  354. std::string RealVfsDirectory::GetName() const {
  355. return path_components.back();
  356. }
  357. VirtualDir RealVfsDirectory::GetParentDirectory() const {
  358. if (path_components.size() <= 1) {
  359. return nullptr;
  360. }
  361. return base.OpenDirectory(parent_path, perms);
  362. }
  363. VirtualDir RealVfsDirectory::CreateSubdirectory(std::string_view name) {
  364. const std::string subdir_path = (path + '/').append(name);
  365. return base.CreateDirectory(subdir_path, perms);
  366. }
  367. VirtualFile RealVfsDirectory::CreateFile(std::string_view name) {
  368. const std::string file_path = (path + '/').append(name);
  369. return base.CreateFile(file_path, perms);
  370. }
  371. bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) {
  372. const std::string subdir_path = (path + '/').append(name);
  373. return base.DeleteDirectory(subdir_path);
  374. }
  375. bool RealVfsDirectory::DeleteFile(std::string_view name) {
  376. const std::string file_path = (path + '/').append(name);
  377. return base.DeleteFile(file_path);
  378. }
  379. bool RealVfsDirectory::Rename(std::string_view name) {
  380. const std::string new_name = (parent_path + '/').append(name);
  381. return base.MoveFile(path, new_name) != nullptr;
  382. }
  383. std::string RealVfsDirectory::GetFullPath() const {
  384. auto out = path;
  385. std::replace(out.begin(), out.end(), '\\', '/');
  386. return out;
  387. }
  388. std::map<std::string, VfsEntryType, std::less<>> RealVfsDirectory::GetEntries() const {
  389. if (perms == Mode::Append) {
  390. return {};
  391. }
  392. std::map<std::string, VfsEntryType, std::less<>> out;
  393. const FS::DirEntryCallable callback = [&out](const std::filesystem::directory_entry& entry) {
  394. const auto filename = FS::PathToUTF8String(entry.path().filename());
  395. out.insert_or_assign(filename,
  396. entry.is_directory() ? VfsEntryType::Directory : VfsEntryType::File);
  397. return true;
  398. };
  399. FS::IterateDirEntries(path, callback);
  400. return out;
  401. }
  402. } // namespace FileSys