Skip to content

Commit ddee517

Browse files
malmond77m-blaha
authored andcommitted
Return "calculated" checksum if requested w/caching
If a file is downloaded via librepo (e.g. `dnf install --downloadonly`) then a request to get the checksum via `lr_checksum_fd_compare()` will not work. It'll only return whether the checksum is valid, and not the actual checksum. This is the simple fix. Addresses #233
1 parent e44fec3 commit ddee517

4 files changed

Lines changed: 69 additions & 5 deletions

File tree

VERSION.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
SET(LIBREPO_MAJOR "1")
22
SET(LIBREPO_MINOR "13")
3-
SET(LIBREPO_PATCH "0")
3+
SET(LIBREPO_PATCH "1")

librepo.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
%global dnf_conflict 2.8.8
1212

1313
Name: librepo
14-
Version: 1.13.0
14+
Version: 1.13.1
1515
Release: 1%{?dist}
1616
Summary: Repodata downloading library
1717

librepo/checksum.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ lr_checksum_fd_compare(LrChecksumType type,
248248
g_debug("%s: Using checksum cached in xattr: [%s] %s",
249249
__func__, checksum_key, buf);
250250
*matches = (strcmp(expected, buf) == 0);
251+
if (calculated)
252+
*calculated = g_strdup(buf);
251253
return TRUE;
252254
}
253255
} else {

tests/test_checksum.c

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ START_TEST(test_checksum_fd)
9090
}
9191
END_TEST
9292

93-
START_TEST(test_cached_checksum)
93+
START_TEST(test_cached_checksum_matches)
9494
{
9595
FILE *f;
9696
int fd, ret;
@@ -105,7 +105,7 @@ START_TEST(test_cached_checksum)
105105
gchar *checksum_key = g_strconcat(XATTR_CHKSUM_PREFIX, "sha256", NULL);
106106
gchar *mtime_str = NULL;
107107

108-
filename = lr_pathconcat(test_globals.tmpdir, "/test_checksum", NULL);
108+
filename = lr_pathconcat(test_globals.tmpdir, "/test_checksum_matches", NULL);
109109
f = fopen(filename, "w");
110110
fail_if(f == NULL);
111111
fwrite("foo\nbar\n", 1, 8, f);
@@ -178,6 +178,67 @@ START_TEST(test_cached_checksum)
178178
}
179179
END_TEST
180180

181+
START_TEST(test_cached_checksum_value)
182+
{
183+
FILE *f;
184+
int fd, ret;
185+
gboolean checksum_ret, matches;
186+
ssize_t attr_ret;
187+
char *filename;
188+
static char *expected = "d78931fcf2660108eec0d6674ecb4e02401b5256a6b5ee82527766ef6d198c67";
189+
struct stat st;
190+
char buf[256];
191+
GError *tmp_err = NULL;
192+
gchar *timestamp_key = g_strconcat(XATTR_CHKSUM_PREFIX, "mtime", NULL);
193+
gchar *checksum_key = g_strconcat(XATTR_CHKSUM_PREFIX, "sha256", NULL);
194+
gchar *mtime_str = NULL;
195+
gchar *calculated = NULL;
196+
197+
filename = lr_pathconcat(test_globals.tmpdir, "/test_checksum_value", NULL);
198+
f = fopen(filename, "w");
199+
fail_if(f == NULL);
200+
fwrite("foo\nbar\n", 1, 8, f);
201+
fclose(f);
202+
203+
// Assert no cached checksum exists
204+
attr_ret = GETXATTR(filename, timestamp_key, &buf, sizeof(buf)-1);
205+
fail_if(attr_ret != -1); // Cached timestamp should not exists
206+
attr_ret = GETXATTR(filename, checksum_key, &buf, sizeof(buf)-1);
207+
fail_if(attr_ret != -1); // Cached checksum should not exists
208+
209+
// Calculate checksum
210+
fd = open(filename, O_RDONLY);
211+
fail_if(fd < 0);
212+
checksum_ret = lr_checksum_fd_compare(LR_CHECKSUM_SHA256,
213+
fd,
214+
"",
215+
1,
216+
&matches,
217+
&calculated,
218+
&tmp_err);
219+
fail_if(tmp_err);
220+
fail_if(!checksum_ret);
221+
// We pass in an empty string for expected, so we must not match.
222+
fail_if(matches);
223+
close(fd);
224+
fail_if(strcmp(calculated, expected));
225+
226+
// Assert no cached checksum exists
227+
// This assumes issue #235 is unresolved. Once it is, this code
228+
// should fail and the test will need updated.
229+
attr_ret = GETXATTR(filename, timestamp_key, &buf, sizeof(buf)-1);
230+
fail_if(attr_ret != -1); // Cached timestamp should not exists
231+
attr_ret = GETXATTR(filename, checksum_key, &buf, sizeof(buf)-1);
232+
fail_if(attr_ret != -1); // Cached checksum should not exists
233+
234+
lr_free(calculated);
235+
lr_free(filename);
236+
lr_free(timestamp_key);
237+
lr_free(checksum_key);
238+
lr_free(mtime_str);
239+
}
240+
END_TEST
241+
181242
START_TEST(test_cached_checksum_clear)
182243
{
183244
FILE *f;
@@ -242,7 +303,8 @@ checksum_suite(void)
242303
Suite *s = suite_create("checksum");
243304
TCase *tc = tcase_create("Main");
244305
tcase_add_test(tc, test_checksum_fd);
245-
tcase_add_test(tc, test_cached_checksum);
306+
tcase_add_test(tc, test_cached_checksum_matches);
307+
tcase_add_test(tc, test_cached_checksum_value);
246308
tcase_add_test(tc, test_cached_checksum_clear);
247309
suite_add_tcase(s, tc);
248310
return s;

0 commit comments

Comments
 (0)