Debian Codegraph @

src/byzanzqueueoutputstream.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 "byzanzqueueoutputstream.h"
23
24 #include <unistd.h>
25
26 G_DEFINE_TYPE (ByzanzQueueOutputStream, byzanz_queue_output_stream, G_TYPE_OUTPUT_STREAM)
27
28 static void
29 byzanz_queue_output_stream_dispose (GObject *object)
30 {
31 ByzanzQueueOutputStream *stream = BYZANZ_QUEUE_OUTPUT_STREAM (object);
32
33 if (g_atomic_int_dec_and_test (&stream->queue->shared_count)) {
34 stream->queue->output = NULL;
35 g_object_unref (stream->queue);
36 } else {
37 g_object_ref (stream);
38 return;
39 }
40
41 G_OBJECT_CLASS (byzanz_queue_output_stream_parent_class)->dispose (object);
42 }
43
44 static void
45 byzanz_queue_output_stream_finalize (GObject *object)
46 {
47 ByzanzQueueOutputStream *stream = BYZANZ_QUEUE_OUTPUT_STREAM (object);
48
49 if (stream->output)
50 g_object_unref (stream->output);
51
52 G_OBJECT_CLASS (byzanz_queue_output_stream_parent_class)->finalize (object);
53 }
54
55 static gboolean
56 byzanz_queue_output_stream_ensure_output (ByzanzQueueOutputStream *stream,
57 GCancellable * cancellable,
58 GError ** error)
59 {
60 GFile *file;
61 int fd;
62 char *filename;
63
64 if (stream->output_bytes == 0 && stream->output)
65 {
66 if (!g_output_stream_close (stream->output, cancellable, error))
67 return FALSE;
68 g_object_unref (stream->output);
69 stream->output = NULL;
70 }
71
72 if (stream->output != NULL)
73 return TRUE;
74
75 g_async_queue_lock (stream->queue->files);
76
77 if (stream->queue->input_closed) {
78 g_async_queue_unlock (stream->queue->files);
79 return TRUE;
80 }
81
82 fd = g_file_open_tmp ("byzanzcacheXXXXXX", &filename, error);
83 if (fd < 0) {
84 file = NULL;
85 } else {
86 close (fd);
87 file = g_file_new_for_path (filename);
88 g_free (filename);
89 g_object_ref (file);
90 g_async_queue_push_unlocked (stream->queue->files, file);
91 }
92
93 g_async_queue_unlock (stream->queue->files);
94
95 if (file == NULL)
96 return FALSE;
97
98 stream->output = G_OUTPUT_STREAM (g_file_append_to (file, G_FILE_CREATE_PRIVATE, cancellable, error));
99 g_object_unref (file);
100 if (stream->output == NULL)
101 return FALSE;
102
103 stream->output_bytes = BYZANZ_QUEUE_FILE_SIZE;
104 return TRUE;
105 }
106
107 static gssize
108 byzanz_queue_output_stream_write (GOutputStream *output_stream,
109 const void * buffer,
110 gsize count,
111 GCancellable * cancellable,
112 GError ** error)
113 {
114 ByzanzQueueOutputStream *stream = BYZANZ_QUEUE_OUTPUT_STREAM (output_stream);
115 gssize result;
116
117 if (!byzanz_queue_output_stream_ensure_output (stream, cancellable, error))
118 return -1;
119
120 /* will happen if input stream is closed, and there's no need to continue writing */
121 if (stream->output == NULL)
122 return count;
123
124 result = g_output_stream_write (stream->output, buffer,
125 MIN ((goffset) count, stream->output_bytes), cancellable, error);
126 if (result == -1)
127 return -1;
128
129 stream->output_bytes -= result;
130 return result;
131 }
132
133 static gboolean
134 byzanz_queue_output_stream_close (GOutputStream *output_stream,
135 GCancellable * cancellable,
136 GError ** error)
137 {
138 ByzanzQueueOutputStream *stream = BYZANZ_QUEUE_OUTPUT_STREAM (output_stream);
139
140 if (stream->output &&
141 !g_output_stream_close (stream->output, cancellable, error))
142 return FALSE;
143
144 g_async_queue_lock (stream->queue->files);
145 stream->queue->output_closed = TRUE;
146 g_async_queue_unlock (stream->queue->files);
147 return TRUE;
148 }
149
150 static void
151 byzanz_queue_output_stream_class_init (ByzanzQueueOutputStreamClass *klass)
152 {
153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
154 GOutputStreamClass *output_stream_class = G_OUTPUT_STREAM_CLASS (klass);
155
156 object_class->dispose = byzanz_queue_output_stream_dispose;
157 object_class->finalize = byzanz_queue_output_stream_finalize;
158
159 output_stream_class->write_fn = byzanz_queue_output_stream_write;
160 output_stream_class->close_fn = byzanz_queue_output_stream_close;
161 /* FIXME: implement async ops */
162 }
163
164 static void
165 byzanz_queue_output_stream_init (ByzanzQueueOutputStream *queue_output_stream)
166 {
167 }
168
169 GOutputStream *
170 byzanz_queue_output_stream_new (ByzanzQueue *queue)
171 {
172 ByzanzQueueOutputStream *stream;
173
174 g_return_val_if_fail (BYZANZ_IS_QUEUE (queue), NULL);
175
176 stream = g_object_new (BYZANZ_TYPE_QUEUE_OUTPUT_STREAM, NULL);
177 stream->queue = queue;
178
179 return G_OUTPUT_STREAM (stream);
180 }
181