mirror of
https://github.com/flatpak/flatpak.git
synced 2026-05-10 09:00:20 -04:00
common: Report the age of the configuration
This change will allow applications to determine if their data is older than the flatpak configuration, to aid cache invalidation.
This commit is contained in:
committed by
Sebastian Wick
parent
43642337e4
commit
fffe38a2b0
@@ -2675,6 +2675,41 @@ flatpak_installation_create_monitor (FlatpakInstallation *self,
|
||||
cancellable, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* flatpak_installation_get_timestamp:
|
||||
* @self: a #FlatpakInstallation
|
||||
*
|
||||
* Gets the modification time of the installation, based on the file monitored by
|
||||
* flatpak_installation_create_monitor(). This can be used to detect when
|
||||
* applications or runtimes have been installed, uninstalled, or updated, or when
|
||||
* remotes have been added, removed, or modified, to aid cache invalidation.
|
||||
*
|
||||
* Returns: the modification time (seconds since the Unix epoch) of the
|
||||
* installation configuration, or %G_MAXUINT64 if unavailable
|
||||
*
|
||||
* Since: 1.18.0
|
||||
*/
|
||||
guint64
|
||||
flatpak_installation_get_timestamp (FlatpakInstallation *self)
|
||||
{
|
||||
g_autoptr(FlatpakDir) dir = NULL;
|
||||
g_autoptr(GFile) changed_file = NULL;
|
||||
g_autoptr(GFileInfo) info = NULL;
|
||||
|
||||
dir = flatpak_installation_get_dir_maybe_no_repo (self);
|
||||
changed_file = flatpak_dir_get_changed_path (dir);
|
||||
|
||||
info = g_file_query_info (changed_file,
|
||||
G_FILE_ATTRIBUTE_TIME_MODIFIED,
|
||||
G_FILE_QUERY_INFO_NONE,
|
||||
NULL,
|
||||
NULL);
|
||||
if (info == NULL)
|
||||
return G_MAXUINT64;
|
||||
|
||||
return g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* flatpak_installation_list_remote_related_refs_sync:
|
||||
|
||||
@@ -236,6 +236,7 @@ FLATPAK_EXTERN gboolean flatpak_installation_launch_full (FlatpakIns
|
||||
FLATPAK_EXTERN GFileMonitor *flatpak_installation_create_monitor (FlatpakInstallation *self,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
FLATPAK_EXTERN guint64 flatpak_installation_get_timestamp (FlatpakInstallation *self);
|
||||
FLATPAK_EXTERN GPtrArray *flatpak_installation_list_installed_refs (FlatpakInstallation *self,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
@@ -10,6 +10,7 @@ flatpak_installation_new_for_path
|
||||
flatpak_installation_get_is_user
|
||||
flatpak_installation_get_path
|
||||
flatpak_installation_create_monitor
|
||||
flatpak_installation_get_timestamp
|
||||
flatpak_installation_install
|
||||
flatpak_installation_install_full
|
||||
flatpak_installation_update
|
||||
|
||||
@@ -546,6 +546,49 @@ test_list_remotes (void)
|
||||
g_assert_cmpuint (remotes2->len, ==, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_timestamp (void)
|
||||
{
|
||||
g_autoptr(FlatpakInstallation) inst = NULL;
|
||||
g_autoptr(GError) error = NULL;
|
||||
g_autoptr(FlatpakRemote) remote = NULL;
|
||||
guint64 mtime1, mtime2, mtime3;
|
||||
|
||||
inst = flatpak_installation_new_user (NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
/* Get initial modification time */
|
||||
mtime1 = flatpak_installation_get_timestamp (inst);
|
||||
g_assert_cmpuint (mtime1, <, G_MAXUINT64);
|
||||
|
||||
/* Wait at least 1 second */
|
||||
sleep (1);
|
||||
|
||||
/* Modification time should stay the same when nothing changes */
|
||||
mtime2 = flatpak_installation_get_timestamp (inst);
|
||||
g_assert_cmpuint (mtime2, ==, mtime1);
|
||||
|
||||
/* Modifying a remote should update the modification time */
|
||||
remote = flatpak_installation_get_remote_by_name (inst, repo_name, NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_nonnull (remote);
|
||||
|
||||
flatpak_remote_set_disabled (remote, TRUE);
|
||||
flatpak_installation_modify_remote (inst, remote, NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
/* Need to drop caches to see the new modification time */
|
||||
flatpak_installation_drop_caches (inst, NULL, NULL);
|
||||
|
||||
mtime3 = flatpak_installation_get_timestamp (inst);
|
||||
g_assert_cmpuint (mtime3, >, mtime2);
|
||||
|
||||
/* Restore the remote state */
|
||||
flatpak_remote_set_disabled (remote, FALSE);
|
||||
flatpak_installation_modify_remote (inst, remote, NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
}
|
||||
|
||||
static void
|
||||
test_remote_by_name (void)
|
||||
{
|
||||
@@ -5088,6 +5131,7 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/library/arches", test_arches);
|
||||
g_test_add_func ("/library/ref", test_ref);
|
||||
g_test_add_func ("/library/list-remotes", test_list_remotes);
|
||||
g_test_add_func ("/library/timestamp", test_timestamp);
|
||||
g_test_add_func ("/library/remote-by-name", test_remote_by_name);
|
||||
g_test_add_func ("/library/remote", test_remote);
|
||||
g_test_add_func ("/library/remote-new", test_remote_new);
|
||||
|
||||
Reference in New Issue
Block a user