libsysprof-analyze: add get_relative_path helper

This acts somewhat like g_file_get_relative_path() in that if it is not a
subdirectory of the parent, NULL is returned. Otherwise the relative path
is returned. We can just dive into the substring instead of copying which
is a bonus point.
This commit is contained in:
Christian Hergert
2023-05-19 10:02:20 -07:00
parent 7db4540f9a
commit 63168656a5
2 changed files with 31 additions and 1 deletions

View File

@ -27,7 +27,7 @@ G_BEGIN_DECLS
SysprofMount *_sysprof_mount_new_for_mountinfo (SysprofStrings *strings,
const char *mountinfo);
gboolean _sysprof_mount_contains_path (SysprofMount *self,
const char *_sysprof_mount_get_relative_path (SysprofMount *self,
const char *path);
G_END_DECLS

View File

@ -331,3 +331,33 @@ sysprof_mount_get_parent_mount_id (SysprofMount *self)
{
return self->parent_mount_id;
}
static inline gboolean
mount_is_root (SysprofMount *self)
{
return self->mount_point[0] == '/' && self->mount_point[1] == 0;
}
const char *
_sysprof_mount_get_relative_path (SysprofMount *self,
const char *path)
{
gsize len;
g_return_val_if_fail (SYSPROF_IS_MOUNT (self), NULL);
if (path == NULL || self->mount_point == NULL)
return NULL;
len = g_ref_string_length (self->mount_point);
/* We don't care about directory paths, so ensure that we both
* have the proper prefix and that it is in a subdirectory of this
* mount point.
*/
if (!mount_is_root (self) &&
(!g_str_has_prefix (path, self->mount_point) || path[len] != '/'))
return NULL;
return &path[len];
}