Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/libutil-tests/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,42 @@ TEST(baseNameOf, absoluteNothingSlashNothing)

TEST(isInDir, trivialCase)
{
auto p1 = isInDir("/foo/bar", "/foo");
ASSERT_EQ(p1, true);
EXPECT_TRUE(isInDir("/foo/bar", "/foo"));
}

TEST(isInDir, notInDir)
{
auto p1 = isInDir("/zes/foo/bar", "/foo");
ASSERT_EQ(p1, false);
EXPECT_FALSE(isInDir("/zes/foo/bar", "/foo"));
}

TEST(isInDir, emptyDir)
{
auto p1 = isInDir("/zes/foo/bar", "");
ASSERT_EQ(p1, false);
EXPECT_FALSE(isInDir("/zes/foo/bar", ""));
}

TEST(isInDir, hiddenSubdirectory)
{
EXPECT_TRUE(isInDir("/foo/.ssh", "/foo"));
}

TEST(isInDir, ellipsisEntry)
{
EXPECT_TRUE(isInDir("/foo/...", "/foo"));
}

TEST(isInDir, sameDir)
{
EXPECT_FALSE(isInDir("/foo", "/foo"));
}

TEST(isInDir, sameDirDot)
{
EXPECT_FALSE(isInDir("/foo/.", "/foo"));
}

TEST(isInDir, dotDotPrefix)
{
EXPECT_FALSE(isInDir("/foo/../bar", "/foo"));
}

/* ----------------------------------------------------------------------------
Expand Down
8 changes: 5 additions & 3 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ bool isInDir(const std::filesystem::path & path, const std::filesystem::path & d
/* Note that while the standard doesn't guarantee this, the
`lexically_*` functions should do no IO and not throw. */
auto rel = path.lexically_relative(dir);
/* Method from
https://stackoverflow.com/questions/62503197/check-if-path-contains-another-in-c++ */
return !rel.empty() && rel.native()[0] != OS_STR('.');
if (rel.empty())
return false;

auto first = *rel.begin();
return first != "." && first != "..";
}

bool isDirOrInDir(const std::filesystem::path & path, const std::filesystem::path & dir)
Expand Down
Loading