From 628e1c5893717a3489934052e52212847aef3d4b Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 10 Mar 2021 19:10:04 +0000 Subject: [PATCH 1/2] gdummyfile: Return NULL from get_basename() if no path is stored MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rather than returning a URI, which definitely won’t be a valid basename. Signed-off-by: Philip Withnall Helps: #2328 --- gio/gdummyfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gio/gdummyfile.c b/gio/gdummyfile.c index 58442ccbc..3bf5ba920 100644 --- a/gio/gdummyfile.c +++ b/gio/gdummyfile.c @@ -126,7 +126,7 @@ g_dummy_file_get_basename (GFile *file) if (dummy->decoded_uri) return g_path_get_basename (dummy->decoded_uri->path); - return g_strdup (dummy->text_uri); + return NULL; } static char * From d52728f994058182f748d0a4e46b0141d93f4390 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 10 Mar 2021 19:10:38 +0000 Subject: [PATCH 2/2] glocalvfs: Create a dummy file for g_file_new_for_path("") MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `""` is not a valid path (`stat()` on it returns `ENOENT`). Previously, a full `GLocalFile` was being created, which ended up resolving to `$CWD`, through path canonicalisation. That isn’t right. Fix it by creating a `GDummyFile` instead, and adding a unit test. Signed-off-by: Philip Withnall Fixes: #2328 --- gio/glocalvfs.c | 5 ++++- gio/tests/file.c | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/gio/glocalvfs.c b/gio/glocalvfs.c index f8d85e458..2dc0f2de7 100644 --- a/gio/glocalvfs.c +++ b/gio/glocalvfs.c @@ -80,7 +80,10 @@ static GFile * g_local_vfs_get_file_for_path (GVfs *vfs, const char *path) { - return _g_local_file_new (path); + if (*path == '\0') + return _g_dummy_file_new (path); + else + return _g_local_file_new (path); } static GFile * diff --git a/gio/tests/file.c b/gio/tests/file.c index c1a2a9360..7f5ee8e78 100644 --- a/gio/tests/file.c +++ b/gio/tests/file.c @@ -96,6 +96,26 @@ test_child (void) g_object_unref (file); } +static void +test_empty_path (void) +{ + GFile *file = NULL; + + g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2328"); + g_test_summary ("Check that creating a file with an empty path results in errors"); + + /* Creating the file must always succeed. */ + file = g_file_new_for_path (""); + g_assert_nonnull (file); + + /* But then querying its path should indicate it’s invalid. */ + g_assert_null (g_file_get_path (file)); + g_assert_null (g_file_get_basename (file)); + g_assert_null (g_file_get_parent (file)); + + g_object_unref (file); +} + static void test_type (void) { @@ -2875,6 +2895,7 @@ main (int argc, char *argv[]) g_test_add_func ("/file/build-filename", test_build_filename); g_test_add_func ("/file/parent", test_parent); g_test_add_func ("/file/child", test_child); + g_test_add_func ("/file/empty-path", test_empty_path); g_test_add_func ("/file/type", test_type); g_test_add_func ("/file/parse-name", test_parse_name); g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);