| 155 | } |
| 156 | |
| 157 | void export_tree(const lmpfs::vfs::NodePtr& n, const std::filesystem::path& out) { |
| 158 | namespace fs = std::filesystem; |
| 159 | if (n->is_dir()) { |
| 160 | fs::create_directories(out); |
| 161 | for (auto& e : n->list()) export_tree(e.node, out / e.name); |
| 162 | } else { |
| 163 | std::ofstream f(out, std::ios::binary); |
| 164 | if (!f) |
| 165 | lmpfs::throw_error("export: cannot create '{}'", out.string()); |
| 166 | const std::size_t kBuf = 64 * 1024; |
| 167 | std::vector<char> buf(kBuf); |
| 168 | std::uint64_t off = 0; |
| 169 | while (true) { |
| 170 | std::size_t got = n->read(off, buf.data(), buf.size()); |
| 171 | if (got == 0) break; |
| 172 | f.write(buf.data(), got); |
| 173 | if (!f) |
| 174 | lmpfs::throw_error("export: write failed for '{}'", out.string()); |
| 175 | off += got; |
| 176 | if (got < buf.size()) break; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void print_tree(const lmpfs::vfs::NodePtr& n, std::string indent = "") { |
| 182 | fmt::print("{}{}{}\n", indent, n->is_dir() ? "[DIR] " : " ", n->name()); |