From 336dae4d1f3e17cc70d64d67f4d6cccfe39d6add Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Mon, 27 May 2019 15:43:00 -0700 Subject: [PATCH] libsysprof-capture: add helper to list files in capture --- .../sysprof-capture-reader.c | 36 +++++++++++++++++++ .../sysprof-capture-reader.h | 2 ++ src/tests/test-capture.c | 7 ++++ 3 files changed, 45 insertions(+) diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c index 640bfd41..b65fe67c 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.c +++ b/src/libsysprof-capture/sysprof-capture-reader.c @@ -1173,3 +1173,39 @@ sysprof_capture_reader_read_file (SysprofCaptureReader *self) return file_chunk; } + +gchar ** +sysprof_capture_reader_list_files (SysprofCaptureReader *self) +{ + g_autoptr(GHashTable) files = NULL; + g_autoptr(GPtrArray) ar = NULL; + SysprofCaptureFrameType type; + GHashTableIter iter; + const gchar *key; + + g_assert (self != NULL); + + ar = g_ptr_array_new_with_free_func (g_free); + files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); + + while (sysprof_capture_reader_peek_type (self, &type)) + { + const SysprofCaptureFileChunk *file; + + if (type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK) + continue; + + if (!(file = sysprof_capture_reader_read_file (self))) + break; + + if (!g_hash_table_contains (files, file->path)) + g_hash_table_insert (files, g_strdup (file->path), NULL); + } + + g_hash_table_iter_init (&iter, files); + while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL)) + g_ptr_array_add (ar, g_strdup (key)); + g_ptr_array_add (ar, NULL); + + return (gchar **)g_ptr_array_free (g_steal_pointer (&ar), FALSE); +} diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h index f14128e3..32560037 100644 --- a/src/libsysprof-capture/sysprof-capture-reader.h +++ b/src/libsysprof-capture/sysprof-capture-reader.h @@ -97,6 +97,8 @@ gboolean sysprof_capture_reader_get_stat SYSPROF_AVAILABLE_IN_ALL void sysprof_capture_reader_set_stat (SysprofCaptureReader *self, const SysprofCaptureStat *st_buf); +SYSPROF_AVAILABLE_IN_ALL +gchar **sysprof_capture_reader_list_files (SysprofCaptureReader *self); G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureReader, sysprof_capture_reader_unref) diff --git a/src/tests/test-capture.c b/src/tests/test-capture.c index 3f603252..8c2b2a55 100644 --- a/src/tests/test-capture.c +++ b/src/tests/test-capture.c @@ -716,6 +716,7 @@ test_reader_writer_file (void) { g_autofree gchar *data = NULL; GByteArray *buf = g_byte_array_new (); + g_auto(GStrv) files = NULL; SysprofCaptureWriter *writer; SysprofCaptureReader *reader; SysprofCaptureFrameType type; @@ -772,6 +773,12 @@ test_reader_writer_file (void) r = sysprof_capture_reader_peek_type (reader, &type); g_assert_cmpint (r, ==, FALSE); + sysprof_capture_reader_reset (reader); + files = sysprof_capture_reader_list_files (reader); + g_assert_nonnull (files); + g_assert_cmpstr (files[0], ==, "/proc/kallsyms"); + g_assert_null (files[1]); + g_clear_pointer (&reader, sysprof_capture_reader_unref); g_clear_pointer (&buf, g_byte_array_unref);