add interface

This commit is contained in:
lnk
2026-04-02 16:23:15 +08:00
parent 2dab1369f3
commit ea176eceaf
6 changed files with 457 additions and 13 deletions

View File

@@ -92,16 +92,35 @@ std::string build_debug_key(const std::string& id, const std::string& level, int
// 递归创建目录
bool create_directory_recursive(const std::string& path) {
size_t pos = 0;
bool create_directory_recursive(const std::string& path)
{
if (path.empty()) return false;
std::string current;
while (pos != std::string::npos) {
pos = path.find('/', pos + 1);
current = path.substr(0, pos);
if (!current.empty() && access(current.c_str(), F_OK) != 0) {
if (mkdir(current.c_str(), 0755) != 0) {
perror(("mkdir failed: " + current).c_str());
return false;
current.reserve(path.size());
for (size_t i = 0; i < path.size(); ++i) {
current += path[i];
// 遇到 '/' 或最后一个字符时创建
if (path[i] == '/' || i == path.size() - 1) {
if (current.empty()) continue;
// 去掉末尾 '/'
std::string dir = current;
if (dir.back() == '/' && dir.size() > 1) {
dir.pop_back();
}
struct stat st;
if (stat(dir.c_str(), &st) != 0) {
if (mkdir(dir.c_str(), 0755) != 0) {
// 如果已经存在(并发场景),忽略
if (errno != EEXIST) {
perror(("mkdir failed: " + dir).c_str());
return false;
}
}
}
}
}