mkdir: Replace LibC function calls with LibCore equivalents

This commit is contained in:
Tim Ledbetter 2023-06-20 18:11:31 +01:00 committed by Jelle Raaijmakers
parent f619b61b00
commit 925ea4e854
Notes: sideshowbarker 2024-07-17 06:51:48 +09:00

View File

@ -2,6 +2,7 @@
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021, Xavier Defrang <xavier.defrang@gmail.com>
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -12,17 +13,14 @@
#include <LibCore/FilePermissionsMask.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio cpath rpath"));
bool create_parents = false;
DeprecatedString mode_string;
Vector<DeprecatedString> directories;
StringView mode_string;
Vector<StringView> directories;
Core::ArgsParser args_parser;
args_parser.add_option(create_parents, "Create parent directories if they don't exist", "parents", 'p');
@ -46,8 +44,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
for (auto& directory : directories) {
LexicalPath lexical_path(directory);
if (!create_parents) {
if (mkdir(lexical_path.string().characters(), mask.apply(mask_reference_mode)) < 0) {
perror("mkdir");
auto maybe_error = Core::System::mkdir(lexical_path.string(), mask.apply(mask_reference_mode));
if (maybe_error.is_error()) {
warnln("mkdir: {}", strerror(maybe_error.error().code()));
has_errors = true;
}
continue;
@ -63,12 +62,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto& part = parts[idx];
path_builder.append(part);
auto path = path_builder.to_deprecated_string();
auto path = path_builder.string_view();
struct stat st;
if (stat(path.characters(), &st) < 0) {
if (errno != ENOENT) {
perror("stat");
auto stat_or_error = Core::System::stat(path);
if (stat_or_error.is_error()) {
if (stat_or_error.error().code() != ENOENT) {
warnln("mkdir: {}", strerror(stat_or_error.error().code()));
has_errors = true;
break;
}
@ -76,13 +75,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool is_final = (idx == (num_parts - 1));
mode_t mode = is_final ? mask.apply(mask_reference_mode) : default_mode;
if (mkdir(path.characters(), mode) < 0) {
perror("mkdir");
auto maybe_error = Core::System::mkdir(path, mode);
if (maybe_error.is_error()) {
warnln("mkdir: {}", strerror(maybe_error.error().code()));
has_errors = true;
break;
}
} else {
if (!S_ISDIR(st.st_mode)) {
if (!S_ISDIR(stat_or_error.value().st_mode)) {
warnln("mkdir: cannot create directory '{}': not a directory", path);
has_errors = true;
break;