mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:17:35 +00:00
LibCore: Add File::ensure_directories()
This commit is contained in:
parent
f44cd168b0
commit
62f0fa818d
2 changed files with 14 additions and 8 deletions
|
@ -183,17 +183,22 @@ String File::real_path_for(String const& filename)
|
||||||
|
|
||||||
bool File::ensure_parent_directories(String const& path)
|
bool File::ensure_parent_directories(String const& path)
|
||||||
{
|
{
|
||||||
VERIFY(path.starts_with("/"));
|
|
||||||
|
|
||||||
int saved_errno = 0;
|
|
||||||
ScopeGuard restore_errno = [&saved_errno] { errno = saved_errno; };
|
|
||||||
|
|
||||||
char* parent_buffer = strdup(path.characters());
|
char* parent_buffer = strdup(path.characters());
|
||||||
ScopeGuard free_buffer = [parent_buffer] { free(parent_buffer); };
|
ScopeGuard free_buffer = [parent_buffer] { free(parent_buffer); };
|
||||||
|
|
||||||
char const* parent = dirname(parent_buffer);
|
char const* parent = dirname(parent_buffer);
|
||||||
|
|
||||||
int rc = mkdir(parent, 0755);
|
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;
|
saved_errno = errno;
|
||||||
|
|
||||||
if (rc == 0 || errno == EEXIST)
|
if (rc == 0 || errno == EEXIST)
|
||||||
|
@ -202,12 +207,12 @@ bool File::ensure_parent_directories(String const& path)
|
||||||
if (errno != ENOENT)
|
if (errno != ENOENT)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool ok = ensure_parent_directories(parent);
|
bool ok = ensure_parent_directories(path);
|
||||||
saved_errno = errno;
|
saved_errno = errno;
|
||||||
if (!ok)
|
if (!ok)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
rc = mkdir(parent, 0755);
|
rc = mkdir(path.characters(), 0755);
|
||||||
saved_errno = errno;
|
saved_errno = errno;
|
||||||
return rc == 0;
|
return rc == 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ 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_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);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue