summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/maintenance/useGlobalProcessor.cpp
blob: 811cd8779e9200f3349b5615c6a23faeacdd0bd3 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "useGlobalProcessor.h"
#include "maintenance/abstractFileProcessor.h"
#include "utils/dbUtils.h"
#include "wrap/regex.h"
#include "wrap/ustring.h"
#include <command_fwd.h>
#include <connection.h>
#include <ext/alloc_traits.h>
#include <factory.h>
#include <fileUtils.h>
#include <memory>
#include <modifycommand.h>
#include <string>
#include <tablepatch.h>

namespace Glib {
	class Regex;
}

using namespace AdHoc::FileUtils;

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

	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(u.sv()));
		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);