Add cache to g_unix_mount_points_get()

This commit is contained in:
rim 2022-02-16 10:53:40 +00:00 committed by Philip Withnall
parent c70673d011
commit 37b39c525f

View File

@ -1666,6 +1666,14 @@ g_unix_mount_for (const char *file_path,
return entry;
}
static gpointer
copy_mount_point_cb (gconstpointer src,
gpointer data)
{
GUnixMountPoint *src_mount_point = (GUnixMountPoint *) src;
return g_unix_mount_point_copy (src_mount_point);
}
/**
* g_unix_mount_points_get:
* @time_read: (out) (optional): guint64 to contain a timestamp.
@ -1681,10 +1689,29 @@ g_unix_mount_for (const char *file_path,
GList *
g_unix_mount_points_get (guint64 *time_read)
{
if (time_read)
*time_read = get_mount_points_timestamp ();
static GList *mnt_pts_last = NULL;
static guint64 time_read_last = 0;
GList *mnt_pts = NULL;
guint64 time_read_now;
G_LOCK_DEFINE_STATIC (unix_mount_points);
return _g_get_unix_mount_points ();
G_LOCK (unix_mount_points);
time_read_now = get_mount_points_timestamp ();
if (time_read_now != time_read_last || mnt_pts_last == NULL)
{
time_read_last = time_read_now;
g_list_free_full (mnt_pts_last, (GDestroyNotify) g_unix_mount_point_free);
mnt_pts_last = _g_get_unix_mount_points ();
}
mnt_pts = g_list_copy_deep (mnt_pts_last, copy_mount_point_cb, NULL);
G_UNLOCK (unix_mount_points);
if (time_read)
*time_read = time_read_now;
return mnt_pts;
}
/**