mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-26 07:04:37 +02:00
Make sort order for Utility::{Glob,GlobRecursive} deterministic.
Fixes #5854
This commit is contained in:
parent
95d9b6f221
commit
46165dbccf
@ -450,6 +450,8 @@ String Utility::NewUniqueID(void)
|
|||||||
*/
|
*/
|
||||||
bool Utility::Glob(const String& pathSpec, const boost::function<void (const String&)>& callback, int type)
|
bool Utility::Glob(const String& pathSpec, const boost::function<void (const String&)>& callback, int type)
|
||||||
{
|
{
|
||||||
|
std::vector<String> files, dirs;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
HANDLE handle;
|
HANDLE handle;
|
||||||
WIN32_FIND_DATA wfd;
|
WIN32_FIND_DATA wfd;
|
||||||
@ -472,13 +474,12 @@ bool Utility::Glob(const String& pathSpec, const boost::function<void (const Str
|
|||||||
if (strcmp(wfd.cFileName, ".") == 0 || strcmp(wfd.cFileName, "..") == 0)
|
if (strcmp(wfd.cFileName, ".") == 0 || strcmp(wfd.cFileName, "..") == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(type & GlobDirectory))
|
String path = DirName(pathSpec) + "/" + wfd.cFileName;
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(type & GlobFile))
|
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (type & GlobDirectory))
|
||||||
continue;
|
dirs.push_back(path)
|
||||||
|
else if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (type & GlobFile)
|
||||||
callback(DirName(pathSpec) + "/" + wfd.cFileName);
|
files.push_back(path);
|
||||||
} while (FindNextFile(handle, &wfd));
|
} while (FindNextFile(handle, &wfd));
|
||||||
|
|
||||||
if (!FindClose(handle)) {
|
if (!FindClose(handle)) {
|
||||||
@ -486,8 +487,6 @@ bool Utility::Glob(const String& pathSpec, const boost::function<void (const Str
|
|||||||
<< boost::errinfo_api_function("FindClose")
|
<< boost::errinfo_api_function("FindClose")
|
||||||
<< errinfo_win32_error(GetLastError()));
|
<< errinfo_win32_error(GetLastError()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
#else /* _WIN32 */
|
#else /* _WIN32 */
|
||||||
glob_t gr;
|
glob_t gr;
|
||||||
|
|
||||||
@ -519,19 +518,26 @@ bool Utility::Glob(const String& pathSpec, const boost::function<void (const Str
|
|||||||
if (!S_ISDIR(statbuf.st_mode) && !S_ISREG(statbuf.st_mode))
|
if (!S_ISDIR(statbuf.st_mode) && !S_ISREG(statbuf.st_mode))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (S_ISDIR(statbuf.st_mode) && !(type & GlobDirectory))
|
if (S_ISDIR(statbuf.st_mode) && (type & GlobDirectory))
|
||||||
continue;
|
dirs.push_back(*gp);
|
||||||
|
else if (!S_ISDIR(statbuf.st_mode) && (type & GlobFile))
|
||||||
if (!S_ISDIR(statbuf.st_mode) && !(type & GlobFile))
|
files.push_back(*gp);
|
||||||
continue;
|
|
||||||
|
|
||||||
callback(*gp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
globfree(&gr);
|
globfree(&gr);
|
||||||
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
|
std::sort(files.begin(), files.end());
|
||||||
|
BOOST_FOREACH(const String& cpath, files) {
|
||||||
|
callback(cpath);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(dirs.begin(), dirs.end());
|
||||||
|
BOOST_FOREACH(const String& cpath, dirs) {
|
||||||
|
callback(cpath);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
#endif /* _WIN32 */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -546,6 +552,8 @@ bool Utility::Glob(const String& pathSpec, const boost::function<void (const Str
|
|||||||
*/
|
*/
|
||||||
bool Utility::GlobRecursive(const String& path, const String& pattern, const boost::function<void (const String&)>& callback, int type)
|
bool Utility::GlobRecursive(const String& path, const String& pattern, const boost::function<void (const String&)>& callback, int type)
|
||||||
{
|
{
|
||||||
|
std::vector<String> files, dirs, alldirs;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
HANDLE handle;
|
HANDLE handle;
|
||||||
WIN32_FIND_DATA wfd;
|
WIN32_FIND_DATA wfd;
|
||||||
@ -573,18 +581,16 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const boo
|
|||||||
String cpath = path + "/" + wfd.cFileName;
|
String cpath = path + "/" + wfd.cFileName;
|
||||||
|
|
||||||
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||||
GlobRecursive(cpath, pattern, callback, type);
|
alldirs.push_back(cpath);
|
||||||
|
|
||||||
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(type & GlobDirectory))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(type & GlobFile))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!Utility::Match(pattern, wfd.cFileName))
|
if (!Utility::Match(pattern, wfd.cFileName))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
callback(cpath);
|
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (type & GlobFile))
|
||||||
|
files.push_back(cpath);
|
||||||
|
|
||||||
|
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (type & GlobDirectory))
|
||||||
|
dirs.push_back(cpath);
|
||||||
} while (FindNextFile(handle, &wfd));
|
} while (FindNextFile(handle, &wfd));
|
||||||
|
|
||||||
if (!FindClose(handle)) {
|
if (!FindClose(handle)) {
|
||||||
@ -625,45 +631,41 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const boo
|
|||||||
|
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
|
|
||||||
if (lstat(cpath.CStr(), &statbuf) < 0) {
|
if (lstat(cpath.CStr(), &statbuf) < 0)
|
||||||
closedir(dirp);
|
continue;
|
||||||
|
|
||||||
BOOST_THROW_EXCEPTION(posix_error()
|
|
||||||
<< boost::errinfo_api_function("lstat")
|
|
||||||
<< boost::errinfo_errno(errno)
|
|
||||||
<< boost::errinfo_file_name(cpath));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (S_ISDIR(statbuf.st_mode))
|
if (S_ISDIR(statbuf.st_mode))
|
||||||
GlobRecursive(cpath, pattern, callback, type);
|
alldirs.push_back(cpath);
|
||||||
|
|
||||||
if (stat(cpath.CStr(), &statbuf) < 0) {
|
|
||||||
closedir(dirp);
|
|
||||||
|
|
||||||
BOOST_THROW_EXCEPTION(posix_error()
|
|
||||||
<< boost::errinfo_api_function("stat")
|
|
||||||
<< boost::errinfo_errno(errno)
|
|
||||||
<< boost::errinfo_file_name(cpath));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!S_ISDIR(statbuf.st_mode) && !S_ISREG(statbuf.st_mode))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (S_ISDIR(statbuf.st_mode) && !(type & GlobDirectory))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!S_ISDIR(statbuf.st_mode) && !(type & GlobFile))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!Utility::Match(pattern, ent.d_name))
|
if (!Utility::Match(pattern, ent.d_name))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
callback(cpath);
|
if (S_ISDIR(statbuf.st_mode) && (type & GlobDirectory))
|
||||||
|
dirs.push_back(cpath);
|
||||||
|
|
||||||
|
if (!S_ISDIR(statbuf.st_mode) && (type & GlobFile))
|
||||||
|
files.push_back(cpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dirp);
|
closedir(dirp);
|
||||||
|
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
|
std::sort(files.begin(), files.end());
|
||||||
|
BOOST_FOREACH(const String& cpath, files) {
|
||||||
|
callback(cpath);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(dirs.begin(), dirs.end());
|
||||||
|
BOOST_FOREACH(const String& cpath, dirs) {
|
||||||
|
callback(cpath);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(alldirs.begin(), alldirs.end());
|
||||||
|
BOOST_FOREACH(const String& cpath, alldirs) {
|
||||||
|
GlobRecursive(cpath, pattern, callback, type);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user