diff options
author | Andrew Stitcher <astitcher@apache.org> | 2013-01-08 21:43:42 +0000 |
---|---|---|
committer | Andrew Stitcher <astitcher@apache.org> | 2013-01-08 21:43:42 +0000 |
commit | b3cc9da195f5573a9ebeb022c8fd9680c82496bb (patch) | |
tree | a00b7556151a916f21c431333a733216a8f4c773 | |
parent | cb9808307d9e27cb9de04eb131427ced0d8d22d8 (diff) | |
download | qpid-python-b3cc9da195f5573a9ebeb022c8fd9680c82496bb.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
[Cherry picked from trunk@1424247]
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/0.20@1430554 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | qpid/cpp/src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | qpid/cpp/src/Makefile.am | 2 | ||||
-rw-r--r-- | qpid/cpp/src/qpid/Modules.cpp | 49 | ||||
-rw-r--r-- | qpid/cpp/src/qpid/Modules.h | 2 | ||||
-rw-r--r-- | qpid/cpp/src/qpid/client/LoadPlugins.cpp | 2 | ||||
-rwxr-xr-x | qpid/cpp/src/qpid/sys/FileSysDir.h | 9 | ||||
-rwxr-xr-x | qpid/cpp/src/qpid/sys/posix/FileSysDir.cpp | 26 | ||||
-rw-r--r-- | qpid/cpp/src/qpid/sys/windows/FileSysDir.cpp | 14 | ||||
-rw-r--r-- | qpid/cpp/src/qpidd.cpp | 2 |
9 files changed, 75 insertions, 33 deletions
diff --git a/qpid/cpp/src/CMakeLists.txt b/qpid/cpp/src/CMakeLists.txt index 579e792b62..6da3bcbc0b 100644 --- a/qpid/cpp/src/CMakeLists.txt +++ b/qpid/cpp/src/CMakeLists.txt @@ -1470,8 +1470,6 @@ install_pdb (qmfconsole ${QPID_COMPONENT_QMF}) # file whereas older builds only have config.h on autoconf-generated builds. add_definitions(-DHAVE_CONFIG_H) -add_definitions(-DBOOST_FILESYSTEM_VERSION=2) - # Now create the config file from all the info learned above. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h) diff --git a/qpid/cpp/src/Makefile.am b/qpid/cpp/src/Makefile.am index 91ff0621c0..f64ce41cc0 100644 --- a/qpid/cpp/src/Makefile.am +++ b/qpid/cpp/src/Makefile.am @@ -138,8 +138,6 @@ qpidtest_SCRIPTS = tmoduleexecdir = $(libdir)/qpid/tests tmoduleexec_LTLIBRARIES= -AM_CXXFLAGS += -DBOOST_FILESYSTEM_VERSION=2 - ## Automake macros to build libraries and executables. qpidd_CXXFLAGS = $(AM_CXXFLAGS) -DQPIDD_MODULE_DIR=\"$(dmoduleexecdir)\" -DQPIDD_CONF_FILE=\"$(sysconfdir)/qpidd.conf\" -DQPIDC_CONF_FILE=\"$(confdir)/qpidc.conf\" libqpidclient_la_CXXFLAGS = $(AM_CXXFLAGS) -DQPIDC_MODULE_DIR=\"$(cmoduleexecdir)\" -DQPIDC_CONF_FILE=\"$(confdir)/qpidc.conf\" diff --git a/qpid/cpp/src/qpid/Modules.cpp b/qpid/cpp/src/qpid/Modules.cpp index 727e05d212..049ededaa7 100644 --- a/qpid/cpp/src/qpid/Modules.cpp +++ b/qpid/cpp/src/qpid/Modules.cpp @@ -24,11 +24,7 @@ #include "qpid/Exception.h" #include "qpid/log/Statement.h" #include "qpid/sys/Shlib.h" - -#include <boost/filesystem/operations.hpp> -#include <boost/filesystem/path.hpp> - -namespace fs=boost::filesystem; +#include "qpid/sys/FileSysDir.h" namespace { @@ -43,7 +39,7 @@ inline std::string& suffix() { } bool isShlibName(const std::string& name) { - return name.find (suffix()) == name.length() - suffix().length(); + return name.substr(name.size()-suffix().size()) == suffix(); } } @@ -59,39 +55,40 @@ ModuleOptions::ModuleOptions(const std::string& defaultModuleDir) ("no-module-dir", optValue(noLoad), "Don't load modules from module directory"); } -void tryShlib(const char* libname_, bool noThrow) { - std::string libname(libname_); - if (!isShlibName(libname)) libname += suffix(); +void tryShlib(const std::string& libname) { + sys::Shlib shlib( isShlibName(libname) ? libname : (libname + suffix())); +} + +namespace { + +void tryOnlyShlib(const std::string& libname) throw() { try { - sys::Shlib shlib(libname); + if (isShlibName(libname)) sys::Shlib shlib( libname ); } catch (const std::exception& /*e*/) { - if (!noThrow) - throw; } } +} + void loadModuleDir (std::string dirname, bool isDefault) { - fs::path dirPath (dirname, fs::native); - if (!fs::exists (dirPath)) + sys::FileSysDir dirPath (dirname); + + bool exists; + try { - if (isDefault) - return; - throw Exception ("Directory not found: " + dirname); + exists = dirPath.exists(); + } catch (Exception& e) { + throw Exception ("Invalid value for module-dir: " + e.getMessage()); } - if (!fs::is_directory(dirPath)) - { - throw Exception ("Invalid value for module-dir: " + dirname + " is not a directory"); + if (!exists) { + if (isDefault) return; + throw Exception ("Directory not found: " + dirname); } - fs::directory_iterator endItr; - for (fs::directory_iterator itr (dirPath); itr != endItr; ++itr) - { - if (!fs::is_directory(*itr) && isShlibName(itr->string())) - tryShlib (itr->string().data(), true); - } + dirPath.forEachFile(&tryOnlyShlib); } } // namespace qpid diff --git a/qpid/cpp/src/qpid/Modules.h b/qpid/cpp/src/qpid/Modules.h index 159dd156c1..9fb91d60eb 100644 --- a/qpid/cpp/src/qpid/Modules.h +++ b/qpid/cpp/src/qpid/Modules.h @@ -36,7 +36,7 @@ struct ModuleOptions : public qpid::Options { QPID_COMMON_EXTERN ModuleOptions(const std::string& defaultModuleDir); }; -QPID_COMMON_EXTERN void tryShlib(const char* libname, bool noThrow); +QPID_COMMON_EXTERN void tryShlib(const std::string& libname); QPID_COMMON_EXTERN void loadModuleDir (std::string dirname, bool isDefault); } // namespace qpid diff --git a/qpid/cpp/src/qpid/client/LoadPlugins.cpp b/qpid/cpp/src/qpid/client/LoadPlugins.cpp index d76e1d458e..c5d8924014 100644 --- a/qpid/cpp/src/qpid/client/LoadPlugins.cpp +++ b/qpid/cpp/src/qpid/client/LoadPlugins.cpp @@ -48,7 +48,7 @@ struct LoadtimeInitialise { for (vector<string>::iterator iter = moduleOptions.load.begin(); iter != moduleOptions.load.end(); iter++) - qpid::tryShlib (iter->data(), false); + qpid::tryShlib (*iter); if (!moduleOptions.noLoad) { bool isDefault = defaultPath == moduleOptions.loadDir; diff --git a/qpid/cpp/src/qpid/sys/FileSysDir.h b/qpid/cpp/src/qpid/sys/FileSysDir.h index ffe7823f0a..7432fe39c9 100755 --- a/qpid/cpp/src/qpid/sys/FileSysDir.h +++ b/qpid/cpp/src/qpid/sys/FileSysDir.h @@ -54,6 +54,15 @@ class FileSysDir void mkdir(void); + typedef void Callback(const std::string&); + + /** + * Call the Callback function for every regular file in the directory + * + * @param cb Callback function that receives the full path to the file + */ + void forEachFile(Callback cb) const; + std::string getPath () { return dirPath; } }; diff --git a/qpid/cpp/src/qpid/sys/posix/FileSysDir.cpp b/qpid/cpp/src/qpid/sys/posix/FileSysDir.cpp index 22dc487e74..cec580164d 100755 --- a/qpid/cpp/src/qpid/sys/posix/FileSysDir.cpp +++ b/qpid/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 diff --git a/qpid/cpp/src/qpid/sys/windows/FileSysDir.cpp b/qpid/cpp/src/qpid/sys/windows/FileSysDir.cpp index 88f1637d48..88a2e62acc 100644 --- a/qpid/cpp/src/qpid/sys/windows/FileSysDir.cpp +++ b/qpid/cpp/src/qpid/sys/windows/FileSysDir.cpp @@ -25,6 +25,11 @@ #include <direct.h> #include <errno.h> +#include <boost/filesystem/operations.hpp> +#include <boost/filesystem/path.hpp> + +namespace fs=boost::filesystem; + namespace qpid { namespace sys { @@ -50,4 +55,13 @@ void FileSysDir::mkdir(void) throw Exception ("Can't create directory: " + dirPath); } +void FileSysDir::forEachFile(Callback cb) const { + fs::directory_iterator dirP(dirPath); + fs::directory_iterator endItr; + for (fs::directory_iterator itr (dirP); itr != endItr; ++itr) + { + cb(itr->path().string()); + } +} + }} // namespace qpid::sys diff --git a/qpid/cpp/src/qpidd.cpp b/qpid/cpp/src/qpidd.cpp index 920009580c..8e0c52f770 100644 --- a/qpid/cpp/src/qpidd.cpp +++ b/qpid/cpp/src/qpidd.cpp @@ -75,7 +75,7 @@ int run_broker(int argc, char *argv[], bool hidden) for (vector<string>::iterator iter = bootOptions.module.load.begin(); iter != bootOptions.module.load.end(); iter++) - qpid::tryShlib (iter->data(), false); + qpid::tryShlib (*iter); if (!bootOptions.module.noLoad) { bool isDefault = defaultPath == bootOptions.module.loadDir; |