Skip to content

Commit

Permalink
[api] Added pipy.unmount()
Browse files Browse the repository at this point in the history
  • Loading branch information
pajama-coder committed Jul 26, 2024
1 parent 40ec6b3 commit f9e7969
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/api/pipy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,16 @@ template<> void ClassDef<Pipy>::init() {
}
});

method("unmount", [](Context &ctx, Object*, Value &ret) {
std::string path;
if (!ctx.arguments(1, &path)) return;
try {
Codebase::current()->mount(path, nullptr);
} catch (std::runtime_error &err) {
ctx.error(err);
}
});

method("load", [](Context &ctx, Object*, Value &ret) {
std::string filename;
if (!ctx.arguments(1, &filename)) return;
Expand Down
19 changes: 13 additions & 6 deletions src/codebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,22 @@ CodebaseFromRoot::~CodebaseFromRoot() {

void CodebaseFromRoot::mount(const std::string &name, Codebase *codebase) {
if (name.find('/') != std::string::npos) throw std::runtime_error("invalid mount name");
if (get(name)) throw std::runtime_error("mount path already exists");
if (list(name).size() > 0) throw std::runtime_error("mount path already exists");
std::lock_guard<std::mutex> lock(m_mutex);
for (const auto &p : m_mounts) {
if (p.first == name) {
if (codebase) {
if (get(name)) throw std::runtime_error("mount path already exists");
if (list(name).size() > 0) throw std::runtime_error("mount path already exists");
std::lock_guard<std::mutex> lock(m_mutex);
if (m_mounts.find(name) != m_mounts.end()) {
throw std::runtime_error("mount path already exists");
}
m_mounts[name] = codebase;
} else {
std::lock_guard<std::mutex> lock(m_mutex);
auto i = m_mounts.find(name);
if (i != m_mounts.end()) {
delete i->second;
m_mounts.erase(i);
}
}
m_mounts[name] = codebase;
}

auto CodebaseFromRoot::list(const std::string &path) -> std::list<std::string> {
Expand Down

0 comments on commit f9e7969

Please sign in to comment.