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

View File

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