Skip to content

Commit

Permalink
Improve POSIX compliance in CLI/FileUtils.cpp (#1064)
Browse files Browse the repository at this point in the history
Some POSIX platforms, such as Haiku and some BSDs, don't supply DT_*
identifiers and the corresponding d_type field in stat. This fixes that
and has been tested to still work on 64-bit Linux as well as 64-bit
Haiku.
  • Loading branch information
SamuraiCrow authored Oct 12, 2023
1 parent 3d4e99f commit 9843364
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions CLI/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,14 @@ static bool traverseDirectoryRec(const std::string& path, const std::function<vo
{
joinPaths(buf, path.c_str(), data.d_name);

int type = data.d_type;
int mode = -1;
#if defined(DTTOIF)
mode_t mode = DTTOIF(data.d_type);
#else
mode_t mode = 0;
#endif

// we need to stat DT_UNKNOWN to be able to tell the type
if (type == DT_UNKNOWN)
// we need to stat an UNKNOWN to be able to tell the type
if ((mode & S_IFMT) == 0)
{
struct stat st = {};
#ifdef _ATFILE_SOURCE
Expand All @@ -181,15 +184,15 @@ static bool traverseDirectoryRec(const std::string& path, const std::function<vo
mode = st.st_mode;
}

if (type == DT_DIR || mode == S_IFDIR)
if (mode == S_IFDIR)
{
traverseDirectoryRec(buf, callback);
}
else if (type == DT_REG || mode == S_IFREG)
else if (mode == S_IFREG)
{
callback(buf);
}
else if (type == DT_LNK || mode == S_IFLNK)
else if (mode == S_IFLNK)
{
// Skip symbolic links to avoid handling cycles
}
Expand Down

0 comments on commit 9843364

Please sign in to comment.