116 lines
3.5 KiB
C++
116 lines
3.5 KiB
C++
#include "vslam/node_constants.h"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <cerrno>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <ctime>
|
|
|
|
namespace vslam {
|
|
namespace {
|
|
|
|
// Tra ve $name neu co va khong rong, khong thi `fallback`.
|
|
std::string EnvOr(const char* name, const char* fallback) {
|
|
const char* v = std::getenv(name);
|
|
if (v != nullptr && v[0] != '\0') return std::string(v);
|
|
return std::string(fallback);
|
|
}
|
|
|
|
std::string Join(const std::string& dir, const std::string& rel) {
|
|
if (dir.empty()) return rel;
|
|
if (dir.back() == '/') return dir + 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); }
|
|
|
|
std::string ResourceDir() { return EnvOr(kResourceDirEnv, kResourceDirectory); }
|
|
|
|
std::string ConfigPath(const std::string& file_name) {
|
|
return Join(ConfigDir(), file_name);
|
|
}
|
|
|
|
std::string ResourcePath(const std::string& rel) { return Join(ResourceDir(), rel); }
|
|
|
|
std::string MapDir() { return Join(ResourceDir(), "maps"); }
|
|
|
|
std::string RecordDir() { return Join(ResourceDir(), "records"); }
|
|
|
|
std::string CorpusDir() {
|
|
const char* v = std::getenv(kCorpusDirEnv);
|
|
if (v != nullptr && v[0] != '\0') return std::string(v);
|
|
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
|