+
+set<string> pkgadd::apply_install_rules(const string& name, pkginfo_t& info, const vector<rule_t>& rules)
+{
+ // TODO: better algo(?)
+ set<string> install_set;
+ set<string> non_install_set;
+ vector<rule_t> found;
+
+ find_rules(rules, INSTALL, found);
+
+ for (set<string>::const_iterator i = info.files.begin(); i != info.files.end(); i++) {
+ bool install_file = true;
+
+ for (vector<rule_t>::reverse_iterator j = found.rbegin(); j != found.rend(); j++) {
+ if (rule_applies_to_file(*j, *i)) {
+ install_file = (*j).action;
+ break;
+ }
+ }
+
+ if (install_file)
+ install_set.insert(install_set.end(), *i);
+ else
+ non_install_set.insert(*i);
+ }
+
+ info.files.clear();
+ info.files = install_set;
+
+#ifndef NDEBUG
+ cerr << "Install set:" << endl;
+ for (set<string>::iterator j = info.files.begin(); j != info.files.end(); j++) {
+ cerr << " " << (*j) << endl;
+ }
+ cerr << endl;
+
+ cerr << "Non-Install set:" << endl;
+ for (set<string>::iterator j = non_install_set.begin(); j != non_install_set.end(); j++) {
+ cerr << " " << (*j) << endl;
+ }
+ cerr << endl;
+#endif
+
+ return non_install_set;
+}
+
+void pkgadd::find_rules(const vector<rule_t>& rules, rule_event_t event, vector<rule_t>& found) const
+{
+ for (vector<rule_t>::const_iterator i = rules.begin(); i != rules.end(); i++)
+ if (i->event == event)
+ found.push_back(*i);
+}
+
+bool pkgadd::rule_applies_to_file(const rule_t& rule, const string& file) const
+{
+ regex_t preg;
+ bool ret;
+
+ if (regcomp(&preg, rule.pattern.c_str(), REG_EXTENDED | REG_NOSUB))
+ throw runtime_error("error compiling regular expression '" + rule.pattern + "', aborting");
+
+ ret = !regexec(&preg, file.c_str(), 0, 0, 0);
+ regfree(&preg);
+
+ return ret;
+}