implement fetch --purge

which will delete any .apk package on output directory that were
not downloaded by fetch

this allows apk fetch to incrementally build repositories for
binary images
cute-signatures
Timo Teräs 2016-02-16 15:19:15 +02:00
parent 22d56cfe28
commit 445ea07220
1 changed files with 36 additions and 0 deletions

View File

@ -264,6 +264,37 @@ err:
mark_error(ctx, match, name);
}
static int purge_package(void *pctx, int dirfd, const char *filename)
{
char tmp[PATH_MAX];
struct fetch_ctx *ctx = (struct fetch_ctx *) pctx;
struct apk_database *db = ctx->db;
struct apk_provider *p0;
struct apk_name *name;
apk_blob_t b = APK_BLOB_STR(filename), bname, bver;
size_t l;
if (apk_pkg_parse_name(b, &bname, &bver)) return 0;
name = apk_db_get_name(db, bname);
if (!name) return 0;
foreach_array_item(p0, name->providers) {
if (p0->pkg->name != name) continue;
l = snprintf(tmp, sizeof tmp, PKG_FILE_FMT, PKG_FILE_PRINTF(p0->pkg));
if (l > sizeof tmp) continue;
if (apk_blob_compare(b, APK_BLOB_PTR_LEN(tmp, l)) != 0) continue;
if (p0->pkg->marked) return 0;
break;
}
apk_message("Purging %s", filename);
if (apk_flags & APK_SIMULATE)
return 0;
unlinkat(dirfd, filename, 0);
return 0;
}
static int fetch_main(void *pctx, struct apk_database *db, struct apk_string_array *args)
{
struct fetch_ctx *ctx = (struct fetch_ctx *) pctx;
@ -290,6 +321,11 @@ static int fetch_main(void *pctx, struct apk_database *db, struct apk_string_arr
if (!ctx->errors)
apk_hash_foreach(&db->available.packages, fetch_package, ctx);
/* Remove packages not matching download spec from the output directory */
if (!ctx->errors && (apk_flags & APK_PURGE) &&
!(ctx->flags & FETCH_STDOUT) && ctx->outdir_fd > 0)
apk_dir_foreach_file(ctx->outdir_fd, purge_package, ctx);
return ctx->errors;
}