Merge branch 'file-uri-cleanup' into 'main'

glocalvfs: Remove unnecessary and buggy code

See merge request GNOME/glib!3776
This commit is contained in:
Philip Withnall 2023-12-21 12:42:27 +00:00
commit 833d3fb6cf
4 changed files with 51 additions and 58 deletions

View File

@ -94,32 +94,9 @@ g_local_vfs_get_file_for_uri (GVfs *vfs,
{ {
char *path; char *path;
GFile *file; GFile *file;
char *stripped_uri, *hash, *question_mark;
/* As per https://url.spec.whatwg.org/#file-state, file: URIs can contain path = g_filename_from_uri (uri, NULL, NULL);
* query and fragment sections. We ignore them in order to get only the file
* path. Compliance to this part of the WhatWG spec doesnt necessarily mean
* we comply with the entire spec. */
if (strchr (uri, '#') != NULL)
{
stripped_uri = g_strdup (uri);
hash = strchr (stripped_uri, '#');
*hash = 0;
}
else if (strchr (uri, '?') != NULL)
{
stripped_uri = g_strdup (uri);
question_mark = strchr (stripped_uri, '?');
*question_mark = 0;
}
else
stripped_uri = (char *)uri;
path = g_filename_from_uri (stripped_uri, NULL, NULL);
if (stripped_uri != uri)
g_free (stripped_uri);
if (path != NULL) if (path != NULL)
file = _g_local_file_new (path); file = _g_local_file_new (path);
else else

View File

@ -3958,35 +3958,42 @@ test_enumerator_cancellation (void)
} }
static void static void
test_from_uri_ignores_fragment (void) test_path_from_uri_helper (const gchar *uri,
const gchar *expected_path)
{ {
GFile *file; GFile *file;
gchar *path; gchar *path;
file = g_file_new_for_uri ("file:///tmp/foo#bar"); gchar *expected_platform_path;
path = g_file_get_path (file);
expected_platform_path = g_strdup (expected_path);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
g_assert_cmpstr (path, ==, "\\tmp\\foo"); for (gchar *p = expected_platform_path; *p; p++)
#else {
g_assert_cmpstr (path, ==, "/tmp/foo"); if (*p == '/')
*p = '\\';
}
#endif #endif
file = g_file_new_for_uri (uri);
path = g_file_get_path (file);
g_assert_cmpstr (path, ==, expected_platform_path);
g_free (path); g_free (path);
g_object_unref (file); g_object_unref (file);
g_free (expected_platform_path);
}
static void
test_from_uri_ignores_fragment (void)
{
test_path_from_uri_helper ("file:///tmp/foo#bar", "/tmp/foo");
test_path_from_uri_helper ("file:///tmp/foo#bar?baz", "/tmp/foo");
} }
static void static void
test_from_uri_ignores_query_string (void) test_from_uri_ignores_query_string (void)
{ {
GFile *file; test_path_from_uri_helper ("file:///tmp/foo?bar", "/tmp/foo");
gchar *path; test_path_from_uri_helper ("file:///tmp/foo?bar#baz", "/tmp/foo");
file = g_file_new_for_uri ("file:///tmp/foo?bar");
path = g_file_get_path (file);
#ifdef G_OS_WIN32
g_assert_cmpstr (path, ==, "\\tmp\\foo");
#else
g_assert_cmpstr (path, ==, "/tmp/foo");
#endif
g_free (path);
g_object_unref (file);
} }
int int

View File

@ -950,35 +950,42 @@ test_no_conv (void)
} }
static void static void
test_filename_from_uri_query_is_ignored (void) test_filename_from_uri_helper (const gchar *uri,
const gchar *expected_filename)
{ {
gchar *filename; gchar *filename;
gchar *expected_platform_filename;
GError *error = NULL; GError *error = NULL;
filename = g_filename_from_uri ("file:///tmp/foo?bar", NULL, &error); expected_platform_filename = g_strdup (expected_filename);
g_assert_no_error (error);
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
g_assert_cmpstr (filename, ==, "\\tmp\\foo"); for (gchar *p = expected_platform_filename; *p; p++)
#else {
g_assert_cmpstr (filename, ==, "/tmp/foo"); if (*p == '/')
*p = '\\';
}
#endif #endif
filename = g_filename_from_uri (uri, NULL, &error);
g_assert_no_error (error);
g_assert_cmpstr (filename, ==, expected_platform_filename);
g_free (filename); g_free (filename);
g_free (expected_platform_filename);
}
static void
test_filename_from_uri_query_is_ignored (void)
{
test_filename_from_uri_helper ("file:///tmp/foo?bar", "/tmp/foo");
test_filename_from_uri_helper ("file:///tmp/foo?bar#baz", "/tmp/foo");
} }
static void static void
test_filename_from_uri_fragment_is_ignored (void) test_filename_from_uri_fragment_is_ignored (void)
{ {
gchar *filename; test_filename_from_uri_helper ("file:///tmp/foo#bar", "/tmp/foo");
GError *error = NULL; /* this doesn't have a query, only a bizarre anchor */
test_filename_from_uri_helper ("file:///tmp/foo#bar?baz", "/tmp/foo");
filename = g_filename_from_uri ("file:///tmp/foo#bar", NULL, &error);
g_assert_no_error (error);
#ifdef G_OS_WIN32
g_assert_cmpstr (filename, ==, "\\tmp\\foo");
#else
g_assert_cmpstr (filename, ==, "/tmp/foo");
#endif
g_free (filename);
} }
int int

View File

@ -132,6 +132,8 @@ file_from_uri_tests[] = {
{ "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, { "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file:///some/file?query", "/some/file", NULL, 0 }, { "file:///some/file?query", "/some/file", NULL, 0 },
{ "file:///some/file#bad", "/some/file", NULL, 0 }, { "file:///some/file#bad", "/some/file", NULL, 0 },
{ "file:///some/file?query#frag", "/some/file", NULL, 0 },
{ "file:///some/file#fr?ag", "/some/file", NULL, 0 },
{ "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
{ "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI}, { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},