vfs_real.cpp 15 KB

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