From 1ec8f89599b40f9364461b1c3a089595e12cadf2 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 4 Feb 2025 14:10:23 +0300 Subject: [PATCH] add io::Device::mkdir --- src/io/devices/Device.hpp | 11 ++++++++--- src/io/devices/StdfsDevice.cpp | 17 +++++++++++++++-- src/io/devices/StdfsDevice.hpp | 3 ++- src/io/io.cpp | 12 ++++++++++-- src/io/io.hpp | 1 + src/logic/scripting/lua/libs/libfile.cpp | 2 +- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/io/devices/Device.hpp b/src/io/devices/Device.hpp index ee40d2f9..bb000602 100644 --- a/src/io/devices/Device.hpp +++ b/src/io/devices/Device.hpp @@ -20,7 +20,8 @@ namespace io { virtual bool exists(std::string_view path) = 0; virtual bool isdir(std::string_view path) = 0; virtual bool isfile(std::string_view path) = 0; - virtual void mkdirs(std::string_view path) = 0; + virtual bool mkdir(std::string_view path) = 0; + virtual bool mkdirs(std::string_view path) = 0; virtual bool remove(std::string_view path) = 0; virtual uint64_t removeAll(std::string_view path) = 0; virtual std::unique_ptr list(std::string_view path) = 0; @@ -62,8 +63,12 @@ namespace io { return parent->isfile((root / path).pathPart()); } - void mkdirs(std::string_view path) override { - parent->mkdirs((root / path).pathPart()); + bool mkdir(std::string_view path) override { + return parent->mkdir((root / path).pathPart()); + } + + bool mkdirs(std::string_view path) override { + return parent->mkdirs((root / path).pathPart()); } bool remove(std::string_view path) override { diff --git a/src/io/devices/StdfsDevice.cpp b/src/io/devices/StdfsDevice.cpp index 3951dc32..f9598e6f 100644 --- a/src/io/devices/StdfsDevice.cpp +++ b/src/io/devices/StdfsDevice.cpp @@ -64,15 +64,28 @@ bool StdfsDevice::isfile(std::string_view path) { return fs::is_regular_file(resolved); } -void StdfsDevice::mkdirs(std::string_view path) { +bool StdfsDevice::mkdir(std::string_view path) { auto resolved = resolve(path); std::error_code ec; - fs::create_directories(resolved, ec); + bool created = fs::create_directory(resolved, ec); if (ec) { logger.error() << "error creating directory " << resolved << ": " << ec.message(); } + return created; +} + +bool StdfsDevice::mkdirs(std::string_view path) { + auto resolved = resolve(path); + + std::error_code ec; + bool created = fs::create_directories(resolved, ec); + if (ec) { + logger.error() << "error creating directories " << resolved << ": " + << ec.message(); + } + return created; } bool StdfsDevice::remove(std::string_view path) { diff --git a/src/io/devices/StdfsDevice.hpp b/src/io/devices/StdfsDevice.hpp index f518af93..819dfe4f 100644 --- a/src/io/devices/StdfsDevice.hpp +++ b/src/io/devices/StdfsDevice.hpp @@ -12,7 +12,8 @@ namespace io { bool exists(std::string_view path) override; bool isdir(std::string_view path) override; bool isfile(std::string_view path) override; - void mkdirs(std::string_view path) override; + bool mkdir(std::string_view path) override; + bool mkdirs(std::string_view path) override; bool remove(std::string_view path) override; uint64_t removeAll(std::string_view path) override; std::unique_ptr list(std::string_view path) override; diff --git a/src/io/io.cpp b/src/io/io.cpp index 2cfa75d9..a9ee86de 100644 --- a/src/io/io.cpp +++ b/src/io/io.cpp @@ -213,13 +213,21 @@ bool io::exists(const io::path& file) { return device->exists(file.pathPart()); } +bool io::create_directory(const io::path& file) { + auto& device = io::require_device(file.entryPoint()); + if (device.isdir(file.pathPart())) { + return false; + } + return device.mkdirs(file.pathPart()); +} + + bool io::create_directories(const io::path& file) { auto& device = io::require_device(file.entryPoint()); if (device.isdir(file.pathPart())) { return false; } - device.mkdirs(file.pathPart()); - return true; + return device.mkdirs(file.pathPart()); } bool io::remove(const io::path& file) { diff --git a/src/io/io.hpp b/src/io/io.hpp index fdccc6ac..14541eea 100644 --- a/src/io/io.hpp +++ b/src/io/io.hpp @@ -151,6 +151,7 @@ namespace io { bool is_regular_file(const io::path& file); bool is_directory(const io::path& file); bool exists(const io::path& file); + bool create_directory(const io::path& file); bool create_directories(const io::path& file); bool remove(const io::path& file); uint64_t remove_all(const io::path& file); diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index 175d3171..f95c0679 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -98,7 +98,7 @@ static int l_length(lua::State* L) { static int l_mkdir(lua::State* L) { io::path path = lua::require_string(L, 1); - return lua::pushboolean(L, io::create_directories(path)); // FIXME + return lua::pushboolean(L, io::create_directory(path)); } static int l_mkdirs(lua::State* L) {