GFile: Add g_file_peek_path()

This is a variant of g_file_get_path() which returns a const string to
the caller, rather than transferring ownership.

I've been carrying `gs_file_get_path_cached()` in libgsystem and it
has seen a lot of use in the ostree and flatpak codebases.  There are
probably others too.

I think language bindings like Python/Gjs could also use this to avoid
an extra malloc (i.e. we could transparently replace
`g_file_get_path()` with `g_file_peek_path()`.

(Originally by Colin Walters. Tweaked by Philip Withnall to update to
2.56, change the function name and drop the locking.)

https://bugzilla.gnome.org/show_bug.cgi?id=767976
This commit is contained in:
Colin Walters
2016-06-23 08:42:53 -04:00
committed by Philip Withnall
parent 261cb8ea86
commit 4808a957b5
4 changed files with 109 additions and 3 deletions

View File

@@ -169,9 +169,12 @@ monitor_changed (GFileMonitor *monitor,
{
CreateDeleteData *data = user_data;
gchar *path;
const gchar *peeked_path;
path = g_file_get_path (file);
peeked_path = g_file_peek_path (file);
g_assert_cmpstr (data->monitor_path, ==, path);
g_assert_cmpstr (path, ==, peeked_path);
g_free (path);
if (event_type == G_FILE_MONITOR_EVENT_CREATED)
@@ -619,7 +622,7 @@ static void
test_replace_load (void)
{
ReplaceLoadData *data;
gchar *path;
const gchar *path;
GFileIOStream *iostream;
data = g_new0 (ReplaceLoadData, 1);
@@ -631,7 +634,7 @@ test_replace_load (void)
g_assert (data->file != NULL);
g_object_unref (iostream);
path = g_file_get_path (data->file);
path = g_file_peek_path (data->file);
remove (path);
g_assert (!g_file_query_exists (data->file, NULL));
@@ -653,7 +656,6 @@ test_replace_load (void)
g_main_loop_unref (data->loop);
g_object_unref (data->file);
g_free (data);
free (path);
}
static void