update
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
#include "vslam/node_constants.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
|
||||
namespace vslam {
|
||||
namespace {
|
||||
@@ -18,6 +23,19 @@ std::string Join(const std::string& dir, const std::string& rel) {
|
||||
return dir + "/" + rel;
|
||||
}
|
||||
|
||||
constexpr char kStampToken[] = "{stamp}";
|
||||
|
||||
// Dau thoi gian DIA PHUONG. Sap xep duoc theo thu tu chu cai, va nguoi van
|
||||
// hanh doc no bang `ls` tren chinh con robot -- do la ly do khong dung UTC.
|
||||
std::string NowStamp() {
|
||||
const std::time_t t = std::time(nullptr);
|
||||
std::tm tm {};
|
||||
localtime_r(&t, &tm);
|
||||
char buf[32];
|
||||
std::strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", &tm);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string ConfigDir() { return EnvOr(kConfigDirEnv, kConfigurationDirectory); }
|
||||
@@ -40,4 +58,58 @@ std::string CorpusDir() {
|
||||
return MapDir();
|
||||
}
|
||||
|
||||
std::string ResolveMapPath(const std::string& raw) {
|
||||
if (raw.empty()) return raw; // DB trong RAM -- giu nguyen
|
||||
|
||||
std::string p = raw;
|
||||
if (p.find(kStampToken) != std::string::npos) {
|
||||
// MOT dau thoi gian cho ca chuoi: hai lan xuat hien phai ra cung gia tri,
|
||||
// khong thi ten file va ten thu muc lech nhau mot giay.
|
||||
const std::string stamp = NowStamp();
|
||||
std::string out;
|
||||
out.reserve(p.size() + 16);
|
||||
size_t i = 0;
|
||||
for (;;) {
|
||||
const size_t k = p.find(kStampToken, i);
|
||||
if (k == std::string::npos) {
|
||||
out.append(p, i, std::string::npos);
|
||||
break;
|
||||
}
|
||||
out.append(p, i, k - i);
|
||||
out += stamp;
|
||||
i = k + sizeof(kStampToken) - 1;
|
||||
}
|
||||
p.swap(out);
|
||||
}
|
||||
|
||||
// Cung khuon mau voi dcf_path o app_config.cc: tuyet doi thi dung nguyen,
|
||||
// tuong doi thi giai theo thu muc cua chinh loai file do.
|
||||
if (p.front() == '/') return p;
|
||||
return Join(MapDir(), p);
|
||||
}
|
||||
|
||||
StatusResponse EnsureParentDir(const std::string& path) {
|
||||
const size_t slash = path.find_last_of('/');
|
||||
// Khong co '/' = file o thu muc hien tai; slash == 0 = ngay duoi root.
|
||||
if (slash == std::string::npos || slash == 0) return StatusResponse::Ok();
|
||||
|
||||
const std::string dir = path.substr(0, slash);
|
||||
for (size_t i = 1; i <= dir.size(); ++i) {
|
||||
if (i != dir.size() && dir[i] != '/') continue;
|
||||
const std::string part = dir.substr(0, i);
|
||||
if (::mkdir(part.c_str(), 0775) == 0) continue;
|
||||
if (errno == EEXIST) {
|
||||
// EEXIST chua phai tin tot: co the la mot FILE thuong trung ten.
|
||||
struct stat st {};
|
||||
if (::stat(part.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) continue;
|
||||
return StatusResponse::Error(StatusCode::kInvalidArgument,
|
||||
"khong phai thu muc: " + part);
|
||||
}
|
||||
return StatusResponse::Error(
|
||||
StatusCode::kUnavailable,
|
||||
"khong tao duoc thu muc " + part + ": " + std::strerror(errno));
|
||||
}
|
||||
return StatusResponse::Ok();
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
Reference in New Issue
Block a user