+std::string DiskFolder::Path(const std::string &path) {
+ return path_ + "/" + path;
+}
+
+DiskFolder::DiskFolder(const std::string &path) :
+ path_(path)
+{
+}
+
+DiskFolder::~DiskFolder() {
+ if (!std::uncaught_exception())
+ for (const auto &commit : commit_)
+ Commit(commit.first, commit.second);
+}
+
+void DiskFolder::Find(const std::string &root, const std::string &base, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)>&code) {
+ std::string path(Path(root) + base);
+
+ DIR *dir(opendir(path.c_str()));
+ _assert(dir != NULL);
+ _scope({ _syscall(closedir(dir)); });
+
+ while (auto child = readdir(dir)) {
+ std::string name(child->d_name);
+ if (name == "." || name == "..")
+ continue;
+ if (Starts(name, ".ldid."))
+ continue;
+
+ bool directory;
+
+#ifdef __WIN32__
+ struct stat info;
+ _syscall(stat(path.c_str(), &info));
+ if (false);
+ else if (S_ISDIR(info.st_mode))
+ directory = true;
+ else if (S_ISREG(info.st_mode))
+ directory = false;
+ else
+ _assert_(false, "st_mode=%x", info.st_mode);
+#else
+ switch (child->d_type) {
+ case DT_DIR:
+ directory = true;
+ break;
+ case DT_REG:
+ directory = false;
+ break;
+ default:
+ _assert_(false, "d_type=%u", child->d_type);
+ }
+#endif
+
+ if (directory)
+ Find(root, base + name + "/", code);
+ else
+ code(base + name, fun([&](const Functor<void (std::streambuf &, std::streambuf &)> &code) {
+ std::string access(root + base + name);
+ _assert_(Open(access, fun([&](std::streambuf &data) {
+ NullBuffer save;
+ code(data, save);
+ })), "open(): %s", access.c_str());
+ }));
+ }
+}
+
+void DiskFolder::Save(const std::string &path, const Functor<void (std::streambuf &)> &code) {
+ std::filebuf save;
+ auto from(Path(path));
+ commit_[from] = Temporary(save, from);
+ code(save);
+}
+
+bool DiskFolder::Open(const std::string &path, const Functor<void (std::streambuf &)> &code) {
+ std::filebuf data;
+ auto result(data.open(Path(path).c_str(), std::ios::binary | std::ios::in));
+ if (result == NULL)
+ return false;
+ _assert(result == &data);
+ code(data);
+ return true;
+}
+
+void DiskFolder::Find(const std::string &path, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)>&code) {
+ Find(path, "", code);
+}
+
+SubFolder::SubFolder(Folder &parent, const std::string &path) :
+ parent_(parent),
+ path_(path)
+{
+}
+
+void SubFolder::Save(const std::string &path, const Functor<void (std::streambuf &)> &code) {
+ return parent_.Save(path_ + path, code);
+}
+
+bool SubFolder::Open(const std::string &path, const Functor<void (std::streambuf &)> &code) {
+ return parent_.Open(path_ + path, code);
+}
+
+void SubFolder::Find(const std::string &path, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)> &code) {
+ return parent_.Find(path_ + path, code);
+}
+
+UnionFolder::UnionFolder(Folder &parent) :
+ parent_(parent)
+{
+}
+
+void UnionFolder::Save(const std::string &path, const Functor<void (std::streambuf &)> &code) {
+ return parent_.Save(path, code);
+}
+
+bool UnionFolder::Open(const std::string &path, const Functor<void (std::streambuf &)> &code) {
+ auto file(files_.find(path));
+ if (file == files_.end())
+ return parent_.Open(path, code);
+
+ auto &data(file->second);
+ data.pubseekpos(0, std::ios::in);
+ code(data);
+ return true;
+}
+
+void UnionFolder::Find(const std::string &path, const Functor<void (const std::string &, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &)> &code) {
+ parent_.Find(path, fun([&](const std::string &name, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &save) {
+ if (files_.find(name) == files_.end())
+ code(name, save);
+ }));
+
+ for (auto &file : files_)
+ code(file.first, fun([&](const Functor<void (std::streambuf &, std::streambuf &)> &code) {
+ parent_.Save(file.first, fun([&](std::streambuf &save) {
+ file.second.pubseekpos(0, std::ios::in);
+ code(file.second, save);
+ }));
+ }));
+}
+
+static size_t copy(std::streambuf &source, std::streambuf &target) {
+ size_t total(0);
+ for (;;) {
+ char data[4096];
+ size_t writ(source.sgetn(data, sizeof(data)));
+ if (writ == 0)
+ break;
+ _assert(target.sputn(data, writ) == writ);
+ total += writ;
+ }
+ return total;
+}
+
+#ifndef LDID_NOPLIST
+static plist_t plist(const std::string &data) {
+ plist_t plist(NULL);
+ if (Starts(data, "bplist00"))
+ plist_from_bin(data.data(), data.size(), &plist);
+ else
+ plist_from_xml(data.data(), data.size(), &plist);
+ _assert(plist != NULL);
+ return plist;
+}
+
+static void plist_d(std::streambuf &buffer, const Functor<void (plist_t)> &code) {
+ std::stringbuf data;
+ copy(buffer, data);
+ auto node(plist(data.str()));
+ _scope({ plist_free(node); });
+ _assert(plist_get_node_type(node) == PLIST_DICT);
+ code(node);
+}
+
+static std::string plist_s(plist_t node) {
+ _assert(node != NULL);
+ _assert(plist_get_node_type(node) == PLIST_STRING);
+ char *data;
+ plist_get_string_val(node, &data);
+ _scope({ free(data); });
+ return data;
+}
+#endif
+
+enum Mode {
+ NoMode,
+ OptionalMode,
+ OmitMode,
+ NestedMode,
+ TopMode,
+};
+
+class Expression {
+ private:
+ regex_t regex_;
+
+ public:
+ Expression(const std::string &code) {
+ _assert_(regcomp(®ex_, code.c_str(), REG_EXTENDED | REG_NOSUB) == 0, "regcomp()");
+ }
+
+ ~Expression() {
+ regfree(®ex_);
+ }
+
+ bool operator ()(const std::string &data) const {
+ auto value(regexec(®ex_, data.c_str(), 0, NULL, 0));
+ if (value == REG_NOMATCH)
+ return false;
+ _assert_(value == 0, "regexec()");
+ return true;
+ }
+};
+
+struct Rule {
+ unsigned weight_;
+ Mode mode_;
+ std::string code_;
+
+ mutable std::auto_ptr<Expression> regex_;
+
+ Rule(unsigned weight, Mode mode, const std::string &code) :
+ weight_(weight),
+ mode_(mode),
+ code_(code)
+ {
+ }
+
+ Rule(const Rule &rhs) :
+ weight_(rhs.weight_),
+ mode_(rhs.mode_),
+ code_(rhs.code_)
+ {
+ }
+
+ void Compile() const {
+ regex_.reset(new Expression(code_));
+ }
+
+ bool operator ()(const std::string &data) const {
+ _assert(regex_.get() != NULL);
+ return (*regex_)(data);
+ }
+
+ bool operator <(const Rule &rhs) const {
+ if (weight_ > rhs.weight_)
+ return true;
+ if (weight_ < rhs.weight_)
+ return false;
+ return mode_ > rhs.mode_;
+ }
+};
+
+struct RuleCode {
+ bool operator ()(const Rule *lhs, const Rule *rhs) const {
+ return lhs->code_ < rhs->code_;
+ }
+};
+
+#ifndef LDID_NOPLIST
+std::string Bundle(const std::string &root, Folder &folder, const std::string &key, std::map<std::string, std::vector<char>> &remote, const std::string &entitlements) {
+ std::string executable;
+ std::string identifier;
+
+ static const std::string info("Info.plist");
+
+ _assert_(folder.Open(info, fun([&](std::streambuf &buffer) {
+ plist_d(buffer, fun([&](plist_t node) {
+ executable = plist_s(plist_dict_get_item(node, "CFBundleExecutable"));
+ identifier = plist_s(plist_dict_get_item(node, "CFBundleIdentifier"));
+ }));
+ })), "open(): Info.plist");
+
+ std::map<std::string, std::multiset<Rule>> versions;
+
+ auto &rules1(versions[""]);
+ auto &rules2(versions["2"]);
+
+ static const std::string signature("_CodeSignature/CodeResources");
+
+ folder.Open(signature, fun([&](std::streambuf &buffer) {
+ plist_d(buffer, fun([&](plist_t node) {
+ // XXX: maybe attempt to preserve existing rules
+ }));
+ }));
+
+ if (true) {
+ rules1.insert(Rule{1, NoMode, "^"});
+ rules1.insert(Rule{10000, OmitMode, "^(Frameworks/[^/]+\\.framework/|PlugIns/[^/]+\\.appex/|PlugIns/[^/]+\\.appex/Frameworks/[^/]+\\.framework/|())SC_Info/[^/]+\\.(sinf|supf|supp)$"});
+ rules1.insert(Rule{1000, OptionalMode, "^.*\\.lproj/"});
+ rules1.insert(Rule{1100, OmitMode, "^.*\\.lproj/locversion.plist$"});
+ rules1.insert(Rule{10000, OmitMode, "^Watch/[^/]+\\.app/(Frameworks/[^/]+\\.framework/|PlugIns/[^/]+\\.appex/|PlugIns/[^/]+\\.appex/Frameworks/[^/]+\\.framework/)SC_Info/[^/]+\\.(sinf|supf|supp)$"});
+ rules1.insert(Rule{1, NoMode, "^version.plist$"});
+ }
+
+ if (true) {
+ rules2.insert(Rule{11, NoMode, ".*\\.dSYM($|/)"});
+ rules2.insert(Rule{20, NoMode, "^"});
+ rules2.insert(Rule{2000, OmitMode, "^(.*/)?\\.DS_Store$"});
+ rules2.insert(Rule{10000, OmitMode, "^(Frameworks/[^/]+\\.framework/|PlugIns/[^/]+\\.appex/|PlugIns/[^/]+\\.appex/Frameworks/[^/]+\\.framework/|())SC_Info/[^/]+\\.(sinf|supf|supp)$"});
+ rules2.insert(Rule{10, NestedMode, "^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/"});
+ rules2.insert(Rule{1, NoMode, "^.*"});
+ rules2.insert(Rule{1000, OptionalMode, "^.*\\.lproj/"});
+ rules2.insert(Rule{1100, OmitMode, "^.*\\.lproj/locversion.plist$"});
+ rules2.insert(Rule{20, OmitMode, "^Info\\.plist$"});
+ rules2.insert(Rule{20, OmitMode, "^PkgInfo$"});
+ rules2.insert(Rule{10000, OmitMode, "^Watch/[^/]+\\.app/(Frameworks/[^/]+\\.framework/|PlugIns/[^/]+\\.appex/|PlugIns/[^/]+\\.appex/Frameworks/[^/]+\\.framework/)SC_Info/[^/]+\\.(sinf|supf|supp)$"});
+ rules2.insert(Rule{10, NestedMode, "^[^/]+$"});
+ rules2.insert(Rule{20, NoMode, "^embedded\\.provisionprofile$"});
+ rules2.insert(Rule{20, NoMode, "^version\\.plist$"});
+ }
+
+ std::map<std::string, std::vector<char>> local;
+
+ static Expression nested("^PlugIns/[^/]*\\.appex/Info\\.plist$");
+
+ folder.Find("", fun([&](const std::string &name, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &code) {
+ if (!nested(name))
+ return;
+ auto bundle(root + Split(name).dir);
+ SubFolder subfolder(folder, bundle);
+ Bundle(bundle, subfolder, key, local, "");
+ }));
+
+ folder.Find("", fun([&](const std::string &name, const Functor<void (const Functor<void (std::streambuf &, std::streambuf &)> &)> &code) {
+ if (name == executable || name == signature)
+ return;
+
+ auto &hash(local[name]);
+ if (!hash.empty())
+ return;
+
+ code(fun([&](std::streambuf &data, std::streambuf &save) {
+ HashProxy proxy(hash, save);
+ copy(data, proxy);
+ }));
+
+ _assert(hash.size() == LDID_SHA1_DIGEST_LENGTH);
+ }));
+
+ auto plist(plist_new_dict());
+ _scope({ plist_free(plist); });
+
+ for (const auto &version : versions) {
+ auto files(plist_new_dict());
+ plist_dict_set_item(plist, ("files" + version.first).c_str(), files);
+
+ for (const auto &rule : version.second)
+ rule.Compile();
+
+ for (const auto &hash : local)
+ for (const auto &rule : version.second)
+ if (rule(hash.first)) {
+ if (rule.mode_ == NoMode)
+ plist_dict_set_item(files, hash.first.c_str(), plist_new_data(hash.second.data(), hash.second.size()));
+ else if (rule.mode_ == OptionalMode) {
+ auto entry(plist_new_dict());
+ plist_dict_set_item(entry, "hash", plist_new_data(hash.second.data(), hash.second.size()));
+ plist_dict_set_item(entry, "optional", plist_new_bool(true));
+ plist_dict_set_item(files, hash.first.c_str(), entry);
+ }
+
+ break;
+ }
+ }
+
+ for (const auto &version : versions) {
+ auto rules(plist_new_dict());
+ plist_dict_set_item(plist, ("rules" + version.first).c_str(), rules);
+
+ std::multiset<const Rule *, RuleCode> ordered;
+ for (const auto &rule : version.second)
+ ordered.insert(&rule);
+
+ for (const auto &rule : ordered)
+ if (rule->weight_ == 1 && rule->mode_ == NoMode)
+ plist_dict_set_item(rules, rule->code_.c_str(), plist_new_bool(true));
+ else {
+ auto entry(plist_new_dict());
+ plist_dict_set_item(rules, rule->code_.c_str(), entry);
+
+ switch (rule->mode_) {
+ case NoMode:
+ break;
+ case OmitMode:
+ plist_dict_set_item(entry, "omit", plist_new_bool(true));
+ break;
+ case OptionalMode:
+ plist_dict_set_item(entry, "optional", plist_new_bool(true));
+ break;
+ case NestedMode:
+ plist_dict_set_item(entry, "nested", plist_new_bool(true));
+ break;
+ case TopMode:
+ plist_dict_set_item(entry, "top", plist_new_bool(true));
+ break;
+ }
+
+ if (rule->weight_ >= 10000)
+ plist_dict_set_item(entry, "weight", plist_new_uint(rule->weight_));
+ else if (rule->weight_ != 1)
+ plist_dict_set_item(entry, "weight", plist_new_real(rule->weight_));
+ }
+ }
+
+ folder.Save(signature, fun([&](std::streambuf &save) {
+ HashProxy proxy(local[signature], save);
+ char *xml(NULL);
+ uint32_t size;
+ plist_to_xml(plist, &xml, &size);
+ _scope({ free(xml); });
+ put(proxy, xml, size);
+ }));
+
+ folder.Open(executable, fun([&](std::streambuf &buffer) {
+ // XXX: this is a miserable fail
+ std::stringbuf temp;
+ copy(buffer, temp);
+ auto data(temp.str());
+
+ folder.Save(executable, fun([&](std::streambuf &save) {
+ Slots slots;
+ slots[1] = local.at(info);
+ slots[3] = local.at(signature);
+
+ HashProxy proxy(local[executable], save);
+ Sign(data.data(), data.size(), proxy, identifier, entitlements, key, slots);
+ }));
+ }));
+
+ for (const auto &hash : local)
+ remote[root + hash.first] = hash.second;
+
+ return executable;
+}
+#endif
+