From 7c49f771048db99041e5544530282df07f4b0bae Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Fri, 29 Jul 2016 00:58:22 -0700 Subject: [PATCH] Support renaming files under an ldid::UnionFolder. --- ldid.cpp | 44 ++++++++++++++++++++++++++++++++------------ ldid.hpp | 20 ++++++++++++++++++-- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/ldid.cpp b/ldid.cpp index 62b7498..02e2675 100644 --- a/ldid.cpp +++ b/ldid.cpp @@ -1727,19 +1727,35 @@ void SubFolder::Find(const std::string &path, const Functorsecond; +} + +void UnionFolder::Map(const std::string &path, const Functor &)> &)> &code, const std::string &file, const Functor &)> &save) { + if (file.size() >= path.size() && file.substr(0, path.size()) == path) + code(file.substr(path.size()), fun([&](const Functor &code) { + parent_.Save(file, fun([&](std::streambuf &data) { + save(data, code); + })); + })); +} + UnionFolder::UnionFolder(Folder &parent) : parent_(parent) { } void UnionFolder::Save(const std::string &path, const Functor &code) { - return parent_.Save(path, code); + return parent_.Save(Map(path), code); } bool UnionFolder::Open(const std::string &path, const Functor &code) { - auto file(files_.find(path)); - if (file == files_.end()) - return parent_.Open(path, code); + auto file(resets_.find(path)); + if (file == resets_.end()) + return parent_.Open(Map(path), code); auto &data(file->second); data.pubseekpos(0, std::ios::in); @@ -1749,18 +1765,22 @@ bool UnionFolder::Open(const std::string &path, const Functor &)> &)> &code) { parent_.Find(path, fun([&](const std::string &name, const Functor &)> &save) { - if (files_.find(path + name) == files_.end()) + if (deletes_.find(path + name) == deletes_.end()) code(name, save); })); - for (auto &file : files_) - if (file.first.size() >= path.size() && file.first.substr(0, path.size()) == path) - code(file.first.substr(path.size()), fun([&](const Functor &code) { - parent_.Save(file.first, fun([&](std::streambuf &save) { - file.second.pubseekpos(0, std::ios::in); - code(file.second, save); - })); + for (auto &reset : resets_) + Map(path, code, reset.first, fun([&](std::streambuf &save, const Functor &code) { + reset.second.pubseekpos(0, std::ios::in); + code(reset.second, save); + })); + + for (auto &remap : remaps_) + Map(path, code, remap.first, fun([&](std::streambuf &save, const Functor &code) { + parent_.Open(remap.second, fun([&](std::streambuf &data) { + code(data, save); })); + })); } #ifndef LDID_NOTOOLS diff --git a/ldid.hpp b/ldid.hpp index 40b1c8b..5b2efda 100644 --- a/ldid.hpp +++ b/ldid.hpp @@ -107,7 +107,13 @@ class UnionFolder : }; Folder &parent_; - std::map files_; + std::set deletes_; + + std::map remaps_; + std::map resets_; + + std::string Map(const std::string &path); + void Map(const std::string &path, const Functor &)> &)> &code, const std::string &file, const Functor &)> &save); public: UnionFolder(Folder &parent); @@ -116,8 +122,18 @@ class UnionFolder : virtual bool Open(const std::string &path, const Functor &code); virtual void Find(const std::string &path, const Functor &)> &)> &code); + void operator ()(const std::string &from) { + deletes_.insert(from); + } + + void operator ()(const std::string &from, const std::string &to) { + operator ()(from); + remaps_[to] = from; + } + std::stringbuf &operator [](const std::string &path) { - return files_[path]; + operator ()(path); + return resets_[path]; } }; -- 2.45.2