From bc2b5b69b7c016c055e7d25cbfc96b3004e64af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Mon, 28 Mar 2022 11:10:03 +0300 Subject: [PATCH] db: make --no-cache disable the cache completely Including using files found from the cache, or creating the cache directories with --initdb. Based on patch by Paul Spooren. --- src/database.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/database.c b/src/database.c index 79a5c4a..6758e98 100644 --- a/src/database.c +++ b/src/database.c @@ -628,6 +628,7 @@ int apk_repo_format_item(struct apk_database *db, struct apk_repository *repo, s int *fd, char *buf, size_t len) { if (repo->url == db->repos[APK_REPOSITORY_CACHED].url) { + if (db->cache_fd < 0) return db->cache_fd; *fd = db->cache_fd; return apk_pkg_format_cache_pkg(APK_BLOB_PTR_LEN(buf, len), pkg); } else { @@ -651,6 +652,8 @@ int apk_cache_download(struct apk_database *db, struct apk_repository *repo, int r; time_t now = time(NULL); + if (db->cache_fd < 0) return db->cache_fd; + if (pkg != NULL) r = apk_pkg_format_cache_pkg(APK_BLOB_BUF(cacheitem), pkg); else @@ -1664,6 +1667,7 @@ int apk_db_open(struct apk_database *db, struct apk_ctx *ac) apk_db_setup_repositories(db, ac->cache_dir); db->root_fd = apk_ctx_fd_root(ac); + db->cache_fd = -APKE_CACHE_NOT_AVAILABLE; db->permanent = !detect_tmpfs_root(db); if (ac->root && ac->arch) { @@ -1720,9 +1724,11 @@ int apk_db_open(struct apk_database *db, struct apk_ctx *ac) add_protected_paths_from_file, db); /* figure out where to have the cache */ - if ((r = setup_cache(db, ac)) < 0) { - apk_err(out, "Unable to remount cache read/write"); - goto ret_r; + if (!(db->ctx->flags & APK_NO_CACHE)) { + if ((r = setup_cache(db, ac)) < 0) { + apk_err(out, "Unable to remount cache read/write"); + goto ret_r; + } } if (db->ctx->flags & APK_OVERLAY_FROM_STDIN) { @@ -2116,7 +2122,7 @@ void apk_db_update_directory_permissions(struct apk_database *db) int apk_db_cache_active(struct apk_database *db) { - return db->cache_dir != apk_static_cache_dir; + return db->cache_fd > 0 && db->cache_dir != apk_static_cache_dir; } struct foreach_cache_item_ctx { @@ -2160,6 +2166,7 @@ int apk_db_cache_foreach_item(struct apk_database *db, apk_cache_item_cb cb) { struct foreach_cache_item_ctx ctx = { db, cb }; + if (db->cache_fd < 0) return db->cache_fd; return apk_dir_foreach_file(dup(db->cache_fd), foreach_cache_file, &ctx); }