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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#include "ebuildMetaProcessor.h"
#include <modifycommand.h>
#include <sqlWriter.h>
#include <boost/filesystem/operations.hpp>
#include <glibmm/regex.h>
#include <tablepatch.h>
#include "fileUtils.h"
#include "xmlUtils.h"
#include "dbUtils.h"
#include <sql/maintenance/categoryInsert.sql.h>
#include <sql/maintenance/packageInsert.sql.h>
#include <sql/maintenance/packagePrune.sql.h>
#include <sql/maintenance/categoryPrune.sql.h>
#include <sql/maintenance/ebuildInsert.sql.h>
#include <sql/maintenance/ebuildUpdate.sql.h>
#include <sql/maintenance/ebuildDelete.sql.h>
namespace U = Gentoo::Utils;
using namespace Gentoo::Utils::File;
static Glib::RefPtr<Glib::Regex> packageVersion = Glib::Regex::create("^(.+)-([0-9].*)$");
namespace Gentoo {
namespace Service {
class EbuildCacheParser : public U::MemMap {
public:
typedef std::pair<const char *, const char *> Range;
typedef std::map<std::string, const Range> KVs;
EbuildCacheParser(const boost::filesystem::path & p) :
U::MemMap(p)
{
const char * chardata = (const char *)this->data;
while (const char * eq = strchr(chardata, '=')) {
if (const char * nl = strchr(eq + 1, '\n')) {
kvs.insert({ std::string(chardata, eq), { eq + 1, nl } });
chardata = nl + 1;
}
else {
kvs.insert({ std::string(chardata, eq), { eq + 1, (const char *)this->data + st.st_size } });
return;
}
}
}
boost::optional<Glib::ustring> get(const std::string & key) const
{
auto kvi = kvs.find(key);
if (kvi == kvs.end()) {
return boost::optional<Glib::ustring>();
}
return Glib::ustring(kvi->second.first, kvi->second.second);
}
private:
KVs kvs;
};
class SplitEbuildProps : public DB::SqlWriter {
public:
SplitEbuildProps(int e, const std::string & p) :
ebuildId(e),
props(p)
{
}
void writeSql(AdHoc::Buffer & sql) override
{
sql.append("(SELECT ?::int ebuildId, trim(regexp_split_to_table(?, '\\s+'), '+') use)");
}
void bindParams(DB::Command * c, unsigned int & offset) override
{
c->bindParamI(offset++, ebuildId);
c->bindParamS(offset++, props);
}
const int ebuildId;
const std::string props;
};
const int EbuildMetaProcessor::FILETYPEID = 1;
void
EbuildMetaProcessor::created(DB::Connection * dbc, const boost::filesystem::path & fn, const boost::filesystem::path & path) const
{
Glib::MatchInfo matches;
const Glib::ustring pv = (fn / 4).string();
if (packageVersion->match(pv, matches)) {
EbuildCacheParser ecp(path);
const std::string repoName = (fn / 0).string();
const std::string categoryName = (fn / 3).string();
const std::string packageName = matches.fetch(1);
const std::string ebuildVersion = matches.fetch(2);
// Maybe create a category
auto ci = dbc->modify(sql::maintenance::categoryInsert::sql);
ci->bindParamS(0, categoryName);
ci->bindParamS(1, categoryName);
ci->execute();
// Maybe create a package
auto pi = dbc->modify(sql::maintenance::packageInsert::sql);
pi->bindParamS(0, packageName);
Utils::Database::bindOptionalsS(pi.get(), 1, { ecp.get("DESCRIPTION") });
pi->bindParamS(2, packageName);
pi->bindParamS(3, categoryName);
pi->execute();
// Create an ebuild
auto m = dbc->modify(sql::maintenance::ebuildInsert::sql);
m->bindParamS(0, ebuildVersion);
m->bindParamS(1, ebuildVersion);
Utils::Database::bindOptionalsS(m.get(), 2, { ecp.get("SLOT") });
Utils::Database::bindOptionalsS(m.get(), 3, { ecp.get("LICENSE") });
m->bindParamT(4, boost::posix_time::from_time_t(ecp.getStat().st_mtim.tv_sec));
m->bindParamS(5, repoName);
m->bindParamS(6, categoryName);
m->bindParamS(7, packageName);
m->forEachRow<int64_t>([dbc,&ecp] (auto ebuildId) {
// USE flags
DB::TablePatch t;
SplitEbuildProps sep(ebuildId, ecp.get("USE").value_or(Glib::ustring()));
t.dest = "gentoobrowse.ebuild_uses";
t.srcExpr = &sep;
t.pk = { "ebuildid", "use" };
t.cols = { "ebuildid", "use" };
dbc->patchTable(&t);
});
}
}
void
EbuildMetaProcessor::modified(DB::Connection * dbc, const boost::filesystem::path & fn, const boost::filesystem::path & path) const
{
Glib::MatchInfo matches;
const Glib::ustring pv = (fn / 4).string();
if (packageVersion->match(pv, matches)) {
auto m = dbc->modify(sql::maintenance::ebuildUpdate::sql);
EbuildCacheParser ecp(path);
Utils::Database::bindOptionalsS(m, 0, { ecp.get("SLOT") });
Utils::Database::bindOptionalsS(m, 1, { ecp.get("LICENSE") });
m->bindParamT(2, boost::posix_time::from_time_t(ecp.getStat().st_mtim.tv_sec));
m->bindParamS(3, (fn / 0).string()); // repo
m->bindParamS(4, (fn / 3).string()); // category
m->bindParamS(5, matches.fetch(1)); // package
m->bindParamS(6, matches.fetch(2)); // version
m->execute(false);
}
}
void
EbuildMetaProcessor::deleted(DB::Connection * dbc, const boost::filesystem::path & fn) const
{
Glib::MatchInfo matches;
const Glib::ustring pv = (fn / 4).string();
if (packageVersion->match(pv, matches)) {
const std::string repoName = (fn / 0).string();
const std::string categoryName = (fn / 3).string();
const std::string packageName = matches.fetch(1);
const std::string ebuildVersion = matches.fetch(2);
// Delete ebuild
auto m = dbc->modify(sql::maintenance::ebuildDelete::sql);
m->bindParamS(0, repoName);
m->bindParamS(1, categoryName);
m->bindParamS(2, packageName);
m->bindParamS(3, ebuildVersion);
m->execute(false);
// Prune packages
auto pp = dbc->modify(sql::maintenance::packagePrune::sql);
pp->bindParamS(0, categoryName);
pp->execute();
// Prune categories
dbc->execute(sql::maintenance::categoryPrune::sql);
}
}
}
}
|