Skip to content

Commit

Permalink
generic boost based file scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Aug 18, 2011
1 parent 7d7a948 commit 5f993df
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cpp/core/CMakeLists.txt
Expand Up @@ -137,8 +137,8 @@ else()
set(CORE_SOURCE_FILES ${CORE_SOURCE_FILES}
Win32StringUtils.cpp
system/DirectoryMonitor.cpp
system/FileScanner.cpp
system/RegistryKey.cpp
system/Win32FileScanner.cpp
system/Win32ParentProcessMonitor.cpp
system/Win32OutputCapture.cpp
system/Win32System.cpp
Expand Down
70 changes: 70 additions & 0 deletions src/cpp/core/system/FileScanner.cpp
Expand Up @@ -13,18 +13,88 @@

#include <core/system/FileScanner.hpp>

#include <boost/foreach.hpp>

#include <core/Error.hpp>
#include <core/FileInfo.hpp>
#include <core/FilePath.hpp>

namespace core {
namespace system {

namespace {

inline FileInfo toFileInfo(const FilePath& filePath)
{
return FileInfo(filePath);
}

} // anonymous namespace

Error scanFiles(const FileInfo& fromRoot,
bool recursive,
tree<FileInfo>* pTree)
{
return scanFiles(pTree->set_head(fromRoot), recursive, pTree);
}

Error scanFiles(const tree<FileInfo>::iterator_base& fromNode,
bool recursive,
tree<FileInfo>* pTree)
{
// clear all existing
pTree->erase_children(fromNode);

// create FilePath for root
FilePath rootPath(fromNode->absolutePath());

// read directory entries
std::vector<FilePath> children;
Error error = rootPath.children(&children);
if (error)
return error;

// convert to FileInfo and sort using alphasort equivilant (for
// compatability with scandir, which is what is used in our
// posix-specific implementation
std::vector<FileInfo> childrenFileInfo;
std::transform(children.begin(),
children.end(),
std::back_inserter(childrenFileInfo),
toFileInfo);
std::sort(childrenFileInfo.begin(),
childrenFileInfo.end(),
fileInfoPathLessThan);

// iterate over entries
BOOST_FOREACH(const FileInfo& childFileInfo, childrenFileInfo)
{
// add the correct type of FileEntry
if (childFileInfo.isDirectory())
{
tree<FileInfo>::iterator_base child =
pTree->append_child(fromNode, childFileInfo);
if (recursive)
{
Error error = scanFiles(child, true, pTree);
if (error)
{
LOG_ERROR(error);
continue;
}
}
}
else
{
pTree->append_child(fromNode, childFileInfo);
}
}

// return success
return Success();
}


} // namespace system
} // namespace core

7 changes: 7 additions & 0 deletions src/cpp/core/system/PosixFileScanner.cpp
Expand Up @@ -35,6 +35,13 @@ int entryFilter(struct dirent *entry)

} // anonymous namespace

Error scanFiles(const FileInfo& fromRoot,
bool recursive,
tree<FileInfo>* pTree)
{
return scanFiles(pTree->set_head(fromRoot), recursive, pTree);
}

Error scanFiles(const tree<FileInfo>::iterator_base& fromNode,
bool recursive,
tree<FileInfo>* pTree)
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/core/system/file_monitor/FileMonitor.cpp
Expand Up @@ -13,6 +13,8 @@

// TODO: see if there are filesystems/scenarios where filemon won't work

// TODO: think more deeply about failure cases during scanning

#include <core/system/FileMonitor.hpp>

#include <list>
Expand Down

0 comments on commit 5f993df

Please sign in to comment.