libsysprof-capture: add helper to list files in capture

This commit is contained in:
Christian Hergert
2019-05-27 15:43:00 -07:00
parent 7650d6e7c6
commit 336dae4d1f
3 changed files with 45 additions and 0 deletions

View File

@ -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);
}

View File

@ -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)

View File

@ -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);