fetch: implement progress (fixes #1170)

cute-signatures
Timo Teräs 2013-06-17 17:51:41 +03:00
parent 656190b0a0
commit 72139b82d3
2 changed files with 65 additions and 45 deletions

View File

@ -96,7 +96,6 @@ struct apk_package {
union { union {
struct apk_solver_package_state ss; struct apk_solver_package_state ss;
struct { struct {
unsigned int marked;
unsigned int foreach_genid; unsigned int foreach_genid;
union { union {
int state_int; int state_int;
@ -113,6 +112,7 @@ struct apk_package {
struct apk_dependency_array *depends, *install_if, *provides; struct apk_dependency_array *depends, *install_if, *provides;
size_t installed_size, size; size_t installed_size, size;
time_t build_time; time_t build_time;
unsigned marked : 1;
unsigned repos : APK_MAX_REPOS; unsigned repos : APK_MAX_REPOS;
struct apk_checksum csum; struct apk_checksum csum;
}; };

View File

@ -28,7 +28,9 @@
struct fetch_ctx { struct fetch_ctx {
unsigned int flags; unsigned int flags;
int outdir_fd; int outdir_fd, errors, prog_flags;
struct apk_database *db;
size_t done, total;
}; };
static int cup(void) static int cup(void)
@ -89,16 +91,27 @@ static int fetch_parse(void *ctx, struct apk_db_options *dbopts,
return 0; return 0;
} }
static int fetch_package(struct fetch_ctx *fctx, static void progress_cb(void *pctx, size_t bytes_done)
struct apk_database *db,
struct apk_package *pkg)
{ {
struct fetch_ctx *ctx = (struct fetch_ctx *) pctx;
apk_print_progress(muldiv(100, ctx->done + bytes_done, ctx->total) | ctx->prog_flags);
ctx->prog_flags = 0;
}
static int fetch_package(apk_hash_item item, void *pctx)
{
struct fetch_ctx *ctx = (struct fetch_ctx *) pctx;
struct apk_database *db = ctx->db;
struct apk_package *pkg = (struct apk_package *) item;
struct apk_istream *is; struct apk_istream *is;
struct apk_repository *repo; struct apk_repository *repo;
struct apk_file_info fi; struct apk_file_info fi;
char url[PATH_MAX], filename[256]; char url[PATH_MAX], filename[256];
int r, fd, urlfd; int r, fd, urlfd;
if (!pkg->marked)
return 0;
repo = apk_db_select_repo(db, pkg); repo = apk_db_select_repo(db, pkg);
if (repo == NULL) { if (repo == NULL) {
r = -ENOPKG; r = -ENOPKG;
@ -110,8 +123,8 @@ static int fetch_package(struct fetch_ctx *fctx,
goto err; goto err;
} }
if (!(fctx->flags & FETCH_STDOUT)) { if (!(ctx->flags & FETCH_STDOUT)) {
if (apk_file_get_info(fctx->outdir_fd, filename, APK_CHECKSUM_NONE, &fi) == 0 && if (apk_file_get_info(ctx->outdir_fd, filename, APK_CHECKSUM_NONE, &fi) == 0 &&
fi.size == pkg->size) fi.size == pkg->size)
return 0; return 0;
} }
@ -120,20 +133,22 @@ static int fetch_package(struct fetch_ctx *fctx,
if (apk_flags & APK_SIMULATE) if (apk_flags & APK_SIMULATE)
return 0; return 0;
ctx->prog_flags = APK_PRINT_PROGRESS_FORCE;
r = apk_repo_format_item(db, repo, pkg, &urlfd, url, sizeof(url)); r = apk_repo_format_item(db, repo, pkg, &urlfd, url, sizeof(url));
if (r < 0) if (r < 0)
goto err; goto err;
if (fctx->flags & FETCH_STDOUT) { if (ctx->flags & FETCH_STDOUT) {
fd = STDOUT_FILENO; fd = STDOUT_FILENO;
} else { } else {
if ((fctx->flags & FETCH_LINK) && urlfd >= 0) { if ((ctx->flags & FETCH_LINK) && urlfd >= 0) {
if (linkat(urlfd, url, if (linkat(urlfd, url,
fctx->outdir_fd, filename, ctx->outdir_fd, filename,
AT_SYMLINK_FOLLOW) == 0) AT_SYMLINK_FOLLOW) == 0)
return 0; return 0;
} }
fd = openat(fctx->outdir_fd, filename, fd = openat(ctx->outdir_fd, filename,
O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC, 0644); O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC, 0644);
if (fd < 0) { if (fd < 0) {
r = -errno; r = -errno;
@ -147,31 +162,43 @@ static int fetch_package(struct fetch_ctx *fctx,
goto err; goto err;
} }
r = apk_istream_splice(is, fd, pkg->size, NULL, NULL); r = apk_istream_splice(is, fd, pkg->size, progress_cb, ctx);
is->close(is); is->close(is);
if (fd != STDOUT_FILENO) if (fd != STDOUT_FILENO)
close(fd); close(fd);
if (r != pkg->size) { if (r != pkg->size) {
unlinkat(fctx->outdir_fd, filename, 0); unlinkat(ctx->outdir_fd, filename, 0);
if (r >= 0) r = -EIO; if (r >= 0) r = -EIO;
goto err; goto err;
} }
ctx->done += pkg->size;
return 0; return 0;
err: err:
apk_error(PKG_VER_FMT ": %s", PKG_VER_PRINTF(pkg), apk_error_str(r)); apk_error(PKG_VER_FMT ": %s", PKG_VER_PRINTF(pkg), apk_error_str(r));
return r; ctx->errors++;
return 0;
} }
static int fetch_main(void *ctx, struct apk_database *db, int argc, char **argv) static void mark_package(struct fetch_ctx *ctx, struct apk_package *pkg)
{ {
struct fetch_ctx *fctx = (struct fetch_ctx *) ctx; if (pkg == NULL || pkg->marked)
int r = 0, i, j; return;
ctx->total += pkg->size;
pkg->marked = 1;
}
if (fctx->outdir_fd == 0) static int fetch_main(void *pctx, struct apk_database *db, int argc, char **argv)
fctx->outdir_fd = AT_FDCWD; {
struct fetch_ctx *ctx = (struct fetch_ctx *) pctx;
struct apk_dependency_array *world;
struct apk_change *change;
int r = 0, i;
if (ctx->outdir_fd == 0)
ctx->outdir_fd = AT_FDCWD;
if ((argc > 0) && (strcmp(argv[0], "coffee") == 0)) { if ((argc > 0) && (strcmp(argv[0], "coffee") == 0)) {
if (apk_flags & APK_FORCE) if (apk_flags & APK_FORCE)
@ -187,8 +214,7 @@ static int fetch_main(void *ctx, struct apk_database *db, int argc, char **argv)
.result_mask = APK_DEPMASK_ANY, .result_mask = APK_DEPMASK_ANY,
}; };
if (fctx->flags & FETCH_RECURSIVE) { if (ctx->flags & FETCH_RECURSIVE) {
struct apk_dependency_array *world;
struct apk_changeset changeset = {}; struct apk_changeset changeset = {};
apk_dependency_array_init(&world); apk_dependency_array_init(&world);
@ -196,40 +222,34 @@ static int fetch_main(void *ctx, struct apk_database *db, int argc, char **argv)
r = apk_solver_solve(db, 0, world, &changeset); r = apk_solver_solve(db, 0, world, &changeset);
apk_dependency_array_free(&world); apk_dependency_array_free(&world);
if (r != 0) { if (r != 0) {
apk_error("Unable to install '%s'", argv[i]); apk_error("%s: unable to get dependencies", argv[i]);
goto err; ctx->errors++;
} } else {
foreach_array_item(change, changeset.changes)
for (j = 0; j < changeset.changes->num; j++) { mark_package(ctx, change->new_pkg);
struct apk_change *change = &changeset.changes->item[j];
r = fetch_package(fctx, db, change->new_pkg);
if (r != 0)
goto err;
} }
apk_change_array_free(&changeset.changes);
} else { } else {
struct apk_package *pkg = NULL; struct apk_package *pkg = NULL;
struct apk_provider *p;
for (j = 0; j < dep.name->providers->num; j++) foreach_array_item(p, dep.name->providers)
if (pkg == NULL || if (pkg == NULL || apk_pkg_version_compare(p->pkg, pkg) == APK_VERSION_GREATER)
apk_pkg_version_compare(dep.name->providers->item[j].pkg, pkg = p->pkg;
pkg)
== APK_VERSION_GREATER)
pkg = dep.name->providers->item[j].pkg;
if (pkg == NULL) { if (pkg == NULL) {
apk_message("Unable to get '%s'", dep.name->name); apk_message("%s: unable to select a version", dep.name->name);
r = -1; ctx->errors++;
break; } else {
mark_package(ctx, pkg);
} }
r = fetch_package(fctx, db, pkg);
if (r != 0)
goto err;
} }
} }
err: ctx->db = db;
return r; apk_hash_foreach(&db->available.packages, fetch_package, ctx);
return ctx->errors;
} }
static struct apk_option fetch_options[] = { static struct apk_option fetch_options[] = {