abuild-fetch.c 5.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* 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.

*/


#include <sys/wait.h>

#include <err.h>
29
#include <errno.h>
30
#include <fcntl.h>
A. Wilcox's avatar
A. Wilcox committed
31
#include <limits.h>
32
#include <stdbool.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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)
{
54
	printf("usage: %s [-h] [-d DESTDIR] URL\n", program);
55 56 57
	return eval;
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
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;
}

80
/* create or wait for an NFS-safe lockfile and fetch url with curl or wget */
81
int fetch(char *url, const char *destdir, bool insecure)
82 83
{
	int lockfd, status=0;
84
	char outfile[PATH_MAX-5], partfile[PATH_MAX];
85 86 87 88 89 90 91 92
	char *name, *p;
	struct flock fl = {
		.l_type = F_WRLCK,
		.l_whence = SEEK_SET,
		.l_start = 1,
		.l_len = 0,
	};
	struct cmdarray curlcmd = {
93 94
		.argc = 5,
		.argv = { "curl", "-L", "-f", "-o", partfile, NULL }
95 96 97 98 99 100 101 102
	};
	struct cmdarray wgetcmd = {
		.argc = 3,
		.argv = { "wget", "-O", partfile, NULL }
	};

	name = strrchr(url, '/');
	if (name == NULL)
103
		errx(1, "%s: no '/' in url", url);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	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);

	lockfd = open(lockfile, O_WRONLY|O_CREAT, 0660);
	if (lockfd < 0)
119
		err(1, "%s", lockfile);
120 121

	if (fcntl(lockfd, F_SETLK, &fl) < 0) {
122
		int i;
123
		printf("Waiting for %s ...\n", lockfile);
124 125 126 127 128 129 130 131
		for (i=0; i<10; i++) {
			int r = fcntl(lockfd, F_SETLKW, &fl);
			if (r == 0)
				break;
			if (r == -1 && errno != ESTALE)
				err(1, "fcntl(F_SETLKW)");
			sleep(1);
		}
132 133 134 135 136
	}

	if (access(outfile, F_OK) == 0)
		goto fetch_done;

137 138
	/* enable insecure mode when http. This may be useful when it redirects to https */
	if (insecure || strstr(url, "http://") == url) {
139 140 141 142
		add_opt(&curlcmd, "--insecure");
		add_opt(&wgetcmd, "--no-check-certificate");
	}

143 144
	if (access(partfile, F_OK) == 0) {
		printf("Partial download found. Trying to resume.\n");
145
		add_opt(&curlcmd, "--continue-at");
146 147 148 149 150 151 152
		add_opt(&curlcmd, "-");
		add_opt(&wgetcmd, "-c");
	}

	add_opt(&curlcmd, url);
	add_opt(&wgetcmd, url);

153
	status = fork_exec(curlcmd.argv, 0);
154

155 156
	/* CURLE_RANGE_ERROR (33)
	   The server does not support or accept range requests. */
157
	if (status == 33) {
158
		unlink(partfile);
159 160 161 162 163 164 165 166
		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);
		}
	}
167

168 169 170
	/* is we failed execute curl, then fallback to wget */
	if (status == 201)
		status = fork_exec(wgetcmd.argv, 1);
171

172
	/* only rename completed downloads */
173 174
	if (status == 0)
		rename(partfile, outfile);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

fetch_done:
	unlink(lockfile);
	close(lockfd);
	lockfile[0] = '\0';
	return status;
}

void sighandler(int sig)
{
	switch(sig) {
	case SIGABRT:
	case SIGINT:
	case SIGQUIT:
	case SIGTERM:
		unlink(lockfile);
		exit(0);
		break;
	default:
		break;
	}
}

198 199 200 201 202 203
/* 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 */
204 205
int main(int argc, char *argv[])
{
206
	int opt;
207
	bool insecure = false;
208 209 210
	char *destdir = "/var/cache/distfiles";

	program = argv[0];
211
	while ((opt = getopt(argc, argv, "hd:k")) != -1) {
212 213 214 215 216 217 218
		switch (opt) {
		case 'h':
			return usage(0);
			break;
		case 'd':
			destdir = optarg;
			break;
219 220 221
		case 'k':
			insecure = true;
			break;
222
		default:
J. Neuschäfer's avatar
J. Neuschäfer committed
223
			printf("Unknown option '%c'\n", opt);
224 225 226 227 228 229 230 231
			return usage(1);
			break;
		}
	}

	argv += optind;
	argc -= optind;

232 233
	if (argc != 1)
		return usage(203);
234 235 236 237 238 239

	signal(SIGABRT, sighandler);
	signal(SIGINT, sighandler);
	signal(SIGQUIT, sighandler);
	signal(SIGTERM, sighandler);

240
	return fetch(argv[0], destdir, insecure);
241
}