mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
LibCore: Add File method to determine absolute path
This will generate absolute paths lexically rather than through a call to realpath. The motivation for this is to generate absolute paths for non-existent files in unveil calls, as realpath will not work if the file does not exist.
This commit is contained in:
parent
3ae64c7c3d
commit
068802d58d
Notes:
sideshowbarker
2024-07-18 22:57:59 +09:00
Author: https://github.com/timmot Commit: https://github.com/SerenityOS/serenity/commit/068802d58db Pull-request: https://github.com/SerenityOS/serenity/pull/8586 Reviewed-by: https://github.com/alimpfard ✅
2 changed files with 15 additions and 0 deletions
|
@ -207,6 +207,20 @@ String File::current_working_directory()
|
|||
return cwd_as_string;
|
||||
}
|
||||
|
||||
String File::absolute_path(String const& path)
|
||||
{
|
||||
if (File::exists(path))
|
||||
return File::real_path_for(path);
|
||||
|
||||
if (path.starts_with("/"sv))
|
||||
return LexicalPath::canonicalized_path(path);
|
||||
|
||||
auto working_directory = File::current_working_directory();
|
||||
auto full_path = LexicalPath::join(working_directory, path);
|
||||
|
||||
return LexicalPath::canonicalized_path(full_path.string());
|
||||
}
|
||||
|
||||
#ifdef __serenity__
|
||||
|
||||
String File::read_link(String const& link_path)
|
||||
|
|
|
@ -36,6 +36,7 @@ public:
|
|||
static bool exists(const String& filename);
|
||||
static bool ensure_parent_directories(const String& path);
|
||||
static String current_working_directory();
|
||||
static String absolute_path(String const& path);
|
||||
|
||||
enum class RecursionMode {
|
||||
Allowed,
|
||||
|
|
Loading…
Reference in a new issue