45 lines
1.0 KiB
C++
45 lines
1.0 KiB
C++
|
|
#include <eqpalg/utility/instance_lock.h>
|
||
|
|
#include <filesystem>
|
||
|
|
#include <mix_cc/type/mix_time.h>
|
||
|
|
#include <fstream>
|
||
|
|
#include <eqpalg/define/stat.h>
|
||
|
|
|
||
|
|
#define STAT_DATA_PATH "/users/dsc/stat_data"
|
||
|
|
|
||
|
|
namespace utility {
|
||
|
|
|
||
|
|
std::string get_lock_path(std::string ruleId) {
|
||
|
|
return std::string(STAT_DATA_PATH) + "/" + ruleId;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool lock_file(std::string ruleId) {
|
||
|
|
auto lck_path = get_lock_path(ruleId) + ".lck";
|
||
|
|
if (is_file_locked(ruleId)) {
|
||
|
|
return false;
|
||
|
|
} else {
|
||
|
|
std::ofstream ofs(lck_path, std::ios::trunc);
|
||
|
|
ofs << mix_cc::mix_time_t(time(0)).to_formatted_time();
|
||
|
|
ofs.close();
|
||
|
|
ofs.clear();
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool unlock_file(std::string ruleId) {
|
||
|
|
auto lck_path = get_lock_path(ruleId) + ".lck";
|
||
|
|
if (is_file_locked(ruleId)) {
|
||
|
|
std::filesystem::remove(lck_path);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool is_file_locked(std::string ruleId) {
|
||
|
|
auto lck_path = get_lock_path(ruleId) + ".lck";
|
||
|
|
if (std::filesystem::exists(lck_path)) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
} // namespace utility
|