Debian Codegraph @

src/libsysprof/sysprof-document-file.c

raw
1 /* sysprof-document-file.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include "sysprof-document-file-chunk.h"
24 #include "sysprof-document-file-private.h"
25 #include "sysprof-document-frame-private.h"
26
27 struct _SysprofDocumentFile
28 {
29 GObject parent_instance;
30 char *path;
31 GPtrArray *file_chunks;
32 guint compressed : 1;
33 };
34
35 enum {
36 PROP_0,
37 PROP_BYTES,
38 PROP_IS_COMPRESSED,
39 PROP_PATH,
40 PROP_SIZE,
41 N_PROPS
42 };
43
44 G_DEFINE_FINAL_TYPE (SysprofDocumentFile, sysprof_document_file, G_TYPE_OBJECT)
45
46 static GParamSpec *properties [N_PROPS];
47
48 static void
49 sysprof_document_file_finalize (GObject *object)
50 {
51 SysprofDocumentFile *self = (SysprofDocumentFile *)object;
52
53 g_clear_pointer (&self->path, g_free);
54 g_clear_pointer (&self->file_chunks, g_ptr_array_unref);
55
56 G_OBJECT_CLASS (sysprof_document_file_parent_class)->finalize (object);
57 }
58
59 static void
60 sysprof_document_file_get_property (GObject *object,
61 guint prop_id,
62 GValue *value,
63 GParamSpec *pspec)
64 {
65 SysprofDocumentFile *self = SYSPROF_DOCUMENT_FILE (object);
66
67 switch (prop_id)
68 {
69 case PROP_PATH:
70 g_value_set_string (value, sysprof_document_file_get_path (self));
71 break;
72
73 case PROP_BYTES:
74 g_value_take_boxed (value, sysprof_document_file_dup_bytes (self));
75 break;
76
77 case PROP_IS_COMPRESSED:
78 g_value_set_boolean (value, sysprof_document_file_is_compressed (self));
79 break;
80
81 case PROP_SIZE:
82 g_value_set_uint64 (value, sysprof_document_file_get_size (self));
83 break;
84
85 default:
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 }
88 }
89
90 static void
91 sysprof_document_file_class_init (SysprofDocumentFileClass *klass)
92 {
93 GObjectClass *object_class = G_OBJECT_CLASS (klass);
94
95 object_class->finalize = sysprof_document_file_finalize;
96 object_class->get_property = sysprof_document_file_get_property;
97
98 properties [PROP_PATH] =
99 g_param_spec_string ("path", NULL, NULL,
100 NULL,
101 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
102
103 properties [PROP_BYTES] =
104 g_param_spec_boxed ("bytes", NULL, NULL,
105 G_TYPE_BYTES,
106 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
107
108 properties [PROP_IS_COMPRESSED] =
109 g_param_spec_boolean ("is-compressed", NULL, NULL,
110 FALSE,
111 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
112
113 properties [PROP_SIZE] =
114 g_param_spec_uint64 ("size", NULL, NULL,
115 0, G_MAXUINT64, 0,
116 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
117
118 g_object_class_install_properties (object_class, N_PROPS, properties);
119 }
120
121 static void
122 sysprof_document_file_init (SysprofDocumentFile *self)
123 {
124 }
125
126 SysprofDocumentFile *
127 _sysprof_document_file_new (const char *path,
128 GPtrArray *file_chunks,
129 gboolean compressed)
130 {
131 SysprofDocumentFile *self;
132
133 g_return_val_if_fail (path != NULL, NULL);
134 g_return_val_if_fail (file_chunks != NULL, NULL);
135
136 self = g_object_new (SYSPROF_TYPE_DOCUMENT_FILE, NULL);
137 self->path = g_strdup (path);
138 self->file_chunks = file_chunks;
139 self->compressed = !!compressed;
140
141 return self;
142 }
143
144 const char *
145 sysprof_document_file_get_path (SysprofDocumentFile *self)
146 {
147 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FILE (self), NULL);
148
149 return self->path;
150 }
151
152 /**
153 * sysprof_document_file_dup_contents:
154 * @self: a #SysprofDocumentFile
155 *
156 * Creates a new #GBytes containing the contents of the file.
157 *
158 * Returns: (transfer full): a #GBytes
159 */
160 GBytes *
161 sysprof_document_file_dup_bytes (SysprofDocumentFile *self)
162 {
163 static const char zero = 0;
164 GArray *ar;
165 guint len;
166
167 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FILE (self), NULL);
168
169 ar = g_array_new (TRUE, FALSE, sizeof (char));
170
171 for (guint i = 0; i < self->file_chunks->len; i++)
172 {
173 SysprofDocumentFileChunk *file_chunk = g_ptr_array_index (self->file_chunks, i);
174 const guint8 *data = sysprof_document_file_chunk_get_data (file_chunk, &len);
175
176 g_array_append_vals (ar, data, len);
177 }
178
179 len = ar->len;
180
181 if (self->compressed)
182 {
183 guint8 *data = (guint8 *)g_array_free (ar, FALSE);
184 g_autoptr(GInputStream) input = g_memory_input_stream_new_from_data (data, len, g_free);
185 g_autoptr(GOutputStream) memory_output = g_memory_output_stream_new_resizable ();
186 g_autoptr(GZlibDecompressor) zlib = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP);
187 g_autoptr(GOutputStream) zlib_output = g_converter_output_stream_new (memory_output, G_CONVERTER (zlib));
188 g_autoptr(GBytes) bytes = NULL;
189
190 g_output_stream_splice (zlib_output,
191 input,
192 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
193 NULL, NULL);
194 g_output_stream_write (memory_output, &zero, 1, NULL, NULL);
195 g_output_stream_close (memory_output, NULL, NULL);
196 bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (memory_output));
197
198 g_assert (bytes != NULL);
199 g_assert (g_bytes_get_size (bytes) > 0);
200 g_assert (((char *)g_bytes_get_data (bytes, NULL))[g_bytes_get_size (bytes)-1] == 0);
201
202 return g_bytes_new_from_bytes (bytes, 0, g_bytes_get_size (bytes) - 1);
203 }
204
205 g_array_append_val (ar, zero);
206 g_assert (ar->data[len] == 0);
207
208 return g_bytes_new_take (g_array_free (ar, FALSE), len);
209 }
210
211 /**
212 * sysprof_document_file_read:
213 * @self: a #SysprofDocumentFile
214 *
215 * Creates a new input stream containing the contents of the file
216 * within the document.
217 *
218 * Returns: (transfer full): a #GInputstream
219 */
220 GInputStream *
221 sysprof_document_file_read (SysprofDocumentFile *self)
222 {
223 g_autoptr(GInputStream) input = NULL;
224
225 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FILE (self), NULL);
226
227 input = g_memory_input_stream_new ();
228
229 for (guint i = 0; i < self->file_chunks->len; i++)
230 {
231 g_autoptr(GBytes) bytes = NULL;
232 SysprofDocumentFileChunk *file_chunk;
233 const guint8 *data;
234 guint len;
235
236 file_chunk = g_ptr_array_index (self->file_chunks, i);
237 data = sysprof_document_file_chunk_get_data (file_chunk, &len);
238
239 bytes = g_bytes_new_with_free_func (data,
240 len,
241 (GDestroyNotify)g_mapped_file_unref,
242 g_mapped_file_ref (SYSPROF_DOCUMENT_FRAME (file_chunk)->mapped_file));
243
244 g_memory_input_stream_add_bytes (G_MEMORY_INPUT_STREAM (input), bytes);
245 }
246
247 if (self->compressed)
248 {
249 g_autoptr(GZlibDecompressor) zlib = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP);
250
251 return g_converter_input_stream_new (input, G_CONVERTER (zlib));
252 }
253
254 return g_steal_pointer (&input);
255 }
256
257 gsize
258 sysprof_document_file_get_size (SysprofDocumentFile *self)
259 {
260 gsize size = 0;
261
262 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FILE (self), 0);
263
264 for (guint i = 0; i < self->file_chunks->len; i++)
265 {
266 SysprofDocumentFileChunk *file_chunk = g_ptr_array_index (self->file_chunks, i);
267
268 size += sysprof_document_file_chunk_get_size (file_chunk);
269 }
270
271 return size;
272 }
273
274 /**
275 * sysprof_document_file_is_compressed:
276 * @self: a #SysprofDocumentFile
277 *
278 * Checks if the embedded file is compressed. If so, %TRUE is returned.
279 *
280 * Note that files are transparently decompressed when opening streams.
281 *
282 * Returns: %TRUE if the embedded file was compressed
283 */
284 gboolean
285 sysprof_document_file_is_compressed (SysprofDocumentFile *self)
286 {
287 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FILE (self), FALSE);
288
289 return self->compressed;
290 }