static gboolean g_local_file_input_stream_close (GInputStream *stream, GCancellable *cancellable, GError **error) { GLocalFileInputStream *file; int res; file = G_LOCAL_FILE_INPUT_STREAM (stream); if (!file->priv->do_close) return TRUE; if (file->priv->fd == -1) return TRUE; while (1) { res = close (file->priv->fd); if (res == -1) { int errsv = errno; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error closing file: %s"), g_strerror (errsv)); } break; } return res != -1; }
static gboolean g_local_file_input_stream_seek (GFileInputStream *stream, goffset offset, GSeekType type, GCancellable *cancellable, GError **error) { GLocalFileInputStream *file; off_t pos; file = G_LOCAL_FILE_INPUT_STREAM (stream); pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type)); if (pos == (off_t)-1) { int errsv = errno; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error seeking in file: %s"), g_strerror (errsv)); return FALSE; } return TRUE; }
static void g_local_file_input_stream_finalize (GObject *object) { GLocalFileInputStream *file; file = G_LOCAL_FILE_INPUT_STREAM (object); G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object); }
static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream) { GLocalFileInputStream *file; off_t pos; file = G_LOCAL_FILE_INPUT_STREAM (stream); pos = lseek (file->priv->fd, 0, SEEK_CUR); if (pos == (off_t)-1 && errno == ESPIPE) return FALSE; return TRUE; }
static goffset g_local_file_input_stream_tell (GFileInputStream *stream) { GLocalFileInputStream *file; off_t pos; file = G_LOCAL_FILE_INPUT_STREAM (stream); pos = lseek (file->priv->fd, 0, SEEK_CUR); if (pos == (off_t)-1) return 0; return pos; }
GFileIOStream * _g_local_file_io_stream_new (GLocalFileOutputStream *output_stream) { GLocalFileIOStream *stream; int fd; stream = g_object_new (G_TYPE_LOCAL_FILE_IO_STREAM, NULL); stream->output_stream = g_object_ref (output_stream); _g_local_file_output_stream_set_do_close (output_stream, FALSE); fd = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (output_stream)); stream->input_stream = (GInputStream *)_g_local_file_input_stream_new (fd); _g_local_file_input_stream_set_do_close (G_LOCAL_FILE_INPUT_STREAM (stream->input_stream), FALSE); return G_FILE_IO_STREAM (stream); }
static GFileInfo * g_local_file_input_stream_query_info (GFileInputStream *stream, const char *attributes, GCancellable *cancellable, GError **error) { GLocalFileInputStream *file; file = G_LOCAL_FILE_INPUT_STREAM (stream); if (g_cancellable_set_error_if_cancelled (cancellable, error)) return NULL; return _g_local_file_info_get_from_fd (file->priv->fd, attributes, error); }
static gssize g_local_file_input_stream_skip (GInputStream *stream, gsize count, GCancellable *cancellable, GError **error) { off_t res, start; GLocalFileInputStream *file; file = G_LOCAL_FILE_INPUT_STREAM (stream); if (g_cancellable_set_error_if_cancelled (cancellable, error)) return -1; start = lseek (file->priv->fd, 0, SEEK_CUR); if (start == -1) { int errsv = errno; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error seeking in file: %s"), g_strerror (errsv)); return -1; } res = lseek (file->priv->fd, count, SEEK_CUR); if (res == -1) { int errsv = errno; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error seeking in file: %s"), g_strerror (errsv)); return -1; } return res - start; }
static gssize g_local_file_input_stream_read (GInputStream *stream, void *buffer, gsize count, GCancellable *cancellable, GError **error) { GLocalFileInputStream *file; gssize res; file = G_LOCAL_FILE_INPUT_STREAM (stream); res = -1; while (1) { if (g_cancellable_set_error_if_cancelled (cancellable, error)) break; res = read (file->priv->fd, buffer, count); if (res == -1) { int errsv = errno; if (errsv == EINTR) continue; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error reading from file: %s"), g_strerror (errsv)); } break; } return res; }