1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
34 #include <glib/gstdio.h>
35 #include "gcancellable.h"
37 #include "glocalfileinputstream.h"
38 #include "glocalfileinfo.h"
42 #include "glib-unix.h"
43 #include "gfiledescriptorbased.h"
53 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface
*iface
);
56 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
58 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream
, g_local_file_input_stream
, G_TYPE_FILE_INPUT_STREAM
,
59 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED
,
60 g_file_descriptor_based_iface_init
)
63 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream
, g_local_file_input_stream
, G_TYPE_FILE_INPUT_STREAM
,);
66 struct _GLocalFileInputStreamPrivate
{
71 static gssize
g_local_file_input_stream_read (GInputStream
*stream
,
74 GCancellable
*cancellable
,
76 static gssize
g_local_file_input_stream_skip (GInputStream
*stream
,
78 GCancellable
*cancellable
,
80 static gboolean
g_local_file_input_stream_close (GInputStream
*stream
,
81 GCancellable
*cancellable
,
83 static goffset
g_local_file_input_stream_tell (GFileInputStream
*stream
);
84 static gboolean
g_local_file_input_stream_can_seek (GFileInputStream
*stream
);
85 static gboolean
g_local_file_input_stream_seek (GFileInputStream
*stream
,
88 GCancellable
*cancellable
,
90 static GFileInfo
*g_local_file_input_stream_query_info (GFileInputStream
*stream
,
91 const char *attributes
,
92 GCancellable
*cancellable
,
95 static int g_local_file_input_stream_get_fd (GFileDescriptorBased
*stream
);
99 g_local_file_input_stream_finalize (GObject
*object
)
101 G_OBJECT_CLASS (g_local_file_input_stream_parent_class
)->finalize (object
);
105 _g_local_file_input_stream_set_do_close (GLocalFileInputStream
*in
,
108 in
->priv
->do_close
= do_close
;
112 g_local_file_input_stream_class_init (GLocalFileInputStreamClass
*klass
)
114 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
115 GInputStreamClass
*stream_class
= G_INPUT_STREAM_CLASS (klass
);
116 GFileInputStreamClass
*file_stream_class
= G_FILE_INPUT_STREAM_CLASS (klass
);
118 g_type_class_add_private (klass
, sizeof (GLocalFileInputStreamPrivate
));
120 gobject_class
->finalize
= g_local_file_input_stream_finalize
;
122 stream_class
->read_fn
= g_local_file_input_stream_read
;
123 stream_class
->skip
= g_local_file_input_stream_skip
;
124 stream_class
->close_fn
= g_local_file_input_stream_close
;
125 file_stream_class
->tell
= g_local_file_input_stream_tell
;
126 file_stream_class
->can_seek
= g_local_file_input_stream_can_seek
;
127 file_stream_class
->seek
= g_local_file_input_stream_seek
;
128 file_stream_class
->query_info
= g_local_file_input_stream_query_info
;
133 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface
*iface
)
135 iface
->get_fd
= g_local_file_input_stream_get_fd
;
140 g_local_file_input_stream_init (GLocalFileInputStream
*info
)
142 info
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (info
,
143 G_TYPE_LOCAL_FILE_INPUT_STREAM
,
144 GLocalFileInputStreamPrivate
);
145 info
->priv
->do_close
= TRUE
;
149 _g_local_file_input_stream_new (int fd
)
151 GLocalFileInputStream
*stream
;
153 stream
= g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM
, NULL
);
154 stream
->priv
->fd
= fd
;
156 return G_FILE_INPUT_STREAM (stream
);
160 g_local_file_input_stream_read (GInputStream
*stream
,
163 GCancellable
*cancellable
,
166 GLocalFileInputStream
*file
;
169 file
= G_LOCAL_FILE_INPUT_STREAM (stream
);
174 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
176 res
= read (file
->priv
->fd
, buffer
, count
);
184 g_set_error (error
, G_IO_ERROR
,
185 g_io_error_from_errno (errsv
),
186 _("Error reading from file: %s"),
197 g_local_file_input_stream_skip (GInputStream
*stream
,
199 GCancellable
*cancellable
,
203 GLocalFileInputStream
*file
;
205 file
= G_LOCAL_FILE_INPUT_STREAM (stream
);
207 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
210 start
= lseek (file
->priv
->fd
, 0, SEEK_CUR
);
215 g_set_error (error
, G_IO_ERROR
,
216 g_io_error_from_errno (errsv
),
217 _("Error seeking in file: %s"),
222 res
= lseek (file
->priv
->fd
, count
, SEEK_CUR
);
227 g_set_error (error
, G_IO_ERROR
,
228 g_io_error_from_errno (errsv
),
229 _("Error seeking in file: %s"),
238 g_local_file_input_stream_close (GInputStream
*stream
,
239 GCancellable
*cancellable
,
242 GLocalFileInputStream
*file
;
244 file
= G_LOCAL_FILE_INPUT_STREAM (stream
);
246 if (!file
->priv
->do_close
)
249 if (file
->priv
->fd
== -1)
252 if (!g_close (file
->priv
->fd
, NULL
))
256 g_set_error (error
, G_IO_ERROR
,
257 g_io_error_from_errno (errsv
),
258 _("Error closing file: %s"),
268 g_local_file_input_stream_tell (GFileInputStream
*stream
)
270 GLocalFileInputStream
*file
;
273 file
= G_LOCAL_FILE_INPUT_STREAM (stream
);
275 pos
= lseek (file
->priv
->fd
, 0, SEEK_CUR
);
277 if (pos
== (off_t
)-1)
284 g_local_file_input_stream_can_seek (GFileInputStream
*stream
)
286 GLocalFileInputStream
*file
;
289 file
= G_LOCAL_FILE_INPUT_STREAM (stream
);
291 pos
= lseek (file
->priv
->fd
, 0, SEEK_CUR
);
293 if (pos
== (off_t
)-1 && errno
== ESPIPE
)
300 seek_type_to_lseek (GSeekType type
)
317 g_local_file_input_stream_seek (GFileInputStream
*stream
,
320 GCancellable
*cancellable
,
323 GLocalFileInputStream
*file
;
326 file
= G_LOCAL_FILE_INPUT_STREAM (stream
);
328 pos
= lseek (file
->priv
->fd
, offset
, seek_type_to_lseek (type
));
330 if (pos
== (off_t
)-1)
334 g_set_error (error
, G_IO_ERROR
,
335 g_io_error_from_errno (errsv
),
336 _("Error seeking in file: %s"),
345 g_local_file_input_stream_query_info (GFileInputStream
*stream
,
346 const char *attributes
,
347 GCancellable
*cancellable
,
350 GLocalFileInputStream
*file
;
352 file
= G_LOCAL_FILE_INPUT_STREAM (stream
);
354 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
357 return _g_local_file_info_get_from_fd (file
->priv
->fd
,
364 g_local_file_input_stream_get_fd (GFileDescriptorBased
*fd_based
)
366 GLocalFileInputStream
*stream
= G_LOCAL_FILE_INPUT_STREAM (fd_based
);
367 return stream
->priv
->fd
;