From 77e203bf3224293d8f8e01dd15d6262eed840cf9 Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Wed, 22 Feb 2012 13:57:05 +0200 Subject: db, io: load repositories also from etc/apk/repositories.d/*.list Load additional repositories from $ROOT/etc/apk/repositories.d/*.list unless --repositories-file is given as parameter. --- src/apk_database.h | 2 +- src/apk_io.h | 2 +- src/cache.c | 10 +++++----- src/database.c | 43 +++++++++++++++++++++++++++++++------------ src/io.c | 2 +- 5 files changed, 39 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/apk_database.h b/src/apk_database.h index ba5dee4..177f18f 100644 --- a/src/apk_database.h +++ b/src/apk_database.h @@ -210,7 +210,7 @@ int apk_cache_download(struct apk_database *db, const char *url, apk_blob_t *arc const char *item, const char *cache_item, int verify); typedef void (*apk_cache_item_cb)(struct apk_database *db, - const char *filename, + int dirfd, const char *name, struct apk_package *pkg); int apk_db_cache_foreach_item(struct apk_database *db, apk_cache_item_cb cb); diff --git a/src/apk_io.h b/src/apk_io.h index 6379c3b..9a6c63a 100644 --- a/src/apk_io.h +++ b/src/apk_io.h @@ -106,7 +106,7 @@ size_t apk_ostream_write_string(struct apk_ostream *ostream, const char *string) apk_blob_t apk_blob_from_istream(struct apk_istream *istream, size_t size); apk_blob_t apk_blob_from_file(int atfd, const char *file); -typedef int apk_dir_file_cb(void *ctx, const char *entry); +typedef int apk_dir_file_cb(void *ctx, int dirfd, const char *entry); #define APK_FI_NOFOLLOW 0x80000000 diff --git a/src/cache.c b/src/cache.c index 45f701f..95de682 100644 --- a/src/cache.c +++ b/src/cache.c @@ -64,16 +64,16 @@ static int cache_download(struct apk_database *db) return ret; } -static void cache_clean_item(struct apk_database *db, const char *filename, struct apk_package *pkg) +static void cache_clean_item(struct apk_database *db, int dirfd, const char *name, struct apk_package *pkg) { char tmp[PATH_MAX]; apk_blob_t b; int i; - if (pkg != NULL || strcmp(filename, "installed") == 0) + if (pkg != NULL || strcmp(name, "installed") == 0) return; - b = APK_BLOB_STR(filename); + b = APK_BLOB_STR(name); for (i = 0; i < db->num_repos; i++) { /* Check if this is a valid index */ apk_cache_format_index(APK_BLOB_BUF(tmp), &db->repos[i]); @@ -82,9 +82,9 @@ static void cache_clean_item(struct apk_database *db, const char *filename, stru } if (apk_verbosity >= 2) - apk_message("deleting %s", filename); + apk_message("deleting %s", name); if (!(apk_flags & APK_SIMULATE)) - unlinkat(db->cache_fd, filename, 0); + unlinkat(dirfd, name, 0); } static int cache_clean(struct apk_database *db) diff --git a/src/database.c b/src/database.c index 0f79393..2d3142a 100644 --- a/src/database.c +++ b/src/database.c @@ -1224,7 +1224,7 @@ static void relocate_database(struct apk_database *db) apk_move_file(db->root_fd, apk_installed_file_old, apk_installed_file); } -static void mark_in_cache(struct apk_database *db, const char *name, struct apk_package *pkg) +static void mark_in_cache(struct apk_database *db, int dirfd, const char *name, struct apk_package *pkg) { if (pkg == NULL) return; @@ -1232,6 +1232,29 @@ static void mark_in_cache(struct apk_database *db, const char *name, struct apk_ pkg->in_cache = 1; } +static int add_repos_from_file(void *ctx, int dirfd, const char *file) +{ + struct apk_database *db = (struct apk_database *) ctx; + apk_blob_t blob; + + if (dirfd != db->root_fd) { + /* if loading from repositories.d, + * the name must end in .list */ + const char *ext = strrchr(file, '.'); + if (ext == NULL || strcmp(ext, ".list") != 0) + return 0; + } + + blob = apk_blob_from_file(dirfd, file); + if (APK_BLOB_IS_NULL(blob)) + return 0; + + apk_blob_for_each_segment(blob, "\n", apk_db_add_repository, db); + free(blob.ptr); + + return 0; +} + int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts) { const char *msg = NULL; @@ -1396,15 +1419,11 @@ int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts) if (!(dbopts->open_flags & APK_OPENF_NO_SYS_REPOS)) { list_for_each_entry(repo, &dbopts->repository_list, list) apk_db_add_repository(db, APK_BLOB_STR(repo->url)); - blob = apk_blob_from_file( - db->root_fd, - dbopts->repositories_file ?: "etc/apk/repositories"); - if (!APK_BLOB_IS_NULL(blob)) { - apk_blob_for_each_segment( - blob, "\n", - apk_db_add_repository, db); - free(blob.ptr); - } + + add_repos_from_file(db, db->root_fd, dbopts->repositories_file ?: "etc/apk/repositories"); + apk_dir_foreach_file(openat(db->root_fd, "etc/apk/repositories.d", O_RDONLY | O_CLOEXEC), + add_repos_from_file, db); + if (apk_flags & APK_UPDATE_CACHE) apk_db_index_write_nr_cache(db); } @@ -1630,7 +1649,7 @@ struct foreach_cache_item_ctx { apk_cache_item_cb cb; }; -static int foreach_cache_file(void *pctx, const char *name) +static int foreach_cache_file(void *pctx, int dirfd, const char *name) { struct foreach_cache_item_ctx *ctx = (struct foreach_cache_item_ctx *) pctx; struct apk_database *db = ctx->db; @@ -1655,7 +1674,7 @@ static int foreach_cache_file(void *pctx, const char *name) } } no_pkg: - ctx->cb(db, name, pkg); + ctx->cb(db, dirfd, name, pkg); return 0; } diff --git a/src/io.c b/src/io.c index 8bb80c0..71ccfda 100644 --- a/src/io.c +++ b/src/io.c @@ -562,7 +562,7 @@ int apk_dir_foreach_file(int dirfd, apk_dir_file_cb cb, void *ctx) while ((de = readdir(dir)) != NULL) { if (de->d_name[0] == '.') continue; - cb(ctx, de->d_name); + cb(ctx, dirfd, de->d_name); } closedir(dir); -- cgit v1.2.3-60-g2f50