[pacman-dev] [PATCH v2 1/3] libalpm: introduce get_sync_pkg_ops() helper
Currently default_pkg_ops is accessed in two different ways. There is get_file_pkg_ops (in be_package.c) creating a local once-off 'tweaked' copy. As well as load_pkg_for_entry (be_sync.c) which modifies in-place and uses default_pkg_ops. This seems rather misleading and fragile approach. Introduce a helper for the second use-case so that default_pkg_ops is handled consistently and essentially remains unchanged throughout. Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> --- lib/libalpm/be_sync.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c index 225df76d..8fdf0fb2 100644 --- a/lib/libalpm/be_sync.c +++ b/lib/libalpm/be_sync.c @@ -281,6 +281,23 @@ static int _sync_get_validation(alpm_pkg_t *pkg) return pkg->validation; } +/** Package sync operations struct accessor. We implement this as a method + * rather than a static struct as in be_files because we want to reuse the + * majority of the default_pkg_ops struct and add only a few operations of + * our own on top. + */ +static struct pkg_operations *get_sync_pkg_ops(void) +{ + static struct pkg_operations sync_pkg_ops; + static int sync_pkg_ops_initalized = 0; + if(!sync_pkg_ops_initalized) { + sync_pkg_ops = default_pkg_ops; + sync_pkg_ops.get_validation = _sync_get_validation; + sync_pkg_ops_initalized = 1; + } + return &sync_pkg_ops; +} + static alpm_pkg_t *load_pkg_for_entry(alpm_db_t *db, const char *entryname, const char **entry_filename, alpm_pkg_t *likely_pkg) { @@ -321,8 +338,7 @@ static alpm_pkg_t *load_pkg_for_entry(alpm_db_t *db, const char *entryname, pkg->origin = ALPM_PKG_FROM_SYNCDB; pkg->origin_data.db = db; - pkg->ops = &default_pkg_ops; - pkg->ops->get_validation = _sync_get_validation; + pkg->ops = get_sync_pkg_ops(); pkg->handle = db->handle; /* add to the collection */ -- 2.30.0
v2: split out get_sync_pkg_ops() into separate patch Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> --- lib/libalpm/be_local.c | 2 +- lib/libalpm/be_package.c | 2 +- lib/libalpm/be_sync.c | 2 +- lib/libalpm/package.c | 2 +- lib/libalpm/package.h | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index e73a97bb..9c3c8bb3 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -326,7 +326,7 @@ static int _cache_force_load(alpm_pkg_t *pkg) * lazy accessor methods that handle any backend loading and caching * logic. */ -static struct pkg_operations local_pkg_ops = { +static const struct pkg_operations local_pkg_ops = { .get_base = _cache_get_base, .get_desc = _cache_get_desc, .get_url = _cache_get_url, diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c index 4dde7167..f855003a 100644 --- a/lib/libalpm/be_package.c +++ b/lib/libalpm/be_package.c @@ -138,7 +138,7 @@ static int _package_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp) * majority of the default_pkg_ops struct and add only a few operations of * our own on top. */ -static struct pkg_operations *get_file_pkg_ops(void) +static const struct pkg_operations *get_file_pkg_ops(void) { static struct pkg_operations file_pkg_ops; static int file_pkg_ops_initialized = 0; diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c index 8fdf0fb2..5356b544 100644 --- a/lib/libalpm/be_sync.c +++ b/lib/libalpm/be_sync.c @@ -286,7 +286,7 @@ static int _sync_get_validation(alpm_pkg_t *pkg) * majority of the default_pkg_ops struct and add only a few operations of * our own on top. */ -static struct pkg_operations *get_sync_pkg_ops(void) +static const struct pkg_operations *get_sync_pkg_ops(void) { static struct pkg_operations sync_pkg_ops; static int sync_pkg_ops_initalized = 0; diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c index a4356518..5766c600 100644 --- a/lib/libalpm/package.c +++ b/lib/libalpm/package.c @@ -138,7 +138,7 @@ static int _pkg_force_load(alpm_pkg_t UNUSED *pkg) { return 0; } /** The standard package operations struct. Get fields directly from the * struct itself with no abstraction layer or any type of lazy loading. */ -struct pkg_operations default_pkg_ops = { +const struct pkg_operations default_pkg_ops = { .get_base = _pkg_get_base, .get_desc = _pkg_get_desc, .get_url = _pkg_get_url, diff --git a/lib/libalpm/package.h b/lib/libalpm/package.h index c37bd11e..b134ad5a 100644 --- a/lib/libalpm/package.h +++ b/lib/libalpm/package.h @@ -83,7 +83,7 @@ struct pkg_operations { * The actual definition is in package.c so it can have access to the * default accessor functions which are defined there. */ -extern struct pkg_operations default_pkg_ops; +extern const struct pkg_operations default_pkg_ops; struct __alpm_pkg_t { unsigned long name_hash; @@ -121,7 +121,7 @@ struct __alpm_pkg_t { alpm_list_t *removes; /* in transaction targets only */ alpm_pkg_t *oldpkg; /* in transaction targets only */ - struct pkg_operations *ops; + const struct pkg_operations *ops; alpm_filelist_t files; -- 2.30.0
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> --- lib/libalpm/be_local.c | 2 +- lib/libalpm/db.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 9c3c8bb3..1d7748d5 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -1158,7 +1158,7 @@ int SYMEXPORT alpm_pkg_set_reason(alpm_pkg_t *pkg, alpm_pkgreason_t reason) return 0; } -struct db_operations local_db_ops = { +static const struct db_operations local_db_ops = { .validate = local_db_validate, .populate = local_db_populate, .unregister = _alpm_db_unregister, diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h index c642e077..bc0f8bfc 100644 --- a/lib/libalpm/db.h +++ b/lib/libalpm/db.h @@ -70,7 +70,7 @@ struct __alpm_db_t { alpm_pkghash_t *pkgcache; alpm_list_t *grpcache; alpm_list_t *servers; - struct db_operations *ops; + const struct db_operations *ops; /* bitfields for validity, local, loaded caches, etc. */ /* From _alpm_dbstatus_t */ -- 2.30.0
On 5/1/21 10:36 am, Emil Velikov via pacman-dev wrote:
Currently default_pkg_ops is accessed in two different ways.
There is get_file_pkg_ops (in be_package.c) creating a local once-off 'tweaked' copy. As well as load_pkg_for_entry (be_sync.c) which modifies in-place and uses default_pkg_ops.
This seems rather misleading and fragile approach. Introduce a helper for the second use-case so that default_pkg_ops is handled consistently and essentially remains unchanged throughout.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> --- lib/libalpm/be_sync.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c index 225df76d..8fdf0fb2 100644 --- a/lib/libalpm/be_sync.c +++ b/lib/libalpm/be_sync.c @@ -281,6 +281,23 @@ static int _sync_get_validation(alpm_pkg_t *pkg) return pkg->validation; }
+/** Package sync operations struct accessor. We implement this as a method + * rather than a static struct as in be_files because we want to reuse the + * majority of the default_pkg_ops struct and add only a few operations of + * our own on top.
This confused me a lot... What is be_files? Turns out the only reference to be_files in the codebase is where this comment was copied from in be_package.c! So this comment needs fixed, and the one in be_package.c updated. I'll handle both.
+ */ +static struct pkg_operations *get_sync_pkg_ops(void) +{ + static struct pkg_operations sync_pkg_ops; + static int sync_pkg_ops_initalized = 0; + if(!sync_pkg_ops_initalized) { + sync_pkg_ops = default_pkg_ops; + sync_pkg_ops.get_validation = _sync_get_validation; + sync_pkg_ops_initalized = 1; + } + return &sync_pkg_ops; +} + static alpm_pkg_t *load_pkg_for_entry(alpm_db_t *db, const char *entryname, const char **entry_filename, alpm_pkg_t *likely_pkg) { @@ -321,8 +338,7 @@ static alpm_pkg_t *load_pkg_for_entry(alpm_db_t *db, const char *entryname,
pkg->origin = ALPM_PKG_FROM_SYNCDB; pkg->origin_data.db = db; - pkg->ops = &default_pkg_ops; - pkg->ops->get_validation = _sync_get_validation; + pkg->ops = get_sync_pkg_ops(); pkg->handle = db->handle;
/* add to the collection */
participants (2)
-
Allan McRae
-
Emil Velikov