|
1 | 1 | #include "utils.hpp" |
2 | 2 | #include "libdnf/dnf-sack-private.hpp" |
3 | 3 | #include "libdnf/sack/advisorymodule.hpp" |
| 4 | +#include <librepo/librepo.h> |
4 | 5 |
|
5 | 6 | #include <tinyformat/tinyformat.hpp> |
6 | 7 |
|
@@ -311,6 +312,55 @@ void decompress(const char * inPath, const char * outPath, mode_t outMode, const |
311 | 312 | fclose(inFile); |
312 | 313 | } |
313 | 314 |
|
| 315 | +void checksum(const char * type, const char * inPath, const char * checksum_valid, bool * valid_out, gchar ** calculated_out) |
| 316 | +{ |
| 317 | + GError * errP{nullptr}; |
| 318 | + gboolean valid; |
| 319 | + LrChecksumType lr_type = lr_checksum_type(type); |
| 320 | + |
| 321 | + if (lr_type == LR_CHECKSUM_UNKNOWN) |
| 322 | + throw libdnf::Error(tfm::format("Unknown checksum type %s", type)); |
| 323 | + |
| 324 | + auto inFd = open(inPath, O_RDONLY); |
| 325 | + |
| 326 | + if (inFd == -1) |
| 327 | + throw libdnf::Error(tfm::format("Error opening %s: %s", inPath, strerror(errno))); |
| 328 | + |
| 329 | + auto ret = lr_checksum_fd_compare(lr_type, |
| 330 | + inFd, |
| 331 | + /** |
| 332 | + * If checksum_valid references a string, pass it in, else use |
| 333 | + * an empty string |
| 334 | + */ |
| 335 | + checksum_valid ? checksum_valid : "", |
| 336 | + TRUE, |
| 337 | + &valid, |
| 338 | + calculated_out, |
| 339 | + &errP); |
| 340 | + |
| 341 | + close(inFd); |
| 342 | + if (!ret) |
| 343 | + throw libdnf::Error(tfm::format("Error calculating checksum %s: (%d, %s)", inPath, errP->code, errP->message)); |
| 344 | + if (valid_out) |
| 345 | + *valid_out = valid == TRUE; /* gboolean -> bool */ |
| 346 | +} |
| 347 | + |
| 348 | + |
| 349 | +bool checksum_check(const char * type, const char * inPath, const char * checksum_valid) |
| 350 | +{ |
| 351 | + bool valid; |
| 352 | + checksum(type, inPath, checksum_valid, &valid, NULL); |
| 353 | + return valid; |
| 354 | +} |
| 355 | + |
| 356 | +std::string checksum_value(const char * type, const char * inPath) |
| 357 | +{ |
| 358 | + g_autofree gchar *calculated = NULL; |
| 359 | + checksum(type, inPath, NULL, NULL, &calculated); |
| 360 | + std::string out(calculated); |
| 361 | + return out; |
| 362 | +} |
| 363 | + |
314 | 364 | } |
315 | 365 |
|
316 | 366 | namespace numeric { |
|
0 commit comments