directory_sdmc.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. std::string absolute_path = archive->GetMountPoint() + path.AsString();
  17. FileUtil::ScanDirectoryTree(absolute_path, directory);
  18. children_iterator = directory.children.begin();
  19. }
  20. Directory_SDMC::~Directory_SDMC() {
  21. Close();
  22. }
  23. /**
  24. * List files contained in the directory
  25. * @param count Number of entries to return at once in entries
  26. * @param entries Buffer to read data into
  27. * @return Number of entries listed
  28. */
  29. u32 Directory_SDMC::Read(const u32 count, Entry* entries) {
  30. u32 entries_read = 0;
  31. while (entries_read < count && children_iterator != directory.children.cend()) {
  32. const FileUtil::FSTEntry& file = *children_iterator;
  33. const std::string& filename = file.virtualName;
  34. Entry& entry = entries[entries_read];
  35. WARN_LOG(FILESYS, "File %s: size=%llu dir=%d", filename.c_str(), file.size, file.isDirectory);
  36. // TODO(Link Mauve): use a proper conversion to UTF-16.
  37. for (size_t j = 0; j < FILENAME_LENGTH; ++j) {
  38. entry.filename[j] = filename[j];
  39. if (!filename[j])
  40. break;
  41. }
  42. FileUtil::SplitFilename83(filename, entry.short_name, entry.extension);
  43. entry.is_directory = file.isDirectory;
  44. entry.is_hidden = (filename[0] == '.');
  45. entry.is_read_only = 0;
  46. entry.file_size = file.size;
  47. // We emulate a SD card where the archive bit has never been cleared, as it would be on
  48. // most user SD cards.
  49. // Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a
  50. // file bit.
  51. entry.is_archive = !file.isDirectory;
  52. ++entries_read;
  53. ++children_iterator;
  54. }
  55. return entries_read;
  56. }
  57. /**
  58. * Close the directory
  59. * @return true if the directory closed correctly
  60. */
  61. bool Directory_SDMC::Close() const {
  62. return true;
  63. }
  64. } // namespace FileSys