Skip to content

Commit 9ec1711

Browse files
authored
perf: less nest in filesystem iteration When CleanOldLogFiles::Run (#801)
1 parent 72f833d commit 9ec1711

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/rime/lever/deployment_tasks.cc

+19-12
Original file line numberDiff line numberDiff line change
@@ -641,19 +641,26 @@ bool CleanOldLogFiles::Run(Deployer* deployer) {
641641
DLOG(INFO) << "scanning " << dirs.size() << " temp directory for log files.";
642642

643643
int removed = 0;
644-
for (auto i = dirs.cbegin(); i != dirs.cend(); ++i) {
645-
DLOG(INFO) << "temp directory: " << *i;
646-
for (fs::directory_iterator j(*i), end; j != end; ++j) {
647-
fs::path entry(j->path());
648-
string file_name(entry.filename().string());
644+
for (const auto& dir : dirs) {
645+
vector<fs::path> files;
646+
DLOG(INFO) << "temp directory: " << dir;
647+
// preparing files
648+
for (const auto& entry : fs::directory_iterator(dir)) {
649+
const string& file_name(entry.path().filename().string());
650+
if (entry.is_regular_file() && !entry.is_symlink() &&
651+
boost::starts_with(file_name, "rime.") &&
652+
!boost::contains(file_name, today)) {
653+
files.push_back(entry.path());
654+
}
655+
}
656+
// remove files
657+
for (const auto& file : files) {
649658
try {
650-
if (fs::is_regular_file(entry) && !fs::is_symlink(entry) &&
651-
boost::starts_with(file_name, "rime.") &&
652-
!boost::contains(file_name, today)) {
653-
DLOG(INFO) << "removing log file '" << file_name << "'.";
654-
fs::remove(entry);
655-
++removed;
656-
}
659+
DLOG(INFO) << "removing log file '" << file.filename() << "'.";
660+
// ensure write permission
661+
fs::permissions(file, fs::perms::owner_write);
662+
fs::remove(file);
663+
++removed;
657664
} catch (const fs::filesystem_error& ex) {
658665
LOG(ERROR) << ex.what();
659666
success = false;

0 commit comments

Comments
 (0)