[virt-tools-list] [PATCH 1/3] Change install script output (tool)
Daniel P. Berrange
berrange at redhat.com
Thu Aug 9 10:01:13 UTC 2012
This patch is labelled 1 of 3, but the other 2 patches don't appear
on the list. Something went missing, or wrong subject line ?
On Thu, Aug 02, 2012 at 06:39:08PM +0200, Fabiano Fidêncio wrote:
> +void osinfo_install_script_set_output_prefix(OsinfoInstallScript *script,
> + const gchar *prefix)
> +{
> + GValue value = G_VALUE_INIT;
> + g_value_init(&value, G_TYPE_STRING);
> + g_value_set_string(&value, prefix);
> +
> + osinfo_os_set_property(G_OBJECT (script),
> + PROP_OUTPUT_PREFIX,
> + &value,
> + NULL);
Eww, no don't call this internal helper API - just use the regular
GOBject APIs for this, avoiding the GValue horror.
g_object_set(script, PROP_OUTPUT_PREFIX, prefix, NULL);
> @@ -308,6 +359,24 @@ static void osinfo_install_script_generate_free(struct OsinfoInstallScriptGenera
> g_free(data);
> }
>
> +struct OsinfoInstallScriptGenerateOutput {
> + GSimpleAsyncResult *res;
> + GCancellable *cancellable;
> + GError *error;
> + GFile *file;
> + GFileOutputStream *stream;
> + gchar *output;
> + gssize output_len;
> + gssize output_pos;
> +};
> +
> +static void osinfo_install_script_generate_output_free(struct OsinfoInstallScriptGenerateOutput *data)
> +{
> + g_object_unref(data->stream);
> + g_object_unref(data->res);
> + g_free(data);
> +}
> +
>
> static xsltStylesheetPtr osinfo_install_script_load_template(const gchar *uri,
> const gchar *template,
> @@ -621,9 +690,9 @@ void osinfo_install_script_generate_async(OsinfoInstallScript *script,
> }
> }
>
> -gchar *osinfo_install_script_generate_finish(OsinfoInstallScript *script,
> - GAsyncResult *res,
> - GError **error)
> +static gpointer osinfo_install_script_generate_finish_common(OsinfoInstallScript *script,
> + GAsyncResult *res,
> + GError **error)
> {
> GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT(res);
>
> @@ -635,13 +704,61 @@ gchar *osinfo_install_script_generate_finish(OsinfoInstallScript *script,
> return g_simple_async_result_get_op_res_gpointer(simple);
> }
>
> +gchar *osinfo_install_script_generate_finish(OsinfoInstallScript *script,
> + GAsyncResult *res,
> + GError **error)
> +{
> + return osinfo_install_script_generate_finish_common(script,
> + res,
> + error);
> +}
> +
> +GFile *osinfo_install_script_generate_output_finish(OsinfoInstallScript *script,
> + GAsyncResult *res,
> + GError **error)
> +{
> + return osinfo_install_script_generate_finish_common(script,
> + res,
> + error);
> +}
>
> struct OsinfoInstallScriptGenerateSync {
> GMainLoop *loop;
> GError *error;
> gchar *output;
> + GFile *file;
> };
>
> +static void osinfo_install_script_generate_output_done(GObject *src,
> + GAsyncResult *res,
> + gpointer user_data)
> +{
> + struct OsinfoInstallScriptGenerateSync *data = user_data;
> +
> + data->file =
> + osinfo_install_script_generate_output_finish(OSINFO_INSTALL_SCRIPT(src),
> + res,
> + &data->error);
> + g_main_loop_quit(data->loop);
> +}
> +
> +static void osinfo_install_script_generate_output_close_file(GObject *src,
> + GAsyncResult *res,
> + gpointer user_data)
> +{
> + struct OsinfoInstallScriptGenerateOutput *data = user_data;
> +
> + g_output_stream_close_finish(G_OUTPUT_STREAM(src),
> + res,
> + &data->error);
> +
> + g_simple_async_result_set_op_res_gpointer(data->res,
> + data->file, NULL);
> + g_simple_async_result_complete_in_idle(data->res);
> +
> + osinfo_install_script_generate_output_free(data);
> +}
> +
> static void osinfo_install_script_generate_done(GObject *src,
> GAsyncResult *res,
> gpointer user_data)
> @@ -655,7 +772,6 @@ static void osinfo_install_script_generate_done(GObject *src,
> g_main_loop_quit(data->loop);
> }
>
> -
> gchar *osinfo_install_script_generate(OsinfoInstallScript *script,
> OsinfoOs *os,
> OsinfoInstallConfig *config,
> @@ -665,7 +781,7 @@ gchar *osinfo_install_script_generate(OsinfoInstallScript *script,
> GMainLoop *loop = g_main_loop_new(g_main_context_get_thread_default(),
> TRUE);
> struct OsinfoInstallScriptGenerateSync data = {
> - loop, NULL, NULL
> + loop, NULL, NULL, NULL
> };
>
> osinfo_install_script_generate_async(script,
> @@ -686,6 +802,124 @@ gchar *osinfo_install_script_generate(OsinfoInstallScript *script,
> return data.output;
> }
>
> +static void osinfo_install_script_generate_output_write_file(GObject *src,
> + GAsyncResult *res,
> + gpointer user_data)
> +{
> + struct OsinfoInstallScriptGenerateOutput *data = user_data;
> +
> + if (data->stream == NULL)
> + data->stream = g_file_replace_finish(G_FILE (src), res, &data->error);
> + else
> + data->output_pos += g_output_stream_write_finish(G_OUTPUT_STREAM(data->stream),
> + res,
> + &data->error);
> +
> + g_assert_no_error(data->error);
> +
> + if (data->output_pos < data->output_len) {
> + g_output_stream_write_async(G_OUTPUT_STREAM (data->stream),
> + data->output + data->output_pos,
> + data->output_len - data->output_pos,
> + G_PRIORITY_DEFAULT,
> + data->cancellable,
> + osinfo_install_script_generate_output_write_file,
> + data);
> +
> + } else {
> + g_output_stream_close_async(G_OUTPUT_STREAM (data->stream),
> + G_PRIORITY_DEFAULT,
> + data->cancellable,
> + osinfo_install_script_generate_output_close_file,
> + data);
> + }
> +}
> +
> +void osinfo_install_script_generate_output_async(OsinfoInstallScript *script,
> + OsinfoOs *os,
> + OsinfoInstallConfig *config,
> + GFile *output_dir,
> + GCancellable *cancellable,
> + GAsyncReadyCallback callback,
> + gpointer user_data)
> +{
> + const gchar *filename;
> + const gchar *prefix;
> + const gchar *output_filename;
Don't declare this one 'const'...
> + struct OsinfoInstallScriptGenerateOutput *data =
> + g_new0(struct OsinfoInstallScriptGenerateOutput, 1);
> +
> + struct OsinfoInstallScriptGenerateSync *data_sync = user_data;
> +
> + data->res = g_simple_async_result_new(G_OBJECT(script),
> + callback,
> + user_data,
> + osinfo_install_script_generate_output_async);
> +
> + data->cancellable = cancellable;
> + data->error = data_sync->error;
> + data->output = osinfo_install_script_generate(script,
> + os,
> + config,
> + cancellable,
> + &data->error);
> + data->output_pos = 0;
> + data->output_len = strlen(data->output);
> +
> + prefix =
> + osinfo_install_script_get_output_prefix(script);
> + filename =
> + osinfo_install_script_get_output_filename(script);
> +
> + if (prefix) {
> + output_filename = g_strdup_printf("%s-%s", prefix, filename);
> + data->file = g_file_get_child(output_dir, output_filename);
> + g_free((gchar *)output_filename);
...then remove this cast.
> + } else {
> + data->file = g_file_get_child(output_dir, filename);
> + }
> +
> + g_file_replace_async(data->file,
> + NULL,
> + TRUE,
> + G_FILE_CREATE_NONE,
> + G_PRIORITY_DEFAULT,
> + cancellable,
> + osinfo_install_script_generate_output_write_file,
> + data);
> +}
> +
> +const GFile *osinfo_install_script_generate_output(OsinfoInstallScript *script,
IIUC, the returned GFile * instance must be unref'd by the caller,
so declaring it 'const' is misleading/wrong.
> + OsinfoOs *os,
> + OsinfoInstallConfig *config,
> + GFile *output_dir,
> + GCancellable *cancellable,
> + GError **error)
> +{
> + GMainLoop *loop = g_main_loop_new(g_main_context_get_thread_default(),
> + TRUE);
> + struct OsinfoInstallScriptGenerateSync data = {
> + loop, NULL, NULL, NULL
> + };
> +
> + osinfo_install_script_generate_output_async(script,
> + os,
> + config,
> + output_dir,
> + cancellable,
> + osinfo_install_script_generate_output_done,
> + &data);
> +
> + if (g_main_loop_is_running(loop))
> + g_main_loop_run(loop);
> +
> + if (data.error)
> + g_propagate_error(error, data.error);
> +
> + g_main_loop_unref(loop);
> +
> + return data.file;
> +}
>
> /*
> * Local variables:
Looks good apart from a few minor fixes
Daniel
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org -o- http://virt-manager.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
More information about the virt-tools-list
mailing list