mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2026-02-11 07:30:54 +00:00
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:
@ -27,7 +27,7 @@ G_BEGIN_DECLS
|
|||||||
|
|
||||||
SysprofMount *_sysprof_mount_new_for_mountinfo (SysprofStrings *strings,
|
SysprofMount *_sysprof_mount_new_for_mountinfo (SysprofStrings *strings,
|
||||||
const char *mountinfo);
|
const char *mountinfo);
|
||||||
gboolean _sysprof_mount_contains_path (SysprofMount *self,
|
const char *_sysprof_mount_get_relative_path (SysprofMount *self,
|
||||||
const char *path);
|
const char *path);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|||||||
@ -331,3 +331,33 @@ sysprof_mount_get_parent_mount_id (SysprofMount *self)
|
|||||||
{
|
{
|
||||||
return self->parent_mount_id;
|
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];
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user