Local file implementation of GFileIOStream and ops
[platform/upstream/glib.git] / gio / glocalfileiostream.c
1 /* GIO - GLib Input, IO and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <glib/gstdio.h>
27 #include "glibintl.h"
28 #include "gioerror.h"
29 #include "gcancellable.h"
30 #include "glocalfileiostream.h"
31 #include "glocalfileinputstream.h"
32 #include "glocalfileinfo.h"
33
34 #include "gioalias.h"
35
36 #define g_local_file_io_stream_get_type _g_local_file_io_stream_get_type
37 G_DEFINE_TYPE (GLocalFileIOStream, g_local_file_io_stream, G_TYPE_FILE_IO_STREAM);
38
39 static void
40 g_local_file_io_stream_finalize (GObject *object)
41 {
42   GLocalFileIOStream *file;
43
44   file = G_LOCAL_FILE_IO_STREAM (object);
45
46   g_object_unref (file->input_stream);
47   g_object_unref (file->output_stream);
48
49   G_OBJECT_CLASS (g_local_file_io_stream_parent_class)->finalize (object);
50 }
51
52 GFileIOStream *
53 _g_local_file_io_stream_new (GLocalFileOutputStream *output_stream)
54 {
55   GLocalFileIOStream *stream;
56   int fd;
57
58   stream = g_object_new (G_TYPE_LOCAL_FILE_IO_STREAM, NULL);
59   stream->output_stream = g_object_ref (output_stream);
60   _g_local_file_output_stream_set_do_close (output_stream, FALSE);
61   fd = _g_local_file_output_stream_get_fd (output_stream);
62   stream->input_stream = (GInputStream *)_g_local_file_input_stream_new (fd);
63   _g_local_file_input_stream_set_do_close (G_LOCAL_FILE_INPUT_STREAM (stream->input_stream),
64                                            FALSE);
65
66   return G_FILE_IO_STREAM (stream);
67 }
68
69 static GInputStream *
70 g_local_file_io_stream_get_input_stream (GIOStream *stream)
71 {
72   return G_LOCAL_FILE_IO_STREAM (stream)->input_stream;
73 }
74
75 static GOutputStream *
76 g_local_file_io_stream_get_output_stream (GIOStream *stream)
77 {
78   return G_LOCAL_FILE_IO_STREAM (stream)->output_stream;
79 }
80
81
82 static gboolean
83 g_local_file_io_stream_close (GIOStream  *stream,
84                               GCancellable   *cancellable,
85                               GError        **error)
86 {
87   GLocalFileIOStream *file = G_LOCAL_FILE_IO_STREAM (stream);
88
89   /* There are shortcutted and can't fail */
90   g_output_stream_close (file->output_stream, cancellable, NULL);
91   g_input_stream_close (file->input_stream, cancellable, NULL);
92
93   return
94     _g_local_file_output_stream_really_close (G_LOCAL_FILE_OUTPUT_STREAM (file->output_stream),
95                                               cancellable, error);
96 }
97
98 static void
99 g_local_file_io_stream_class_init (GLocalFileIOStreamClass *klass)
100 {
101   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
102   GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
103
104   gobject_class->finalize = g_local_file_io_stream_finalize;
105
106   stream_class->get_input_stream = g_local_file_io_stream_get_input_stream;
107   stream_class->get_output_stream = g_local_file_io_stream_get_output_stream;
108   stream_class->close_fn = g_local_file_io_stream_close;
109 }
110
111 static void
112 g_local_file_io_stream_init (GLocalFileIOStream *stream)
113 {
114 }