static void end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error) { ParseState *state = user_data; GError *my_error = NULL; if (strcmp (element_name, "gresource") == 0) { g_free (state->prefix); state->prefix = NULL; } else if (strcmp (element_name, "file") == 0) { gchar *file, *real_file; gchar *key; FileData *data; char *tmp_file = NULL; char *tmp_file2 = NULL; file = state->string->str; key = file; if (state->alias) key = state->alias; if (state->prefix) key = g_build_path ("/", "/", state->prefix, key, NULL); else key = g_build_path ("/", "/", key, NULL); if (g_hash_table_lookup (state->table, key) != NULL) { g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, _("File %s appears multiple times in the resource"), key); return; } data = g_new0 (FileData, 1); if (sourcedirs != NULL) { real_file = find_file (file); if (real_file == NULL) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed to locate '%s' in any source directory"), file); return; } } else { gboolean exists; exists = g_file_test (file, G_FILE_TEST_EXISTS); if (!exists) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed to locate '%s' in current directory"), file); return; } real_file = g_strdup (file); } data->filename = g_strdup (real_file); if (!state->collect_data) goto done; if (state->preproc_options) { gchar **options; guint i; gboolean xml_stripblanks = FALSE; gboolean to_pixdata = FALSE; options = g_strsplit (state->preproc_options, ",", -1); for (i = 0; options[i]; i++) { if (!strcmp (options[i], "xml-stripblanks")) xml_stripblanks = TRUE; else if (!strcmp (options[i], "to-pixdata")) to_pixdata = TRUE; else { g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, _("Unknown processing option \"%s\""), options[i]); g_strfreev (options); goto cleanup; } } g_strfreev (options); if (xml_stripblanks && xmllint != NULL) { int fd; GSubprocess *proc; tmp_file = g_strdup ("resource-XXXXXXXX"); if ((fd = g_mkstemp (tmp_file)) == -1) { int errsv = errno; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Failed to create temp file: %s"), g_strerror (errsv)); g_free (tmp_file); tmp_file = NULL; goto cleanup; } close (fd); proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error, xmllint, "--nonet", "--noblanks", "--output", tmp_file, real_file, NULL); g_free (real_file); real_file = NULL; if (!proc) goto cleanup; if (!g_subprocess_wait_check (proc, NULL, error)) { g_object_unref (proc); goto cleanup; } g_object_unref (proc); real_file = g_strdup (tmp_file); } if (to_pixdata) { int fd; GSubprocess *proc; if (gdk_pixbuf_pixdata == NULL) { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "to-pixbuf preprocessing requested but GDK_PIXBUF_PIXDATA " "not set and gdk-pixbuf-pixdata not found in path"); goto cleanup; } tmp_file2 = g_strdup ("resource-XXXXXXXX"); if ((fd = g_mkstemp (tmp_file2)) == -1) { int errsv = errno; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Failed to create temp file: %s"), g_strerror (errsv)); g_free (tmp_file2); tmp_file2 = NULL; goto cleanup; } close (fd); proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error, gdk_pixbuf_pixdata, real_file, tmp_file2, NULL); g_free (real_file); real_file = NULL; if (!g_subprocess_wait_check (proc, NULL, error)) { g_object_unref (proc); goto cleanup; } g_object_unref (proc); real_file = g_strdup (tmp_file2); } } if (!g_file_get_contents (real_file, &data->content, &data->size, &my_error)) { g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, _("Error reading file %s: %s"), real_file, my_error->message); g_clear_error (&my_error); goto cleanup; } /* Include zero termination in content_size for uncompressed files (but not in size) */ data->content_size = data->size + 1; if (state->compressed) { GOutputStream *out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free); GZlibCompressor *compressor = g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB, 9); GOutputStream *out2 = g_converter_output_stream_new (out, G_CONVERTER (compressor)); if (!g_output_stream_write_all (out2, data->content, data->size, NULL, NULL, NULL) || !g_output_stream_close (out2, NULL, NULL)) { g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, _("Error compressing file %s"), real_file); goto cleanup; } g_free (data->content); data->content_size = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (out)); data->content = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out)); g_object_unref (compressor); g_object_unref (out); g_object_unref (out2); data->flags |= G_RESOURCE_FLAGS_COMPRESSED; } done: g_hash_table_insert (state->table, key, data); cleanup: /* Cleanup */ g_free (state->alias); state->alias = NULL; g_string_free (state->string, TRUE); state->string = NULL; g_free (state->preproc_options); state->preproc_options = NULL; g_free (real_file); if (tmp_file) { unlink (tmp_file); g_free (tmp_file); } if (tmp_file2) { unlink (tmp_file2); g_free (tmp_file2); } } }
static gboolean archive_spawnv (GFile *dir, char **output, GError **error, const gchar * const *argv) { g_autoptr(GSubprocessLauncher) launcher = NULL; g_autoptr(GSubprocess) subp = NULL; GInputStream *in; g_autoptr(GOutputStream) out = NULL; g_autoptr(GMainLoop) loop = NULL; SpawnData data = {0}; g_autofree gchar *commandline = NULL; launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE); if (output) g_subprocess_launcher_set_flags (launcher, G_SUBPROCESS_FLAGS_STDOUT_PIPE); if (dir) { g_autofree char *path = g_file_get_path (dir); g_subprocess_launcher_set_cwd (launcher, path); } commandline = g_strjoinv (" ", (gchar **) argv); g_debug ("Running '%s'", commandline); subp = g_subprocess_launcher_spawnv (launcher, argv, error); if (subp == NULL) return FALSE; loop = g_main_loop_new (NULL, FALSE); data.loop = loop; data.refs = 1; if (output) { data.refs++; in = g_subprocess_get_stdout_pipe (subp); out = g_memory_output_stream_new_resizable (); g_output_stream_splice_async (out, in, G_OUTPUT_STREAM_SPLICE_NONE, 0, NULL, spawn_output_spliced_cb, &data); } g_subprocess_wait_async (subp, NULL, spawn_exit_cb, &data); g_main_loop_run (loop); if (data.error) { g_propagate_error (error, data.error); g_clear_error (&data.splice_error); return FALSE; } if (out) { if (data.splice_error) { g_propagate_error (error, data.splice_error); return FALSE; } /* Null terminate */ g_output_stream_write (out, "\0", 1, NULL, NULL); g_output_stream_close (out, NULL, NULL); *output = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out)); } return TRUE; }