From 1cac40db855942f074fdd905cb7a51ebf0bc2c42 Mon Sep 17 00:00:00 2001 From: jongmyeongko Date: Sat, 26 Nov 2016 09:29:28 +0900 Subject: [PATCH] add internal api for version comparison. Change-Id: I10cbc25ce0517856352558ba5a5de45da481c3b2 Signed-off-by: jongmyeongko --- include/pkgmgr-info.h | 4 ++ include/pkgmgrinfo_type.h | 15 ++++-- packaging/pkgmgr-info.spec | 2 +- src/pkgmgrinfo_pkginfo.c | 125 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 141 insertions(+), 5 deletions(-) diff --git a/include/pkgmgr-info.h b/include/pkgmgr-info.h index bbd7d8a..684317f 100644 --- a/include/pkgmgr-info.h +++ b/include/pkgmgr-info.h @@ -5790,6 +5790,10 @@ static int set_app_installed_storage(const char *appid, INSTALL_LOCATION locatio int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path); int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid); +/* version compare */ +int pkgmgrinfo_compare_package_version(const char *current_version, + const char *target_version, pkgmgrinfo_version_compare_type *res); + /** * @brief TEMP */ diff --git a/include/pkgmgrinfo_type.h b/include/pkgmgrinfo_type.h index 228e77a..4bb96b9 100644 --- a/include/pkgmgrinfo_type.h +++ b/include/pkgmgrinfo_type.h @@ -386,6 +386,15 @@ typedef enum { } pkgmgrinfo_cert_type; /** + * @brief version comparison Types + */ +typedef enum { + PMINFO_VERSION_OLD = -1, + PMINFO_VERSION_SAME, + PMINFO_VERSION_NEW, +} pkgmgrinfo_version_compare_type; + +/** * @brief Install Location Types to be used when setting data in DB */ typedef enum { @@ -393,9 +402,9 @@ typedef enum { INSTALL_EXTERNAL, /**< External Installation*/ } INSTALL_LOCATION; - /** - * @brief permission Types - */ +/** + * @brief permission Types + */ typedef enum { PMINFO_PERMISSION_NORMAL = 0, /**< permission normal*/ PMINFO_PERMISSION_SIGNATURE, /**< permission type is signature*/ diff --git a/packaging/pkgmgr-info.spec b/packaging/pkgmgr-info.spec index a9f93a5..575cc73 100644 --- a/packaging/pkgmgr-info.spec +++ b/packaging/pkgmgr-info.spec @@ -1,6 +1,6 @@ Name: pkgmgr-info Summary: Packager Manager infomation api for package -Version: 0.1.1 +Version: 0.1.2 Release: 1 Group: Application Framework/Package Management License: Apache-2.0 diff --git a/src/pkgmgrinfo_pkginfo.c b/src/pkgmgrinfo_pkginfo.c index fec1406..2944e65 100644 --- a/src/pkgmgrinfo_pkginfo.c +++ b/src/pkgmgrinfo_pkginfo.c @@ -1604,7 +1604,7 @@ API int pkgmgrinfo_pkginfo_filter_create(pkgmgrinfo_pkginfo_filter_h *handle) retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle output parameter is NULL\n"); - filter = (pkgmgrinfo_filter_x*)calloc(1, sizeof(pkgmgrinfo_filter_x)); + filter = (pkgmgrinfo_filter_x *)calloc(1, sizeof(pkgmgrinfo_filter_x)); if (filter == NULL) { _LOGE("Out of Memory!!!"); return PMINFO_R_ERROR; @@ -1869,3 +1869,126 @@ API int pkgmgrinfo_pkginfo_foreach_privilege(pkgmgrinfo_pkginfo_h handle, } return PMINFO_R_OK; } + +int __compare_package_version(const char *version, int *major, + int *minor, int *macro, int *nano) +{ + char *version_temp = NULL; + char *major_str = NULL; + char *minor_str = NULL; + char *macro_str = NULL; + char *nano_str = NULL; + char *save_str = NULL; + + if (version == NULL || major == NULL || minor == NULL || + macro == NULL || nano == NULL) { + return PMINFO_R_EINVAL; + } + + version_temp = strdup(version); + if (version_temp == NULL) { + LOGE("Out of memory"); + return PMINFO_R_ERROR; + } + + major_str = strtok_r(version_temp, ".", &save_str); + if (major_str == NULL) { + _LOGE("major version is NULL"); + free(version_temp); + return PMINFO_R_ERROR; + } + + minor_str = strtok_r(NULL, ".", &save_str); + if (minor_str == NULL) { + _LOGE("minor version is NULL"); + free(version_temp); + return PMINFO_R_ERROR; + } + + *major = atoi(major_str); + *minor = atoi(minor_str); + *macro = 0; + *minor = 0; + macro_str = strtok_r(NULL, ".", &save_str); + if (macro_str == NULL) { + _LOGD("macro version is NULL"); + } else { + *macro = atoi(macro_str); + nano_str = strtok_r(NULL, ".", &save_str); + if (nano_str) { + *nano = atoi(nano_str); + _LOGD("nano version exists"); + } + } + _LOGD("version = [%s] -> major = [%d], minor = [%d]," \ + " macro = [%d], nano = [%d]", version, *major, + *minor, *macro, *nano); + + free(version_temp); + + return PMINFO_R_OK; +} + +API int pkgmgrinfo_compare_package_version(const char *current_version, + const char *target_version, + pkgmgrinfo_version_compare_type *res) +{ + int ret = 0; + int current_version_major = 0; + int current_version_minor = 0; + int current_version_macro = 0; + int current_version_nano = 0; + int target_version_major = 0; + int target_version_minor = 0; + int target_version_macro = 0; + int target_version_nano = 0; + + if (current_version == NULL || target_version == NULL || + res == NULL) { + _LOGE("Invalid parameter"); + return PMINFO_R_EINVAL; + } + + ret = __compare_package_version(target_version, + &target_version_major, &target_version_minor, + &target_version_macro, &target_version_nano); + if (ret < 0) { + _LOGE("Failed to compare target version(%d)", ret); + return PMINFO_R_ERROR; + } + + ret = __compare_package_version(current_version, + ¤t_version_major, ¤t_version_minor, + ¤t_version_macro, ¤t_version_nano); + if (ret < 0) { + _LOGE("Failed to compare current version(%d)", ret); + return PMINFO_R_ERROR; + } + + _LOGD("new[%d.%d.%d.%d] old[%d.%d.%d.%d]", target_version_major, + target_version_minor, target_version_macro, + target_version_nano, current_version_major, + current_version_minor, current_version_macro, + target_version_nano); + + if (target_version_major > current_version_major) + *res = PMINFO_VERSION_NEW; + else if (target_version_major < current_version_major) + *res = PMINFO_VERSION_OLD; + else if (target_version_minor > current_version_minor) + *res = PMINFO_VERSION_NEW; + else if (target_version_minor < current_version_minor) + *res = PMINFO_VERSION_OLD; + else if (target_version_macro > current_version_macro) + *res = PMINFO_VERSION_NEW; + else if (target_version_macro < current_version_macro) + *res = PMINFO_VERSION_OLD; + else if (target_version_nano > current_version_nano) + *res = PMINFO_VERSION_NEW; + else if (target_version_nano < current_version_nano) + *res = PMINFO_VERSION_OLD; + else + *res = PMINFO_VERSION_SAME; + + return PMINFO_R_OK; +} -- 2.7.4