io: get rid of APK_PROGRESS_SCALE

no need to muldiv all the time, just pass the current amount of bytes
done, and let callbacks use that directly.
cute-signatures
Timo Teräs 2013-06-17 17:28:03 +03:00
parent ade8d0b4e9
commit 656190b0a0
5 changed files with 11 additions and 30 deletions

View File

@ -16,6 +16,7 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define BIT(x) (1 << (x)) #define BIT(x) (1 << (x))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b))
#ifndef TRUE #ifndef TRUE
@ -102,8 +103,6 @@ static inline size_t muldiv(size_t a, size_t b, size_t c)
typedef void (*apk_progress_cb)(void *cb_ctx, size_t); typedef void (*apk_progress_cb)(void *cb_ctx, size_t);
#define APK_PROGRESS_SCALE 0x100
void *apk_array_resize(void *array, size_t new_size, size_t elem_size); void *apk_array_resize(void *array, size_t new_size, size_t elem_size);
#define APK_ARRAY(array_type_name, elem_type_name) \ #define APK_ARRAY(array_type_name, elem_type_name) \

View File

@ -363,8 +363,7 @@ int apk_archive_entry_extract(int atfd, const struct apk_file_info *ae,
r = -1; r = -1;
break; break;
} }
if (apk_istream_splice(is, fd, ae->size, cb, cb_ctx) if (apk_istream_splice(is, fd, ae->size, cb, cb_ctx) == ae->size)
== ae->size)
r = 0; r = 0;
close(fd); close(fd);
} else { } else {

View File

@ -125,16 +125,14 @@ struct progress {
int flags; int flags;
}; };
static void progress_cb(void *ctx, size_t pkg_percent) static void progress_cb(void *ctx, size_t installed_bytes)
{ {
struct progress *prog = (struct progress *) ctx; struct progress *prog = (struct progress *) ctx;
size_t partial = 0, percent, total; size_t percent, total;
if (prog->pkg != NULL)
partial = muldiv(pkg_percent, prog->pkg->installed_size, APK_PROGRESS_SCALE);
total = prog->total.bytes + prog->total.packages; total = prog->total.bytes + prog->total.packages;
if (total > 0) if (total > 0)
percent = muldiv(100, prog->done.bytes + prog->done.packages + partial, percent = muldiv(100, prog->done.bytes + prog->done.packages + installed_bytes,
prog->total.bytes + prog->total.packages); prog->total.bytes + prog->total.packages);
else else
percent = 0; percent = 0;

View File

@ -2133,19 +2133,12 @@ int apk_db_add_repository(apk_database_t _db, apk_blob_t _repository)
return 0; return 0;
} }
static void extract_cb(void *_ctx, size_t progress) static void extract_cb(void *_ctx, size_t bytes_done)
{ {
struct install_ctx *ctx = (struct install_ctx *) _ctx; struct install_ctx *ctx = (struct install_ctx *) _ctx;
if (!ctx->cb)
if (ctx->cb) { return;
size_t size = ctx->installed_size; ctx->cb(ctx->cb_ctx, min(ctx->installed_size + bytes_done, ctx->pkg->installed_size));
size += muldiv(progress, ctx->current_file_size, APK_PROGRESS_SCALE);
if (size > ctx->pkg->installed_size)
size = ctx->pkg->installed_size;
ctx->cb(ctx->cb_ctx, muldiv(APK_PROGRESS_SCALE, size, ctx->pkg->installed_size));
}
} }
static int apk_db_run_pending_script(struct install_ctx *ctx) static int apk_db_run_pending_script(struct install_ctx *ctx)
@ -2256,14 +2249,6 @@ static int apk_db_install_archive_entry(void *_ctx,
if (r != 0) if (r != 0)
return r; return r;
/* Show progress */
if (ctx->cb) {
size_t size = ctx->installed_size;
if (size > pkg->installed_size)
size = pkg->installed_size;
ctx->cb(ctx->cb_ctx, muldiv(APK_PROGRESS_SCALE, size, pkg->installed_size));
}
/* Installable entry */ /* Installable entry */
ctx->current_file_size = apk_calc_installed_size(ae->size); ctx->current_file_size = apk_calc_installed_size(ae->size);
if (!S_ISDIR(ae->mode)) { if (!S_ISDIR(ae->mode)) {

View File

@ -156,8 +156,8 @@ size_t apk_istream_splice(void *stream, int fd, size_t size,
} }
while (done < size) { while (done < size) {
if (done != 0 && cb != NULL) if (cb != NULL)
cb(cb_ctx, muldiv(APK_PROGRESS_SCALE, done, size)); cb(cb_ctx, done);
togo = size - done; togo = size - done;
if (togo > bufsz) if (togo > bufsz)