gfileutils: Improve performance of g_canonicalize_filename()

Improve the performance of canonicalising filenames with many `..` or
`.` components, by modifying the path inline rather than calling
`memmove()`.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2541
This commit is contained in:
Sebastian Wilhelmi
2021-11-26 13:43:56 +00:00
committed by Philip Withnall
parent 72377e3b6e
commit 9a30a495ec
2 changed files with 94 additions and 57 deletions

View File

@@ -1051,6 +1051,18 @@ test_paths (void)
{ "///triple/slash", ".", "/triple/slash" },
{ "//double/slash", ".", "//double/slash" },
{ "/cwd/../with/./complexities/", "./hello", "/with/complexities/hello" },
{ "/", ".dot-dir", "/.dot-dir" },
{ "/cwd", "..", "/" },
{ "/etc", "hello/..", "/etc" },
{ "/etc", "hello/../", "/etc" },
{ "/", "..", "/" },
{ "/", "../", "/" },
{ "/", "/..", "/" },
{ "/", "/../", "/" },
{ "/", ".", "/" },
{ "/", "./", "/" },
{ "/", "/.", "/" },
{ "/", "/./", "/" },
#else
{ "/etc", "../usr/share", "\\usr\\share" },
{ "/", "/foo/bar", "\\foo\\bar" },
@@ -1066,6 +1078,18 @@ test_paths (void)
{ "///triple/slash", ".", "\\triple\\slash" },
{ "//double/slash", ".", "//double/slash\\" },
{ "/cwd/../with/./complexities/", "./hello", "\\with\\complexities\\hello" },
{ "/", ".dot-dir", "/.dot-dir" },
{ "/cwd", "..", "/" },
{ "/etc", "hello/..", "/etc" },
{ "/etc", "hello/../", "/etc" },
{ "/", "..", "/" },
{ "/", "../", "/" },
{ "/", "/..", "/" },
{ "/", "/../", "/" },
{ "/", ".", "/" },
{ "/", "./", "/" },
{ "/", "/.", "/" },
{ "/", "/./", "/" },
{ "\\etc", "..\\usr\\share", "\\usr\\share" },
{ "\\", "\\foo\\bar", "\\foo\\bar" },
@@ -1081,6 +1105,18 @@ test_paths (void)
{ "\\\\\\triple\\slash", ".", "\\triple\\slash" },
{ "\\\\double\\slash", ".", "\\\\double\\slash\\" },
{ "\\cwd\\..\\with\\.\\complexities\\", ".\\hello", "\\with\\complexities\\hello" },
{ "\\", ".dot-dir", "\\.dot-dir" },
{ "\\cwd", "..", "\\" },
{ "\\etc", "hello\\..", "\\etc" },
{ "\\etc", "hello\\..\\", "\\etc" },
{ "\\", "..", "\\" },
{ "\\", "..\\", "\\" },
{ "\\", "\\..", "\\" },
{ "\\", "\\..\\", "\\" },
{ "\\", ".", "\\" },
{ "\\", ".\\", "\\" },
{ "\\", "\\.", "\\" },
{ "\\", "\\.\\", "\\" },
#endif
};
const guint n_canonicalize_filename_checks = G_N_ELEMENTS (canonicalize_filename_checks);