Skip to content

Commit

Permalink
Better implementation of directory creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan63 committed Dec 9, 2023
1 parent 23a3fe2 commit 980c37f
Showing 1 changed file with 49 additions and 29 deletions.
78 changes: 49 additions & 29 deletions src/hx/libs/asys/libuv/filesystem/LibuvDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <memory>
#include <filesystem>
#include <functional>
#include <array>
#include "../LibuvUtils.h"

namespace
Expand Down Expand Up @@ -166,54 +167,73 @@ void hx::asys::filesystem::Directory_obj::create(Context ctx, String path, int p
auto libuvCtx = hx::asys::libuv::context(ctx);
auto request = std::make_unique<uv_fs_t>();

if (recursive)
{
// TODO : This isn't really async, eventually move this loop into haxe.
// Maybe TODO : this is not async and would be a ball ache to do so.

hx::EnterGCFreeZone();
auto separator = std::array<char, 2>();

wcstombs(separator.data(), &std::filesystem::path::preferred_separator, 1);

auto filePath = std::filesystem::u8path(path.utf8_str());
auto accumulated = std::filesystem::path();
auto items = path.split(separator.data());
auto accumulated = std::filesystem::path();
auto result = 0;

for (auto&& part : filePath)
for (auto i = 0; i < items->length - 1; i++)
{
if (accumulated.empty())
{
if (accumulated.empty())
{
accumulated = part;
}
else
{
accumulated = accumulated / part;
}
accumulated = items[i].utf8_str();
}
else
{
accumulated = accumulated / items[i].utf8_str();
}

if (!recursive)
{
hx::EnterGCFreeZone();

auto result = uv_fs_mkdir(libuvCtx->uvLoop, request.get(), accumulated.u8string().c_str(), permissions, nullptr);
if (result < 0 && result != EEXIST)
if ((result = uv_fs_stat(libuvCtx->uvLoop, request.get(), accumulated.u8string().c_str(), nullptr)) < 0 && result != UV_EEXIST)
{
hx::ExitGCFreeZone();

cbFailure(hx::asys::libuv::uv_err_to_enum(result));

return;
}

hx::ExitGCFreeZone();
}

hx::ExitGCFreeZone();
hx::EnterGCFreeZone();

cbSuccess();
}
else
{
auto result = uv_fs_mkdir(libuvCtx->uvLoop, request.get(), path.utf8_str(), permissions, hx::asys::libuv::basic_callback);
if (result < 0)
if ((result = uv_fs_mkdir(libuvCtx->uvLoop, request.get(), accumulated.u8string().c_str(), permissions, nullptr)) < 0 && result != UV_EEXIST)
{
hx::ExitGCFreeZone();

cbFailure(hx::asys::libuv::uv_err_to_enum(result));

return;
}
else
{
request->data = new hx::asys::libuv::BaseRequest(cbSuccess, cbFailure);
request.release();
}

hx::ExitGCFreeZone();
}

accumulated = accumulated / items[items->length - 1].utf8_str();

hx::EnterGCFreeZone();

if ((result = uv_fs_mkdir(libuvCtx->uvLoop, request.get(), accumulated.u8string().c_str(), permissions, nullptr)) < 0 && result != UV_EEXIST)
{
hx::ExitGCFreeZone();

cbFailure(hx::asys::libuv::uv_err_to_enum(result));

return;
}

hx::ExitGCFreeZone();

cbSuccess();
}

void hx::asys::filesystem::Directory_obj::move(Context ctx, String oldPath, String newPath, Dynamic cbSuccess, Dynamic cbFailure)
Expand Down

0 comments on commit 980c37f

Please sign in to comment.