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 <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
This commit is contained in:
Ashod Nakashian
2017-01-30 00:04:10 -05:00
committed by Ashod Nakashian
parent 65be8a3e56
commit 0d4d506ea3
2 changed files with 40 additions and 11 deletions

View File

@ -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

View File

@ -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)
{