diff options
| author | Andrew Stitcher <astitcher@apache.org> | 2012-12-20 00:35:54 +0000 |
|---|---|---|
| committer | Andrew Stitcher <astitcher@apache.org> | 2012-12-20 00:35:54 +0000 |
| commit | 4a0452844f85edc5b0be0edcf877e6b5d33302ed (patch) | |
| tree | a1e5f62f65fb9cf767c393bda9020f6e1b63a187 /cpp/src/qpid/sys/posix/FileSysDir.cpp | |
| parent | 24e9d70d7bcefae3a68d3cc4b19e684440776a05 (diff) | |
| download | qpid-python-4a0452844f85edc5b0be0edcf877e6b5d33302ed.tar.gz | |
QPID-4095: Move the directory iteration into FileSysDir:
- For Posix implement with direct calls
- For windows implement with v2/v3 boost::filesystem to be replaced later
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1424247 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/sys/posix/FileSysDir.cpp')
| -rwxr-xr-x | cpp/src/qpid/sys/posix/FileSysDir.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/cpp/src/qpid/sys/posix/FileSysDir.cpp b/cpp/src/qpid/sys/posix/FileSysDir.cpp index 22dc487e74..cec580164d 100755 --- a/cpp/src/qpid/sys/posix/FileSysDir.cpp +++ b/cpp/src/qpid/sys/posix/FileSysDir.cpp @@ -18,6 +18,7 @@ #include "qpid/sys/FileSysDir.h" #include "qpid/sys/StrError.h" +#include "qpid/log/Statement.h" #include "qpid/Exception.h" #include <sys/types.h> @@ -25,6 +26,8 @@ #include <fcntl.h> #include <cerrno> #include <unistd.h> +#include <dirent.h> +#include <stdlib.h> namespace qpid { namespace sys { @@ -51,4 +54,27 @@ void FileSysDir::mkdir(void) throw Exception ("Can't create directory: " + dirPath); } +void FileSysDir::forEachFile(Callback cb) const { + + ::dirent** namelist; + + int n = scandir(dirPath.c_str(), &namelist, 0, alphasort); + if (n == -1) throw Exception (strError(errno) + ": Can't scan directory: " + dirPath); + + for (int i = 0; i<n; ++i) { + std::string fullpath = dirPath + "/" + namelist[i]->d_name; + // Filter out non files/stat problems etc. + struct ::stat s; + // Can't throw here without leaking memory, so just do nothing with + // entries for which stat() fails. + if (!::stat(fullpath.c_str(), &s)) { + if (S_ISREG(s.st_mode)) { + cb(fullpath); + } + } + ::free(namelist[i]); + } + ::free(namelist); +} + }} // namespace qpid::sys |
