1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:27:45 +00:00

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

View file

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