Use GIO so we can later move the operation into the background

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2016-01-20 04:12:10 +01:00
parent 241d302b09
commit d1c38a703c

View file

@ -5,7 +5,6 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
@ -21,6 +20,7 @@
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
#include <glib-unix.h> #include <glib-unix.h>
#include <gio/gunixinputstream.h>
#endif #endif
#include "bookmarks.h" #include "bookmarks.h"
@ -525,6 +525,7 @@ zathura_set_argv(zathura_t* zathura, char** argv)
zathura->global.arguments = argv; zathura->global.arguments = argv;
} }
#ifdef G_OS_UNIX
static gchar* static gchar*
prepare_document_open_from_stdin(const char* path) prepare_document_open_from_stdin(const char* path)
{ {
@ -547,41 +548,46 @@ prepare_document_open_from_stdin(const char* path)
return NULL; return NULL;
} }
GError* error = NULL; GInputStream* input_stream = g_unix_input_stream_new(infileno, false);
gchar* file = NULL; if (input_stream == NULL) {
gint handle = g_file_open_tmp("zathura.stdin.XXXXXX", &file, &error); girara_error("Can not read from file descriptor.");
if (handle == -1) { return NULL;
}
GFileIOStream* iostream = NULL;
GError* error = NULL;
GFile* tmpfile = g_file_new_tmp("zathura.stdin.XXXXXX", &iostream, &error);
if (tmpfile == NULL) {
if (error != NULL) { if (error != NULL) {
girara_error("Can not create temporary file: %s", error->message); girara_error("Can not create temporary file: %s", error->message);
g_error_free(error); g_error_free(error);
} }
g_object_unref(input_stream);
return NULL; return NULL;
} }
// read and dump to temporary file const ssize_t count = g_output_stream_splice(
char buffer[BUFSIZ]; g_io_stream_get_output_stream(G_IO_STREAM(iostream)), input_stream,
ssize_t count = 0; G_OUTPUT_STREAM_SPLICE_NONE, NULL, &error);
while ((count = read(infileno, buffer, BUFSIZ)) > 0) { g_object_unref(input_stream);
if (write(handle, buffer, count) != count) { g_object_unref(iostream);
girara_error("Can not write to temporary file: %s", file); if (count == -1) {
close(handle); if (error != NULL) {
g_unlink(file); girara_error("Can not write to temporary file: %s", error->message);
g_free(file); g_error_free(error);
return NULL;
} }
} g_file_delete(tmpfile, NULL, NULL);
g_object_unref(tmpfile);
close(handle);
if (count != 0) {
girara_error("Can not read from file descriptor.");
g_unlink(file);
g_free(file);
return NULL; return NULL;
} }
char* file = g_file_get_path(tmpfile);
g_object_unref(tmpfile);
return file; return file;
} }
#endif
static gchar* static gchar*
prepare_document_open_from_gfile(GFile* source) prepare_document_open_from_gfile(GFile* source)
@ -590,7 +596,7 @@ prepare_document_open_from_gfile(GFile* source)
GFileIOStream* iostream = NULL; GFileIOStream* iostream = NULL;
GError* error = NULL; GError* error = NULL;
GFile *tmpfile = g_file_new_tmp("zathura.gio.XXXXXX", &iostream, &error); GFile* tmpfile = g_file_new_tmp("zathura.gio.XXXXXX", &iostream, &error);
if (tmpfile == NULL) { if (tmpfile == NULL) {
if (error != NULL) { if (error != NULL) {
girara_error("Can not create temporary file: %s", error->message); girara_error("Can not create temporary file: %s", error->message);
@ -599,7 +605,8 @@ prepare_document_open_from_gfile(GFile* source)
return NULL; return NULL;
} }
gboolean rc = g_file_copy(source, tmpfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error); gboolean rc = g_file_copy(source, tmpfile, G_FILE_COPY_OVERWRITE, NULL, NULL,
NULL, &error);
if (rc == FALSE) { if (rc == FALSE) {
if (error != NULL) { if (error != NULL) {
girara_error("Can not copy to temporary file: %s", error->message); girara_error("Can not copy to temporary file: %s", error->message);
@ -628,7 +635,9 @@ document_info_open(gpointer data)
char* file = NULL; char* file = NULL;
if (g_strcmp0(document_info->path, "-") == 0 || if (g_strcmp0(document_info->path, "-") == 0 ||
g_str_has_prefix(document_info->path, "/proc/self/fd/") == true) { g_str_has_prefix(document_info->path, "/proc/self/fd/") == true) {
#ifdef G_OS_UNIX
file = prepare_document_open_from_stdin(document_info->path); file = prepare_document_open_from_stdin(document_info->path);
#endif
if (file == NULL) { if (file == NULL) {
girara_notify(document_info->zathura->ui.session, GIRARA_ERROR, girara_notify(document_info->zathura->ui.session, GIRARA_ERROR,
_("Could not read file from stdin and write it to a temporary file.")); _("Could not read file from stdin and write it to a temporary file."));