2019-06-12 21:27:03 +05:30
|
|
|
/*-
|
2020-04-29 17:42:10 +05:30
|
|
|
* Copyright (c) 2019 Juan Romero Pardines.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2019-06-12 21:27:03 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
2020-01-18 21:06:12 +05:30
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2019-06-12 21:27:03 +05:30
|
|
|
#include <xbps.h>
|
|
|
|
#include "../xbps-install/defs.h"
|
|
|
|
|
|
|
|
static void __attribute__((noreturn))
|
2020-04-21 00:55:14 +05:30
|
|
|
usage(bool fail)
|
2019-06-12 21:27:03 +05:30
|
|
|
{
|
|
|
|
fprintf(stdout,
|
|
|
|
"Usage: xbps-fetch [options] <url> <url+N>\n\n"
|
2020-04-21 00:55:14 +05:30
|
|
|
"OPTIONS\n"
|
|
|
|
" -d, --debug Enable debug messages to stderr\n"
|
|
|
|
" -h, --help Show usage\n"
|
|
|
|
" -o, --out <file> Rename downloaded file to <file>\n"
|
|
|
|
" -s, --sha256 Output sha256sums of the files\n"
|
|
|
|
" -v, --verbose Enable verbose output\n"
|
|
|
|
" -V, --version Show XBPS version\n");
|
|
|
|
exit(fail ? EXIT_FAILURE : EXIT_SUCCESS);
|
2019-06-12 21:27:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
fname(char *url)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
|
|
|
|
if ((filename = strrchr(url, '>'))) {
|
|
|
|
*filename = '\0';
|
|
|
|
} else {
|
|
|
|
filename = strrchr(url, '/');
|
|
|
|
}
|
|
|
|
if (filename == NULL)
|
|
|
|
return NULL;
|
|
|
|
return filename + 1;
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:06:12 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 21:27:03 +05:30
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int flags = 0, c = 0, rv = 0;
|
2020-06-26 17:14:54 +05:30
|
|
|
bool failure = false;
|
2019-06-12 21:27:03 +05:30
|
|
|
bool verbose = false;
|
2020-01-18 21:06:12 +05:30
|
|
|
bool shasum = false;
|
2020-12-06 03:30:45 +05:30
|
|
|
struct xbps_handle xh = { 0 };
|
|
|
|
struct xferstat xfer = { 0 };
|
2019-06-12 21:27:03 +05:30
|
|
|
const char *filename = NULL, *progname = argv[0];
|
|
|
|
const struct option longopts[] = {
|
2020-04-21 00:55:14 +05:30
|
|
|
{ "out", required_argument, NULL, 'o' },
|
|
|
|
{ "debug", no_argument, NULL, 'd' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "sha256", no_argument, NULL, 's' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
2019-06-12 21:27:03 +05:30
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2020-01-18 21:06:12 +05:30
|
|
|
while ((c = getopt_long(argc, argv, "o:dhsVv", longopts, NULL)) != -1) {
|
2019-06-12 21:27:03 +05:30
|
|
|
switch (c) {
|
2020-04-21 00:55:14 +05:30
|
|
|
case 'h':
|
|
|
|
usage(false);
|
|
|
|
/* NOTREACHED */
|
2019-06-12 21:27:03 +05:30
|
|
|
case 'o':
|
|
|
|
filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
flags |= XBPS_FLAG_DEBUG;
|
|
|
|
break;
|
2020-01-18 21:06:12 +05:30
|
|
|
case 's':
|
|
|
|
shasum = true;
|
|
|
|
break;
|
2019-06-12 21:27:03 +05:30
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
printf("%s\n", XBPS_RELVER);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case '?':
|
|
|
|
default:
|
2020-04-21 00:55:14 +05:30
|
|
|
usage(true);
|
|
|
|
/* NOTREACHED */
|
2019-06-12 21:27:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2020-04-21 00:55:14 +05:30
|
|
|
if (!argc) {
|
|
|
|
usage(true);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2019-06-12 21:27:03 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize libxbps.
|
|
|
|
*/
|
|
|
|
xh.flags = flags;
|
|
|
|
xh.fetch_cb = fetch_file_progress_cb;
|
|
|
|
xh.fetch_cb_data = &xfer;
|
|
|
|
if ((rv = xbps_init(&xh)) != 0) {
|
|
|
|
xbps_error_printf("%s: failed to initialize libxbps: %s\n",
|
|
|
|
progname, strerror(rv));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++) {
|
2020-02-10 06:24:52 +05:30
|
|
|
unsigned char digest[XBPS_SHA256_DIGEST_SIZE];
|
2020-01-18 21:06:12 +05:30
|
|
|
|
2019-06-20 02:38:31 +05:30
|
|
|
if (i > 0 || !filename)
|
2019-06-12 21:27:03 +05:30
|
|
|
filename = fname(argv[i]);
|
|
|
|
|
2020-01-18 21:06:12 +05:30
|
|
|
if (shasum) {
|
2020-02-10 06:24:52 +05:30
|
|
|
rv = xbps_fetch_file_dest_sha256(&xh, argv[i], filename, verbose ? "v" : "", digest, sizeof digest);
|
2020-01-18 21:06:12 +05:30
|
|
|
} else {
|
|
|
|
rv = xbps_fetch_file_dest(&xh, argv[i], filename, verbose ? "v" : "");
|
|
|
|
}
|
2019-06-12 21:27:03 +05:30
|
|
|
|
|
|
|
if (rv == -1) {
|
2020-06-26 17:10:45 +05:30
|
|
|
xbps_error_printf("%s: failed to fetch: %s: %s\n",
|
|
|
|
progname, argv[i], xbps_fetch_error_string());
|
2020-06-26 17:14:54 +05:30
|
|
|
failure = true;
|
2020-06-26 17:10:45 +05:30
|
|
|
continue;
|
2020-06-26 17:14:54 +05:30
|
|
|
} else if (rv == -1) {
|
2020-01-18 21:06:12 +05:30
|
|
|
fprintf(stderr, "%s: file is identical with remote.\n", argv[i]);
|
2020-02-10 06:24:52 +05:30
|
|
|
if (shasum) {
|
|
|
|
if (!xbps_file_sha256_raw(digest, sizeof digest, filename)) {
|
2020-06-26 17:10:45 +05:30
|
|
|
xbps_error_printf("%s: failed to hash: %s: %s\n",
|
|
|
|
progname, filename, strerror(rv));
|
2020-06-26 17:14:54 +05:30
|
|
|
failure = true;
|
2020-06-26 17:10:45 +05:30
|
|
|
continue;
|
2020-02-10 06:24:52 +05:30
|
|
|
}
|
|
|
|
}
|
2019-06-12 21:27:03 +05:30
|
|
|
}
|
2020-03-05 17:23:48 +05:30
|
|
|
if (shasum) {
|
2020-01-18 21:06:12 +05:30
|
|
|
print_digest(digest, SHA256_DIGEST_LENGTH);
|
|
|
|
printf(" %s\n", filename);
|
|
|
|
}
|
2019-06-12 21:27:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
xbps_end(&xh);
|
2020-06-26 17:14:54 +05:30
|
|
|
exit(failure ? EXIT_FAILURE : EXIT_SUCCESS);
|
2019-06-12 21:27:03 +05:30
|
|
|
}
|