mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05: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
7 changed files with 14 additions and 48 deletions
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <LibCore/Directory.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibGUI/BoxLayout.h>
|
#include <LibGUI/BoxLayout.h>
|
||||||
#include <LibGUI/Button.h>
|
#include <LibGUI/Button.h>
|
||||||
|
@ -200,8 +201,8 @@ void NewProjectDialog::do_create_project()
|
||||||
if (result != GUI::MessageBox::ExecResult::ExecYes)
|
if (result != GUI::MessageBox::ExecResult::ExecYes)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto created = Core::File::ensure_parent_directories(maybe_project_full_path.value());
|
auto created = Core::Directory::create(maybe_project_full_path.value(), Core::Directory::CreateDirectories::Yes);
|
||||||
if (!created) {
|
if (!created.is_error()) {
|
||||||
GUI::MessageBox::show_error(this, String::formatted("Could not create directory {}", create_in));
|
GUI::MessageBox::show_error(this, String::formatted("Could not create directory {}", create_in));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -181,42 +181,6 @@ String File::real_path_for(String const& filename)
|
||||||
return real_path;
|
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()
|
String File::current_working_directory()
|
||||||
{
|
{
|
||||||
char* cwd = getcwd(nullptr, 0);
|
char* cwd = getcwd(nullptr, 0);
|
||||||
|
|
|
@ -37,8 +37,6 @@ public:
|
||||||
|
|
||||||
static bool exists(String const& filename);
|
static bool exists(String const& filename);
|
||||||
static ErrorOr<size_t> size(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 current_working_directory();
|
||||||
static String absolute_path(String const& path);
|
static String absolute_path(String const& path);
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/Directory.h>
|
||||||
#include <LibCore/LockFile.h>
|
#include <LibCore/LockFile.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -16,7 +16,7 @@ namespace Core {
|
||||||
LockFile::LockFile(char const* filename, Type type)
|
LockFile::LockFile(char const* filename, Type type)
|
||||||
: m_filename(filename)
|
: 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;
|
return;
|
||||||
|
|
||||||
m_fd = open(filename, O_RDONLY | O_CREAT | O_CLOEXEC, 0666);
|
m_fd = open(filename, O_RDONLY | O_CREAT | O_CLOEXEC, 0666);
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibCore/ConfigFile.h>
|
#include <LibCore/ConfigFile.h>
|
||||||
|
#include <LibCore/Directory.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/SocketAddress.h>
|
#include <LibCore/SocketAddress.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
|
@ -34,8 +35,7 @@ void Service::setup_socket(SocketDescriptor& socket)
|
||||||
{
|
{
|
||||||
VERIFY(socket.fd == -1);
|
VERIFY(socket.fd == -1);
|
||||||
|
|
||||||
auto ok = Core::File::ensure_parent_directories(socket.path);
|
MUST(Core::Directory::create(LexicalPath(socket.path).parent(), Core::Directory::CreateDirectories::Yes));
|
||||||
VERIFY(ok);
|
|
||||||
|
|
||||||
// Note: we use SOCK_CLOEXEC here to make sure we don't leak every socket to
|
// 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
|
// 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/LexicalPath.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/Directory.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/FilePermissionsMask.h>
|
#include <LibCore/FilePermissionsMask.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
|
@ -35,7 +36,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
if (create_leading_dest_components) {
|
if (create_leading_dest_components) {
|
||||||
String destination_dir_absolute = Core::File::absolute_path(destination_dir);
|
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) {
|
for (auto const& source : sources) {
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "LibCore/Directory.h"
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/Span.h>
|
#include <AK/Span.h>
|
||||||
|
@ -136,11 +137,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
Archive::TarFileStream file_stream = tar_stream.file_contents();
|
Archive::TarFileStream file_stream = tar_stream.file_contents();
|
||||||
|
|
||||||
String absolute_path = Core::File::absolute_path(filename);
|
String absolute_path = Core::File::absolute_path(filename);
|
||||||
|
auto parent_path = LexicalPath(absolute_path).parent();
|
||||||
|
|
||||||
switch (header.type_flag()) {
|
switch (header.type_flag()) {
|
||||||
case Archive::TarFileType::NormalFile:
|
case Archive::TarFileType::NormalFile:
|
||||||
case Archive::TarFileType::AlternateNormalFile: {
|
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()));
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case Archive::TarFileType::SymLink: {
|
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));
|
TRY(Core::System::symlink(header.link_name(), absolute_path));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Archive::TarFileType::Directory: {
|
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());
|
auto result_or_error = Core::System::mkdir(absolute_path, header.mode());
|
||||||
if (result_or_error.is_error() && result_or_error.error().code() != EEXIST)
|
if (result_or_error.is_error() && result_or_error.error().code() != EEXIST)
|
||||||
|
|
Loading…
Add table
Reference in a new issue