2023-04-13 16:33:56 +00:00
|
|
|
#include <luna/NumberParsing.h>
|
|
|
|
#include <luna/PathParser.h>
|
2023-04-08 12:47:58 +00:00
|
|
|
#include <os/ArgumentParser.h>
|
2023-04-13 16:33:56 +00:00
|
|
|
#include <os/FileSystem.h>
|
2023-04-08 12:47:58 +00:00
|
|
|
|
2023-04-13 16:33:56 +00:00
|
|
|
Result<void> mkdir_recursively(StringView path, mode_t mode)
|
|
|
|
{
|
|
|
|
begin:
|
|
|
|
auto rc = os::FileSystem::create_directory(path, mode);
|
|
|
|
if (!rc.has_error()) return {};
|
|
|
|
|
|
|
|
if (rc.error() == EEXIST) return {};
|
|
|
|
if (rc.error() == ENOENT)
|
|
|
|
{
|
|
|
|
PathParser parser = TRY(PathParser::create(path.chars()));
|
|
|
|
auto parent = TRY(parser.dirname());
|
|
|
|
|
|
|
|
TRY(mkdir_recursively(parent.view(), mode));
|
|
|
|
|
|
|
|
goto begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc.release_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<int> luna_main(int argc, char** argv)
|
2023-04-08 12:47:58 +00:00
|
|
|
{
|
|
|
|
StringView path;
|
|
|
|
StringView mode_string;
|
2023-04-13 16:33:56 +00:00
|
|
|
bool recursive;
|
2023-04-08 12:47:58 +00:00
|
|
|
|
|
|
|
os::ArgumentParser parser;
|
2023-04-19 17:16:45 +00:00
|
|
|
parser.add_description("Create directories."_sv);
|
2023-04-28 14:33:05 +00:00
|
|
|
parser.add_system_program_info("mkdir"_sv);
|
2023-04-08 12:47:58 +00:00
|
|
|
parser.add_positional_argument(path, "path"_sv, true);
|
|
|
|
parser.add_positional_argument(mode_string, "mode"_sv, "755"_sv);
|
2023-04-19 17:16:45 +00:00
|
|
|
parser.add_switch_argument(recursive, 'p', "parents"_sv,
|
|
|
|
"if parent directories do not exist, create them as well"_sv);
|
2023-04-08 12:47:58 +00:00
|
|
|
parser.parse(argc, argv);
|
|
|
|
|
2023-04-13 16:33:56 +00:00
|
|
|
mode_t mode = (mode_t)parse_unsigned_integer(mode_string.chars(), nullptr, 8);
|
2023-04-08 12:47:58 +00:00
|
|
|
|
2023-04-13 16:33:56 +00:00
|
|
|
if (recursive)
|
2023-04-08 12:47:58 +00:00
|
|
|
{
|
2023-04-13 16:33:56 +00:00
|
|
|
TRY(mkdir_recursively(path, mode));
|
|
|
|
return 0;
|
2023-04-08 12:47:58 +00:00
|
|
|
}
|
2023-04-13 16:33:56 +00:00
|
|
|
|
|
|
|
TRY(os::FileSystem::create_directory(path, mode));
|
|
|
|
|
|
|
|
return 0;
|
2023-04-08 12:47:58 +00:00
|
|
|
}
|