From 5016bca2323f560fb029341bffcaade2fc2b6a24 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 19 Feb 2025 11:57:54 +0100 Subject: [PATCH] Improve handling of input path and map backend --- src/TileGenerator.cpp | 36 ++++++++++++++++++++++++++---------- src/mapper.cpp | 4 ++-- src/util.cpp | 20 +++++++++++++++++++- src/util.h | 4 ++++ 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/TileGenerator.cpp b/src/TileGenerator.cpp index abfd63d..fa7b147 100644 --- a/src/TileGenerator.cpp +++ b/src/TileGenerator.cpp @@ -372,35 +372,51 @@ std::set TileGenerator::getSupportedBackends() void TileGenerator::openDb(const std::string &input_path) { + if (dir_exists(input_path.c_str())) { + // ok + } else if (file_exists(input_path.c_str())) { + throw std::runtime_error("Input path is a file, it should point to the world folder instead"); + } else { + throw std::runtime_error("Input path does not exist"); + } + std::string input = input_path; if (input.back() != PATH_SEPARATOR) input += PATH_SEPARATOR; + std::ifstream ifs(input + "world.mt"); + std::string backend = m_backend; - if (backend.empty()) { - std::ifstream ifs(input + "world.mt"); - if(!ifs.good()) - throw std::runtime_error("Failed to open world.mt"); + if (backend.empty() && !ifs.good()) { + throw std::runtime_error("Failed to open world.mt"); + } else if (backend.empty()) { backend = read_setting_default("backend", ifs, "sqlite3"); - ifs.close(); } - if (backend == "sqlite3") + if (backend == "dummy") { + throw std::runtime_error("This map uses the dummy backend and contains no data"); + } else if (backend == "sqlite3") { m_db = new DBSQLite3(input); #if USE_POSTGRESQL - else if (backend == "postgresql") + } else if (backend == "postgresql") { m_db = new DBPostgreSQL(input); #endif #if USE_LEVELDB - else if (backend == "leveldb") + } else if (backend == "leveldb") { m_db = new DBLevelDB(input); #endif #if USE_REDIS - else if (backend == "redis") + } else if (backend == "redis") { m_db = new DBRedis(input); #endif - else + } else { throw std::runtime_error(std::string("Unknown map backend: ") + backend); + } + + if (!read_setting_default("readonly_backend", ifs, "").empty()) { + std::cerr << "Warning: Map with readonly_backend is not supported. " + "The result may be incomplete." << std::endl; + } // Determine how we're going to traverse the database (heuristic) if (m_exhaustiveSearch == EXH_AUTO) { diff --git a/src/mapper.cpp b/src/mapper.cpp index 3b061c8..79fc2cd 100644 --- a/src/mapper.cpp +++ b/src/mapper.cpp @@ -15,7 +15,7 @@ static void usage() { const std::pair options[] = { {"-i/--input", ""}, - {"-o/--output", ""}, + {"-o/--output", ""}, {"--bgcolor", ""}, {"--scalecolor", ""}, {"--playercolor", ""}, @@ -38,7 +38,7 @@ static void usage() {"--dumpblock", "x,y,z"}, }; const char *top_text = - "minetestmapper -i -o [options]\n" + "minetestmapper -i -o [options]\n" "Generate an overview image of a Luanti map.\n" "\n" "Options:\n"; diff --git a/src/util.cpp b/src/util.cpp index 46a1cab..541a96b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,11 +1,15 @@ #include #include +#include +#include #include "util.h" static std::string trim(const std::string &s) { - auto isspace = [] (char c) -> bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n'; }; + auto isspace = [] (char c) { + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; + }; size_t front = 0; while (isspace(s[front])) @@ -20,6 +24,7 @@ static std::string trim(const std::string &s) std::string read_setting(const std::string &name, std::istream &is) { char linebuf[512]; + is.seekg(0); while (is.good()) { is.getline(linebuf, sizeof(linebuf)); @@ -53,3 +58,16 @@ std::string read_setting_default(const std::string &name, std::istream &is, return def; } } + +bool file_exists(const char *path) +{ + struct stat s{}; + // check for !dir to allow symlinks or such + return stat(path, &s) == 0 && (s.st_mode & S_IFDIR) != S_IFDIR; +} + +bool dir_exists(const char *path) +{ + struct stat s{}; + return stat(path, &s) == 0 && (s.st_mode & S_IFDIR) == S_IFDIR; +} diff --git a/src/util.h b/src/util.h index 5819e8e..76d429e 100644 --- a/src/util.h +++ b/src/util.h @@ -19,3 +19,7 @@ std::string read_setting(const std::string &name, std::istream &is); std::string read_setting_default(const std::string &name, std::istream &is, const std::string &def); + +bool file_exists(const char *path); + +bool dir_exists(const char *path);