Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add mirror flag.
Vsevolod Stakhov committed 11 years ago
commit 7d2013838ab429cc1a725c182270f31fe6166be0
parent dd519bf
4 files changed +63 -12
modified libpkg/pkg.h.in
@@ -355,7 +355,8 @@ typedef enum _pkg_flags {
	PKG_FLAG_PKG_VERSION_TEST = (1U << 6),
	PKG_FLAG_UPGRADES_FOR_INSTALLED = (1U << 7),
	PKG_FLAG_SKIP_INSTALL = (1U << 8),
-
	PKG_FLAG_FORCE_MISSING = (1U << 9)
+
	PKG_FLAG_FORCE_MISSING = (1U << 9),
+
	PKG_FLAG_FETCH_MIRROR = (1U << 10)
} pkg_flags;

typedef enum _pkg_stats_t {
@@ -1161,6 +1162,7 @@ void pkg_jobs_free(struct pkg_jobs *jobs);
int pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc);
int pkg_jobs_solve(struct pkg_jobs *j);
int pkg_jobs_set_repository(struct pkg_jobs *j, const char *name);
+
int pkg_jobs_set_destdir(struct pkg_jobs *j, const char *name);
void pkg_jobs_set_flags(struct pkg_jobs *j, pkg_flags f);
pkg_jobs_t pkg_jobs_type(struct pkg_jobs *j);

modified libpkg/pkg_jobs.c
@@ -97,6 +97,17 @@ pkg_jobs_set_repository(struct pkg_jobs *j, const char *ident)
	return (EPKG_OK);
}

+
int
+
pkg_jobs_set_destdir(struct pkg_jobs *j, const char *dir)
+
{
+
	if (dir == NULL)
+
		return (EPKG_FATAL);
+

+
	j->destdir = dir;
+

+
	return (EPKG_OK);
+
}
+

static void
pkg_jobs_pattern_free(struct job_pattern *jp)
{
@@ -2640,14 +2651,21 @@ pkg_jobs_fetch(struct pkg_jobs *j)
	struct statfs fs;
	struct stat st;
	int64_t dlsize = 0;
-
	const char *cachedir = NULL;
+
	const char *cachedir = NULL, *repopath;
	char cachedpath[MAXPATHLEN];
+
	bool mirror = (j->flags & PKG_FLAG_FETCH_MIRROR) ? true : false;
+

	
-
	cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));
+
	if (j->destdir == NULL)
+
		cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));
+
	else
+
		cachedir = j->destdir;

	/* check for available size to fetch */
	DL_FOREACH(j->jobs, ps) {
		if (ps->type != PKG_SOLVED_DELETE && ps->type != PKG_SOLVED_UPGRADE_REMOVE) {
+
			int64_t pkgsize;
+

			if (ps->items[0]->reinstall)
				p = ps->items[0]->reinstall;
			else
@@ -2655,9 +2673,15 @@ pkg_jobs_fetch(struct pkg_jobs *j)

			if (p->type != PKG_REMOTE)
				continue;
-
			int64_t pkgsize;
-
			pkg_get(p, PKG_PKGSIZE, &pkgsize);
-
			pkg_repo_cached_name(p, cachedpath, sizeof(cachedpath));
+

+
			pkg_get(p, PKG_PKGSIZE, &pkgsize, PKG_REPOPATH, &repopath);
+
			if (mirror) {
+
				snprintf(cachedpath, sizeof(cachedpath), "%s/%s", cachedir,
+
					repopath);
+
			}
+
			else
+
				pkg_repo_cached_name(p, cachedpath, sizeof(cachedpath));
+

			if (stat(cachedpath, &st) == -1)
				dlsize += pkgsize;
			else
@@ -2703,17 +2727,21 @@ pkg_jobs_fetch(struct pkg_jobs *j)

			if (p->type != PKG_REMOTE)
				continue;
-
			if (pkg_repo_fetch_package(p) != EPKG_OK)
-
				return (EPKG_FATAL);
+

+
			if (mirror) {
+
				if (pkg_repo_mirror_package(p, cachedir) != EPKG_OK)
+
					return (EPKG_FATAL);
+
			}
+
			else {
+
				if (pkg_repo_fetch_package(p) != EPKG_OK)
+
					return (EPKG_FATAL);
+
			}
		}
	}

	return (EPKG_OK);
}

-
#undef PKG_JOBS_FETCH_CALCULATE
-
#undef PKG_JOBS_DO_FETCH
-

static int
pkg_jobs_check_conflicts(struct pkg_jobs *j)
{
modified libpkg/pkg_repo.c
@@ -1101,6 +1101,25 @@ pkg_repo_fetch_package(struct pkg *pkg)
	return (repo->ops->fetch_pkg(repo, pkg));
}

+
int
+
pkg_repo_mirror_package(struct pkg *pkg, const char *destdir)
+
{
+
	struct pkg_repo *repo;
+

+
	if (pkg->repo == NULL) {
+
		pkg_emit_error("Trying to mirror package without repository");
+
		return (EPKG_FATAL);
+
	}
+

+
	repo = pkg->repo;
+
	if (repo->ops->mirror_pkg == NULL) {
+
		pkg_emit_error("Repository %s does not support mirroring", repo->name);
+
		return (EPKG_FATAL);
+
	}
+

+
	return (repo->ops->mirror_pkg(repo, pkg, destdir));
+
}
+

void
pkg_repo_cached_name(struct pkg *pkg, char *dest, size_t destlen)
{
modified libpkg/private/pkg.h
@@ -265,7 +265,8 @@ struct pkg_jobs {
	int total;
	int conflicts_registered;
	bool need_fetch;
-
	const char *	 reponame;
+
	const char *reponame;
+
	const char *destdir;
	struct job_pattern *patterns;
};

@@ -464,6 +465,7 @@ extern struct pkg_key {
int pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url,
		int dest, time_t *t);
int pkg_repo_fetch_package(struct pkg *pkg);
+
int pkg_repo_mirror_package(struct pkg *pkg, const char *destdir);
FILE* pkg_repo_fetch_remote_extract_tmp(struct pkg_repo *repo,
		const char *filename, time_t *t, int *rc);
int pkg_repo_fetch_meta(struct pkg_repo *repo, time_t *t);