diff options
author | Richard Hughes <richard@hughsie.com> | 2014-12-17 19:09:55 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2014-12-17 19:43:12 +0000 |
commit | d4acd936d44108034f3734e183778ec71d6fb3c9 (patch) | |
tree | fc741b79806137ff021090a9e9417af526b5d199 /client | |
parent | 85dfc0cb9676f49e42cd309b7256bcd222b7207f (diff) | |
download | appstream-glib-d4acd936d44108034f3734e183778ec71d6fb3c9.tar.gz |
Move as_utils_install_filename() into libappstream-glib
This means we pick up a libarchive dep, but means we can use the installing
code from PackageKit in the future.
Diffstat (limited to 'client')
-rw-r--r-- | client/as-util.c | 259 |
1 files changed, 8 insertions, 251 deletions
diff --git a/client/as-util.c b/client/as-util.c index 9bda500..18d7240 100644 --- a/client/as-util.c +++ b/client/as-util.c @@ -827,253 +827,6 @@ as_util_search (AsUtilPrivate *priv, gchar **values, GError **error) } /** - * as_util_install_icons: - **/ -static gboolean -as_util_install_icons (const gchar *filename, const gchar *origin, GError **error) -{ - const gchar *destdir; - const gchar *pathname; - const gchar *tmp; - gboolean ret = TRUE; - gsize len; - int r; - struct archive *arch = NULL; - struct archive_entry *entry; - _cleanup_free_ gchar *data = NULL; - _cleanup_free_ gchar *dir = NULL; - - destdir = g_getenv ("DESTDIR"); - dir = g_strdup_printf ("%s/usr/share/app-info/icons/%s", - destdir != NULL ? destdir : "", origin); - - /* load file at once to avoid seeking */ - ret = g_file_get_contents (filename, &data, &len, error); - if (!ret) - goto out; - - /* read anything */ - arch = archive_read_new (); - archive_read_support_format_all (arch); - archive_read_support_filter_all (arch); - r = archive_read_open_memory (arch, data, len); - if (r) { - ret = FALSE; - g_set_error (error, - AS_ERROR, - AS_ERROR_FAILED, - "Cannot open: %s", - archive_error_string (arch)); - goto out; - } - - /* decompress each file */ - for (;;) { - _cleanup_free_ gchar *buf = NULL; - - r = archive_read_next_header (arch, &entry); - if (r == ARCHIVE_EOF) - break; - if (r != ARCHIVE_OK) { - ret = FALSE; - g_set_error (error, - AS_ERROR, - AS_ERROR_FAILED, - "Cannot read header: %s", - archive_error_string (arch)); - goto out; - } - - /* no output file */ - pathname = archive_entry_pathname (entry); - if (pathname == NULL) - continue; - - /* update output path */ - buf = g_build_filename (dir, pathname, NULL); - archive_entry_update_pathname_utf8 (entry, buf); - - /* update hardlinks */ - tmp = archive_entry_hardlink (entry); - if (tmp != NULL) { - _cleanup_free_ gchar *buf_link = NULL; - buf_link = g_build_filename (dir, tmp, NULL); - archive_entry_update_hardlink_utf8 (entry, buf_link); - } - - /* update symlinks */ - tmp = archive_entry_symlink (entry); - if (tmp != NULL) { - _cleanup_free_ gchar *buf_link = NULL; - buf_link = g_build_filename (dir, tmp, NULL); - archive_entry_update_symlink_utf8 (entry, buf_link); - } - - r = archive_read_extract (arch, entry, 0); - if (r != ARCHIVE_OK) { - ret = FALSE; - g_set_error (error, - AS_ERROR, - AS_ERROR_FAILED, - "Cannot extract: %s", - archive_error_string (arch)); - goto out; - } - } -out: - if (arch != NULL) { - archive_read_close (arch); - archive_read_free (arch); - } - return ret; -} - -/** - * as_util_install_xml: - **/ -static gboolean -as_util_install_xml (const gchar *filename, - const gchar *origin, - const gchar *dir, - GError **error) -{ - const gchar *destdir; - gchar *tmp; - _cleanup_free_ gchar *basename = NULL; - _cleanup_free_ gchar *path_dest = NULL; - _cleanup_free_ gchar *path_parent = NULL; - _cleanup_object_unref_ GFile *file_dest = NULL; - _cleanup_object_unref_ GFile *file_src = NULL; - - /* create directory structure */ - destdir = g_getenv ("DESTDIR"); - path_parent = g_strdup_printf ("%s%s", destdir != NULL ? destdir : "", dir); - if (g_mkdir_with_parents (path_parent, 0777) != 0) { - g_set_error (error, - AS_ERROR, - AS_ERROR_FAILED, - "Failed to create %s", path_parent); - return FALSE; - } - - /* calculate the new destination */ - file_src = g_file_new_for_path (filename); - basename = g_path_get_basename (filename); - if (origin != NULL) { - _cleanup_free_ gchar *basename_new = NULL; - tmp = g_strstr_len (basename, -1, "."); - if (tmp == NULL) { - g_set_error (error, - AS_ERROR, - AS_ERROR_FAILED, - "Name of XML file invalid %s", - basename); - return FALSE; - } - basename_new = g_strdup_printf ("%s%s", origin, tmp); - /* replace the fedora.xml.gz into %{origin}.xml.gz */ - path_dest = g_build_filename (path_parent, basename_new, NULL); - } else { - path_dest = g_build_filename (path_parent, basename, NULL); - } - - /* actually copy file */ - file_dest = g_file_new_for_path (path_dest); - if (!g_file_copy (file_src, file_dest, - G_FILE_COPY_OVERWRITE | - G_FILE_COPY_TARGET_DEFAULT_PERMS, - NULL, NULL, NULL, error)) - return FALSE; - - /* fix the origin */ - if (origin != NULL) { - _cleanup_object_unref_ AsStore *store = NULL; - store = as_store_new (); - if (!as_store_from_file (store, file_dest, NULL, NULL, error)) - return FALSE; - as_store_set_origin (store, origin); - if (!as_store_to_file (store, file_dest, - AS_NODE_TO_XML_FLAG_ADD_HEADER | - AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE, - NULL, error)) - return FALSE; - } - return TRUE; -} - -typedef enum { - AS_UTIL_INSTALL_TARGET_SHARED, - AS_UTIL_INSTALL_TARGET_CACHE, - AS_UTIL_INSTALL_TARGET_LAST -} AsUtilInstallTarget; - -/** - * as_util_install_filename: - **/ -static gboolean -as_util_install_filename (AsUtilInstallTarget install_target, - const gchar *filename, - const gchar *origin, - GError **error) -{ - const gchar *target = NULL; - gboolean ret = FALSE; - gchar *tmp; - _cleanup_free_ gchar *basename = NULL; - _cleanup_free_ gchar *path = NULL; - - /* use the correct target dir */ - switch (install_target) { - case AS_UTIL_INSTALL_TARGET_SHARED: - target = "/usr/share"; - break; - case AS_UTIL_INSTALL_TARGET_CACHE: - target = "/var/cache"; - break; - default: - break; - } - - switch (as_app_guess_source_kind (filename)) { - case AS_APP_SOURCE_KIND_APPSTREAM: - if (g_strstr_len (filename, -1, ".yml.gz") != NULL) { - path = g_build_filename (target, "app-info", "yaml", NULL); - ret = as_util_install_xml (filename, origin, path, error); - } else { - path = g_build_filename (target, "app-info", "xmls", NULL); - ret = as_util_install_xml (filename, origin, path, error); - } - break; - case AS_APP_SOURCE_KIND_APPDATA: - case AS_APP_SOURCE_KIND_METAINFO: - path = g_build_filename (target, "appdata", NULL); - ret = as_util_install_xml (filename, NULL, path, error); - break; - default: - /* icons */ - if (origin != NULL) { - ret = as_util_install_icons (filename, origin, error); - break; - } - basename = g_path_get_basename (filename); - tmp = g_strstr_len (basename, -1, "-icons.tar.gz"); - if (tmp != NULL) { - *tmp = '\0'; - ret = as_util_install_icons (filename, basename, error); - break; - } - - /* unrecognised */ - g_set_error_literal (error, - AS_ERROR, - AS_ERROR_FAILED, - "No idea how to process files of this type"); - break; - } - return ret; -} - -/** * as_util_install: **/ static gboolean @@ -1094,8 +847,10 @@ as_util_install (AsUtilPrivate *priv, gchar **values, GError **error) /* for each item on the command line, install the xml files and * explode the icon files */ for (i = 0; values[i] != NULL; i++) { - if (!as_util_install_filename (AS_UTIL_INSTALL_TARGET_SHARED, - values[i], NULL, error)) + if (!as_utils_install_filename (AS_UTILS_LOCATION_SHARED, + values[i], NULL, + g_getenv ("DESTDIR"), + error)) return FALSE; } return TRUE; @@ -1122,8 +877,10 @@ as_util_install_origin (AsUtilPrivate *priv, gchar **values, GError **error) /* for each item on the command line, install the xml files and * explode the icon files */ for (i = 1; values[i] != NULL; i++) { - if (!as_util_install_filename (AS_UTIL_INSTALL_TARGET_CACHE, - values[i], values[0], error)) + if (!as_utils_install_filename (AS_UTILS_LOCATION_CACHE, + values[i], values[0], + g_getenv ("DESTDIR"), + error)) return FALSE; } return TRUE; |