Debian Codegraph @

src/byzanzqueueinputstream.c

raw symbols SCIP
1 /* desktop session recorder
2 * Copyright (C) 2009 Benjamin Otte <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "byzanzqueueinputstream.h"
23
24 G_DEFINE_TYPE (ByzanzQueueInputStream, byzanz_queue_input_stream, G_TYPE_INPUT_STREAM)
25
26 static gboolean
27 byzanz_queue_input_stream_close_input (ByzanzQueueInputStream *stream,
28 GCancellable * cancellable,
29 GError ** error)
30 {
31 if (stream->input == NULL)
32 return TRUE;
33
34 if (!g_input_stream_close (stream->input, cancellable, error))
35 return FALSE;
36
37 g_object_unref (stream->input);
38 stream->input = NULL;
39 stream->input_bytes = 0;
40 return TRUE;
41 }
42
43 static void
44 byzanz_queue_input_stream_dispose (GObject *object)
45 {
46 ByzanzQueueInputStream *stream = BYZANZ_QUEUE_INPUT_STREAM (object);
47
48 if (g_atomic_int_dec_and_test (&stream->queue->shared_count)) {
49 stream->queue->input = NULL;
50 g_object_unref (stream->queue);
51 } else {
52 g_object_ref (stream);
53 return;
54 }
55
56 G_OBJECT_CLASS (byzanz_queue_input_stream_parent_class)->dispose (object);
57 }
58
59 static void
60 byzanz_queue_input_stream_finalize (GObject *object)
61 {
62 ByzanzQueueInputStream *stream = BYZANZ_QUEUE_INPUT_STREAM (object);
63
64 if (!byzanz_queue_input_stream_close_input (stream, NULL, NULL))
65 g_object_unref (stream->input);
66
67 G_OBJECT_CLASS (byzanz_queue_input_stream_parent_class)->finalize (object);
68 }
69
70 static gboolean
71 byzanz_queue_input_stream_wait (ByzanzQueueInputStream *stream,
72 GCancellable * cancellable,
73 GError ** error)
74 {
75 GPollFD fd;
76 guint n_fds;
77
78 /* FIXME: Use a file monitor here */
79
80 /* Do the same thing that the UNIX tail program does: sleep a second */
81 n_fds = 0;
82 if (cancellable)
83 {
84 g_cancellable_make_pollfd (cancellable, &fd);
85 n_fds++;
86 }
87
88 g_poll (&fd, n_fds, 1000);
89
90 return !g_cancellable_set_error_if_cancelled (cancellable, error);
91 }
92
93 static gboolean
94 byzanz_queue_input_stream_ensure_input (ByzanzQueueInputStream *stream,
95 GCancellable * cancellable,
96 GError ** error)
97 {
98 GFile *file;
99
100 if (stream->input_bytes >= BYZANZ_QUEUE_FILE_SIZE)
101 {
102 if (!byzanz_queue_input_stream_close_input (stream, cancellable, error))
103 return FALSE;
104 stream->input = NULL;
105 }
106
107 if (stream->input != NULL)
108 return TRUE;
109
110 g_async_queue_lock (stream->queue->files);
111 do {
112 file = g_async_queue_try_pop_unlocked (stream->queue->files);
113 if (file != NULL || stream->queue->output_closed)
114 break;
115
116 g_async_queue_unlock (stream->queue->files);
117 if (!byzanz_queue_input_stream_wait (stream, cancellable, error))
118 return FALSE;
119 g_async_queue_lock (stream->queue->files);
120
121 } while (TRUE);
122 g_async_queue_unlock (stream->queue->files);
123
124 if (file == NULL)
125 return TRUE;
126
127 stream->input = G_INPUT_STREAM (g_file_read (file, cancellable, error));
128 g_file_delete (file, NULL, NULL);
129 g_object_unref (file);
130
131 return stream->input != NULL;
132 }
133
134 static gssize
135 byzanz_queue_input_stream_read (GInputStream *input_stream,
136 void * buffer,
137 gsize count,
138 GCancellable *cancellable,
139 GError ** error)
140 {
141 ByzanzQueueInputStream *stream = BYZANZ_QUEUE_INPUT_STREAM (input_stream);
142 gssize result;
143
144 retry:
145 if (!byzanz_queue_input_stream_ensure_input (stream, cancellable, error))
146 return -1;
147
148 /* No more data to read from the queue */
149 if (stream->input == NULL)
150 return 0;
151
152 result = g_input_stream_read (stream->input, buffer, count, cancellable, error);
153 if (result == -1)
154 return -1;
155
156 /* no data in file. Let's wait for more. */
157 if (result == 0) {
158 if (!byzanz_queue_input_stream_wait (stream, cancellable, error))
159 return -1;
160 goto retry;
161 }
162
163 stream->input_bytes += result;
164 return result;
165 }
166
167 static gssize
168 byzanz_queue_input_stream_skip (GInputStream *input_stream,
169 gsize count,
170 GCancellable *cancellable,
171 GError ** error)
172 {
173 ByzanzQueueInputStream *stream = BYZANZ_QUEUE_INPUT_STREAM (input_stream);
174 gssize result;
175
176 retry:
177 if (!byzanz_queue_input_stream_ensure_input (stream, cancellable, error))
178 return -1;
179
180 /* No more data to read from the queue */
181 if (stream->input == NULL)
182 return 0;
183
184 result = g_input_stream_skip (stream->input, count, cancellable, error);
185 if (result == -1)
186 return -1;
187
188 /* no data in file. Let's wait for more. */
189 if (result == 0) {
190 if (!byzanz_queue_input_stream_wait (stream, cancellable, error))
191 return -1;
192 goto retry;
193 }
194
195 stream->input_bytes += result;
196 return result;
197 }
198
199 static gboolean
200 byzanz_queue_input_stream_close (GInputStream * input_stream,
201 GCancellable * cancellable,
202 GError ** error)
203 {
204 ByzanzQueueInputStream *stream = BYZANZ_QUEUE_INPUT_STREAM (input_stream);
205 GFile *file;
206
207 if (!byzanz_queue_input_stream_close_input (stream, cancellable, error))
208 return FALSE;
209
210 g_async_queue_lock (stream->queue->files);
211 stream->queue->input_closed = TRUE;
212 file = g_async_queue_try_pop_unlocked (stream->queue->files);
213 g_async_queue_unlock (stream->queue->files);
214
215 while (file) {
216 g_file_delete (file, NULL, NULL);
217 file = g_async_queue_try_pop (stream->queue->files);
218 }
219
220 return TRUE;
221 }
222
223 static void
224 byzanz_queue_input_stream_class_init (ByzanzQueueInputStreamClass *klass)
225 {
226 GObjectClass *object_class = G_OBJECT_CLASS (klass);
227 GInputStreamClass *input_stream_class = G_INPUT_STREAM_CLASS (klass);
228
229 object_class->dispose = byzanz_queue_input_stream_dispose;
230 object_class->finalize = byzanz_queue_input_stream_finalize;
231
232 input_stream_class->read_fn = byzanz_queue_input_stream_read;
233 input_stream_class->skip = byzanz_queue_input_stream_skip;
234 input_stream_class->close_fn = byzanz_queue_input_stream_close;
235 /* FIXME: implement async ops */
236 }
237
238 static void
239 byzanz_queue_input_stream_init (ByzanzQueueInputStream *queue_input_stream)
240 {
241 }
242
243 GInputStream *
244 byzanz_queue_input_stream_new (ByzanzQueue *queue)
245 {
246 ByzanzQueueInputStream *stream;
247
248 g_return_val_if_fail (BYZANZ_IS_QUEUE (queue), NULL);
249
250 stream = g_object_new (BYZANZ_TYPE_QUEUE_INPUT_STREAM, NULL);
251 stream->queue = queue;
252
253 return G_INPUT_STREAM (stream);
254 }
255