mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:18:11 +00:00
LibCore: Add ensure_parent_directories to LibCore::File
Moved the implementation in SystemServer/Service.cpp to LibCore.
This commit is contained in:
parent
c1fc27cab2
commit
fec4152220
3 changed files with 38 additions and 25 deletions
|
@ -27,10 +27,13 @@
|
|||
#ifdef __serenity__
|
||||
# include <Kernel/API/Syscall.h>
|
||||
#endif
|
||||
#include <AK/ScopeGuard.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -132,6 +135,37 @@ String File::real_path_for(const String& filename)
|
|||
return real_path;
|
||||
}
|
||||
|
||||
bool File::ensure_parent_directories(const String& path)
|
||||
{
|
||||
ASSERT(path.starts_with("/"));
|
||||
|
||||
int saved_errno = 0;
|
||||
ScopeGuard restore_errno = [&saved_errno] { errno = saved_errno; };
|
||||
|
||||
char* parent_buffer = strdup(path.characters());
|
||||
ScopeGuard free_buffer = [parent_buffer] { free(parent_buffer); };
|
||||
|
||||
const char* parent = dirname(parent_buffer);
|
||||
|
||||
int rc = mkdir(parent, 0755);
|
||||
saved_errno = errno;
|
||||
|
||||
if (rc == 0 || errno == EEXIST)
|
||||
return true;
|
||||
|
||||
if (errno != ENOENT)
|
||||
return false;
|
||||
|
||||
bool ok = ensure_parent_directories(parent);
|
||||
saved_errno = errno;
|
||||
if (!ok)
|
||||
return false;
|
||||
|
||||
rc = mkdir(parent, 0755);
|
||||
saved_errno = errno;
|
||||
return rc == 0;
|
||||
}
|
||||
|
||||
#ifdef __serenity__
|
||||
|
||||
String File::read_link(const StringView& link_path)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue