[pacman-dev] [PATCH] Add alpm_list_previous method
Helper function to get the previous item in a list Signed-off-by: Allan McRae <allan@archlinux.org> --- lib/libalpm/alpm_list.c | 18 ++++++++++++++++++ lib/libalpm/alpm_list.h | 1 + 2 files changed, 19 insertions(+), 0 deletions(-) diff --git a/lib/libalpm/alpm_list.c b/lib/libalpm/alpm_list.c index c2b30ad..1976e13 100644 --- a/lib/libalpm/alpm_list.c +++ b/lib/libalpm/alpm_list.c @@ -556,6 +556,24 @@ inline alpm_list_t SYMEXPORT *alpm_list_next(const alpm_list_t *node) } /** + * @brief Get the previous element of a list. + * + * @param list the list head + * @param node the list node + * + * @return the previous element, or NULL when no previous element exist + */ +inline alpm_list_t SYMEXPORT *alpm_list_previous(const alpm_list_t *list, + const alpm_list_t *node) +{ + if(node && node != list) { + return node->prev; + } else { + return NULL; + } +} + +/** * @brief Get the last item in the list. * * @param list the list diff --git a/lib/libalpm/alpm_list.h b/lib/libalpm/alpm_list.h index 1f6393a..27a76d1 100644 --- a/lib/libalpm/alpm_list.h +++ b/lib/libalpm/alpm_list.h @@ -70,6 +70,7 @@ alpm_list_t *alpm_list_reverse(alpm_list_t *list); alpm_list_t *alpm_list_first(const alpm_list_t *list); alpm_list_t *alpm_list_nth(const alpm_list_t *list, size_t n); alpm_list_t *alpm_list_next(const alpm_list_t *list); +alpm_list_t *alpm_list_previous(const alpm_list_t *list, const alpm_list_t *node); alpm_list_t *alpm_list_last(const alpm_list_t *list); void *alpm_list_getdata(const alpm_list_t *entry); -- 1.7.6
This saves replicating the potentially large list of files in a package that is being removed. Signed-off-by: Allan McRae <allan@archlinux.org> --- lib/libalpm/remove.c | 12 +++--------- 1 files changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c index 86571a4..48adf04 100644 --- a/lib/libalpm/remove.c +++ b/lib/libalpm/remove.c @@ -285,8 +285,7 @@ static void unlink_file(alpm_handle_t *handle, alpm_pkg_t *info, const char *fil int _alpm_upgraderemove_package(alpm_handle_t *handle, alpm_pkg_t *oldpkg, alpm_pkg_t *newpkg) { - alpm_list_t *skip_remove, *b; - alpm_list_t *newfiles, *lp; + alpm_list_t *skip_remove, *b, *lp; size_t filenum = 0; alpm_list_t *files = alpm_pkg_get_files(oldpkg); const char *pkgname = alpm_pkg_get_name(oldpkg); @@ -329,11 +328,9 @@ int _alpm_upgraderemove_package(alpm_handle_t *handle, _alpm_log(handle, PM_LOG_DEBUG, "removing %ld files\n", (unsigned long)filenum); /* iterate through the list backwards, unlinking files */ - newfiles = alpm_list_reverse(files); - for(lp = newfiles; lp; lp = alpm_list_next(lp)) { + for(lp = alpm_list_last(files); lp; lp = alpm_list_previous(files, lp)) { unlink_file(handle, oldpkg, lp->data, skip_remove, 0); } - alpm_list_free(newfiles); FREELIST(skip_remove); db: @@ -390,7 +387,6 @@ int _alpm_remove_packages(alpm_handle_t *handle) if(!(trans->flags & PM_TRANS_FLAG_DBONLY)) { alpm_list_t *files = alpm_pkg_get_files(info); - alpm_list_t *newfiles; size_t filenum = 0; for(lp = files; lp; lp = lp->next) { @@ -409,8 +405,7 @@ int _alpm_remove_packages(alpm_handle_t *handle) pkg_count, (pkg_count - targcount + 1)); /* iterate through the list backwards, unlinking files */ - newfiles = alpm_list_reverse(files); - for(lp = newfiles; lp; lp = alpm_list_next(lp)) { + for(lp = alpm_list_last(files); lp; lp = alpm_list_previous(files, lp)) { int percent; unlink_file(handle, info, lp->data, NULL, trans->flags & PM_TRANS_FLAG_NOSAVE); @@ -420,7 +415,6 @@ int _alpm_remove_packages(alpm_handle_t *handle) percent, pkg_count, (pkg_count - targcount + 1)); position++; } - alpm_list_free(newfiles); } /* set progress to 100% after we finish unlinking files */ -- 1.7.6
participants (1)
-
Allan McRae