diff options
-rw-r--r-- | libappstream-builder/plugins/asb-plugin-firmware.c | 55 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 29 | ||||
-rw-r--r-- | libappstream-glib/as-utils.c | 60 | ||||
-rw-r--r-- | libappstream-glib/as-utils.h | 3 |
4 files changed, 98 insertions, 49 deletions
diff --git a/libappstream-builder/plugins/asb-plugin-firmware.c b/libappstream-builder/plugins/asb-plugin-firmware.c index 13fe9f6..2bb72e2 100644 --- a/libappstream-builder/plugins/asb-plugin-firmware.c +++ b/libappstream-builder/plugins/asb-plugin-firmware.c @@ -22,7 +22,6 @@ #include <config.h> #include <fnmatch.h> #include <string.h> -#include <stdlib.h> #include <asb-plugin.h> @@ -83,38 +82,6 @@ asb_plugin_firmware_sanitize_guid (const gchar *id) } /** - * asb_plugin_firmware_parse_date: - */ -static guint64 -asb_plugin_firmware_parse_date (const gchar *date, GError **error) -{ - _cleanup_date_time_unref_ GDateTime *dt = NULL; - _cleanup_strv_free_ gchar **split = NULL; - - /* split up into MM/DD/YYYY, because America */ - split = g_strsplit (date, "/", -1); - if (g_strv_length (split) != 3) { - g_set_error (error, - ASB_PLUGIN_ERROR, - ASB_PLUGIN_ERROR_NOT_SUPPORTED, - "DriverVer date invalid: %s", date); - return 0; - } - dt = g_date_time_new_local (atoi (split[2]), - atoi (split[0]), - atoi (split[1]), - 0, 0, 0); - if (dt == NULL) { - g_set_error (error, - ASB_PLUGIN_ERROR, - ASB_PLUGIN_ERROR_NOT_SUPPORTED, - "DriverVer date invalid: %s", date); - return 0; - } - return g_date_time_to_unix (dt); -} - -/** * asb_plugin_firmware_get_source_package: */ static gchar * @@ -162,7 +129,7 @@ asb_plugin_process_filename (AsbPlugin *plugin, _cleanup_free_ gchar *checksum = NULL; _cleanup_free_ gchar *class = NULL; _cleanup_free_ gchar *comment = NULL; - _cleanup_free_ gchar *date_version = NULL; + _cleanup_free_ gchar *driver_ver = NULL; _cleanup_free_ gchar *filename_full = NULL; _cleanup_free_ gchar *id_new = NULL; _cleanup_free_ gchar *id = NULL; @@ -171,12 +138,12 @@ asb_plugin_process_filename (AsbPlugin *plugin, _cleanup_free_ gchar *name = NULL; _cleanup_free_ gchar *srcpkg = NULL; _cleanup_free_ gchar *vendor = NULL; + _cleanup_free_ gchar *version = NULL; _cleanup_keyfile_unref_ GKeyFile *kf = NULL; _cleanup_object_unref_ AsbApp *app = NULL; _cleanup_object_unref_ AsIcon *icon = NULL; _cleanup_object_unref_ AsProvide *provide = NULL; _cleanup_object_unref_ AsRelease *release = NULL; - _cleanup_strv_free_ gchar **date_version_split = NULL; /* fix up the inf file */ filename_full = g_build_filename (tmpdir, filename, NULL); @@ -230,8 +197,8 @@ asb_plugin_process_filename (AsbPlugin *plugin, as_app_set_comment (AS_APP (app), NULL, comment, -1); /* get the release date and the version in case there's no metainfo */ - date_version = g_key_file_get_string (kf, "Version", "DriverVer", NULL); - if (date_version == NULL) { + driver_ver = g_key_file_get_string (kf, "Version", "DriverVer", NULL); + if (driver_ver == NULL) { g_set_error_literal (error, ASB_PLUGIN_ERROR, ASB_PLUGIN_ERROR_NOT_SUPPORTED, @@ -240,21 +207,13 @@ asb_plugin_process_filename (AsbPlugin *plugin, } /* parse the DriverVer */ - date_version_split = g_strsplit (date_version, ",", -1); - if (g_strv_length (date_version_split) != 2) { - g_set_error (error, - ASB_PLUGIN_ERROR, - ASB_PLUGIN_ERROR_NOT_SUPPORTED, - "DriverVer is invalid: %s", date_version); - return FALSE; - } - timestamp = asb_plugin_firmware_parse_date (date_version_split[0], error); - if (timestamp == 0) + version = as_utils_parse_driver_version (driver_ver, ×tamp, error); + if (version == NULL) return FALSE; /* add a release with no real description */ release = as_release_new (); - as_release_set_version (release, date_version_split[1], -1); + as_release_set_version (release, version, -1); as_release_set_timestamp (release, timestamp); as_app_add_release (AS_APP (app), release); diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 366f4c2..be37bf2 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -3295,9 +3295,35 @@ as_test_store_speed_yaml_func (void) } static void -as_test_utils_vercmp_func (void) +as_test_utils_driver_ver_func (void) { + gchar *tmp; + guint64 ts; + GError *error = NULL; + + /* valid */ + tmp = as_utils_parse_driver_version ("03/01/2015,2.0.0", &ts, &error); + g_assert_no_error (error); + g_assert_cmpstr (tmp, ==, "2.0.0"); + g_assert_cmpint (ts, ==, 1425168000); + g_free (tmp); + + /* invalid date */ + tmp = as_utils_parse_driver_version ("13/01/2015,2.0.0", &ts, &error); + g_assert_error (error, AS_UTILS_ERROR, AS_UTILS_ERROR_INVALID_TYPE); + g_assert_cmpstr (tmp, ==, NULL); + g_clear_error (&error); + /* no date */ + tmp = as_utils_parse_driver_version ("2.0.0", NULL, &error); + g_assert_error (error, AS_UTILS_ERROR, AS_UTILS_ERROR_INVALID_TYPE); + g_assert_cmpstr (tmp, ==, NULL); + g_clear_error (&error); +} + +static void +as_test_utils_vercmp_func (void) +{ /* same */ g_assert_cmpint (as_utils_vercmp ("1.2.3", "1.2.3"), ==, 0); @@ -3463,6 +3489,7 @@ main (int argc, char **argv) g_test_add_func ("/AppStream/utils{install-filename}", as_test_utils_install_filename_func); g_test_add_func ("/AppStream/utils{inf}", as_test_utils_inf_func); g_test_add_func ("/AppStream/utils{vercmp}", as_test_utils_vercmp_func); + g_test_add_func ("/AppStream/utils{driver-version}", as_test_utils_driver_ver_func); g_test_add_func ("/AppStream/yaml", as_test_yaml_func); g_test_add_func ("/AppStream/store", as_test_store_func); g_test_add_func ("/AppStream/store{demote}", as_test_store_demote_func); diff --git a/libappstream-glib/as-utils.c b/libappstream-glib/as-utils.c index 52f48f4..3a3d7b5 100644 --- a/libappstream-glib/as-utils.c +++ b/libappstream-glib/as-utils.c @@ -37,6 +37,7 @@ #include <libsoup/soup.h> #include <archive_entry.h> #include <archive.h> +#include <stdlib.h> #include "as-app.h" #include "as-cleanup.h" @@ -1693,3 +1694,62 @@ as_utils_vercmp (const gchar *version_a, const gchar *version_b) /* we really shouldn't get here */ return 0; } + +/** + * as_utils_parse_driver_version: + * @driver_version: the DriverVer string, e.g. "03/01/2015,2.0.0" + * @timestamp: the returned driverver timestamp, or %NULL + * @error: A #GError or %NULL + * + * Parses the DriverVer string into a recognisable version and timestamp; + * + * Returns: the version string, or %NULL for error. + * + * Since: 0.3.5 + */ +gchar * +as_utils_parse_driver_version (const gchar *driver_version, + guint64 *timestamp, + GError **error) +{ + _cleanup_date_time_unref_ GDateTime *dt = NULL; + _cleanup_strv_free_ gchar **split = NULL; + _cleanup_strv_free_ gchar **dv_split = NULL; + + /* split into driver date and version */ + dv_split = g_strsplit (driver_version, ",", -1); + if (g_strv_length (dv_split) != 2) { + g_set_error (error, + AS_UTILS_ERROR, + AS_UTILS_ERROR_INVALID_TYPE, + "DriverVer is invalid: %s", driver_version); + return NULL; + } + + /* split up into MM/DD/YYYY, because America */ + if (timestamp != NULL) { + split = g_strsplit (dv_split[0], "/", -1); + if (g_strv_length (split) != 3) { + g_set_error (error, + AS_UTILS_ERROR, + AS_UTILS_ERROR_INVALID_TYPE, + "DriverVer date invalid: %s", + dv_split[0]); + return NULL; + } + dt = g_date_time_new_local (atoi (split[2]), + atoi (split[0]), + atoi (split[1]), + 0, 0, 0); + if (dt == NULL) { + g_set_error (error, + AS_UTILS_ERROR, + AS_UTILS_ERROR_INVALID_TYPE, + "DriverVer date invalid: %s", + dv_split[0]); + return NULL; + } + *timestamp = g_date_time_to_unix (dt); + } + return g_strdup (dv_split[1]); +} diff --git a/libappstream-glib/as-utils.h b/libappstream-glib/as-utils.h index b6e6839..f21243a 100644 --- a/libappstream-glib/as-utils.h +++ b/libappstream-glib/as-utils.h @@ -130,6 +130,9 @@ gboolean as_utils_search_token_valid (const gchar *token); gchar **as_utils_search_tokenize (const gchar *search); GKeyFile *as_utils_load_inf_file (const gchar *filename, GError **error); +gchar *as_utils_parse_driver_version (const gchar *driver_version, + guint64 *timestamp, + GError **error); gint as_utils_vercmp (const gchar *version_a, const gchar *version_b); |