Esempio n. 1
0
static GsfInput *
make_local_copy (GFile *file, GInputStream *stream)
{
    GsfOutput *out;
    GsfInput  *copy;
    GFileInfo *info;

    out = gsf_output_memory_new ();

    while (1) {
        guint8 buf[4096];
        gssize nread;

        nread = g_input_stream_read (stream, buf, sizeof(buf), NULL, NULL);

        if (nread > 0) {
            if (!gsf_output_write (out, nread, buf)) {
                copy = NULL;
                goto cleanup_and_exit;
            }
        }
        else if (nread == 0)
            break;
        else {
            copy = NULL;
            goto cleanup_and_exit;
        }
    }

    copy = gsf_input_memory_new_clone
           (gsf_output_memory_get_bytes (GSF_OUTPUT_MEMORY (out)),
            gsf_output_size (out));

    if (copy != NULL) {
        info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, NULL);
        if (info) {
            gsf_input_set_name (GSF_INPUT (copy), g_file_info_get_name (info));
            g_object_unref (info);
        }
    }

cleanup_and_exit:

    gsf_output_close (out);
    g_object_unref (out);

    g_input_stream_close (stream, NULL, NULL);
    g_object_unref (stream);

    set_name_from_file (copy, file);

    return copy;
}
Esempio n. 2
0
/**
 * gsf_input_gio_new:
 * @file:
 * @err: (allow-none): place to store a #GError if anything goes wrong
 *
 * Returns: A new #GsfInputGio or %NULL
 */
GsfInput *
gsf_input_gio_new (GFile *file, GError **err)
{
    GsfInputGio *input;
    GInputStream *stream;
    gsf_off_t filesize;

    g_return_val_if_fail (file != NULL, NULL);

    stream = (GInputStream *)g_file_read (file, NULL, err);
    if (stream == NULL)
        return NULL;

    if (1) {
        /* see https://bugzilla.gnome.org/show_bug.cgi?id=724970 */
        return make_local_copy (file, stream);
    }

    if (!can_seek (stream))
        return make_local_copy (file, stream);

    {
        GFileInfo *info =
            g_file_query_info (file,
                               G_FILE_ATTRIBUTE_STANDARD_SIZE,
                               0, NULL, NULL);
        if (!info)
            return make_local_copy (file, stream);
        filesize = g_file_info_get_size (info);
        g_object_unref (info);
    }

    input = g_object_new (GSF_INPUT_GIO_TYPE, NULL);

    gsf_input_set_size (GSF_INPUT (input), filesize);

    g_object_ref (file);

    input->stream = stream;
    input->file = file;
    input->buf  = NULL;
    input->buf_size = 0;

    set_name_from_file (GSF_INPUT (input), file);
    return GSF_INPUT (input);
}
Esempio n. 3
0
/**
 * gsf_input_gio_new:
 * @file:
 * @err: optionally NULL.
 *
 * Returns: A new #GsfInputGio or NULL
 */
GsfInput *
gsf_input_gio_new (GFile *file, GError **err)
{
	GsfInputGio *input;
	GInputStream *stream;
	gsf_off_t filesize;

	g_return_val_if_fail (file != NULL, NULL);

	stream = (GInputStream *)g_file_read (file, NULL, err);
	if (stream == NULL)
		return NULL;

	if (!can_seek (stream))
		return make_local_copy (file, stream);

	{
		GFileInfo *info =
			g_file_query_info (file,
					   G_FILE_ATTRIBUTE_STANDARD_SIZE,
					   0, NULL, NULL);
		if (!info)
			return make_local_copy (file, stream);
		filesize = g_file_info_get_size (info);
		g_object_unref (info);
	}

	input = g_object_new (GSF_INPUT_GIO_TYPE, NULL);
	if (G_UNLIKELY (NULL == input)) {
		g_input_stream_close (stream, NULL, NULL);
		g_object_unref (stream);
		return NULL;
	}

	gsf_input_set_size (GSF_INPUT (input), filesize);

	g_object_ref (file);

	input->stream = stream;
	input->file = file;
	input->buf  = NULL;
	input->buf_size = 0;

	set_name_from_file (GSF_INPUT (input), file);
	return GSF_INPUT (input);
}