1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 23:17:45 +00: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:
kleines Filmröllchen 2022-04-10 20:51:01 +02:00 committed by Andreas Kling
parent 0fd09b2381
commit 5319e3a03f
7 changed files with 14 additions and 48 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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);