common/FileUtil.cpp | 39 +++++++++++++++++++++++++++++++++++++++ common/FileUtil.hpp | 12 +----------- 2 files changed, 40 insertions(+), 11 deletions(-)
New commits: commit 0d4d506ea39d43beeccff54935b72485cb3fd545 Author: Ashod Nakashian <[email protected]> Date: Mon Jan 30 00:04:10 2017 -0500 wsd: faster jail directory cleanup Around 1.5x faster than Poco, which first enumerates files into a container, then iterates over them and stats before unlinking. Here we enumerate and unlink in a single pass. Change-Id: I79d1c0f1b5ec557ccc4f0e2ec7a0609051d8d212 Reviewed-on: https://gerrit.libreoffice.org/33680 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/common/FileUtil.cpp b/common/FileUtil.cpp index 8973d72..078379b 100644 --- a/common/FileUtil.cpp +++ b/common/FileUtil.cpp @@ -10,6 +10,7 @@ #include "FileUtil.hpp" #include "config.h" +#include <ftw.h> #include <sys/stat.h> #include <sys/vfs.h> @@ -114,6 +115,44 @@ namespace FileUtil } } + static int nftw_cb(const char *fpath, const struct stat*, int type, struct FTW*) + { + if (type == FTW_DP) + { + rmdir(fpath); + } + else if (type == FTW_F || type == FTW_SL) + { + unlink(fpath); + } + + // Always continue even when things go wrong. + return 0; + } + + void removeFile(const std::string& path, const bool recursive) + { + try + { + struct stat sb; + if (!recursive || stat(path.c_str(), &sb) == -1 || S_ISREG(sb.st_mode)) + { + // Non-recursive directories, and files. + Poco::File(path).remove(recursive); + } + else + { + // Directories only. + nftw(path.c_str(), nftw_cb, 128, FTW_DEPTH | FTW_PHYS); + } + } + catch (const std::exception&) + { + // Already removed or we don't care about failures. + } + } + + } // namespace FileUtil namespace diff --git a/common/FileUtil.hpp b/common/FileUtil.hpp index c8e7a3f..0a172b9 100644 --- a/common/FileUtil.hpp +++ b/common/FileUtil.hpp @@ -63,17 +63,7 @@ namespace FileUtil /// Supresses exception when the file is already removed. /// This can happen when there is a race (unavoidable) or when /// we don't care to check before we remove (when no race exists). - inline void removeFile(const std::string& path, const bool recursive = false) - { - try - { - Poco::File(path).remove(recursive); - } - catch (const std::exception&) - { - // Already removed or we don't care about failures. - } - } + void removeFile(const std::string& path, const bool recursive = false); inline void removeFile(const Poco::Path& path, const bool recursive = false) { _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
