directory_sdmc.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include <sys/stat.h>
  5. #include "common/common_types.h"
  6. #include "common/file_util.h"
  7. #include "core/file_sys/directory_sdmc.h"
  8. #include "core/file_sys/archive_sdmc.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // FileSys namespace
  11. namespace FileSys {
  12. Directory_SDMC::Directory_SDMC(const Archive_SDMC* archive, const Path& path) {
  13. // TODO(Link Mauve): normalize path into an absolute path without "..", it can currently bypass
  14. // the root directory we set while opening the archive.
  15. // For example, opening /../../usr/bin can give the emulated program your installed programs.
  16. this->path = archive->GetMountPoint() + path.AsString();
  17. }
  18. Directory_SDMC::~Directory_SDMC() {
  19. Close();
  20. }
  21. bool Directory_SDMC::Open() {
  22. if (!FileUtil::IsDirectory(path))
  23. return false;
  24. FileUtil::ScanDirectoryTree(path, directory);
  25. children_iterator = directory.children.begin();
  26. return true;
  27. }
  28. /**
  29. * List files contained in the directory
  30. * @param count Number of entries to return at once in entries
  31. * @param entries Buffer to read data into
  32. * @return Number of entries listed
  33. */
  34. u32 Directory_SDMC::Read(const u32 count, Entry* entries) {
  35. u32 entries_read = 0;
  36. while (entries_read < count && children_iterator != directory.children.cend()) {
  37. const FileUtil::FSTEntry& file = *children_iterator;
  38. const std::string& filename = file.virtualName;
  39. Entry& entry = entries[entries_read];
  40. LOG_TRACE(Service_FS, "File %s: size=%llu dir=%d", filename.c_str(), file.size, file.isDirectory);
  41. // TODO(Link Mauve): use a proper conversion to UTF-16.
  42. for (size_t j = 0; j < FILENAME_LENGTH; ++j) {
  43. entry.filename[j] = filename[j];
  44. if (!filename[j])
  45. break;
  46. }
  47. FileUtil::SplitFilename83(filename, entry.short_name, entry.extension);
  48. entry.is_directory = file.isDirectory;
  49. entry.is_hidden = (filename[0] == '.');
  50. entry.is_read_only = 0;
  51. entry.file_size = file.size;
  52. // We emulate a SD card where the archive bit has never been cleared, as it would be on
  53. // most user SD cards.
  54. // Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a
  55. // file bit.
  56. entry.is_archive = !file.isDirectory;
  57. ++entries_read;
  58. ++children_iterator;
  59. }
  60. return entries_read;
  61. }
  62. /**
  63. * Close the directory
  64. * @return true if the directory closed correctly
  65. */
  66. bool Directory_SDMC::Close() const {
  67. return true;
  68. }
  69. } // namespace FileSys