pocoproject/poco — pull request #530 by mjdorma (2014-08-28 18:52:24+00:00)
Title: Harden RecursiveDirectoryIterator when walking the filesystem.

In the implementation for the *Traverse strategies the next method performs an unguarded list directory. If the directory is not accessible an unrecoverable error is raised thus ruining the walk. This changeset adopts and adapts the error handling protocol as defined in Python's os.walk function where errors from listdir are ignored or are reported to an optional on error callback function.

Building a unittest to demonstrate this issue did not seem feasible as it would require the test program to contain privileges necessary to alter the access permissions of a directory then setuid to an alternative user.

To exercise this error condition I defined a folder structure as follows:

```
sudo ls -la test/
total 0
drwxr-xr-x 4 mjdorma staff 136 29 Aug 03:10 .
drwxr-xr-x 62 mjdorma staff 2108 29 Aug 04:48 ..
-rw-r--r-- 1 mjdorma staff 0 29 Aug 03:10 blah
drwxr-x--- 3 root wheel 102 29 Aug 03:10 hidden
```

I then added the following functions to the Foundation testsuite DirectoryIteratorsTest:

```C++
void DirectoryIteratorsTest::onerror(const std::string& path)
{
std::cout << "Error on: " << path << std::endl;
}

void DirectoryIteratorsTest::testSimpleRecursiveDirectoryIteratorProtectedTestDirs()
{
Path p("./test");
SimpleRecursiveDirectoryIterator dirIterator(p);
dirIterator.setOnError(TraverseErrorCallback(*this, &DirectoryIteratorsTest::onerror));
SimpleRecursiveDirectoryIterator end;
std::vector result;
std::string file;

while (dirIterator != end)
{
file = dirIterator->path();
++dirIterator;
result.push_back(file);
}
}
````

When ran:
```
$ Foundation/testsuite/bin/Darwin/x86_64/testrunner DirectoryIteratorsTest

testDirectoryIterator:
testSortedDirectoryIterator:
testSimpleRecursiveDirectoryIterator: Error on: test/hidden

testSiblingsFirstRecursiveDirectoryIterator:

OK (4 tests)
```

Desired result achieved.