Skip to content

Commit

Permalink
BUGFIX: CDirectoryExplorer throws on broken symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Sep 3, 2024
1 parent c3f33dc commit 513bf22
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
1 change: 1 addition & 0 deletions doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Fix unstable keypoint KLT response values leading to NaN in some architectures.
- Fix tons of typos and Debian-specific spare install files (lintian --pedantic).
- Fix segfault in arm64 EKF unit tests.
- Fix bug in mrpt::system::CDirectoryExplorer: it would throw and stop if finds a broken symlink.

# Version 2.13.7: Released Aug 22nd, 2024
- Changes in apps:
Expand Down
63 changes: 30 additions & 33 deletions libs/system/src/CDirectoryExplorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,47 +142,44 @@ CDirectoryExplorer::TFileInfoList CDirectoryExplorer::explore(

while ((ent = readdir(dir)) != nullptr)
{
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) continue;

// File name:
newEntry.name = string(ent->d_name);

// Complete absolute file path:
newEntry.wholePath = fs::absolute(fs::path(searchPath + newEntry.name));

// File times:
struct stat statDat
{
// File name:
newEntry.name = string(ent->d_name);
}, lstatDat{};

// Complete absolute file path:
newEntry.wholePath = fs::absolute(fs::path(searchPath + newEntry.name));
if (stat(newEntry.wholePath.c_str(), &statDat))
continue; // Ignore it: permissions problem or broken symlink?

// File times:
struct stat statDat
{
}, lstatDat{};
if (stat(newEntry.wholePath.c_str(), &statDat))
{
closedir(dir);
THROW_EXCEPTION_FMT("Cannot get stat for file: '%s'", newEntry.wholePath.c_str());
}
newEntry.modTime = mrpt::Clock::fromDouble(static_cast<double>(statDat.st_mtime));
newEntry.accessTime = mrpt::Clock::fromDouble(static_cast<double>(statDat.st_atime));

newEntry.modTime = mrpt::Clock::fromDouble(static_cast<double>(statDat.st_mtime));
newEntry.accessTime = mrpt::Clock::fromDouble(static_cast<double>(statDat.st_atime));
// Flags:
newEntry.isDir = S_ISDIR(statDat.st_mode);

// Flags:
newEntry.isDir = S_ISDIR(statDat.st_mode);
if (((mask & FILE_ATTRIB_ARCHIVE) != 0 && !newEntry.isDir) ||
((mask & FILE_ATTRIB_DIRECTORY) != 0 && newEntry.isDir))
{
// File size:
newEntry.fileSize = (intmax_t)statDat.st_size;

if (((mask & FILE_ATTRIB_ARCHIVE) != 0 && !newEntry.isDir) ||
((mask & FILE_ATTRIB_DIRECTORY) != 0 && newEntry.isDir))
// Is it a symbolic link?? Need to call "lstat":
if (!lstat(newEntry.wholePath.c_str(), &lstatDat))
{
// File size:
newEntry.fileSize = (intmax_t)statDat.st_size;

// Is it a symbolic link?? Need to call "lstat":
if (!lstat(newEntry.wholePath.c_str(), &lstatDat))
{
newEntry.isSymLink = S_ISLNK(lstatDat.st_mode);
}
else
newEntry.isSymLink = false;

// Save:
outList.push_back(newEntry);
newEntry.isSymLink = S_ISLNK(lstatDat.st_mode);
}
else
newEntry.isSymLink = false;

// Save:
outList.push_back(newEntry);
}
}

Expand Down

0 comments on commit 513bf22

Please sign in to comment.