Skip to content

Commit 02fc512

Browse files
malmond77m-blaha
authored andcommitted
Expose librepo's checksum functions via SWIG
DNF has been carrying around yum's old checksum function. These functions duplicate code in librepo. They are slower because librepo can employ caching of digests. Lastly, these functions in Python do not know about changes in checksum logic like rpm-software-management/librepo#222 The choices here are: 1. Replicate `lr_checksum_cow_fd()` and caching logic in Python 2. Just use librepo from dnf. This is 2. Note there was bug in librepo that forces no caching for `checksum_value()` (rpm-software-management/librepo#233). This is now fixed, so we depend on librepo-1.13.1 to ensure this fix is present. This change goes hand in hand with a change to `dnf` itself to make use of the new functions and eliminate the old ones. This is rpm-software-management/dnf#1743. We bump the version of this library in the next commit to ensure this dependency is expressed properly. On errors, these functions raise libdnf.error.Error which can be easily mapped into MiscError in dnf
1 parent e99ed03 commit 02fc512

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

bindings/swig/utils.i

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ namespace libdnf { namespace filesystem {
4444

4545
void decompress(const char * inPath, const char * outPath, mode_t outMode, const char * compressType = nullptr);
4646

47+
bool checksum_check(const char * type, const char * inPath, const char * checksum_valid);
48+
std::string checksum_value(const char * type, const char * inPath);
49+
4750
}}

libdnf/utils/utils.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "utils.hpp"
22
#include "libdnf/dnf-sack-private.hpp"
33
#include "libdnf/sack/advisorymodule.hpp"
4+
#include <librepo/librepo.h>
45

56
#include <tinyformat/tinyformat.hpp>
67

@@ -300,6 +301,55 @@ void decompress(const char * inPath, const char * outPath, mode_t outMode, const
300301
fclose(inFile);
301302
}
302303

304+
void checksum(const char * type, const char * inPath, const char * checksum_valid, bool * valid_out, gchar ** calculated_out)
305+
{
306+
GError * errP{nullptr};
307+
gboolean valid;
308+
LrChecksumType lr_type = lr_checksum_type(type);
309+
310+
if (lr_type == LR_CHECKSUM_UNKNOWN)
311+
throw libdnf::Error(tfm::format("Unknown checksum type %s", type));
312+
313+
auto inFd = open(inPath, O_RDONLY);
314+
315+
if (inFd == -1)
316+
throw libdnf::Error(tfm::format("Error opening %s: %s", inPath, strerror(errno)));
317+
318+
auto ret = lr_checksum_fd_compare(lr_type,
319+
inFd,
320+
/**
321+
* If checksum_valid references a string, pass it in, else use
322+
* an empty string
323+
*/
324+
checksum_valid ? checksum_valid : "",
325+
TRUE,
326+
&valid,
327+
calculated_out,
328+
&errP);
329+
330+
close(inFd);
331+
if (!ret)
332+
throw libdnf::Error(tfm::format("Error calculating checksum %s: (%d, %s)", inPath, errP->code, errP->message));
333+
if (valid_out)
334+
*valid_out = valid == TRUE; /* gboolean -> bool */
335+
}
336+
337+
338+
bool checksum_check(const char * type, const char * inPath, const char * checksum_valid)
339+
{
340+
bool valid;
341+
checksum(type, inPath, checksum_valid, &valid, NULL);
342+
return valid;
343+
}
344+
345+
std::string checksum_value(const char * type, const char * inPath)
346+
{
347+
g_autofree gchar *calculated = NULL;
348+
checksum(type, inPath, NULL, NULL, &calculated);
349+
std::string out(calculated);
350+
return out;
351+
}
352+
303353
}
304354

305355
namespace numeric {

libdnf/utils/utils.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ std::vector<std::string> getDirContent(const std::string &dirPath);
6868
* @param compressType Type of compression (".bz2", ".gz", ...), nullptr - detect from inPath filename. Defaults to nullptr.
6969
*/
7070
void decompress(const char * inPath, const char * outPath, mode_t outMode, const char * compressType = nullptr);
71+
72+
/**
73+
* @brief checksum file and return if matching.
74+
*
75+
* @param type Checksum type ("sha", "sha1", "sha256" etc). Raises libdnf::Error if invalid.
76+
* @param inPath Path to input file
77+
* @param valid_checksum hexadecimal encoded checksum string.
78+
*/
79+
bool checksum_check(const char * type, const char * inPath, const char * valid_checksum);
80+
/**
81+
* @brief checksum file and return checksum.
82+
*
83+
* @param type Checksum type ("sha", "sha1", "sha256" etc). Raises libdnf::Error if invalid.
84+
* @param inPath Path to input file
85+
*/
86+
std::string checksum_value(const char * type, const char * inPath);
7187
}
7288

7389
namespace numeric {

0 commit comments

Comments
 (0)