|
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,74 @@ void decompress(const char * inPath, const char * outPath, mode_t outMode, const |
311 | 312 | fclose(inFile); |
312 | 313 | } |
313 | 314 |
|
| 315 | +bool checksum_check(const char * type, const char * inPath, const char * checksum_valid) |
| 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_cmp(lr_type, |
| 330 | + inFd, |
| 331 | + checksum_valid, |
| 332 | + TRUE, /* use xattr value */ |
| 333 | + &valid, |
| 334 | + &errP); |
| 335 | + |
| 336 | + if (!ret) { |
| 337 | + close(inFd); |
| 338 | + throw libdnf::Error(tfm::format("Error checking checksum %s: (%d, %s)", inPath, errP->code, errP->message)); |
| 339 | + } |
| 340 | + close(inFd); |
| 341 | + // gboolean -> bool |
| 342 | + return valid == TRUE; |
| 343 | +} |
| 344 | + |
| 345 | +std::string checksum_value(const char * type, const char * inPath) |
| 346 | +{ |
| 347 | + GError * errP{nullptr}; |
| 348 | + gboolean valid; /* this is thrown away, we're not checking, we're just calculating it */ |
| 349 | + gchar * calculated = NULL; |
| 350 | + LrChecksumType lr_type = lr_checksum_type(type); |
| 351 | + |
| 352 | + if (lr_type == LR_CHECKSUM_UNKNOWN) |
| 353 | + throw libdnf::Error(tfm::format("Unknown checksum type %s", type)); |
| 354 | + |
| 355 | + auto inFd = open(inPath, O_RDONLY); |
| 356 | + |
| 357 | + if (inFd == -1) |
| 358 | + throw libdnf::Error(tfm::format("Error opening %s: %s", inPath, strerror(errno))); |
| 359 | + |
| 360 | + auto ret = lr_checksum_fd_compare(lr_type, |
| 361 | + inFd, |
| 362 | + "", /* we're calling this to get the file's checksum not compare it */ |
| 363 | + /** |
| 364 | + * https://github.com/rpm-software-management/librepo/issues/233 |
| 365 | + * We must pass FALSE here because librepo has a simple bug that |
| 366 | + * doesn't set calculated if the checksum is cached |
| 367 | + */ |
| 368 | + FALSE, |
| 369 | + &valid, |
| 370 | + &calculated, |
| 371 | + &errP); |
| 372 | + |
| 373 | + if (!ret) { |
| 374 | + close(inFd); |
| 375 | + throw libdnf::Error(tfm::format("Error calculating checksum %s: (%d, %s)", inPath, errP->code, errP->message)); |
| 376 | + } |
| 377 | + close(inFd); |
| 378 | + std::string out(calculated); |
| 379 | + g_free(calculated); |
| 380 | + return out; |
| 381 | +} |
| 382 | + |
314 | 383 | } |
315 | 384 |
|
316 | 385 | namespace numeric { |
|
0 commit comments