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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
module;
#include <cstdint>
#include <sqlite3.h>
#include <string.h>
#include <gio/gio.h>
#include <curl/curl.h>
#include <sqlite3.h>
#include <string>
#include <vector>
#include <optional>
export module katja.utils;
export namespace katja
{
enum class Info
{
// Error.
unknown,
// Installed in the same version.
installed,
// A different version is installed.
updating,
// Available, but not installed.
installing,
// Available.
available
};
struct JobData
{
sqlite3 *db;
CURL *curl;
virtual void package(Info info, const char *package_id, const char *summary) = 0;
virtual void files(const std::vector<std::string>&) = 0;
virtual void details(char *package_id,
const char *group, const char *description, const char *homepage, int uncompressed) = 0;
virtual void set_percentage(double) = 0;
};
struct DummyJobData final : JobData
{
void package(Info info, const char *package_id, const char *summary) override
{
}
void files(const std::vector<std::string>&) override
{
}
void details(char *package_id,
const char *group, const char *description, const char *homepage, int uncompressed) override
{
}
void set_percentage(double) override
{
}
};
/**
* katja::get_file:
* @curl: curl easy handle.
* @source_url: source url.
* @dest: destination.
*
* Download the file.
*
* Returns: CURLE_OK (zero) on success, non-zero otherwise.
**/
CURLcode get_file(CURL **curl, const char *source_url, const char *dest)
{
const char *dest_dir_name;
FILE *fout = nullptr;
CURLcode ret;
glong response_code;
if ((*curl == nullptr) && (!(*curl = curl_easy_init())))
{
return CURLE_BAD_FUNCTION_ARGUMENT;
}
curl_easy_setopt(*curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(*curl, CURLOPT_URL, source_url);
if (dest == nullptr)
{
curl_easy_setopt(*curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(*curl, CURLOPT_HEADER, 1L);
ret = curl_easy_perform(*curl);
curl_easy_getinfo(*curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200)
{
ret = CURLE_REMOTE_FILE_NOT_FOUND;
}
}
else
{
if (g_file_test(dest, G_FILE_TEST_IS_DIR))
{
dest_dir_name = dest;
dest = g_strconcat(dest_dir_name, g_strrstr(source_url, "/"), nullptr);
g_free(const_cast<char *>(dest_dir_name));
}
if ((fout = fopen(dest, "ab")) == nullptr)
{
return CURLE_WRITE_ERROR;
}
curl_easy_setopt(*curl, CURLOPT_WRITEDATA, fout);
ret = curl_easy_perform(*curl);
}
curl_easy_reset(*curl);
if (fout != nullptr)
{
fclose(fout);
}
return ret;
}
/**
* katja::split_package_name:
* Got the name of a package, without version-arch-release data.
**/
std::optional<std::vector<std::string>> split_package_name(const std::string& pkg_filename)
{
char *pkg_full_name;
int len = pkg_filename.size();
if (len < 4)
{
return std::nullopt;
}
std::vector<std::string> pkg_tokens(5);
if (pkg_filename[len - 4] == '.')
{
/* Full name without extension */
len -= 4;
pkg_full_name = g_strndup(pkg_filename.data(), len);
pkg_tokens[3] = g_strdup(pkg_full_name);
/* The last 3 characters should be the file extension */
pkg_tokens[4] = g_strdup(pkg_filename.data() + len + 1);
}
else
{
pkg_full_name = g_strdup(pkg_filename.c_str());
}
/* Reverse all of the bytes in the package filename to get the name, version and the architecture */
g_strreverse (pkg_full_name);
char **reversed_tokens = g_strsplit(pkg_full_name, "-", 4);
pkg_tokens[0] = g_strreverse(reversed_tokens[3]); /* Name */
pkg_tokens[1] = g_strreverse(reversed_tokens[2]); /* Version */
pkg_tokens[2] = g_strreverse(reversed_tokens[1]); /* Architecture */
g_free(reversed_tokens[0]); /* Build number */
g_free(reversed_tokens);
g_free(pkg_full_name);
return pkg_tokens;
}
/**
* katja::is_installed:
* Checks if a package is already installed in the system.
*
* @pkg_fullname: Package name should be looked for.
*
* Returns: Package installation information.
**/
Info is_installed(const char *pkg_fullname)
{
GFileEnumerator *pkg_metadata_enumerator;
GFileInfo *pkg_metadata_file_info;
GFile *pkg_metadata_dir;
Info ret = Info::installing;
const char *it;
std::uint8_t dashes = 0;
ptrdiff_t pkg_name;
g_return_val_if_fail(pkg_fullname != nullptr, Info::unknown);
// We want to find the package name without version for the package we're
// looking for.
g_debug("Looking if %s is installed", pkg_fullname);
for (it = pkg_fullname + strlen(pkg_fullname); it != pkg_fullname; --it)
{
if (*it == '-')
{
if (dashes == 2)
{
break;
}
++dashes;
}
}
if (dashes < 2)
{
return Info::unknown;
}
pkg_name = it - pkg_fullname;
// Read the package metadata directory and comprare all installed packages
// with ones in the cache.
pkg_metadata_dir = g_file_new_for_path("/var/log/packages");
if (!(pkg_metadata_enumerator = g_file_enumerate_children(pkg_metadata_dir,
"standard::name",
G_FILE_QUERY_INFO_NONE,
nullptr,
nullptr)))
{
g_object_unref(pkg_metadata_dir);
return Info::unknown;
}
while ((pkg_metadata_file_info = g_file_enumerator_next_file(pkg_metadata_enumerator, nullptr, nullptr)))
{
const char *dir = g_file_info_get_name(pkg_metadata_file_info);
dashes = 0;
if (strcmp(dir, pkg_fullname) == 0)
{
ret = Info::installed;
}
else
{
for (it = dir + strlen(dir); it != dir; --it)
{
if (*it == '-')
{
if (dashes == 2)
{
break;
}
++dashes;
}
}
if (pkg_name == (it - dir) && strncmp(pkg_fullname, dir, pkg_name) == 0)
{
ret = Info::updating;
}
}
g_object_unref(pkg_metadata_file_info);
if (ret != Info::installing) /* If installed */
{
break;
}
}
g_object_unref(pkg_metadata_enumerator);
g_object_unref(pkg_metadata_dir);
return ret;
}
}
|