static gboolean download_archive (SoupURI *uri, const gchar *sha, GFile *archive_file, GError **error) { g_autoptr(GBytes) content = NULL; g_autofree gchar *sha256 = NULL; content = download_uri (uri, error); if (content == NULL) return FALSE; sha256 = g_compute_checksum_for_bytes (G_CHECKSUM_SHA256, content); if (g_strcmp0 (sha256, sha) != 0) { g_autofree gchar *path = g_file_get_path (archive_file); g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Wrong sha256 for %s, expected %s, was %s", path, sha, sha256); return FALSE; } return g_file_replace_contents (archive_file, g_bytes_get_data (content, NULL), g_bytes_get_size (content), NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, error); }
static gboolean builder_source_file_extract (BuilderSource *source, GFile *dest, BuilderContext *context, GError **error) { BuilderSourceFile *self = BUILDER_SOURCE_FILE (source); g_autoptr(GFile) src = NULL; g_autoptr(GFile) dest_file = NULL; g_autofree char *dest_filename = NULL; gboolean is_local, is_inline; src = get_source_file (self, context, &is_local, &is_inline, error); if (src == NULL) return FALSE; if (self->dest_filename) dest_filename = g_strdup (self->dest_filename); else { if (is_inline) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "No dest-filename set for inline file data"); return FALSE; } dest_filename = g_file_get_basename (src); } dest_file = g_file_get_child (dest, dest_filename); if (is_inline) { g_autoptr(GBytes) content = NULL; content = download_uri (self->url, context, error); if (content == NULL) return FALSE; if (!g_file_replace_contents (dest_file, g_bytes_get_data (content, NULL), g_bytes_get_size (content), NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, error)) return FALSE; } else { if (is_local) { g_autofree char *data = NULL; g_autofree char *base64 = NULL; gsize len; if (!g_file_load_contents (src, NULL, &data, &len, NULL, error)) return FALSE; base64 = g_base64_encode ((const guchar *)data, len); g_free (self->url); self->url = g_strdup_printf ("data:text/plain;charset=utf8;base64,%s", base64); if (self->dest_filename == NULL || *self->dest_filename == 0) { g_free (self->dest_filename); self->dest_filename = g_file_get_basename (src); } } if (!g_file_copy (src, dest_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, error)) return FALSE; } return TRUE; }
static gboolean builder_source_file_download (BuilderSource *source, gboolean update_vcs, BuilderContext *context, GError **error) { BuilderSourceFile *self = BUILDER_SOURCE_FILE (source); g_autoptr(GFile) file = NULL; gboolean is_local, is_inline; g_autoptr (GFile) dir = NULL; g_autofree char *dir_path = NULL; g_autofree char *sha256 = NULL; g_autofree char *base_name = NULL; g_autoptr(GBytes) content = NULL; file = get_source_file (self, context, &is_local, &is_inline, error); if (file == NULL) return FALSE; base_name = g_file_get_basename (file); if (g_file_query_exists (file, NULL)) { if (is_local && self->sha256 != NULL && *self->sha256 != 0) { g_autofree char *data = NULL; gsize len; if (!g_file_load_contents (file, NULL, &data, &len, NULL, error)) return FALSE; sha256 = g_compute_checksum_for_string (G_CHECKSUM_SHA256, data, len); if (strcmp (sha256, self->sha256) != 0) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Wrong sha256 for %s, expected %s, was %s", base_name, self->sha256, sha256); return FALSE; } } return TRUE; } if (is_local) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Can't find file at %s", self->path); return FALSE; } content = download_uri (self->url, context, error); if (content == NULL) return FALSE; sha256 = g_compute_checksum_for_string (G_CHECKSUM_SHA256, g_bytes_get_data (content, NULL), g_bytes_get_size (content)); /* sha256 is optional for inline data */ if (((self->sha256 != NULL && *self->sha256 != 0) || !is_inline) && strcmp (sha256, self->sha256) != 0) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Wrong sha256 for %s, expected %s, was %s", base_name, self->sha256, sha256); return FALSE; } dir = g_file_get_parent (file); dir_path = g_file_get_path (dir); g_mkdir_with_parents (dir_path, 0755); if (!g_file_replace_contents (file, g_bytes_get_data (content, NULL), g_bytes_get_size (content), NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, error)) return FALSE; return TRUE; }
static gboolean install_from (FlatpakDir *dir, GOptionContext *context, int argc, char **argv, GCancellable *cancellable, GError **error) { g_autoptr(GFile) file = NULL; g_autoptr(GBytes) file_data = NULL; g_autofree char *data = NULL; gsize data_len; const char *filename; g_autoptr(FlatpakTransaction) transaction = NULL; if (argc < 2) return usage_error (context, _("Filename or uri must be specified"), error); if (argc > 2) return usage_error (context, _("Too many arguments"), error); filename = argv[1]; if (g_str_has_prefix (filename, "http:") || g_str_has_prefix (filename, "https:")) { file_data = download_uri (filename, error); if (file_data == NULL) { g_prefix_error (error, "Can't load uri %s: ", filename); return FALSE; } } else { file = g_file_new_for_commandline_arg (filename); if (!g_file_load_contents (file, cancellable, &data, &data_len, NULL, error)) return FALSE; file_data = g_bytes_new_take (g_steal_pointer (&data), data_len); } if (opt_noninteractive) transaction = flatpak_quiet_transaction_new (dir, error); else transaction = flatpak_cli_transaction_new (dir, opt_yes, TRUE, error); if (transaction == NULL) return FALSE; flatpak_transaction_set_no_pull (transaction, opt_no_pull); flatpak_transaction_set_no_deploy (transaction, opt_no_deploy); flatpak_transaction_set_disable_static_deltas (transaction, opt_no_static_deltas); flatpak_transaction_set_disable_dependencies (transaction, opt_no_deps); flatpak_transaction_set_disable_related (transaction, opt_no_related); flatpak_transaction_set_reinstall (transaction, opt_reinstall); flatpak_transaction_set_default_arch (transaction, opt_arch); if (!flatpak_transaction_add_install_flatpakref (transaction, file_data, error)) return FALSE; if (!flatpak_transaction_run (transaction, cancellable, error)) { if (g_error_matches (*error, FLATPAK_ERROR, FLATPAK_ERROR_ABORTED)) { g_clear_error (error); return TRUE; } return FALSE; } return TRUE; }