From 9fe0ae53063631cf3d09cbdc0f9672937d78bdc8 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Tue, 9 May 2023 12:22:34 -0700 Subject: [PATCH] libsysprof-analyze: add type to represent an embedded file We have "chunks" within the capture file, this object will represent the collection of those chunks which is easier for applications to deal with if they want to view the contents. --- src/libsysprof-analyze/meson.build | 2 + src/libsysprof-analyze/sysprof-analyze.h | 1 + .../sysprof-document-file-private.h | 30 ++++ .../sysprof-document-file.c | 157 ++++++++++++++++++ .../sysprof-document-file.h | 39 +++++ 5 files changed, 229 insertions(+) create mode 100644 src/libsysprof-analyze/sysprof-document-file-private.h create mode 100644 src/libsysprof-analyze/sysprof-document-file.c create mode 100644 src/libsysprof-analyze/sysprof-document-file.h diff --git a/src/libsysprof-analyze/meson.build b/src/libsysprof-analyze/meson.build index 782685ee..0939bcf2 100644 --- a/src/libsysprof-analyze/meson.build +++ b/src/libsysprof-analyze/meson.build @@ -3,6 +3,7 @@ libsysprof_analyze_public_sources = [ 'sysprof-document.c', 'sysprof-document-allocation.c', 'sysprof-document-exit.c', + 'sysprof-document-file.c', 'sysprof-document-file-chunk.c', 'sysprof-document-fork.c', 'sysprof-document-frame.c', @@ -34,6 +35,7 @@ libsysprof_analyze_public_headers = [ 'sysprof-document.h', 'sysprof-document-allocation.h', 'sysprof-document-exit.h', + 'sysprof-document-file.h', 'sysprof-document-file-chunk.h', 'sysprof-document-fork.h', 'sysprof-document-frame.h', diff --git a/src/libsysprof-analyze/sysprof-analyze.h b/src/libsysprof-analyze/sysprof-analyze.h index ba5180dc..f81e185d 100644 --- a/src/libsysprof-analyze/sysprof-analyze.h +++ b/src/libsysprof-analyze/sysprof-analyze.h @@ -29,6 +29,7 @@ G_BEGIN_DECLS # include "sysprof-document.h" # include "sysprof-document-allocation.h" # include "sysprof-document-exit.h" +# include "sysprof-document-file.h" # include "sysprof-document-file-chunk.h" # include "sysprof-document-fork.h" # include "sysprof-document-frame.h" diff --git a/src/libsysprof-analyze/sysprof-document-file-private.h b/src/libsysprof-analyze/sysprof-document-file-private.h new file mode 100644 index 00000000..c904124d --- /dev/null +++ b/src/libsysprof-analyze/sysprof-document-file-private.h @@ -0,0 +1,30 @@ +/* sysprof-document-file-private.h + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include "sysprof-document-file.h" + +G_BEGIN_DECLS + +SysprofDocumentFile *_sysprof_document_file_new (const char *path, + GPtrArray *file_chunks); + +G_END_DECLS diff --git a/src/libsysprof-analyze/sysprof-document-file.c b/src/libsysprof-analyze/sysprof-document-file.c new file mode 100644 index 00000000..56e7a00b --- /dev/null +++ b/src/libsysprof-analyze/sysprof-document-file.c @@ -0,0 +1,157 @@ +/* sysprof-document-file.c + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include "sysprof-document-file-chunk.h" +#include "sysprof-document-file-private.h" + +struct _SysprofDocumentFile +{ + GObject parent_instance; + char *path; + GPtrArray *file_chunks; +}; + +enum { + PROP_0, + PROP_BYTES, + PROP_PATH, + N_PROPS +}; + +G_DEFINE_FINAL_TYPE (SysprofDocumentFile, sysprof_document_file, G_TYPE_OBJECT) + +static GParamSpec *properties [N_PROPS]; + +static void +sysprof_document_file_finalize (GObject *object) +{ + SysprofDocumentFile *self = (SysprofDocumentFile *)object; + + g_clear_pointer (&self->path, g_free); + g_clear_pointer (&self->file_chunks, g_ptr_array_unref); + + G_OBJECT_CLASS (sysprof_document_file_parent_class)->finalize (object); +} + +static void +sysprof_document_file_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SysprofDocumentFile *self = SYSPROF_DOCUMENT_FILE (object); + + switch (prop_id) + { + case PROP_PATH: + g_value_set_string (value, sysprof_document_file_get_path (self)); + break; + + case PROP_BYTES: + g_value_take_boxed (value, sysprof_document_file_dup_bytes (self)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_document_file_class_init (SysprofDocumentFileClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = sysprof_document_file_finalize; + object_class->get_property = sysprof_document_file_get_property; + + properties [PROP_PATH] = + g_param_spec_string ("path", NULL, NULL, + NULL, + (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + properties [PROP_BYTES] = + g_param_spec_boxed ("bytes", NULL, NULL, + G_TYPE_BYTES, + (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, N_PROPS, properties); +} + +static void +sysprof_document_file_init (SysprofDocumentFile *self) +{ +} + +SysprofDocumentFile * +_sysprof_document_file_new (const char *path, + GPtrArray *file_chunks) +{ + SysprofDocumentFile *self; + + g_return_val_if_fail (path != NULL, NULL); + g_return_val_if_fail (file_chunks != NULL, NULL); + + self = g_object_new (SYSPROF_TYPE_DOCUMENT_FILE, NULL); + self->path = g_strdup (path); + self->file_chunks = file_chunks; + + return self; +} + +const char * +sysprof_document_file_get_path (SysprofDocumentFile *self) +{ + g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FILE (self), NULL); + + return self->path; +} + +/** + * sysprof_document_file_dup_contents: + * @self: a #SysprofDocumentFile + * + * Creates a new #GBytes containing the contents of the file. + * + * Returns: (transfer full): a #GBytes + */ +GBytes * +sysprof_document_file_dup_bytes (SysprofDocumentFile *self) +{ + GArray *ar; + guint len; + + g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FILE (self), NULL); + + ar = g_array_new (TRUE, FALSE, sizeof (char)); + + for (guint i = 0; i < self->file_chunks->len; i++) + { + SysprofDocumentFileChunk *file_chunk = g_ptr_array_index (self->file_chunks, i); + const guint8 *data = sysprof_document_file_chunk_get_data (file_chunk, &len); + + g_array_append_vals (ar, data, len); + } + + len = ar->len; + + return g_bytes_new_take (g_array_free (ar, FALSE), len); +} diff --git a/src/libsysprof-analyze/sysprof-document-file.h b/src/libsysprof-analyze/sysprof-document-file.h new file mode 100644 index 00000000..0b66501d --- /dev/null +++ b/src/libsysprof-analyze/sysprof-document-file.h @@ -0,0 +1,39 @@ +/* sysprof-document-file.h + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +#include + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_DOCUMENT_FILE (sysprof_document_file_get_type()) + +SYSPROF_AVAILABLE_IN_ALL +G_DECLARE_FINAL_TYPE (SysprofDocumentFile, sysprof_document_file, SYSPROF, DOCUMENT_FILE, GObject) + +SYSPROF_AVAILABLE_IN_ALL +const char *sysprof_document_file_get_path (SysprofDocumentFile *self); +SYSPROF_AVAILABLE_IN_ALL +GBytes *sysprof_document_file_dup_bytes (SysprofDocumentFile *self); + +G_END_DECLS