Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: less nest in filesystem iteration When CleanOldLogFiles::Run #801

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/rime/lever/deployment_tasks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -641,24 +641,32 @@ bool CleanOldLogFiles::Run(Deployer* deployer) {
DLOG(INFO) << "scanning " << dirs.size() << " temp directory for log files.";

int removed = 0;
for (auto i = dirs.cbegin(); i != dirs.cend(); ++i) {
DLOG(INFO) << "temp directory: " << *i;
for (fs::directory_iterator j(*i), end; j != end; ++j) {
fs::path entry(j->path());
string file_name(entry.filename().string());
for (const auto& dir : dirs) {
vector<fs::path> files;
DLOG(INFO) << "temp directory: " << dir;
// preparing files
for (const auto& entry : fs::directory_iterator(dir)) {
const string& file_name(entry.path().filename().string());
if (entry.is_regular_file() && !entry.is_symlink() &&
boost::starts_with(file_name, "rime.") &&
!boost::contains(file_name, today)) {
files.push_back(entry.path());
}
}
// remove files
for (const auto& file : files) {
try {
if (fs::is_regular_file(entry) && !fs::is_symlink(entry) &&
boost::starts_with(file_name, "rime.") &&
!boost::contains(file_name, today)) {
DLOG(INFO) << "removing log file '" << file_name << "'.";
fs::remove(entry);
++removed;
}
DLOG(INFO) << "removing log file '" << file.filename() << "'.";
// ensure write permission
fs::permissions(file, fs::perms::owner_write);
fs::remove(file);
++removed;
} catch (const fs::filesystem_error& ex) {
LOG(ERROR) << ex.what();
success = false;
}
}
vector<fs::path>().swap(files);
fxliang marked this conversation as resolved.
Show resolved Hide resolved
}
if (removed != 0) {
LOG(INFO) << "cleaned " << removed << " log files.";
Expand Down
Loading