From 6c52c185a624f6e7a319ae1ae00419e000dd3e7b Mon Sep 17 00:00:00 2001 From: CompuCat Date: Mon, 15 Jun 2026 14:50:12 -0500 Subject: [PATCH 1/4] buildfix: missing #include in Utils.hpp --- src/Utils.cpp | 6 +----- src/Utils.hpp | 3 ++- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Utils.cpp b/src/Utils.cpp index 06f7957..386a047 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -26,8 +26,6 @@ #include #endif -#include -#include #include #include #include @@ -36,10 +34,8 @@ #include #include #include -#include #include #include -#include #include #include #include @@ -762,4 +758,4 @@ std::string getHumanReadableBytes(uint64_t bytes) ss.precision(places); ss << std::fixed << size << " " << suffixes[suffix]; return ss.str(); -} \ No newline at end of file +} diff --git a/src/Utils.hpp b/src/Utils.hpp index 8d2eaef..da82ea6 100644 --- a/src/Utils.hpp +++ b/src/Utils.hpp @@ -7,6 +7,7 @@ #endif #include +#include #include #include @@ -90,4 +91,4 @@ std::string dir_name(const std::string& file_path); bool compareLen(const std::string& a, const std::string& b); bool is_dir(std::string_view path, struct dirent* ent); std::string getHumanReadableBytes(uint64_t bytes); -#endif \ No newline at end of file +#endif From 3afaee173d27cd1d11764d93e282aa59a7b36ca6 Mon Sep 17 00:00:00 2001 From: vgmoose Date: Mon, 6 Jul 2026 16:42:08 -0400 Subject: [PATCH 2/4] Utils: comment out my_strptime wrapper this wrapper served to help wii, 3ds, and windows platforms, but has exhibited strange behavior where setlocale returns unexpected values in some build environments, causing crashes when listing apps from 4tu discord: https://discord.com/channels/339118412414582786/616331814021103674/1523757035282366574 "it appears commit e5a9f552ecf908f1639b08729edc191c66f4e7d0 from 2025 refactored newlib's locale code, changing the output of setlocale(LC_ALL, nullptr) = setlocale_r(_, 0, 0) unless DEFAULT_LOCALE_IS_C ... to a value that throws (and crashes if uncaught) when fed to std::locale::locale()" - @nyanpasu64 my_strptime will one day return! See also: https://github.com/fortheusers/hb-appstore/pull/21 --- src/Utils.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Utils.cpp b/src/Utils.cpp index 386a047..a5c7293 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -149,14 +149,17 @@ char* my_strptime(const char* s, const char* f, struct tm* tm) { - std::istringstream input(s); - input.imbue(std::locale(setlocale(LC_ALL, nullptr))); - input >> std::get_time(tm, f); - if (input.fail()) - { - return nullptr; - } - return (char*)(s + input.tellg()); + // just use normal strptime, the DIY implementation was being Weird + return strptime(s, f, tm); + + // std::istringstream input(s); + // input.imbue(std::locale(setlocale(LC_ALL, nullptr))); + // input >> std::get_time(tm, f); + // if (input.fail()) + // { + // return nullptr; + // } + // return (char*)(s + input.tellg()); } // http://stackoverflow.com/a/11366985 From 9b6ae07bcef24ea0c35e4c1ecb4bab80b0929bf8 Mon Sep 17 00:00:00 2001 From: Tim Jochen Kicker Date: Wed, 18 Nov 2026 12:17:19 +0200 Subject: [PATCH 3/4] recover from corrupted repos.json --- Makefile | 2 +- src/Get.cpp | 75 +++++++++++++++++++++++++++++------ tests/15_CorruptReposJson.hpp | 50 +++++++++++++++++++++++ tests/main.cpp | 2 + 4 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 tests/15_CorruptReposJson.hpp diff --git a/Makefile b/Makefile index e6e053c..053db31 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ build: g++ -g cli/*.cpp src/*.cpp src/repos/*.cpp -std=gnu++20 -lm -I $(RAPIDJSON) $(MINIZIP_O) -I $(MINIZIP) -lz -lcurl -o get -DAPP_VERSION=\"$(APP_VERSION)\" run_tests: - rm -rf tests/.get/packages tests/.get/tmp tests/.get/repos + rm -rf tests/.get/packages tests/.get/tmp tests/.get/repos tests/.get-corrupt gcc -c $(MINIZIP)/*.c g++ -g tests/*.cpp src/*.cpp src/repos/*.cpp -std=gnu++20 -lm -L/usr/lib -I $(RAPIDJSON) $(MINIZIP_O) -I $(MINIZIP) -lz -lcurl -o get_tests @echo "Starting test HTTP server with range request support..." diff --git a/src/Get.cpp b/src/Get.cpp index 70e851b..ade1049 100644 --- a/src/Get.cpp +++ b/src/Get.cpp @@ -1,11 +1,17 @@ #include #include +#include +#include #include #include #include #include #include +#if !defined(WIN32) && !defined(_WIN32) +#include +#endif + #include "rapidjson/document.h" #include "rapidjson/istreamwrapper.h" #include "rapidjson/stringbuffer.h" @@ -18,6 +24,26 @@ using namespace rapidjson; +// Writes the whole string in one go and syncs it to the card, so we don't +// leave a half written repos.json behind. No tmp file or rename here, since +// rename doesn't replace an existing file on every homebrew platform. +static bool writeWholeFile(const std::string& path, const char* data) +{ + FILE* file = fopen(path.c_str(), "wb"); + if (!file) + return false; + + size_t len = strlen(data); + bool ok = fwrite(data, 1, len, file) == len && fflush(file) == 0; + +#if !defined(WIN32) && !defined(_WIN32) + if (ok) + fsync(fileno(file)); +#endif + + return fclose(file) == 0 && ok; +} + bool debug = false; Get::Get( @@ -184,13 +210,13 @@ void Get::saveRepos() { d.AddMember("repos", reposOut, allocator); - std::ofstream file(mRepos_path); StringBuffer buffer; Writer writer(buffer); d.Accept(writer); std::cout << buffer.GetString() << std::endl; - file << buffer.GetString(); - file.close(); + + if (!writeWholeFile(mRepos_path, buffer.GetString())) + printf("--> Could not write %s\n", mRepos_path.c_str()); } /** @@ -201,27 +227,32 @@ void Get::loadRepos() repos.clear(); auto& config_path = mRepos_path; + + auto makeDefaultRepo = [this]() -> std::unique_ptr { +#if defined(WII) || defined(_3DS) || defined(WII_MOCK) + return GetRepo::createRepo("Default Repo", this->mDefaultRepo, true, this->mDefaultRepoType, mPkg_path); +#else + return std::make_unique("Default Repo", this->mDefaultRepo, true); +#endif + }; + std::ifstream ifs(config_path); + bool generated = false; if (!ifs.good() || ifs.peek() == std::ifstream::traits_type::eof()) { printf("--> Could not load repos from %s, generating default GET repos.json\n", config_path.c_str()); -#if defined(WII) || defined(_3DS) || defined(WII_MOCK) - auto defaultRepo = GetRepo::createRepo("Default Repo", this->mDefaultRepo, true, this->mDefaultRepoType, mPkg_path); -#else - auto defaultRepo = std::make_unique("Default Repo", this->mDefaultRepo, true); -#endif + auto defaultRepo = makeDefaultRepo(); Document d; d.Parse(Repo::generateRepoJson(*defaultRepo).c_str()); - std::ofstream file(config_path); StringBuffer buffer; Writer writer(buffer); d.Accept(writer); - file << buffer.GetString(); - file.close(); + ifs.close(); + writeWholeFile(config_path, buffer.GetString()); ifs = std::ifstream(config_path); @@ -233,6 +264,7 @@ void Get::loadRepos() repos.push_back(std::move(defaultRepo)); return; } + generated = true; } IStreamWrapper isw(ifs); @@ -242,7 +274,26 @@ void Get::loadRepos() if (!ok || !doc.HasMember("repos")) { - printf("--> Invalid format in %s", config_path.c_str()); + printf("--> Invalid format in %s\n", config_path.c_str()); + ifs.close(); + + // keep the broken file (e.g. NUL bytes after a power cut) as .bad and + // start over with a default one. The old .bad goes first, since rename + // can't overwrite on some platforms. + if (!generated) + { + std::string badPath = config_path + ".bad"; + std::remove(badPath.c_str()); + if (std::rename(config_path.c_str(), badPath.c_str()) == 0) + { + printf("--> Moved %s to %s\n", config_path.c_str(), badPath.c_str()); + loadRepos(); + return; + } + } + + // can't move it or our own default is broken, so stay in memory + repos.push_back(makeDefaultRepo()); return; } diff --git a/tests/15_CorruptReposJson.hpp b/tests/15_CorruptReposJson.hpp new file mode 100644 index 0000000..0f08253 --- /dev/null +++ b/tests/15_CorruptReposJson.hpp @@ -0,0 +1,50 @@ +#include "tests.hpp" +#include + +// A repos.json full of NUL bytes (what a FAT card can leave behind after a +// power cut) should be kept as repos.json.bad and replaced by the default. + +class CorruptReposJsonTest : public Test { + public: + CorruptReposJsonTest() { + purpose = "Recover from a corrupted repos.json"; + } + bool execute() + { + std::string dir = "./tests/.get-corrupt/"; + std::string reposPath = dir + "repos.json"; + std::string badPath = reposPath + ".bad"; + + mkpath("./tests/.get-corrupt"); + std::remove(badPath.c_str()); + + { + std::ofstream file(reposPath, std::ios::binary); + file << std::string(200, '\0'); + } + + Get corrupt(dir, URL "a", false); + auto& repos = corrupt.getRepos(); + if (repos.size() != 1 || repos[0]->getUrl() != URL "a") + { + error << "Expected only the default repo, found " << repos.size() << " repos"; + return false; + } + + if (!exists(badPath.c_str())) + { + error << "The broken file wasn't kept as " << badPath; + return false; + } + + // the new file on disk has to load again + Get reloaded(dir, URL "a", false); + if (reloaded.getRepos().size() != 1) + { + error << "The regenerated repos.json didn't load again"; + return false; + } + + return true; + } +}; diff --git a/tests/main.cpp b/tests/main.cpp index ad6e467..bd6557c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -16,6 +16,7 @@ #include "12_RepoAddAndRemove.hpp" #include "13_ResumeDownloadTest.hpp" #include "14_LargeDownloadProgressTest.hpp" +#include "15_CorruptReposJson.hpp" using namespace std; @@ -65,6 +66,7 @@ int main() tests.push_back(std::make_unique()); tests.push_back(std::make_unique()); tests.push_back(std::make_unique()); + tests.push_back(std::make_unique()); // tests.push_back(std::make_unique()); // tests.push_back(std::make_unique()); From e5a353ff86ebfa4005ac93874e619bedd02edac3 Mon Sep 17 00:00:00 2001 From: Tim Jochen Kicker Date: Thu, 19 Nov 2026 15:38:15 +0200 Subject: [PATCH 4/4] fix ci on ubuntu 26.04 --- .github/workflows/main.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cb19cb5..c6e83c3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,8 +17,7 @@ jobs: submodules: recursive - name: Build and test run: | - sudo apt-get update && sudo apt-get -y install python3 wget zlib1g-dev gcc g++ libcurl4-openssl-dev libxml2 xz-utils lzma build-essential libssl-dev - pip3 install rangehttpserver + sudo apt-get update && sudo apt-get -y install python3 python3-rangehttpserver wget zip zlib1g-dev gcc g++ libcurl4-openssl-dev xz-utils lzma build-essential libssl-dev make && make run_tests - uses: actions/upload-artifact@v4 with: