Skip to content

Commit 3899f5a

Browse files
committed
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 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 ddf5adf commit 3899f5a

5 files changed

Lines changed: 72 additions & 3 deletions

File tree

VERSION.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
set (DEFAULT_LIBDNF_MAJOR_VERSION 0)
22
set (DEFAULT_LIBDNF_MINOR_VERSION 45)
3-
set (DEFAULT_LIBDNF_MICRO_VERSION 1)
3+
set (DEFAULT_LIBDNF_MICRO_VERSION 2)
44

55
if(DEFINED LIBDNF_MAJOR_VERSION)
66
if(NOT ${DEFAULT_LIBDNF_MAJOR_VERSION} STREQUAL ${LIBDNF_MAJOR_VERSION})

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.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
%global libsolv_version 0.7.7
22
%global libmodulemd_version 2.5.0
3-
%global librepo_version 1.11.0
3+
%global librepo_version 1.13.1
44
%global dnf_conflict 4.2.13
55
%global swig_version 3.0.12
66
%global libdnf_major_version 0
77
%global libdnf_minor_version 45
8-
%global libdnf_micro_version 1
8+
%global libdnf_micro_version 2
99

1010
# set sphinx package name according to distro
1111
%global requires_python2_sphinx python2-sphinx

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

@@ -311,6 +312,55 @@ void decompress(const char * inPath, const char * outPath, mode_t outMode, const
311312
fclose(inFile);
312313
}
313314

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+
314364
}
315365

316366
namespace numeric {

libdnf/utils/utils.hpp

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

7187
namespace numeric {

0 commit comments

Comments
 (0)