mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
LibCore+Userland: Remove File::ensure_parent_directories
We have a much safer and more powerful alternative now, so let's move the few users over.
This commit is contained in:
parent
0fd09b2381
commit
5319e3a03f
Notes:
sideshowbarker
2024-07-17 20:58:35 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/5319e3a03f Pull-request: https://github.com/SerenityOS/serenity/pull/13603 Issue: https://github.com/SerenityOS/serenity/issues/13555 Reviewed-by: https://github.com/BertalanD Reviewed-by: https://github.com/alichraghi ✅ Reviewed-by: https://github.com/awesomekling
@ -12,6 +12,7 @@
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Button.h>
|
||||
@ -200,8 +201,8 @@ void NewProjectDialog::do_create_project()
|
||||
if (result != GUI::MessageBox::ExecResult::ExecYes)
|
||||
return;
|
||||
|
||||
auto created = Core::File::ensure_parent_directories(maybe_project_full_path.value());
|
||||
if (!created) {
|
||||
auto created = Core::Directory::create(maybe_project_full_path.value(), Core::Directory::CreateDirectories::Yes);
|
||||
if (!created.is_error()) {
|
||||
GUI::MessageBox::show_error(this, String::formatted("Could not create directory {}", create_in));
|
||||
return;
|
||||
}
|
||||
|
@ -181,42 +181,6 @@ String File::real_path_for(String const& filename)
|
||||
return real_path;
|
||||
}
|
||||
|
||||
bool File::ensure_parent_directories(String const& path)
|
||||
{
|
||||
char* parent_buffer = strdup(path.characters());
|
||||
ScopeGuard free_buffer = [parent_buffer] { free(parent_buffer); };
|
||||
|
||||
char const* parent = dirname(parent_buffer);
|
||||
|
||||
return ensure_directories(parent);
|
||||
}
|
||||
|
||||
bool File::ensure_directories(String const& path)
|
||||
{
|
||||
VERIFY(path.starts_with("/"));
|
||||
|
||||
int saved_errno = 0;
|
||||
ScopeGuard restore_errno = [&saved_errno] { errno = saved_errno; };
|
||||
|
||||
int rc = mkdir(path.characters(), 0755);
|
||||
saved_errno = errno;
|
||||
|
||||
if (rc == 0 || errno == EEXIST)
|
||||
return true;
|
||||
|
||||
if (errno != ENOENT)
|
||||
return false;
|
||||
|
||||
bool ok = ensure_parent_directories(path);
|
||||
saved_errno = errno;
|
||||
if (!ok)
|
||||
return false;
|
||||
|
||||
rc = mkdir(path.characters(), 0755);
|
||||
saved_errno = errno;
|
||||
return rc == 0;
|
||||
}
|
||||
|
||||
String File::current_working_directory()
|
||||
{
|
||||
char* cwd = getcwd(nullptr, 0);
|
||||
|
@ -37,8 +37,6 @@ public:
|
||||
|
||||
static bool exists(String const& filename);
|
||||
static ErrorOr<size_t> size(String const& filename);
|
||||
static bool ensure_parent_directories(String const& path);
|
||||
static bool ensure_directories(String const& path);
|
||||
static String current_working_directory();
|
||||
static String absolute_path(String const& path);
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/LockFile.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
@ -16,7 +16,7 @@ namespace Core {
|
||||
LockFile::LockFile(char const* filename, Type type)
|
||||
: m_filename(filename)
|
||||
{
|
||||
if (!Core::File::ensure_parent_directories(m_filename))
|
||||
if (Core::Directory::create(LexicalPath(m_filename).parent(), Core::Directory::CreateDirectories::Yes).is_error())
|
||||
return;
|
||||
|
||||
m_fd = open(filename, O_RDONLY | O_CREAT | O_CLOEXEC, 0666);
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/SocketAddress.h>
|
||||
#include <LibCore/System.h>
|
||||
@ -34,8 +35,7 @@ void Service::setup_socket(SocketDescriptor& socket)
|
||||
{
|
||||
VERIFY(socket.fd == -1);
|
||||
|
||||
auto ok = Core::File::ensure_parent_directories(socket.path);
|
||||
VERIFY(ok);
|
||||
MUST(Core::Directory::create(LexicalPath(socket.path).parent(), Core::Directory::CreateDirectories::Yes));
|
||||
|
||||
// Note: we use SOCK_CLOEXEC here to make sure we don't leak every socket to
|
||||
// all the clients. We'll make the one we do need to pass down !CLOEXEC later
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/FilePermissionsMask.h>
|
||||
#include <LibCore/System.h>
|
||||
@ -35,7 +36,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
|
||||
if (create_leading_dest_components) {
|
||||
String destination_dir_absolute = Core::File::absolute_path(destination_dir);
|
||||
Core::File::ensure_directories(destination_dir_absolute);
|
||||
MUST(Core::Directory::create(destination_dir_absolute, Core::Directory::CreateDirectories::Yes));
|
||||
}
|
||||
|
||||
for (auto const& source : sources) {
|
||||
|
@ -4,6 +4,7 @@
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "LibCore/Directory.h"
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/Span.h>
|
||||
@ -136,11 +137,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
Archive::TarFileStream file_stream = tar_stream.file_contents();
|
||||
|
||||
String absolute_path = Core::File::absolute_path(filename);
|
||||
auto parent_path = LexicalPath(absolute_path).parent();
|
||||
|
||||
switch (header.type_flag()) {
|
||||
case Archive::TarFileType::NormalFile:
|
||||
case Archive::TarFileType::AlternateNormalFile: {
|
||||
Core::File::ensure_parent_directories(absolute_path);
|
||||
MUST(Core::Directory::create(parent_path, Core::Directory::CreateDirectories::Yes));
|
||||
|
||||
int fd = TRY(Core::System::open(absolute_path, O_CREAT | O_WRONLY, header.mode()));
|
||||
|
||||
@ -153,13 +155,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
break;
|
||||
}
|
||||
case Archive::TarFileType::SymLink: {
|
||||
Core::File::ensure_parent_directories(absolute_path);
|
||||
MUST(Core::Directory::create(parent_path, Core::Directory::CreateDirectories::Yes));
|
||||
|
||||
TRY(Core::System::symlink(header.link_name(), absolute_path));
|
||||
break;
|
||||
}
|
||||
case Archive::TarFileType::Directory: {
|
||||
Core::File::ensure_parent_directories(absolute_path);
|
||||
MUST(Core::Directory::create(parent_path, Core::Directory::CreateDirectories::Yes));
|
||||
|
||||
auto result_or_error = Core::System::mkdir(absolute_path, header.mode());
|
||||
if (result_or_error.is_error() && result_or_error.error().code() != EEXIST)
|
||||
|
Loading…
Reference in New Issue
Block a user