From d34673990b5176c7ab71c239694737a9ac8df14e Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 27 Sep 2014 19:09:04 +0000 Subject: [PATCH 1/5] FileSys: Add forgotten docstrings. --- src/core/file_sys/archive_sdmc.cpp | 4 ++++ src/core/file_sys/archive_sdmc.h | 4 ++++ src/core/file_sys/file.h | 2 +- src/core/file_sys/file_romfs.cpp | 2 +- src/core/file_sys/file_romfs.h | 2 +- src/core/file_sys/file_sdmc.cpp | 23 +++++++++++++++++++++++ src/core/file_sys/file_sdmc.h | 2 +- 7 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/core/file_sys/archive_sdmc.cpp b/src/core/file_sys/archive_sdmc.cpp index 30d33be5f..8d0827380 100644 --- a/src/core/file_sys/archive_sdmc.cpp +++ b/src/core/file_sys/archive_sdmc.cpp @@ -24,6 +24,10 @@ Archive_SDMC::Archive_SDMC(const std::string& mount_point) { Archive_SDMC::~Archive_SDMC() { } +/** + * Initialize the archive. + * @return true if it initialized successfully + */ bool Archive_SDMC::Initialize() { if (!FileUtil::IsDirectory(mount_point)) { WARN_LOG(FILESYS, "Directory %s not found, disabling SDMC.", mount_point.c_str()); diff --git a/src/core/file_sys/archive_sdmc.h b/src/core/file_sys/archive_sdmc.h index 946f8b957..f68648e6f 100644 --- a/src/core/file_sys/archive_sdmc.h +++ b/src/core/file_sys/archive_sdmc.h @@ -20,6 +20,10 @@ public: Archive_SDMC(const std::string& mount_point); ~Archive_SDMC() override; + /** + * Initialize the archive. + * @return true if it initialized successfully + */ bool Initialize(); /** diff --git a/src/core/file_sys/file.h b/src/core/file_sys/file.h index f7b009f5a..3749e4fcf 100644 --- a/src/core/file_sys/file.h +++ b/src/core/file_sys/file.h @@ -31,8 +31,8 @@ public: * Write data to the file * @param offset Offset in bytes to start writing data to * @param length Length in bytes of data to write to file - * @param buffer Buffer to write data from * @param flush The flush parameters (0 == do not flush) + * @param buffer Buffer to read data from * @return Number of bytes written */ virtual size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const = 0; diff --git a/src/core/file_sys/file_romfs.cpp b/src/core/file_sys/file_romfs.cpp index 00f3c2ea8..0709e98f0 100644 --- a/src/core/file_sys/file_romfs.cpp +++ b/src/core/file_sys/file_romfs.cpp @@ -32,8 +32,8 @@ size_t File_RomFS::Read(const u64 offset, const u32 length, u8* buffer) const { * Write data to the file * @param offset Offset in bytes to start writing data to * @param length Length in bytes of data to write to file - * @param buffer Buffer to write data from * @param flush The flush parameters (0 == do not flush) + * @param buffer Buffer to read data from * @return Number of bytes written */ size_t File_RomFS::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const { diff --git a/src/core/file_sys/file_romfs.h b/src/core/file_sys/file_romfs.h index 5db43d4a0..28b4f1158 100644 --- a/src/core/file_sys/file_romfs.h +++ b/src/core/file_sys/file_romfs.h @@ -32,8 +32,8 @@ public: * Write data to the file * @param offset Offset in bytes to start writing data to * @param length Length in bytes of data to write to file - * @param buffer Buffer to write data from * @param flush The flush parameters (0 == do not flush) + * @param buffer Buffer to read data from * @return Number of bytes written */ size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override; diff --git a/src/core/file_sys/file_sdmc.cpp b/src/core/file_sys/file_sdmc.cpp index 07951c9f1..76e7f5d3d 100644 --- a/src/core/file_sys/file_sdmc.cpp +++ b/src/core/file_sys/file_sdmc.cpp @@ -39,11 +39,26 @@ File_SDMC::~File_SDMC() { Close(); } +/** + * Read data from the file + * @param offset Offset in bytes to start reading data from + * @param length Length in bytes of data to read from file + * @param buffer Buffer to read data into + * @return Number of bytes read + */ size_t File_SDMC::Read(const u64 offset, const u32 length, u8* buffer) const { file->Seek(offset, SEEK_SET); return file->ReadBytes(buffer, length); } +/** + * Write data to the file + * @param offset Offset in bytes to start writing data to + * @param length Length in bytes of data to write to file + * @param flush The flush parameters (0 == do not flush) + * @param buffer Buffer to read data from + * @return Number of bytes written + */ size_t File_SDMC::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const { file->Seek(offset, SEEK_SET); size_t written = file->WriteBytes(buffer, length); @@ -52,10 +67,18 @@ size_t File_SDMC::Write(const u64 offset, const u32 length, const u32 flush, con return written; } +/** + * Get the size of the file in bytes + * @return Size of the file in bytes + */ size_t File_SDMC::GetSize() const { return static_cast(file->GetSize()); } +/** + * Close the file + * @return true if the file closed correctly + */ bool File_SDMC::Close() const { return file->Close(); } diff --git a/src/core/file_sys/file_sdmc.h b/src/core/file_sys/file_sdmc.h index b2e46f449..d23020494 100644 --- a/src/core/file_sys/file_sdmc.h +++ b/src/core/file_sys/file_sdmc.h @@ -35,8 +35,8 @@ public: * Write data to the file * @param offset Offset in bytes to start writing data to * @param length Length in bytes of data to write to file - * @param buffer Buffer to write data from * @param flush The flush parameters (0 == do not flush) + * @param buffer Buffer to read data from * @return Number of bytes written */ size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override; From 23c2fbfc7a900ae3c9f8791a87c5ad672f5778fe Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 27 Sep 2014 19:16:51 +0000 Subject: [PATCH 2/5] FileSys/Kernel: Implement SetSize service call for File objects. --- src/core/file_sys/file.h | 7 +++++++ src/core/file_sys/file_romfs.cpp | 9 +++++++++ src/core/file_sys/file_romfs.h | 7 +++++++ src/core/file_sys/file_sdmc.cpp | 11 +++++++++++ src/core/file_sys/file_sdmc.h | 7 +++++++ src/core/hle/kernel/archive.cpp | 8 ++++++++ 6 files changed, 49 insertions(+) diff --git a/src/core/file_sys/file.h b/src/core/file_sys/file.h index 3749e4fcf..443e65319 100644 --- a/src/core/file_sys/file.h +++ b/src/core/file_sys/file.h @@ -43,6 +43,13 @@ public: */ virtual size_t GetSize() const = 0; + /** + * Set the size of the file in bytes + * @param size New size of the file + * @return true if successful + */ + virtual bool SetSize(const u64 size) const = 0; + /** * Close the file * @return true if the file closed correctly diff --git a/src/core/file_sys/file_romfs.cpp b/src/core/file_sys/file_romfs.cpp index 0709e98f0..3ef616e08 100644 --- a/src/core/file_sys/file_romfs.cpp +++ b/src/core/file_sys/file_romfs.cpp @@ -48,6 +48,15 @@ size_t File_RomFS::GetSize() const { return -1; } +/** + * Set the size of the file in bytes + * @param size New size of the file + * @return true if successful + */ +bool File_RomFS::SetSize(const u64 size) const { + return false; +} + /** * Close the file * @return true if the file closed correctly diff --git a/src/core/file_sys/file_romfs.h b/src/core/file_sys/file_romfs.h index 28b4f1158..06973eb93 100644 --- a/src/core/file_sys/file_romfs.h +++ b/src/core/file_sys/file_romfs.h @@ -44,6 +44,13 @@ public: */ size_t GetSize() const override; + /** + * Set the size of the file in bytes + * @param size New size of the file + * @return true if successful + */ + bool SetSize(const u64 size) const override; + /** * Close the file * @return true if the file closed correctly diff --git a/src/core/file_sys/file_sdmc.cpp b/src/core/file_sys/file_sdmc.cpp index 76e7f5d3d..3ef2b0c0e 100644 --- a/src/core/file_sys/file_sdmc.cpp +++ b/src/core/file_sys/file_sdmc.cpp @@ -75,6 +75,17 @@ size_t File_SDMC::GetSize() const { return static_cast(file->GetSize()); } +/** + * Set the size of the file in bytes + * @param size New size of the file + * @return true if successful + */ +bool File_SDMC::SetSize(const u64 size) const { + file->Resize(size); + file->Flush(); + return true; +} + /** * Close the file * @return true if the file closed correctly diff --git a/src/core/file_sys/file_sdmc.h b/src/core/file_sys/file_sdmc.h index d23020494..6b3a1f3a5 100644 --- a/src/core/file_sys/file_sdmc.h +++ b/src/core/file_sys/file_sdmc.h @@ -47,6 +47,13 @@ public: */ size_t GetSize() const override; + /** + * Set the size of the file in bytes + * @param size New size of the file + * @return true if successful + */ + bool SetSize(const u64 size) const override; + /** * Close the file * @return true if the file closed correctly diff --git a/src/core/hle/kernel/archive.cpp b/src/core/hle/kernel/archive.cpp index fa4972994..0a66ab29b 100644 --- a/src/core/hle/kernel/archive.cpp +++ b/src/core/hle/kernel/archive.cpp @@ -181,6 +181,14 @@ public: break; } + case FileCommand::SetSize: + { + u64 size = cmd_buff[1] | ((u64)cmd_buff[2] << 32); + DEBUG_LOG(KERNEL, "SetSize %s %s size=%d", GetTypeName().c_str(), GetName().c_str(), size); + backend->SetSize(size); + break; + } + case FileCommand::Close: { DEBUG_LOG(KERNEL, "Close %s %s", GetTypeName().c_str(), GetName().c_str()); From 0be5c03176236fe602d49c32717a6f3af0a55465 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 27 Sep 2014 19:21:48 +0000 Subject: [PATCH 3/5] FileSys: split the constructor into an Open method, in order to notify the opener something went wrong. Kernel: Return an invalid handle to OpenFile when it failed to open. --- src/core/file_sys/archive_sdmc.cpp | 2 ++ src/core/file_sys/file.h | 6 +++++ src/core/file_sys/file_romfs.cpp | 8 +++++++ src/core/file_sys/file_romfs.h | 6 +++++ src/core/file_sys/file_sdmc.cpp | 38 +++++++++++++++++++----------- src/core/file_sys/file_sdmc.h | 8 +++++++ src/core/hle/kernel/archive.cpp | 3 +++ 7 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/core/file_sys/archive_sdmc.cpp b/src/core/file_sys/archive_sdmc.cpp index 8d0827380..213923c02 100644 --- a/src/core/file_sys/archive_sdmc.cpp +++ b/src/core/file_sys/archive_sdmc.cpp @@ -46,6 +46,8 @@ bool Archive_SDMC::Initialize() { std::unique_ptr Archive_SDMC::OpenFile(const std::string& path, const Mode mode) const { DEBUG_LOG(FILESYS, "called path=%s mode=%d", path.c_str(), mode); File_SDMC* file = new File_SDMC(this, path, mode); + if (!file->Open()) + return nullptr; return std::unique_ptr(file); } diff --git a/src/core/file_sys/file.h b/src/core/file_sys/file.h index 443e65319..4013b6c3e 100644 --- a/src/core/file_sys/file.h +++ b/src/core/file_sys/file.h @@ -18,6 +18,12 @@ public: File() { } virtual ~File() { } + /** + * Open the file + * @return true if the file opened correctly + */ + virtual bool Open() = 0; + /** * Read data from the file * @param offset Offset in bytes to start reading data from diff --git a/src/core/file_sys/file_romfs.cpp b/src/core/file_sys/file_romfs.cpp index 3ef616e08..b55708df4 100644 --- a/src/core/file_sys/file_romfs.cpp +++ b/src/core/file_sys/file_romfs.cpp @@ -17,6 +17,14 @@ File_RomFS::File_RomFS() { File_RomFS::~File_RomFS() { } +/** + * Open the file + * @return true if the file opened correctly + */ +bool File_RomFS::Open() { + return false; +} + /** * Read data from the file * @param offset Offset in bytes to start reading data from diff --git a/src/core/file_sys/file_romfs.h b/src/core/file_sys/file_romfs.h index 06973eb93..5196701d3 100644 --- a/src/core/file_sys/file_romfs.h +++ b/src/core/file_sys/file_romfs.h @@ -19,6 +19,12 @@ public: File_RomFS(); ~File_RomFS() override; + /** + * Open the file + * @return true if the file opened correctly + */ + bool Open() override; + /** * Read data from the file * @param offset Offset in bytes to start reading data from diff --git a/src/core/file_sys/file_sdmc.cpp b/src/core/file_sys/file_sdmc.cpp index 3ef2b0c0e..26204392c 100644 --- a/src/core/file_sys/file_sdmc.cpp +++ b/src/core/file_sys/file_sdmc.cpp @@ -19,26 +19,36 @@ File_SDMC::File_SDMC(const Archive_SDMC* archive, const std::string& path, const // TODO(Link Mauve): normalize path into an absolute path without "..", it can currently bypass // the root directory we set while opening the archive. // For example, opening /../../etc/passwd can give the emulated program your users list. - std::string real_path = archive->GetMountPoint() + path; - - if (!mode.create_flag && !FileUtil::Exists(real_path)) { - file = nullptr; - return; - } - - std::string mode_string; - if (mode.read_flag) - mode_string += "r"; - if (mode.write_flag) - mode_string += "w"; - - file = new FileUtil::IOFile(real_path, mode_string.c_str()); + this->path = archive->GetMountPoint() + path; + this->mode.hex = mode.hex; } File_SDMC::~File_SDMC() { Close(); } +/** + * Open the file + * @return true if the file opened correctly + */ +bool File_SDMC::Open() { + if (!mode.create_flag && !FileUtil::Exists(path)) { + ERROR_LOG(FILESYS, "Non-existing file %s can’t be open without mode create.", path.c_str()); + return false; + } + + std::string mode_string; + if (mode.read_flag && mode.write_flag) + mode_string = "w+"; + else if (mode.read_flag) + mode_string = "r"; + else if (mode.write_flag) + mode_string = "w"; + + file = new FileUtil::IOFile(path, mode_string.c_str()); + return true; +} + /** * Read data from the file * @param offset Offset in bytes to start reading data from diff --git a/src/core/file_sys/file_sdmc.h b/src/core/file_sys/file_sdmc.h index 6b3a1f3a5..df032f7c0 100644 --- a/src/core/file_sys/file_sdmc.h +++ b/src/core/file_sys/file_sdmc.h @@ -22,6 +22,12 @@ public: File_SDMC(const Archive_SDMC* archive, const std::string& path, const Mode mode); ~File_SDMC() override; + /** + * Open the file + * @return true if the file opened correctly + */ + bool Open() override; + /** * Read data from the file * @param offset Offset in bytes to start reading data from @@ -61,6 +67,8 @@ public: bool Close() const override; private: + std::string path; + Mode mode; FileUtil::IOFile* file; }; diff --git a/src/core/hle/kernel/archive.cpp b/src/core/hle/kernel/archive.cpp index 0a66ab29b..86aba7489 100644 --- a/src/core/hle/kernel/archive.cpp +++ b/src/core/hle/kernel/archive.cpp @@ -374,6 +374,9 @@ Handle OpenFileFromArchive(Handle archive_handle, const std::string& path, const file->path = path; file->backend = archive->backend->OpenFile(path, mode); + if (!file->backend) + return 0; + return handle; } From 19c2a96ab07649f901fea9e14fcb6d762c307cbc Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 27 Sep 2014 19:41:21 +0000 Subject: [PATCH 4/5] FileSys: Add static asserts for the Directory struct, and fix its fields position. --- src/core/file_sys/directory.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/file_sys/directory.h b/src/core/file_sys/directory.h index cf9a2010b..9f3546b05 100644 --- a/src/core/file_sys/directory.h +++ b/src/core/file_sys/directory.h @@ -4,6 +4,8 @@ #pragma once +#include + #include "common/common_types.h" #include "core/hle/kernel/kernel.h" @@ -17,9 +19,9 @@ namespace FileSys { const size_t FILENAME_LENGTH = 0x20C / 2; struct Entry { char16_t filename[FILENAME_LENGTH]; // Entry name (UTF-16, null-terminated) - char short_name[8]; // 8.3 file name ('longfilename' -> 'LONGFI~1') + char short_name[9]; // 8.3 file name ('longfilename' -> 'LONGFI~1', null-terminated) char unknown1; // unknown (observed values: 0x0A, 0x70, 0xFD) - char extension[3]; // 8.3 file extension (set to spaces for directories) + char extension[4]; // 8.3 file extension (set to spaces for directories, null-terminated) char unknown2; // unknown (always 0x01) char unknown3; // unknown (0x00 or 0x08) char is_directory; // directory flag @@ -29,6 +31,10 @@ struct Entry { u64 file_size; // file size (for files only) }; static_assert(sizeof(Entry) == 0x228, "Directory Entry struct isn't exactly 0x228 bytes long!"); +static_assert(offsetof(Entry, short_name) == 0x20C, "Wrong offset for short_name in Entry."); +static_assert(offsetof(Entry, extension) == 0x216, "Wrong offset for extension in Entry."); +static_assert(offsetof(Entry, is_archive) == 0x21E, "Wrong offset for is_archive in Entry."); +static_assert(offsetof(Entry, file_size) == 0x220, "Wrong offset for file_size in Entry."); class Directory : NonCopyable { public: From fbd72fd6bf0f02a13be207c9d61f630c594e3156 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 29 Sep 2014 08:34:37 +0000 Subject: [PATCH 5/5] Common: Add a helper function to generate a 8.3 filename from a long one. Core: Fix the SDMC Directory implementation to make blargSnes work. --- src/common/file_util.cpp | 42 +++++++++++++++++++++++++++ src/common/file_util.h | 11 +++++++ src/core/file_sys/directory.h | 4 +-- src/core/file_sys/directory_sdmc.cpp | 43 ++++++++++++---------------- src/core/file_sys/directory_sdmc.h | 9 ++++-- 5 files changed, 80 insertions(+), 29 deletions(-) diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 9292a1cd6..5fd155222 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -780,6 +780,48 @@ size_t ReadFileToString(bool text_file, const char *filename, std::string &str) return file.ReadArray(&str[0], str.size()); } +/** + * Splits the filename into 8.3 format + * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename + * @param filename The normal filename to use + * @param short_name A 9-char array in which the short name will be written + * @param extension A 4-char array in which the extension will be written + */ +void SplitFilename83(const std::string& filename, std::array& short_name, + std::array& extension) { + const std::string forbidden_characters = ".\"/\\[]:;=, "; + + // On a FAT32 partition, 8.3 names are stored as a 11 bytes array, filled with spaces. + short_name = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'}; + extension = {' ', ' ', ' ', '\0'}; + + std::string::size_type point = filename.rfind('.'); + if (point == filename.size() - 1) + point = filename.rfind('.', point); + + // Get short name. + int j = 0; + for (char letter : filename.substr(0, point)) { + if (forbidden_characters.find(letter, 0) != std::string::npos) + continue; + if (j == 8) { + // TODO(Link Mauve): also do that for filenames containing a space. + // TODO(Link Mauve): handle multiple files having the same short name. + short_name[6] = '~'; + short_name[7] = '1'; + break; + } + short_name[j++] = toupper(letter); + } + + // Get extension. + if (point != std::string::npos) { + j = 0; + for (char letter : filename.substr(point + 1, 3)) + extension[j++] = toupper(letter); + } +} + IOFile::IOFile() : m_file(NULL), m_good(true) {} diff --git a/src/common/file_util.h b/src/common/file_util.h index f9d91972f..288734cad 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -131,6 +132,16 @@ std::string &GetExeDirectory(); size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename); size_t ReadFileToString(bool text_file, const char *filename, std::string &str); +/** + * Splits the filename into 8.3 format + * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename + * @param filename The normal filename to use + * @param short_name A 9-char array in which the short name will be written + * @param extension A 4-char array in which the extension will be written + */ +void SplitFilename83(const std::string& filename, std::array& short_name, + std::array& extension); + // simple wrapper for cstdlib file functions to // hopefully will make error checking easier // and make forgetting an fclose() harder diff --git a/src/core/file_sys/directory.h b/src/core/file_sys/directory.h index 9f3546b05..e10431337 100644 --- a/src/core/file_sys/directory.h +++ b/src/core/file_sys/directory.h @@ -19,9 +19,9 @@ namespace FileSys { const size_t FILENAME_LENGTH = 0x20C / 2; struct Entry { char16_t filename[FILENAME_LENGTH]; // Entry name (UTF-16, null-terminated) - char short_name[9]; // 8.3 file name ('longfilename' -> 'LONGFI~1', null-terminated) + std::array short_name; // 8.3 file name ('longfilename' -> 'LONGFI~1', null-terminated) char unknown1; // unknown (observed values: 0x0A, 0x70, 0xFD) - char extension[4]; // 8.3 file extension (set to spaces for directories, null-terminated) + std::array extension; // 8.3 file extension (set to spaces for directories, null-terminated) char unknown2; // unknown (always 0x01) char unknown3; // unknown (0x00 or 0x08) char is_directory; // directory flag diff --git a/src/core/file_sys/directory_sdmc.cpp b/src/core/file_sys/directory_sdmc.cpp index 11e867857..36951564d 100644 --- a/src/core/file_sys/directory_sdmc.cpp +++ b/src/core/file_sys/directory_sdmc.cpp @@ -20,8 +20,8 @@ Directory_SDMC::Directory_SDMC(const Archive_SDMC* archive, const std::string& p // the root directory we set while opening the archive. // For example, opening /../../usr/bin can give the emulated program your installed programs. std::string absolute_path = archive->GetMountPoint() + path; - entry_count = FileUtil::ScanDirectoryTree(absolute_path, entry); - current_entry = 0; + FileUtil::ScanDirectoryTree(absolute_path, directory); + children_iterator = directory.children.begin(); } Directory_SDMC::~Directory_SDMC() { @@ -35,44 +35,39 @@ Directory_SDMC::~Directory_SDMC() { * @return Number of entries listed */ u32 Directory_SDMC::Read(const u32 count, Entry* entries) { - u32 i; - for (i = 0; i < count && current_entry < entry_count; ++i) { - FileUtil::FSTEntry file = entry.children[current_entry]; - std::string filename = file.virtualName; - WARN_LOG(FILESYS, "File %s: size=%d dir=%d", filename.c_str(), file.size, file.isDirectory); + u32 entries_read = 0; - Entry* entry = &entries[i]; + while (entries_read < count && children_iterator != directory.children.cend()) { + const FileUtil::FSTEntry& file = *children_iterator; + const std::string& filename = file.virtualName; + Entry& entry = entries[entries_read]; + + WARN_LOG(FILESYS, "File %s: size=%d dir=%d", filename.c_str(), file.size, file.isDirectory); // TODO(Link Mauve): use a proper conversion to UTF-16. for (int j = 0; j < FILENAME_LENGTH; ++j) { - entry->filename[j] = filename[j]; + entry.filename[j] = filename[j]; if (!filename[j]) break; } - // Split the filename into 8.3 format. - // TODO(Link Mauve): move that to common, I guess, and make it more robust to long filenames. - std::string::size_type n = filename.rfind('.'); - if (n == std::string::npos) { - strncpy(entry->short_name, filename.c_str(), 8); - memset(entry->extension, '\0', 3); - } else { - strncpy(entry->short_name, filename.substr(0, n).c_str(), 8); - strncpy(entry->extension, filename.substr(n + 1).c_str(), 8); - } + FileUtil::SplitFilename83(filename, entry.short_name, entry.extension); - entry->is_directory = file.isDirectory; - entry->file_size = file.size; + entry.is_directory = file.isDirectory; + entry.is_hidden = (filename[0] == '.'); + entry.is_read_only = 0; + entry.file_size = file.size; // We emulate a SD card where the archive bit has never been cleared, as it would be on // most user SD cards. // Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a // file bit. - entry->is_archive = !file.isDirectory; + entry.is_archive = !file.isDirectory; - ++current_entry; + ++entries_read; + ++children_iterator; } - return i; + return entries_read; } /** diff --git a/src/core/file_sys/directory_sdmc.h b/src/core/file_sys/directory_sdmc.h index 0bc6c9eff..cb8d32fda 100644 --- a/src/core/file_sys/directory_sdmc.h +++ b/src/core/file_sys/directory_sdmc.h @@ -37,9 +37,12 @@ public: bool Close() const override; private: - u32 entry_count; - u32 current_entry; - FileUtil::FSTEntry entry; + u32 total_entries_in_directory; + FileUtil::FSTEntry directory; + + // We need to remember the last entry we returned, so a subsequent call to Read will continue + // from the next one. This iterator will always point to the next unread entry. + std::vector::iterator children_iterator; }; } // namespace FileSys