Ability to create a seekable input stream from a file descriptor
I'm using g_unix_fd_list_get()
in my daemon to get a DBus-passed file-descriptor.
I want to stream data from it, so I use g_unix_input_stream_new(fd)
-- but this isn't seekable and so I have to dump it to disk before I can open it as a GFileInputStream
as I need to rewind to do things like calculate the file checksum.
This seems like an artificial limitation, and dumping a 2GB file to tmpfs and then re-opening it isn't feasible on the low-end (4GB RAM) hardware I have here.
Things I've tried:
-
_g_local_file_input_stream_new(fd)
-- not going to work as not exported as API -- but I suppose we could export it. -
g_object_new(g_type_from_name("GLocalFileInputStream"), "fd", fd, NULL)
-- eww, and fd isn't a property onGLocalFileInputStream
- Copying the
GLocalFileInputStream
gseekable code intogunixinputstream.c
-- works, but maybe a layering violation as seeking only works on files, not pipes, sockets or fifos. - Making a new per-project
WhateverUnixStreamableInputStream
deriving fromGUnixInputStream
and copying the seekable code fromGLocalFileInputStream
-- this works, but seems like a giant hack I shouldn't have to do - Specifying the file descriptor as a URI -- e.g.
uri = g_strdup_printf("fd://%i", fd);
,file = g_file_new_for_uri(uri);
thenstream = g_file_read(file, NULL, error);
-- but that's not a valid URI apparently -- and also seems a bit magic and weird. - Adding something like
g_unix_fd_list_get_input_stream(GUnixFDList *list,gint index_,GError **error);
that callsg_unix_fd_list_get()
and then the internal function_g_local_file_input_stream_new()
-- untested, but should work.
Option 1 is also possible, exporting g_local_file_input_stream_new()
(and possibly g_local_file_input_stream_set_do_close()
too) as API.
Option 3 might be best; g_seekable_can_seek()
can be used to see if the seek would work on the file descriptor.
Option 6 seems easiest.
Ideas welcome (and I'm happy to write the merge request),
Richard.