summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/maintenance/useGlobalProcessor.cpp
blob: 4ce8d21693488004efe155fcbe1eb3c8bf03b69f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "useGlobalProcessor.h"
#include <modifycommand.h>
#include <tablepatch.h>
#include <fileUtils.h>
#include "utils/dbUtils.h"
#include <glibmm/regex.h>

using namespace AdHoc::FileUtils;

static Glib::RefPtr<Glib::Regex> useDesc = Glib::Regex::create("^([^#\\s][^ ]*)\\s+-\\s+(.*)$", Glib::RegexCompileFlags::REGEX_MULTILINE);

namespace Gentoo {
	namespace Service {
		unsigned char UseGlobalProcessor::phase() const { return 2; }
		unsigned char UseGlobalProcessor::order() const { return 10; }
		bool UseGlobalProcessor::match(const PathParts & pp) const
		{
			return (pp.size() == 2 && pp[0] == "profiles" && pp[1] == "use.desc");
		}

		void
		UseGlobalProcessor::created(DB::Connection * dbc, int64_t r, const StringList & fn, const std::filesystem::path & path)
		{
			modified(dbc, r, fn, path);
		}

		void
		UseGlobalProcessor::modified(DB::Connection * dbc, int64_t, const StringList &, const std::filesystem::path & path)
		{
			DB::TablePatch p;
			p.dest = "gentoobrowse.use_global";
			p.src = Utils::Database::emptyClone(dbc, p.dest);
			p.pk = { "use" };
			p.cols = { "use", "description" };

			AdHoc::FileUtils::MemMap u(path);
			Glib::ustring d(std::string(reinterpret_cast<const char *>(u.data), u.getStat().st_size));
			Glib::MatchInfo matches;
			auto i = Utils::Database::tablePatchInserter(dbc, p);
			for (useDesc->match(d, matches); matches.get_match_count() == 3; matches.next()) {
				i->bindParamS(0, matches.fetch(2));
				i->bindParamS(1, matches.fetch(1));
				i->execute();
			}
			dbc->patchTable(&p);

			Utils::Database::drop(dbc, p.src);
		}

		void
		UseGlobalProcessor::deleted(DB::Connection * dbc, int64_t, const StringList &)
		{
			dbc->modify("DELETE FROM gentoobrowse.use_global")->execute();
		}
	}
}
FACTORY(Gentoo::Service::UseGlobalProcessor, Gentoo::Service::FileProcessorFactory);