forked from Imagelibrary/binutils-gdb
In this review [1], Eli pointed out that we should be careful when
concatenating file names to avoid duplicated slashes. On Windows, a
double slash at the beginning of a file path has a special meaning. So
naively concatenating "/" and "foo/bar" would give "//foo/bar", which
would not give the desired results. We already have a few spots doing:
if (first_path ends with a slash)
path = first_path + second_path
else
path = first_path + slash + second_path
In general, I think it's nice to avoid superfluous slashes in file
paths, since they might end up visible to the user and look a bit
unprofessional.
Introduce the path_join function that can be used to join multiple path
components together (along with unit tests).
I initially wanted to make it possible to join two absolute paths, to
support the use case of prepending a sysroot path to a target file path,
or the prepending the debug-file-directory to a target file path. But
the code in solib_find_1 shows that it is more complex than this anyway
(for example, when the right hand side is a Windows path with a drive
letter). So I don't think we need to support that case in path_join.
That also keeps the implementation simpler.
Change a few spots to use path_join to show how it can be used. I
believe that all the spots I changed are guarded by some checks that
ensure the right hand side operand is not an absolute path.
Regression-tested on Ubuntu 18.04. Built-tested on Windows, and I also
ran the new unit-test there.
[1] https://sourceware.org/pipermail/gdb-patches/2022-April/187559.html
Change-Id: I0df889f7e3f644e045f42ff429277b732eb6c752
74 lines
2.1 KiB
C
74 lines
2.1 KiB
C
/* Self tests for path_join for GDB, the GNU debugger.
|
|
|
|
Copyright (C) 2022 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#include "defs.h"
|
|
#include "gdbsupport/pathstuff.h"
|
|
#include "gdbsupport/selftest.h"
|
|
|
|
namespace selftests {
|
|
namespace path_join {
|
|
|
|
template <typename ...Args>
|
|
static void
|
|
test_one (const char *expected, Args... paths)
|
|
{
|
|
std::string actual = ::path_join (paths...);
|
|
|
|
SELF_CHECK (actual == expected);
|
|
}
|
|
|
|
/* Test path_join. */
|
|
|
|
static void
|
|
test ()
|
|
{
|
|
test_one ("/foo/bar", "/foo", "bar");
|
|
test_one ("/bar", "/", "bar");
|
|
test_one ("foo/bar/", "foo", "bar", "");
|
|
test_one ("foo", "", "foo");
|
|
test_one ("foo/bar", "foo", "", "bar");
|
|
test_one ("foo/", "foo", "");
|
|
test_one ("foo/", "foo/", "");
|
|
|
|
test_one ("D:/foo/bar", "D:/foo", "bar");
|
|
test_one ("D:/foo/bar", "D:/foo/", "bar");
|
|
|
|
#if defined(_WIN32)
|
|
/* The current implementation doesn't recognize backslashes as directory
|
|
separators on Unix-like systems, so only run these tests on Windows. If
|
|
we ever switch our implementation to use std::filesystem::path, they
|
|
should work anywhere, in theory. */
|
|
test_one ("D:\\foo/bar", "D:\\foo", "bar");
|
|
test_one ("D:\\foo\\bar", "D:\\foo\\", "bar");
|
|
test_one ("\\\\server\\dir\\file", "\\\\server\\dir\\", "file");
|
|
test_one ("\\\\server\\dir/file", "\\\\server\\dir", "file");
|
|
#endif /* _WIN32 */
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
void _initialize_path_join_selftests ();
|
|
void
|
|
_initialize_path_join_selftests ()
|
|
{
|
|
selftests::register_test ("path_join",
|
|
selftests::path_join::test);
|
|
}
|