2015-08-26 06:14:36 +00:00
|
|
|
/* MIT license
|
|
|
|
|
|
|
|
Copyright (C) 2015 Natanael Copa <ncopa@alpinelinux.org>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-04-20 15:05:40 +00:00
|
|
|
#include <sys/file.h>
|
2015-08-26 06:14:36 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
2016-05-16 13:15:42 +00:00
|
|
|
#include <errno.h>
|
2015-08-26 06:14:36 +00:00
|
|
|
#include <fcntl.h>
|
2015-10-08 08:26:54 +00:00
|
|
|
#include <limits.h>
|
2019-05-08 16:47:06 +00:00
|
|
|
#include <stdbool.h>
|
2015-08-26 06:14:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-04-18 08:42:53 +00:00
|
|
|
#include <time.h>
|
2015-08-26 06:14:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static char *program;
|
|
|
|
static char lockfile[PATH_MAX] = "";
|
|
|
|
|
|
|
|
struct cmdarray {
|
|
|
|
size_t argc;
|
|
|
|
char *argv[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
void add_opt(struct cmdarray *cmd, char *opt)
|
|
|
|
{
|
|
|
|
cmd->argv[cmd->argc++] = opt;
|
|
|
|
cmd->argv[cmd->argc] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usage(int eval)
|
|
|
|
{
|
2021-04-20 10:58:32 +00:00
|
|
|
printf("usage: %s [-hk] [-d DESTDIR] URL\n", program);
|
2015-08-26 06:14:36 +00:00
|
|
|
return eval;
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:23:16 +00:00
|
|
|
int fork_exec(char *argv[], int showerr)
|
|
|
|
{
|
|
|
|
int r = 202;
|
|
|
|
int status = 0;
|
|
|
|
pid_t childpid = fork();
|
|
|
|
if (childpid < 0 )
|
|
|
|
err(200, "fork");
|
|
|
|
|
|
|
|
if (childpid == 0) {
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
if (showerr)
|
|
|
|
warn("%s", argv[0]);
|
|
|
|
_exit(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for curl/wget and get the exit code */
|
|
|
|
wait(&status);
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
r = WEXITSTATUS(status);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-07-24 11:53:11 +00:00
|
|
|
static int acquire_lock(const char *lockfile)
|
2015-08-26 06:14:36 +00:00
|
|
|
{
|
2024-05-21 00:36:07 +00:00
|
|
|
int lockfd, i, r;
|
2021-04-20 14:58:02 +00:00
|
|
|
|
2024-05-21 00:36:07 +00:00
|
|
|
/* try to work around an ESTALE error which occurs on NFS */
|
|
|
|
for (i = 0; i < 10; i++, sleep(1)) {
|
|
|
|
lockfd = open(lockfile, O_WRONLY|O_CREAT, 0660);
|
|
|
|
if (lockfd < 0)
|
|
|
|
err(1, "%s", lockfile);
|
|
|
|
|
|
|
|
r = lockf(lockfd, F_LOCK, 0);
|
|
|
|
if (r == 0 || errno != ESTALE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
close(lockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r != 0)
|
2021-07-24 11:53:11 +00:00
|
|
|
err(1, "failed to acquire lock: %s", lockfile);
|
2021-04-20 15:05:40 +00:00
|
|
|
|
2021-04-20 14:58:02 +00:00
|
|
|
return lockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release_lock(int lockfd)
|
|
|
|
{
|
2021-05-06 10:22:55 +00:00
|
|
|
if (lockf(lockfd, F_ULOCK, 0) == -1)
|
|
|
|
err(1, "failed to release lock");
|
2021-04-20 15:05:40 +00:00
|
|
|
|
2021-05-06 10:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int try_lock(int lockfd)
|
|
|
|
{
|
|
|
|
return lockf(lockfd, F_TLOCK, 0) == 0;
|
2021-04-20 14:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create or wait for an NFS-safe lockfile and fetch url with curl or wget */
|
|
|
|
int fetch(char *url, const char *destdir, bool insecure)
|
|
|
|
{
|
|
|
|
int lockfd, status=0;
|
|
|
|
char outfile[PATH_MAX-5], partfile[PATH_MAX];
|
|
|
|
char *name, *p;
|
2015-08-26 06:14:36 +00:00
|
|
|
struct cmdarray curlcmd = {
|
2019-04-19 03:45:00 +00:00
|
|
|
.argc = 5,
|
|
|
|
.argv = { "curl", "-L", "-f", "-o", partfile, NULL }
|
2015-08-26 06:14:36 +00:00
|
|
|
};
|
|
|
|
struct cmdarray wgetcmd = {
|
|
|
|
.argc = 3,
|
|
|
|
.argv = { "wget", "-O", partfile, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
name = strrchr(url, '/');
|
|
|
|
if (name == NULL)
|
2016-05-20 08:22:06 +00:00
|
|
|
errx(1, "%s: no '/' in url", url);
|
2015-08-26 06:14:36 +00:00
|
|
|
p = strstr(url, "::");
|
|
|
|
if (p != NULL) {
|
|
|
|
name = url;
|
|
|
|
*p = '\0';
|
|
|
|
url = p + 2;
|
|
|
|
} else {
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(outfile, sizeof(outfile), "%s/%s", destdir, name);
|
|
|
|
snprintf(lockfile, sizeof(lockfile), "%s.lock", outfile);
|
|
|
|
snprintf(partfile, sizeof(partfile), "%s.part", outfile);
|
|
|
|
|
2021-07-24 11:53:11 +00:00
|
|
|
lockfd = acquire_lock(lockfile);
|
2015-08-26 06:14:36 +00:00
|
|
|
|
|
|
|
if (access(outfile, F_OK) == 0)
|
|
|
|
goto fetch_done;
|
|
|
|
|
2019-05-08 18:07:11 +00:00
|
|
|
/* enable insecure mode when http. This may be useful when it redirects to https */
|
|
|
|
if (insecure || strstr(url, "http://") == url) {
|
2019-05-08 16:47:06 +00:00
|
|
|
add_opt(&curlcmd, "--insecure");
|
|
|
|
add_opt(&wgetcmd, "--no-check-certificate");
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:14:36 +00:00
|
|
|
if (access(partfile, F_OK) == 0) {
|
|
|
|
printf("Partial download found. Trying to resume.\n");
|
2020-07-08 08:10:26 +00:00
|
|
|
add_opt(&curlcmd, "--continue-at");
|
2015-08-26 06:14:36 +00:00
|
|
|
add_opt(&curlcmd, "-");
|
|
|
|
add_opt(&wgetcmd, "-c");
|
|
|
|
}
|
|
|
|
|
|
|
|
add_opt(&curlcmd, url);
|
|
|
|
add_opt(&wgetcmd, url);
|
|
|
|
|
2018-10-03 09:23:16 +00:00
|
|
|
status = fork_exec(curlcmd.argv, 0);
|
2015-08-26 06:14:36 +00:00
|
|
|
|
2018-10-03 09:23:16 +00:00
|
|
|
/* CURLE_RANGE_ERROR (33)
|
|
|
|
The server does not support or accept range requests. */
|
2020-07-08 08:10:26 +00:00
|
|
|
if (status == 33) {
|
2018-10-03 09:23:16 +00:00
|
|
|
unlink(partfile);
|
2020-07-08 08:10:26 +00:00
|
|
|
if( curlcmd.argc >=3) {
|
|
|
|
/* remove --continue-at - options */
|
|
|
|
curlcmd.argv[curlcmd.argc-3] = curlcmd.argv[curlcmd.argc-1];
|
|
|
|
curlcmd.argv[curlcmd.argc-2] = NULL;
|
|
|
|
curlcmd.argc -= 2;
|
|
|
|
status = fork_exec(curlcmd.argv, 0);
|
|
|
|
}
|
|
|
|
}
|
Fix: incomplete partfile gets renamed to distfile
Abuild-fetch uses curl (fallback to wget) to download files. They are
saved with a ".part" extension first, so they can be resumed if
necessary. When the download is through, the ".part" extension gets
removed. However, when the server does not support resume of downloads
(e.g. GitHub's on the fly generated tarballs), then the ".part"
extension got removed anyway. Abuild aborts in that case. But when
running a third time, the distfile exists and it is assumed that this
is the full download.
Changes:
* abuild-fetch:
* Only remove the ".part" extension, when curl/wget exit with 0
* Pass the exit code from curl/wget as exit code of abuild-fetch
* Wherever abuild-fetch would return an exit code on its own, the
codes have been changed to be > 200 (so they don't collide with
curl's as of now 92 exit codes)
* Remove undocumented feature of downloading multiple source URLs at
a time. This doesn't match with the usage description, was not used
in abuild at all and it would have made it impossible to pass the
exit code.
* abuild:
* After downloading, when curl is installed and abuild-fetch has
33 as exit code (curl's HTTP range error), then delete the partfile
and try the download again.
2018-04-11 18:19:35 +00:00
|
|
|
|
2018-10-03 09:23:16 +00:00
|
|
|
/* is we failed execute curl, then fallback to wget */
|
|
|
|
if (status == 201)
|
|
|
|
status = fork_exec(wgetcmd.argv, 1);
|
Fix: incomplete partfile gets renamed to distfile
Abuild-fetch uses curl (fallback to wget) to download files. They are
saved with a ".part" extension first, so they can be resumed if
necessary. When the download is through, the ".part" extension gets
removed. However, when the server does not support resume of downloads
(e.g. GitHub's on the fly generated tarballs), then the ".part"
extension got removed anyway. Abuild aborts in that case. But when
running a third time, the distfile exists and it is assumed that this
is the full download.
Changes:
* abuild-fetch:
* Only remove the ".part" extension, when curl/wget exit with 0
* Pass the exit code from curl/wget as exit code of abuild-fetch
* Wherever abuild-fetch would return an exit code on its own, the
codes have been changed to be > 200 (so they don't collide with
curl's as of now 92 exit codes)
* Remove undocumented feature of downloading multiple source URLs at
a time. This doesn't match with the usage description, was not used
in abuild at all and it would have made it impossible to pass the
exit code.
* abuild:
* After downloading, when curl is installed and abuild-fetch has
33 as exit code (curl's HTTP range error), then delete the partfile
and try the download again.
2018-04-11 18:19:35 +00:00
|
|
|
|
2018-10-03 09:23:16 +00:00
|
|
|
/* only rename completed downloads */
|
Fix: incomplete partfile gets renamed to distfile
Abuild-fetch uses curl (fallback to wget) to download files. They are
saved with a ".part" extension first, so they can be resumed if
necessary. When the download is through, the ".part" extension gets
removed. However, when the server does not support resume of downloads
(e.g. GitHub's on the fly generated tarballs), then the ".part"
extension got removed anyway. Abuild aborts in that case. But when
running a third time, the distfile exists and it is assumed that this
is the full download.
Changes:
* abuild-fetch:
* Only remove the ".part" extension, when curl/wget exit with 0
* Pass the exit code from curl/wget as exit code of abuild-fetch
* Wherever abuild-fetch would return an exit code on its own, the
codes have been changed to be > 200 (so they don't collide with
curl's as of now 92 exit codes)
* Remove undocumented feature of downloading multiple source URLs at
a time. This doesn't match with the usage description, was not used
in abuild at all and it would have made it impossible to pass the
exit code.
* abuild:
* After downloading, when curl is installed and abuild-fetch has
33 as exit code (curl's HTTP range error), then delete the partfile
and try the download again.
2018-04-11 18:19:35 +00:00
|
|
|
if (status == 0)
|
|
|
|
rename(partfile, outfile);
|
2015-08-26 06:14:36 +00:00
|
|
|
|
|
|
|
fetch_done:
|
2021-04-20 14:58:02 +00:00
|
|
|
release_lock(lockfd);
|
2021-05-06 10:22:55 +00:00
|
|
|
|
2021-07-24 11:53:11 +00:00
|
|
|
// give other processes the chance to acquire the lock if they have the file open
|
2023-04-18 08:42:53 +00:00
|
|
|
// sleep for a millisecond
|
|
|
|
const struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000};
|
|
|
|
nanosleep(&ts, NULL);
|
2021-05-06 10:22:55 +00:00
|
|
|
|
|
|
|
if (status == 0 || try_lock(lockfd))
|
|
|
|
unlink(lockfile);
|
|
|
|
close(lockfd);
|
2015-08-26 06:14:36 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sighandler(int sig)
|
|
|
|
{
|
|
|
|
switch(sig) {
|
|
|
|
case SIGABRT:
|
|
|
|
case SIGINT:
|
|
|
|
case SIGQUIT:
|
|
|
|
case SIGTERM:
|
|
|
|
unlink(lockfile);
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Fix: incomplete partfile gets renamed to distfile
Abuild-fetch uses curl (fallback to wget) to download files. They are
saved with a ".part" extension first, so they can be resumed if
necessary. When the download is through, the ".part" extension gets
removed. However, when the server does not support resume of downloads
(e.g. GitHub's on the fly generated tarballs), then the ".part"
extension got removed anyway. Abuild aborts in that case. But when
running a third time, the distfile exists and it is assumed that this
is the full download.
Changes:
* abuild-fetch:
* Only remove the ".part" extension, when curl/wget exit with 0
* Pass the exit code from curl/wget as exit code of abuild-fetch
* Wherever abuild-fetch would return an exit code on its own, the
codes have been changed to be > 200 (so they don't collide with
curl's as of now 92 exit codes)
* Remove undocumented feature of downloading multiple source URLs at
a time. This doesn't match with the usage description, was not used
in abuild at all and it would have made it impossible to pass the
exit code.
* abuild:
* After downloading, when curl is installed and abuild-fetch has
33 as exit code (curl's HTTP range error), then delete the partfile
and try the download again.
2018-04-11 18:19:35 +00:00
|
|
|
/* exit codes get passed through from curl/wget (so we can check in abuild
|
|
|
|
whether the server does not support resuming). Additional exit codes:
|
|
|
|
200: fork failed
|
|
|
|
201: curl/wget could not be started
|
|
|
|
202: curl/wget did not terminate normally
|
|
|
|
203: usage displayed */
|
2015-08-26 06:14:36 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
Fix: incomplete partfile gets renamed to distfile
Abuild-fetch uses curl (fallback to wget) to download files. They are
saved with a ".part" extension first, so they can be resumed if
necessary. When the download is through, the ".part" extension gets
removed. However, when the server does not support resume of downloads
(e.g. GitHub's on the fly generated tarballs), then the ".part"
extension got removed anyway. Abuild aborts in that case. But when
running a third time, the distfile exists and it is assumed that this
is the full download.
Changes:
* abuild-fetch:
* Only remove the ".part" extension, when curl/wget exit with 0
* Pass the exit code from curl/wget as exit code of abuild-fetch
* Wherever abuild-fetch would return an exit code on its own, the
codes have been changed to be > 200 (so they don't collide with
curl's as of now 92 exit codes)
* Remove undocumented feature of downloading multiple source URLs at
a time. This doesn't match with the usage description, was not used
in abuild at all and it would have made it impossible to pass the
exit code.
* abuild:
* After downloading, when curl is installed and abuild-fetch has
33 as exit code (curl's HTTP range error), then delete the partfile
and try the download again.
2018-04-11 18:19:35 +00:00
|
|
|
int opt;
|
2019-05-08 18:12:24 +00:00
|
|
|
bool insecure = false;
|
2015-08-26 06:14:36 +00:00
|
|
|
char *destdir = "/var/cache/distfiles";
|
|
|
|
|
|
|
|
program = argv[0];
|
2019-05-08 16:47:06 +00:00
|
|
|
while ((opt = getopt(argc, argv, "hd:k")) != -1) {
|
2015-08-26 06:14:36 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'h':
|
|
|
|
return usage(0);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
destdir = optarg;
|
|
|
|
break;
|
2019-05-08 16:47:06 +00:00
|
|
|
case 'k':
|
|
|
|
insecure = true;
|
|
|
|
break;
|
2015-08-26 06:14:36 +00:00
|
|
|
default:
|
2018-03-18 10:03:14 +00:00
|
|
|
printf("Unknown option '%c'\n", opt);
|
2015-08-26 06:14:36 +00:00
|
|
|
return usage(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
|
Fix: incomplete partfile gets renamed to distfile
Abuild-fetch uses curl (fallback to wget) to download files. They are
saved with a ".part" extension first, so they can be resumed if
necessary. When the download is through, the ".part" extension gets
removed. However, when the server does not support resume of downloads
(e.g. GitHub's on the fly generated tarballs), then the ".part"
extension got removed anyway. Abuild aborts in that case. But when
running a third time, the distfile exists and it is assumed that this
is the full download.
Changes:
* abuild-fetch:
* Only remove the ".part" extension, when curl/wget exit with 0
* Pass the exit code from curl/wget as exit code of abuild-fetch
* Wherever abuild-fetch would return an exit code on its own, the
codes have been changed to be > 200 (so they don't collide with
curl's as of now 92 exit codes)
* Remove undocumented feature of downloading multiple source URLs at
a time. This doesn't match with the usage description, was not used
in abuild at all and it would have made it impossible to pass the
exit code.
* abuild:
* After downloading, when curl is installed and abuild-fetch has
33 as exit code (curl's HTTP range error), then delete the partfile
and try the download again.
2018-04-11 18:19:35 +00:00
|
|
|
if (argc != 1)
|
|
|
|
return usage(203);
|
2015-08-26 06:14:36 +00:00
|
|
|
|
|
|
|
signal(SIGABRT, sighandler);
|
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
signal(SIGQUIT, sighandler);
|
|
|
|
signal(SIGTERM, sighandler);
|
|
|
|
|
2019-05-08 18:12:24 +00:00
|
|
|
return fetch(argv[0], destdir, insecure);
|
2015-08-26 06:14:36 +00:00
|
|
|
}
|