From 766735b9b1393b422cac814fd9a201364d73a825 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Sun, 6 Sep 2015 22:12:38 -0700 Subject: [PATCH 01/12] Tests show PSP is case sensitive for ISO, so skip tolower() --- Core/FileSystems/ISOFileSystem.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 3d1bf326b3..f2f8967a93 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -325,13 +325,7 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catc for (size_t i=0; ichildren.size(); i++) { std::string n = (e->children[i]->name); - for (size_t j = 0; j < n.size(); j++) { - n[j] = tolower(n[j]); - } std::string curPath = path.substr(0, path.find_first_of('/')); - for (size_t j = 0; j < curPath.size(); j++) { - curPath[j] = tolower(curPath[j]); - } if (curPath == n) { From 7dcb4c9df7a28bbbe917dc61fe3b3ec85b5bda77 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 16:29:46 -0700 Subject: [PATCH 02/12] Move a loop constant substr call out of a for loop --- Core/FileSystems/ISOFileSystem.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index f2f8967a93..3a0a461eb6 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -322,12 +322,12 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catc std::string name = ""; if (path.length()>0) { - for (size_t i=0; ichildren.size(); i++) + std::string firstPathComponent = path.substr(0, path.find_first_of('/')); + for (size_t i = 0; i < e->children.size(); i++) { - std::string n = (e->children[i]->name); - std::string curPath = path.substr(0, path.find_first_of('/')); + std::string n = e->children[i]->name; - if (curPath == n) + if (firstPathComponent == n) { //yay we got it ne = e->children[i]; @@ -695,13 +695,14 @@ std::vector ISOFileSystem::GetDirListing(std::string path) { return myVector; } - +; for (size_t i=0; ichildren.size(); i++) +) { - TreeEntry *e = entry->children[i]; - + TreeEntry *e = entry->children[i] if(!strcmp(e->name.c_str(), ".") || !strcmp(e->name.c_str(), "..")) // do not include the relative entries in the list continue; +ue; PSPFileInfo x; x.name = e->name; From b55015992c5dbad303c113664c103e446e64e34e Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 16:31:37 -0700 Subject: [PATCH 03/12] std::string comparison can be faster than strcmp --- Core/FileSystems/ISOFileSystem.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 3a0a461eb6..7ba84dfcaa 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -695,14 +695,17 @@ std::vector ISOFileSystem::GetDirListing(std::string path) { return myVector; } -; - for (size_t i=0; ichildren.size(); i++) -) + + const std::string dot("."); + const std::string dotdot(".."); + + for (size_t i = 0; i < entry->children.size(); i++) { - TreeEntry *e = entry->children[i] - if(!strcmp(e->name.c_str(), ".") || !strcmp(e->name.c_str(), "..")) // do not include the relative entries in the list - continue; -ue; + TreeEntry *e = entry->children[i]; + + // do not include the relative entries in the list + if (e->name == dot || e->name == dotdot) + continue; PSPFileInfo x; x.name = e->name; From 8fb940373528df1d73e9049b8b9f490f989b2580 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 16:35:23 -0700 Subject: [PATCH 04/12] spacing and readability: add spaces around ==, etc --- Core/FileSystems/ISOFileSystem.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 7ba84dfcaa..3828cbe7f1 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -68,9 +68,9 @@ struct DirectoryEntry { u8 size; u8 sectorsInExtendedRecord; - u32_le firstDataSectorLE; // LBA + u32_le firstDataSectorLE; // LBA u32_be firstDataSectorBE; - u32_le dataLengthLE; // Size + u32_le dataLengthLE; // Size u32_be dataLengthBE; u8 years; u8 month; @@ -79,12 +79,12 @@ struct DirectoryEntry u8 minute; u8 second; u8 offsetFromGMT; - u8 flags; // 2 = directory + u8 flags; // 2 = directory u8 fileUnitSize; u8 interleaveGap; u16_le volSeqNumberLE; u16_be volSeqNumberBE; - u8 identifierLength; //identifier comes right after + u8 identifierLength; //identifier comes right after u8 firstIdChar; #if COMMON_LITTLE_ENDIAN @@ -341,7 +341,7 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catc e = ne; size_t l = name.length(); path.erase(0, l); - if (path.length() == 0 || (path.length()==1 && path[0] == '/')) + if (path.length() == 0 || (path.length() == 1 && path[0] == '/')) return e; path.erase(0, 1); while (path[0] == '/') From 32bea0c0e3cdc11c3488a20e37832aec0f393d77 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 16:36:40 -0700 Subject: [PATCH 05/12] Don't copy each name in a loop --- Core/FileSystems/ISOFileSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 3828cbe7f1..9f3fd37ed0 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -325,7 +325,7 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catc std::string firstPathComponent = path.substr(0, path.find_first_of('/')); for (size_t i = 0; i < e->children.size(); i++) { - std::string n = e->children[i]->name; + const std::string &n = e->children[i]->name; if (firstPathComponent == n) { From fe9055c98e7f25b3c1a6133dc569747ebbd1051e Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 16:42:57 -0700 Subject: [PATCH 06/12] make some things const --- Core/FileSystems/ISOFileSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 9f3fd37ed0..e1fc0e70db 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -263,7 +263,7 @@ void ISOFileSystem::ReadDirectory(u32 startsector, u32 dirsize, TreeEntry *root, } else { - e->name = std::string((char *)&dir.firstIdChar, dir.identifierLength); + e->name = std::string((const char *)&dir.firstIdChar, dir.identifierLength); relative = false; } @@ -322,7 +322,7 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catc std::string name = ""; if (path.length()>0) { - std::string firstPathComponent = path.substr(0, path.find_first_of('/')); + const std::string firstPathComponent = path.substr(0, path.find_first_of('/')); for (size_t i = 0; i < e->children.size(); i++) { const std::string &n = e->children[i]->name; From 481eac71849d6fc17d47b039cc7ee61308c22a19 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 16:44:34 -0700 Subject: [PATCH 07/12] more spaces around operators for readability --- Core/FileSystems/ISOFileSystem.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index e1fc0e70db..2f31318241 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -320,7 +320,7 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catc { TreeEntry *ne = 0; std::string name = ""; - if (path.length()>0) + if (path.length() > 0) { const std::string firstPathComponent = path.substr(0, path.find_first_of('/')); for (size_t i = 0; i < e->children.size(); i++) @@ -416,7 +416,7 @@ u32 ISOFileSystem::OpenFile(std::string filename, FileAccess access, const char return 0; } - if (entry.file==&entireISO) + if (entry.file == &entireISO) entry.isBlockSectorMode = true; entry.seekPos = 0; @@ -682,7 +682,7 @@ PSPFileInfo ISOFileSystem::GetFileInfo(std::string filename) x.exists = true; x.type = entry->isDirectory ? FILETYPE_DIRECTORY : FILETYPE_NORMAL; x.isOnSectorSystem = true; - x.startSector = entry->startingPosition/2048; + x.startSector = entry->startingPosition / 2048; } return x; } From 3f36b476ad916e01f23ebedbf0f1d405b58ba316 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 17:39:17 -0700 Subject: [PATCH 08/12] Add an unordered_map to TreeEntry for faster lookup --- Core/FileSystems/ISOFileSystem.cpp | 23 +++++++++++------------ Core/FileSystems/ISOFileSystem.h | 8 +++++++- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 2f31318241..d80b59c931 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -297,6 +297,11 @@ void ISOFileSystem::ReadDirectory(u32 startsector, u32 dirsize, TreeEntry *root, root->children.push_back(e); } } + + root->fastChildren.reserve(root->children.size()); + for (TreeEntry *e : root->children) { + root->fastChildren[e->name] = e; + } } ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catchError) @@ -323,18 +328,12 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catc if (path.length() > 0) { const std::string firstPathComponent = path.substr(0, path.find_first_of('/')); - for (size_t i = 0; i < e->children.size(); i++) - { - const std::string &n = e->children[i]->name; - - if (firstPathComponent == n) - { - //yay we got it - ne = e->children[i]; - name = n; - break; - } - } + auto child = e->fastChildren.find(firstPathComponent); + if (child != e->fastChildren.end()) { + //yay we got it + ne = child->second; + name = child->first; + } } if (ne) { diff --git a/Core/FileSystems/ISOFileSystem.h b/Core/FileSystems/ISOFileSystem.h index 5e46436ff1..82309bbd7e 100644 --- a/Core/FileSystems/ISOFileSystem.h +++ b/Core/FileSystems/ISOFileSystem.h @@ -19,6 +19,7 @@ #include #include +#include #include "FileSystem.h" @@ -68,7 +69,12 @@ private: bool isDirectory; TreeEntry *parent; - std::vector children; + + // slow lookup, in PSP-accurate sorting order + std::vector children; + + // fast lookup, in undefined order + std::unordered_map fastChildren; }; struct OpenFileEntry From aed469ef9a8009ac637c0c4b4e9a05e093a253f5 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 18:43:10 -0700 Subject: [PATCH 09/12] Advance a pathLength variable instead of calling .erase() in GetFromPath --- Core/FileSystems/ISOFileSystem.cpp | 71 +++++++++++++++++------------- Core/FileSystems/ISOFileSystem.h | 2 +- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index d80b59c931..84b0742ede 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -298,60 +298,69 @@ void ISOFileSystem::ReadDirectory(u32 startsector, u32 dirsize, TreeEntry *root, } } - root->fastChildren.reserve(root->children.size()); - for (TreeEntry *e : root->children) { - root->fastChildren[e->name] = e; - } + root->fastChildren.reserve(root->children.size()); + for (TreeEntry *e : root->children) { + root->fastChildren[e->name] = e; + } } -ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(std::string path, bool catchError) +ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(const std::string &path, bool catchError) { - if (path.length() == 0) { + const size_t pathLength = path.length(); + + if (pathLength == 0) { // Ah, the device! "umd0:" return &entireISO; } - if (path.substr(0,2) == "./") - path.erase(0,2); - - if (path[0] == '/') - path.erase(0,1); + size_t pathIndex = 0; + + // Skip "./" + if (pathLength > pathIndex + 1 && path[pathIndex] == '.' && path[pathIndex + 1] == '/') + pathIndex += 2; + // Skip "/" + if (pathLength > pathIndex && path[pathIndex] == '/') + ++pathIndex; + + if (pathLength <= pathIndex) + return treeroot; + TreeEntry *e = treeroot; - if (path.length() == 0) - return e; - while (true) { - TreeEntry *ne = 0; + TreeEntry *ne = nullptr; std::string name = ""; - if (path.length() > 0) + if (pathLength > pathIndex) { - const std::string firstPathComponent = path.substr(0, path.find_first_of('/')); - auto child = e->fastChildren.find(firstPathComponent); - if (child != e->fastChildren.end()) { - //yay we got it - ne = child->second; - name = child->first; - } + size_t nextSlashIndex = path.find_first_of('/', pathIndex); + if (nextSlashIndex == std::string::npos) + nextSlashIndex = pathLength; + + const std::string firstPathComponent = path.substr(pathIndex, nextSlashIndex - pathIndex); + auto child = e->fastChildren.find(firstPathComponent); + if (child != e->fastChildren.end()) { + //yay we got it + ne = child->second; + name = child->first; + } } + if (ne) { e = ne; - size_t l = name.length(); - path.erase(0, l); - if (path.length() == 0 || (path.length() == 1 && path[0] == '/')) + pathIndex += name.length(); + while (pathIndex < pathLength && path[pathIndex] == '/') + ++pathIndex; + + if (pathLength <= pathIndex) return e; - path.erase(0, 1); - while (path[0] == '/') - path.erase(0, 1); } else { if (catchError) - { ERROR_LOG(FILESYS,"File %s not found", path.c_str()); - } + return 0; } } diff --git a/Core/FileSystems/ISOFileSystem.h b/Core/FileSystems/ISOFileSystem.h index 82309bbd7e..bdfb372f69 100644 --- a/Core/FileSystems/ISOFileSystem.h +++ b/Core/FileSystems/ISOFileSystem.h @@ -100,7 +100,7 @@ private: std::vector restrictTree; void ReadDirectory(u32 startsector, u32 dirsize, TreeEntry *root, size_t level); - TreeEntry *GetFromPath(std::string path, bool catchError = true); + TreeEntry *GetFromPath(const std::string &path, bool catchError = true); std::string EntryFullPath(TreeEntry *e); }; From 4f0ec690a7004e664b0f351536e5711fc2531634 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 18:52:17 -0700 Subject: [PATCH 10/12] Try to fix my whitespace changes --- Core/FileSystems/ISOFileSystem.cpp | 24 +++++++++++------------- Core/FileSystems/ISOFileSystem.h | 10 +++++----- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 84b0742ede..186311cf96 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -307,14 +307,14 @@ void ISOFileSystem::ReadDirectory(u32 startsector, u32 dirsize, TreeEntry *root, ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(const std::string &path, bool catchError) { const size_t pathLength = path.length(); - + if (pathLength == 0) { // Ah, the device! "umd0:" return &entireISO; } size_t pathIndex = 0; - + // Skip "./" if (pathLength > pathIndex + 1 && path[pathIndex] == '.' && path[pathIndex + 1] == '/') pathIndex += 2; @@ -322,10 +322,10 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(const std::string &path, bo // Skip "/" if (pathLength > pathIndex && path[pathIndex] == '/') ++pathIndex; - + if (pathLength <= pathIndex) return treeroot; - + TreeEntry *e = treeroot; while (true) { @@ -336,7 +336,7 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(const std::string &path, bo size_t nextSlashIndex = path.find_first_of('/', pathIndex); if (nextSlashIndex == std::string::npos) nextSlashIndex = pathLength; - + const std::string firstPathComponent = path.substr(pathIndex, nextSlashIndex - pathIndex); auto child = e->fastChildren.find(firstPathComponent); if (child != e->fastChildren.end()) { @@ -699,21 +699,19 @@ std::vector ISOFileSystem::GetDirListing(std::string path) { std::vector myVector; TreeEntry *entry = GetFromPath(path); - if (!entry) - { + if (! entry) return myVector; - } - const std::string dot("."); - const std::string dotdot(".."); + const std::string dot("."); + const std::string dotdot(".."); for (size_t i = 0; i < entry->children.size(); i++) { TreeEntry *e = entry->children[i]; - // do not include the relative entries in the list - if (e->name == dot || e->name == dotdot) - continue; + // do not include the relative entries in the list + if (e->name == dot || e->name == dotdot) + continue; PSPFileInfo x; x.name = e->name; diff --git a/Core/FileSystems/ISOFileSystem.h b/Core/FileSystems/ISOFileSystem.h index bdfb372f69..b9d7cceb0d 100644 --- a/Core/FileSystems/ISOFileSystem.h +++ b/Core/FileSystems/ISOFileSystem.h @@ -69,12 +69,12 @@ private: bool isDirectory; TreeEntry *parent; - - // slow lookup, in PSP-accurate sorting order + + // slow lookup, in PSP-accurate sorting order std::vector children; - - // fast lookup, in undefined order - std::unordered_map fastChildren; + + // fast lookup, in undefined order + std::unordered_map fastChildren; }; struct OpenFileEntry From c7d24407ee67b90f2dba45289becb2da4576fc0e Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Mon, 7 Sep 2015 18:56:18 -0700 Subject: [PATCH 11/12] tests show PSP doesn't tolerate extra /s at the end in an ISO --- Core/FileSystems/ISOFileSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 186311cf96..0ff52440bf 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -336,7 +336,7 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(const std::string &path, bo size_t nextSlashIndex = path.find_first_of('/', pathIndex); if (nextSlashIndex == std::string::npos) nextSlashIndex = pathLength; - + const std::string firstPathComponent = path.substr(pathIndex, nextSlashIndex - pathIndex); auto child = e->fastChildren.find(firstPathComponent); if (child != e->fastChildren.end()) { @@ -350,7 +350,7 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(const std::string &path, bo { e = ne; pathIndex += name.length(); - while (pathIndex < pathLength && path[pathIndex] == '/') + if (pathIndex < pathLength && path[pathIndex] == '/') ++pathIndex; if (pathLength <= pathIndex) From 8c72cb17b1131ce73ea31d74ad0f0b707649e300 Mon Sep 17 00:00:00 2001 From: KentuckyCompass Date: Sat, 12 Sep 2015 21:12:11 -0700 Subject: [PATCH 12/12] remove the unordered_map from ISOFileSystem --- Core/FileSystems/ISOFileSystem.cpp | 23 ++++++++++++----------- Core/FileSystems/ISOFileSystem.h | 6 ------ 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Core/FileSystems/ISOFileSystem.cpp b/Core/FileSystems/ISOFileSystem.cpp index 0ff52440bf..877bcb79ab 100644 --- a/Core/FileSystems/ISOFileSystem.cpp +++ b/Core/FileSystems/ISOFileSystem.cpp @@ -297,11 +297,6 @@ void ISOFileSystem::ReadDirectory(u32 startsector, u32 dirsize, TreeEntry *root, root->children.push_back(e); } } - - root->fastChildren.reserve(root->children.size()); - for (TreeEntry *e : root->children) { - root->fastChildren[e->name] = e; - } } ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(const std::string &path, bool catchError) @@ -338,14 +333,20 @@ ISOFileSystem::TreeEntry *ISOFileSystem::GetFromPath(const std::string &path, bo nextSlashIndex = pathLength; const std::string firstPathComponent = path.substr(pathIndex, nextSlashIndex - pathIndex); - auto child = e->fastChildren.find(firstPathComponent); - if (child != e->fastChildren.end()) { - //yay we got it - ne = child->second; - name = child->first; + for (size_t i = 0; i < e->children.size(); i++) + { + const std::string &n = e->children[i]->name; + + if (firstPathComponent == n) + { + //yay we got it + ne = e->children[i]; + name = n; + break; + } } } - + if (ne) { e = ne; diff --git a/Core/FileSystems/ISOFileSystem.h b/Core/FileSystems/ISOFileSystem.h index b9d7cceb0d..6cf3366a40 100644 --- a/Core/FileSystems/ISOFileSystem.h +++ b/Core/FileSystems/ISOFileSystem.h @@ -19,7 +19,6 @@ #include #include -#include #include "FileSystem.h" @@ -69,12 +68,7 @@ private: bool isDirectory; TreeEntry *parent; - - // slow lookup, in PSP-accurate sorting order std::vector children; - - // fast lookup, in undefined order - std::unordered_map fastChildren; }; struct OpenFileEntry