bin/xbps-fetch: add a mode that prints sha256sums of downloaded files

This commit is contained in:
Duncan Overbruck 2020-01-18 16:36:12 +01:00
parent 013177cec3
commit c98d732d9c
No known key found for this signature in database
GPG Key ID: 335C1D17EC3D6E35
2 changed files with 42 additions and 4 deletions

View File

@ -31,6 +31,8 @@
#include <unistd.h>
#include <getopt.h>
#include <openssl/sha.h>
#include <xbps.h>
#include "../xbps-install/defs.h"
@ -43,6 +45,7 @@ usage(void)
" -d\t\tEnable debug messages to stderr\n"
" -h\t\tShow usage()\n"
" -o <file>\tRename downloaded file to <file>\n"
" -s\t\tOutput sha256sums of the files\n"
" -v\t\tEnable verbose output\n"
" -V\t\tPrints the xbps release version\n");
exit(EXIT_FAILURE);
@ -63,11 +66,28 @@ fname(char *url)
return filename + 1;
}
static void
print_digest(const uint8_t *digest, size_t len)
{
while (len--) {
if (*digest / 16 < 10)
putc('0' + *digest / 16, stdout);
else
putc('a' + *digest / 16 - 10, stdout);
if (*digest % 16 < 10)
putc('0' + *digest % 16, stdout);
else
putc('a' + *digest % 16 - 10, stdout);
++digest;
}
}
int
main(int argc, char **argv)
{
int flags = 0, c = 0, rv = 0;
bool verbose = false;
bool shasum = false;
struct xbps_handle xh = {};
struct xferstat xfer = {};
const char *filename = NULL, *progname = argv[0];
@ -75,7 +95,7 @@ main(int argc, char **argv)
{ NULL, 0, NULL, 0 }
};
while ((c = getopt_long(argc, argv, "o:dhVv", longopts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "o:dhsVv", longopts, NULL)) != -1) {
switch (c) {
case 'o':
filename = optarg;
@ -83,6 +103,9 @@ main(int argc, char **argv)
case 'd':
flags |= XBPS_FLAG_DEBUG;
break;
case 's':
shasum = true;
break;
case 'v':
verbose = true;
break;
@ -115,18 +138,31 @@ main(int argc, char **argv)
}
for (int i = 0; i < argc; i++) {
unsigned char *digest = NULL;
if (i > 0 || !filename)
filename = fname(argv[i]);
rv = xbps_fetch_file_dest(&xh, argv[i], filename, verbose ? "v" : "");
if (shasum) {
rv = xbps_fetch_file_dest_digest(&xh, argv[i], filename, verbose ? "v" : "", &digest);
} else {
rv = xbps_fetch_file_dest(&xh, argv[i], filename, verbose ? "v" : "");
}
if (rv == -1) {
fprintf(stderr, "%s: %s\n", argv[i], xbps_fetch_error_string());
} else if (rv == 0) {
printf("%s: file is identical with remote.\n", argv[i]);
fprintf(stderr, "%s: file is identical with remote.\n", argv[i]);
if (shasum)
digest = xbps_file_hash_raw(filename);
} else {
rv = 0;
}
if (digest != NULL) {
print_digest(digest, SHA256_DIGEST_LENGTH);
printf(" %s\n", filename);
free(digest);
}
}
xbps_end(&xh);

View File

@ -1,4 +1,4 @@
.Dd June 12, 2019
.Dd January 18, 2020
.Dt XBPS-FETCH 1
.Sh NAME
.Nm xbps-fetch
@ -28,6 +28,8 @@ Rename file from specified URL to
.Ar output .
.It Fl v
Enables verbose messages.
.It Fl s
Print sha256sums of downloaded files.
.It Fl V
Show the version information.
.El