Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Make srv mirroring support working, default off
Baptiste Daroussin committed 13 years ago
commit 2812a0e79b2217e48856f11a961504651baf7030
parent e334af3
3 files changed +45 -7
modified libpkg/dns_utils.c
@@ -24,18 +24,17 @@
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

-
#include <sys/cdefs.h>
-
__FBSDID("$FreeBSD$");
-

#include <sys/param.h>
+
#include <sys/stat.h> /* for private.utils.h */
#include <sys/types.h>

+
#include <stdbool.h> /* for private/utils.h */
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
-
#include <libutil.h>
+
#include "private/utils.h"

typedef union {
	HEADER hdr;
modified libpkg/fetch.c
@@ -25,40 +25,70 @@
 */

#include <sys/param.h>
+
#include <sys/stat.h>

#include <fcntl.h>
#include <stdio.h>
+
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fetch.h>

#include "pkg.h"
#include "private/event.h"
+
#include "private/utils.h"

int
pkg_fetch_file(const char *url, const char *dest, time_t t)
{
	int fd = -1;
	FILE *remote = NULL;
+
	struct url *u;
	struct url_stat st;
	off_t done = 0;
	off_t r;
-
	int retry = 3;
+

+
	int64_t max_retry, retry;
	time_t begin_dl;
	time_t now;
	time_t last = 0;
	char buf[10240];
	int retcode = EPKG_OK;
+
	bool srv = false;
+
	char zone[MAXHOSTNAMELEN + 12];
+
	struct dns_srvinfo *mirrors, *current;
+

+
	current = mirrors = NULL;

	fetchTimeout = 30;

+
	if (pkg_config_int64(PKG_CONFIG_FETCH_RETRY, &max_retry) == EPKG_FATAL)
+
		max_retry = 3;
+

+
	retry = max_retry;
+

	if ((fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0600)) == -1) {
		pkg_emit_errno("open", dest);
		return(EPKG_FATAL);
	}

+
	u = fetchParseURL(url);
	while (remote == NULL) {
-
		remote = fetchXGetURL(url, &st, "");
+
		if (retry == max_retry) {
+
			pkg_config_bool(PKG_CONFIG_SRV_MIRROR, &srv);
+
			if (srv) {
+
				if (strcmp(u->scheme, "file") != 0) {
+
					snprintf(zone, sizeof(zone), "_%s._tcp.%s", u->scheme, u->host);
+
					mirrors = dns_getsrvinfo(zone);
+
					current = mirrors;
+
				}
+
			}
+
		}
+

+
		if (mirrors != NULL)
+
			strlcpy(u->host, current->host, sizeof(u->host));
+

+
		remote = fetchXGet(u, &st, "");
		if (remote == NULL) {
			--retry;
			if (retry == 0) {
@@ -66,7 +96,13 @@ pkg_fetch_file(const char *url, const char *dest, time_t t)
				retcode = EPKG_FATAL;
				goto cleanup;
			}
-
			sleep(1);
+
			if (mirrors == NULL) {
+
				sleep(1);
+
			} else {
+
				current = current->next;
+
				if (current == NULL)
+
					current = mirrors;
+
			}
		}
	}
	if (t != 0) {
@@ -110,6 +146,8 @@ pkg_fetch_file(const char *url, const char *dest, time_t t)
	if (remote != NULL)
		fclose(remote);

+
	fetchFreeURL(u);
+

	/* Remove local file if fetch failed */
	if (retcode != EPKG_OK)
		unlink(dest);
modified libpkg/private/utils.h
@@ -32,6 +32,7 @@
#include <sys/sbuf.h>
#include <sys/param.h>

+
#include <openssl/pem.h>
#include <openssl/sha.h>

#define STARTS_WITH(string, needle) (strncasecmp(string, needle, strlen(needle)) == 0)