summaryrefslogtreecommitdiff
path: root/libappstream-builder
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2015-02-02 12:14:30 +0000
committerRichard Hughes <richard@hughsie.com>2015-02-02 12:14:30 +0000
commit5319b33c29b4704a6e6710e762e5067b71909939 (patch)
treead88b2388ddac40bb72079f1bf52a3bcff552689 /libappstream-builder
parentf2fa8848346d7f72cd46d9548ac319847f486753 (diff)
downloadappstream-glib-5319b33c29b4704a6e6710e762e5067b71909939.tar.gz
Guess the NEVRA from the filename if possible
This avoids opening every package at startup just to check the architecture.
Diffstat (limited to 'libappstream-builder')
-rw-r--r--libappstream-builder/asb-package.c52
-rw-r--r--libappstream-builder/asb-self-test.c13
2 files changed, 65 insertions, 0 deletions
diff --git a/libappstream-builder/asb-package.c b/libappstream-builder/asb-package.c
index c08c5cc..d0e9d4f 100644
--- a/libappstream-builder/asb-package.c
+++ b/libappstream-builder/asb-package.c
@@ -811,6 +811,51 @@ asb_package_class_init (AsbPackageClass *klass)
}
/**
+ * asb_package_guess_from_filename:
+ **/
+static void
+asb_package_guess_from_filename (AsbPackage *pkg)
+{
+ AsbPackagePrivate *priv = GET_PRIVATE (pkg);
+ _cleanup_free_ gchar *tmp = NULL;
+ gchar *at;
+
+ /* remove .rpm extension */
+ tmp = g_strdup (priv->filename);
+ at = g_strrstr (tmp, ".rpm");
+ if (at == NULL)
+ return;
+ *at = '\0';
+
+ /* get arch */
+ at = g_strrstr (tmp, ".");
+ if (at == NULL)
+ return;
+ priv->arch = g_strdup (at + 1);
+ *at = '\0';
+
+ /* get release */
+ at = g_strrstr (tmp, "-");
+ if (at == NULL)
+ return;
+ priv->release = g_strdup (at + 1);
+ *at = '\0';
+
+ /* get version */
+ at = g_strrstr (tmp, "-");
+ if (at == NULL)
+ return;
+ priv->version = g_strdup (at + 1);
+ *at = '\0';
+
+ /* get name */
+ at = g_strrstr (tmp, "/");
+ if (at == NULL)
+ return;
+ priv->name = g_strdup (at + 1);
+}
+
+/**
* asb_package_set_filename:
* @pkg: A #AsbPackage
* @filename: package filename
@@ -827,6 +872,9 @@ asb_package_set_filename (AsbPackage *pkg, const gchar *filename)
g_free (priv->filename);
priv->basename = g_path_get_basename (filename);
priv->filename = g_strdup (filename);
+
+ /* this only works for correctly formatted file names */
+ asb_package_guess_from_filename (pkg);
}
/**
@@ -854,6 +902,10 @@ asb_package_open (AsbPackage *pkg, const gchar *filename, GError **error)
return TRUE;
priv->is_open = TRUE;
+ /* save filename if not already set */
+ if (priv->filename == NULL)
+ asb_package_set_filename (pkg, filename);
+
/* call distro-specific method */
if (klass->open != NULL)
return klass->open (pkg, filename, error);
diff --git a/libappstream-builder/asb-self-test.c b/libappstream-builder/asb-self-test.c
index 6145e87..b196cba 100644
--- a/libappstream-builder/asb-self-test.c
+++ b/libappstream-builder/asb-self-test.c
@@ -181,6 +181,18 @@ asb_test_package_rpm_func (void)
#endif
static void
+asb_test_package_func (void)
+{
+ _cleanup_object_unref_ AsbPackage *pkg = NULL;
+ pkg = asb_package_new ();
+ asb_package_set_filename (pkg, "/tmp/gambit-c-doc-4.7.3-2.fc22.noarch.rpm");
+ g_assert_cmpstr (asb_package_get_nevra (pkg), ==, "gambit-c-doc-4.7.3-2.fc22.noarch");
+ g_assert_cmpstr (asb_package_get_name (pkg), ==, "gambit-c-doc");
+ g_assert_cmpstr (asb_package_get_version (pkg), ==, "4.7.3");
+ g_assert_cmpstr (asb_package_get_arch (pkg), ==, "noarch");
+}
+
+static void
asb_test_utils_glob_func (void)
{
_cleanup_ptrarray_unref_ GPtrArray *array = NULL;
@@ -859,6 +871,7 @@ main (int argc, char **argv)
g_setenv ("ASB_IS_SELF_TEST", "", TRUE);
/* tests go here */
+ g_test_add_func ("/AppStreamBuilder/package", asb_test_package_func);
g_test_add_func ("/AppStreamBuilder/utils{replace}", asb_test_utils_replace_func);
g_test_add_func ("/AppStreamBuilder/utils{glob}", asb_test_utils_glob_func);
g_test_add_func ("/AppStreamBuilder/plugin-loader", asb_test_plugin_loader_func);