mkdir: Use os::FileSystem and add -p flag
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
fb3430c56a
commit
1733fc810d
@ -1,23 +1,49 @@
|
||||
#include <luna/NumberParsing.h>
|
||||
#include <luna/PathParser.h>
|
||||
#include <os/ArgumentParser.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <os/FileSystem.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
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)
|
||||
{
|
||||
StringView path;
|
||||
StringView mode_string;
|
||||
bool recursive;
|
||||
|
||||
os::ArgumentParser parser;
|
||||
parser.add_positional_argument(path, "path"_sv, true);
|
||||
parser.add_positional_argument(mode_string, "mode"_sv, "755"_sv);
|
||||
parser.add_switch_argument(recursive, 'p', "parents"_sv);
|
||||
parser.parse(argc, argv);
|
||||
|
||||
mode_t mode = (mode_t)strtoul(mode_string.chars(), NULL, 8);
|
||||
mode_t mode = (mode_t)parse_unsigned_integer(mode_string.chars(), nullptr, 8);
|
||||
|
||||
if (mkdir(path.chars(), mode) < 0)
|
||||
if (recursive)
|
||||
{
|
||||
perror("mkdir");
|
||||
return 1;
|
||||
TRY(mkdir_recursively(path, mode));
|
||||
return 0;
|
||||
}
|
||||
|
||||
TRY(os::FileSystem::create_directory(path, mode));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user