From 38e6679ff5585ec920f15a4feeaa9e1aab837eea Mon Sep 17 00:00:00 2001 From: Juan RP Date: Tue, 19 May 2009 01:42:48 +0200 Subject: [PATCH] Implemented plist caching, that gives >30% performance. --HG-- extra : convert_revision : 6302893f967be96f99a86d499ca62a8c005e56f7 --- bin/xbps-bin/install.c | 71 +++++++++++++++++++++--------------------- bin/xbps-bin/main.c | 35 +++++++++++---------- bin/xbps-bin/remove.c | 30 ++++++++++-------- doc/TODO | 2 -- include/xbps_api.h | 2 +- lib/findpkg.c | 32 +++++++++---------- lib/plist.c | 21 +++++++------ 7 files changed, 100 insertions(+), 93 deletions(-) diff --git a/bin/xbps-bin/install.c b/bin/xbps-bin/install.c index 516ae968f6..55a79a3bc5 100644 --- a/bin/xbps-bin/install.c +++ b/bin/xbps-bin/install.c @@ -168,7 +168,7 @@ xbps_install_pkg(const char *pkg, bool force, bool update) prop_object_iterator_t iter; const char *instver, *origin, *pkgname, *version; int rv = 0; - bool pkg_is_dep, doup = false; + bool pkg_is_dep = false, doup = false; /* * Find and sort all required package dictionaries. @@ -196,8 +196,7 @@ xbps_install_pkg(const char *pkg, bool force, bool update) array = prop_dictionary_get(props, "missing_deps"); if (prop_array_count(array) > 0) { show_missing_deps(props, pkg); - prop_object_release(props); - exit(EXIT_FAILURE); + goto out; } prop_dictionary_get_cstring_nocopy(props, "origin", &origin); @@ -205,14 +204,12 @@ xbps_install_pkg(const char *pkg, bool force, bool update) array = prop_dictionary_get(props, "packages"); if (array == NULL || prop_array_count(array) == 0) { printf("error: empty packages array!\n"); - prop_object_release(props); - exit(EXIT_FAILURE); + goto out; } iter = prop_array_iterator(array); if (iter == NULL) { printf("error: allocating array mem! (%s)\n", strerror(errno)); - prop_object_release(props); - exit(EXIT_FAILURE); + goto out; } /* * Show download/installed size for the transaction. @@ -225,8 +222,7 @@ xbps_install_pkg(const char *pkg, bool force, bool update) if (force == false) { if (xbps_noyes("Do you want to continue?") == false) { printf("Aborting!\n"); - prop_object_release(props); - exit(EXIT_SUCCESS); + goto out2; } } @@ -252,8 +248,7 @@ xbps_install_pkg(const char *pkg, bool force, bool update) if (instpkg == NULL) { printf("error: unable to find %s installed " "dict!\n", pkgname); - prop_object_release(props); - exit(EXIT_FAILURE); + goto out2; } prop_dictionary_get_cstring_nocopy(instpkg, @@ -265,8 +260,7 @@ xbps_install_pkg(const char *pkg, bool force, bool update) if (rv != 0) { printf("error: removing %s-%s (%s)\n", pkgname, instver, strerror(rv)); - prop_object_release(props); - exit(EXIT_FAILURE); + goto out2; } } else { @@ -279,8 +273,7 @@ xbps_install_pkg(const char *pkg, bool force, bool update) if ((rv = xbps_unpack_binary_pkg(obj)) != 0) { printf("error: unpacking %s-%s (%s)\n", pkgname, version, strerror(rv)); - prop_object_release(props); - exit(EXIT_FAILURE); + goto out2; } /* * Register binary package. @@ -291,13 +284,18 @@ xbps_install_pkg(const char *pkg, bool force, bool update) if ((rv = xbps_register_pkg(obj, doup, pkg_is_dep)) != 0) { printf("error: registering %s-%s! (%s)\n", pkgname, version, strerror(rv)); - prop_object_release(props); - exit(EXIT_FAILURE); + goto out2; } pkg_is_dep = false; } +out2: prop_object_iterator_release(iter); +out: prop_object_release(props); + xbps_release_repolist_data(); + xbps_release_regpkgdb_dict(); + if (rv != 0) + exit(EXIT_FAILURE); exit(EXIT_SUCCESS); } @@ -312,7 +310,7 @@ xbps_autoupdate_pkgs(bool force) const char *pkgname, *version, *instver; int rv = 0; - dict = xbps_get_regpkgdb_dict(); + dict = xbps_prepare_regpkgdb_dict(); if (dict == NULL) { printf("No packages currently installed (%s).\n", strerror(errno)); @@ -321,15 +319,12 @@ xbps_autoupdate_pkgs(bool force) iter = xbps_get_array_iter_from_dict(dict, "packages"); if (iter == NULL) { - xbps_release_regpkgdb_dict(); - exit(EXIT_FAILURE); + rv = EINVAL; + goto out; } - if (xbps_prepare_repolist_data() != 0) { - prop_object_iterator_release(iter); - xbps_release_regpkgdb_dict(); - exit(EXIT_FAILURE); - } + if ((rv = xbps_prepare_repolist_data()) != 0) + goto out; while ((obj = prop_object_iterator_next(iter)) != NULL) { prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); @@ -338,7 +333,6 @@ xbps_autoupdate_pkgs(bool force) break; } prop_object_iterator_release(iter); - xbps_release_regpkgdb_dict(); /* Sort the list of packages */ props = xbps_get_pkg_props(); @@ -349,25 +343,25 @@ xbps_autoupdate_pkgs(bool force) } printf("Error while checking for new pkgs: %s\n", strerror(errno)); - exit(EXIT_FAILURE); + goto out; } if ((rv = xbps_sort_pkg_deps(props)) != 0) { printf("Error while sorting packages: %s\n", strerror(rv)); - exit(EXIT_FAILURE); + goto out; } /* Update all packages now */ array = prop_dictionary_get(props, "packages"); if (array == NULL || prop_array_count(array) == 0) { printf("error: empty packages array!\n"); prop_object_release(props); - exit(EXIT_FAILURE); + goto out; } iter = prop_array_iterator(array); if (iter == NULL) { printf("error: allocating array mem! (%s)\n", strerror(errno)); prop_object_release(props); - exit(EXIT_FAILURE); + goto out; } /* @@ -382,7 +376,7 @@ xbps_autoupdate_pkgs(bool force) if (xbps_noyes("Do you want to continue?") == false) { printf("Aborting!\n"); prop_object_release(props); - exit(EXIT_SUCCESS); + goto out2; } } @@ -403,7 +397,7 @@ xbps_autoupdate_pkgs(bool force) printf("error: unable to find %s installed " "dict!\n", pkgname); prop_object_release(props); - exit(EXIT_FAILURE); + goto out2; } prop_dictionary_get_cstring_nocopy(instpkg, @@ -416,7 +410,7 @@ xbps_autoupdate_pkgs(bool force) printf("error: removing %s-%s (%s)\n", pkgname, instver, strerror(rv)); prop_object_release(props); - exit(EXIT_FAILURE); + goto out2; } /* @@ -426,7 +420,7 @@ xbps_autoupdate_pkgs(bool force) printf("error: unpacking %s-%s (%s)\n", pkgname, version, strerror(rv)); prop_object_release(props); - exit(EXIT_FAILURE); + goto out2; } /* * Register binary package. @@ -435,10 +429,17 @@ xbps_autoupdate_pkgs(bool force) printf("error: registering %s-%s! (%s)\n", pkgname, version, strerror(rv)); prop_object_release(props); - exit(EXIT_FAILURE); + goto out2; } } + +out2: prop_object_iterator_release(iter); +out: xbps_release_repolist_data(); + xbps_release_regpkgdb_dict(); + if (rv != 0) + exit(EXIT_FAILURE); + exit(EXIT_SUCCESS); } diff --git a/bin/xbps-bin/main.c b/bin/xbps-bin/main.c index 5b823484fb..0db1329dc2 100644 --- a/bin/xbps-bin/main.c +++ b/bin/xbps-bin/main.c @@ -100,8 +100,6 @@ int main(int argc, char **argv) { prop_dictionary_t dict; - const char *rootdir; - char *plist; int c, flags = 0, rv = 0; bool chkhash = false, force = false, verbose = false; @@ -136,32 +134,30 @@ main(int argc, char **argv) if (flags != 0) xbps_set_flags(flags); + if ((dict = xbps_prepare_regpkgdb_dict()) == NULL) { + if (errno != ENOENT) { + rv = errno; + printf("Couldn't initialized regpkgdb dict: %s\n", + strerror(errno)); + goto out; + } + } + if (strcasecmp(argv[0], "list") == 0) { /* Lists packages currently registered in database. */ if (argc != 1) usage(); - rootdir = xbps_get_rootdir(); - plist = xbps_xasprintf("%s/%s/%s", rootdir, - XBPS_META_PATH, XBPS_REGPKGDB); - if (plist == NULL) - exit(EXIT_FAILURE); - - dict = prop_dictionary_internalize_from_file(plist); if (dict == NULL) { - printf("No packages currently registered.\n"); - free(plist); - exit(EXIT_SUCCESS); + printf("No packages currently installed.\n"); + goto out; } if (!xbps_callback_array_iter_in_dict(dict, "packages", list_pkgs_in_dict, NULL)) { - prop_object_release(dict); - free(plist); - exit(EXIT_FAILURE); + rv = errno; + goto out; } - prop_object_release(dict); - free(plist); } else if (strcasecmp(argv[0], "install") == 0) { /* Installs a binary package and required deps. */ @@ -230,5 +226,10 @@ main(int argc, char **argv) usage(); } +out: + xbps_release_regpkgdb_dict(); + if (rv != 0) + exit(EXIT_FAILURE); + exit(EXIT_SUCCESS); } diff --git a/bin/xbps-bin/remove.c b/bin/xbps-bin/remove.c index c7853d25e1..5237b661bd 100644 --- a/bin/xbps-bin/remove.c +++ b/bin/xbps-bin/remove.c @@ -64,7 +64,7 @@ xbps_autoremove_pkgs(void) iter = prop_array_iterator(orphans); if (iter == NULL) - exit(EXIT_FAILURE); + goto out; printf("The following packages were installed automatically\n" "(as dependencies) and aren't needed anymore:\n\n"); @@ -88,7 +88,7 @@ xbps_autoremove_pkgs(void) if (xbps_noyes("Do you want to remove them?") == false) { printf("Cancelled!\n"); - exit(EXIT_SUCCESS); + goto out2; } while ((obj = prop_object_iterator_next(iter)) != NULL) { @@ -96,14 +96,15 @@ xbps_autoremove_pkgs(void) prop_dictionary_get_cstring_nocopy(obj, "version", &version); printf("Removing package %s-%s ...\n", pkgname, version); - if ((rv = xbps_remove_binary_pkg(pkgname, false)) != 0) { - prop_object_iterator_release(iter); - prop_object_release(orphans); - exit(EXIT_FAILURE); - } + if ((rv = xbps_remove_binary_pkg(pkgname, false)) != 0) + goto out2; } +out2: prop_object_iterator_release(iter); +out: prop_object_release(orphans); + if (rv != 0) + exit(EXIT_FAILURE); exit(EXIT_SUCCESS); } @@ -122,7 +123,7 @@ xbps_remove_pkg(const char *pkgname, bool force) dict = xbps_find_pkg_installed_from_plist(pkgname); if (dict == NULL) { printf("Package %s is not installed.\n", pkgname); - exit(EXIT_FAILURE); + goto out; } prop_dictionary_get_cstring_nocopy(dict, "version", &version); @@ -136,7 +137,7 @@ xbps_remove_pkg(const char *pkgname, bool force) if (!force) { if (!xbps_noyes("Do you want to remove %s?", pkgname)) { printf("Cancelling!\n"); - exit(EXIT_SUCCESS); + goto out; } } printf("Forcing %s-%s for deletion!\n", pkgname, version); @@ -144,7 +145,7 @@ xbps_remove_pkg(const char *pkgname, bool force) if (!force) { if (!xbps_noyes("Do you want to remove %s?", pkgname)) { printf("Cancelling!\n"); - exit(EXIT_SUCCESS); + goto out; } } } @@ -153,11 +154,14 @@ xbps_remove_pkg(const char *pkgname, bool force) if ((rv = xbps_remove_binary_pkg(pkgname, false)) != 0) { printf("Unable to remove %s-%s (%s).\n", pkgname, version, strerror(errno)); - prop_object_release(dict); - exit(EXIT_FAILURE); + goto out; } printf("Package %s-%s removed successfully.\n", pkgname, version); - prop_object_release(dict); + +out: + xbps_release_regpkgdb_dict(); + if (rv != 0) + exit(EXIT_FAILURE); exit(EXIT_SUCCESS); } diff --git a/doc/TODO b/doc/TODO index d9e5287cf0..098fcc19ed 100644 --- a/doc/TODO +++ b/doc/TODO @@ -41,8 +41,6 @@ xbps-bin: libxbps: * Fix glibc updates: removing libc is bad. - * When accessing .plist files, only internalize_from_file once to - avoid multiple open(3) and fstat(3) calls. [IN PROGRESS] * Create xbps_upgrade_pkg() to replace or remove/unpack and register. Remove duplicate code from xbps-bin/install.c. [IN PROGRESS] * Add support to upgrade packages but overwritting current files; diff --git a/include/xbps_api.h b/include/xbps_api.h index 85e8818a33..caead1c4cf 100644 --- a/include/xbps_api.h +++ b/include/xbps_api.h @@ -130,7 +130,7 @@ prop_dictionary_t xbps_find_pkg_from_plist(const char *, const char *); prop_dictionary_t xbps_find_pkg_installed_from_plist(const char *); bool xbps_find_string_in_array(prop_array_t, const char *); -prop_dictionary_t xbps_get_regpkgdb_dict(void); +prop_dictionary_t xbps_prepare_regpkgdb_dict(void); void xbps_release_regpkgdb_dict(void); prop_object_iterator_t xbps_get_array_iter_from_dict(prop_dictionary_t, const char *); diff --git a/lib/findpkg.c b/lib/findpkg.c index 2c9088b459..f95a810245 100644 --- a/lib/findpkg.c +++ b/lib/findpkg.c @@ -35,17 +35,15 @@ #include static prop_dictionary_t pkg_props; +static bool pkg_props_initialized; static int create_pkg_props_dictionary(void) { prop_array_t unsorted, missing; int rv = 0; - static bool pkg_props_avail; - assert(pkgname != NULL); - - if (pkg_props_avail) + if (pkg_props_initialized) return 0; pkg_props = prop_dictionary_create(); @@ -73,7 +71,7 @@ create_pkg_props_dictionary(void) goto fail3; } - pkg_props_avail = true; + pkg_props_initialized = true; return rv; @@ -90,7 +88,7 @@ fail: prop_dictionary_t xbps_get_pkg_props(void) { - if (pkg_props == NULL || prop_dictionary_count(pkg_props) == 0) + if (pkg_props_initialized == false) return NULL; return prop_dictionary_copy(pkg_props); @@ -107,18 +105,12 @@ xbps_prepare_repolist_data(void) const char *rootdir; char *plist; int rv = 0; - static bool repodata_init; + static bool repodata_initialized; - if (repodata_init == false) { - SIMPLEQ_INIT(&repodata_queue); - repodata_init = true; - } + if (repodata_initialized) + return 0; - rdata = malloc(sizeof(struct repository_data)); - if (rdata == NULL) { - rv = errno; - goto out; - } + SIMPLEQ_INIT(&repodata_queue); rootdir = xbps_get_rootdir(); if (rootdir == NULL) @@ -163,6 +155,12 @@ xbps_prepare_repolist_data(void) goto out2; } + rdata = malloc(sizeof(struct repository_data)); + if (rdata == NULL) { + rv = errno; + goto out2; + } + rdata->rd_repod = prop_dictionary_internalize_from_file(plist); if (rdata->rd_repod == NULL) { free(plist); @@ -173,6 +171,8 @@ xbps_prepare_repolist_data(void) SIMPLEQ_INSERT_TAIL(&repodata_queue, rdata, chain); } + repodata_initialized = true; + out2: prop_object_iterator_release(iter); out: diff --git a/lib/plist.c b/lib/plist.c index 497a93c60f..4ccf5b6de4 100644 --- a/lib/plist.c +++ b/lib/plist.c @@ -34,6 +34,7 @@ #include static prop_dictionary_t regpkgdb_dict; +static bool regpkgdb_initialized; bool xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj, @@ -162,16 +163,16 @@ xbps_find_pkg_from_plist(const char *plist, const char *pkgname) prop_dictionary_t xbps_find_pkg_installed_from_plist(const char *pkgname) { - prop_dictionary_t instd, pkgd; + prop_dictionary_t pkgd; - instd = xbps_get_regpkgdb_dict(); - if (instd == NULL) + if (regpkgdb_initialized == false) return NULL; - pkgd = xbps_find_pkg_in_dict(instd, "packages", pkgname); - xbps_release_regpkgdb_dict(); + pkgd = xbps_find_pkg_in_dict(regpkgdb_dict, "packages", pkgname); + if (pkgd == NULL) + return NULL; - return pkgd; + return prop_dictionary_copy(pkgd); } prop_dictionary_t @@ -201,12 +202,12 @@ xbps_find_pkg_in_dict(prop_dictionary_t dict, const char *key, } prop_dictionary_t -xbps_get_regpkgdb_dict(void) +xbps_prepare_regpkgdb_dict(void) { const char *rootdir; char *plist; - if (regpkgdb_dict == NULL) { + if (regpkgdb_initialized == false) { rootdir = xbps_get_rootdir(); plist = xbps_xasprintf("%s/%s/%s", rootdir, XBPS_META_PATH, XBPS_REGPKGDB); @@ -219,6 +220,7 @@ xbps_get_regpkgdb_dict(void) return NULL; } free(plist); + regpkgdb_initialized = true; } return prop_dictionary_copy(regpkgdb_dict); @@ -227,11 +229,12 @@ xbps_get_regpkgdb_dict(void) void xbps_release_regpkgdb_dict(void) { - if (regpkgdb_dict == NULL) + if (regpkgdb_initialized == false) return; prop_object_release(regpkgdb_dict); regpkgdb_dict = NULL; + regpkgdb_initialized = false; } bool