From d54ace5f0441dccf2ae4e98fffa77adb14b2d04f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 12 Jun 2020 21:29:01 +0200 Subject: [PATCH] LibCore: Add Core::File::real_path_for() A slightly convenient wrapper around realpath(3). --- Libraries/LibCore/File.cpp | 11 +++++++++++ Libraries/LibCore/File.h | 1 + 2 files changed, 12 insertions(+) diff --git a/Libraries/LibCore/File.cpp b/Libraries/LibCore/File.cpp index 41056701f0..90c556a063 100644 --- a/Libraries/LibCore/File.cpp +++ b/Libraries/LibCore/File.cpp @@ -119,4 +119,15 @@ bool File::exists(const String& filename) return stat(filename.characters(), &st) == 0; } +String File::real_path_for(const String& filename) +{ + if (filename.is_null()) + return {}; + auto* path = realpath(filename.characters(), nullptr); + String real_path(path); + free(path); + return real_path; +} + + } diff --git a/Libraries/LibCore/File.h b/Libraries/LibCore/File.h index 1e443debef..e02f602ee4 100644 --- a/Libraries/LibCore/File.h +++ b/Libraries/LibCore/File.h @@ -46,6 +46,7 @@ public: static bool is_directory(const String& filename); static bool exists(const String& filename); + static String real_path_for(const String& filename); virtual bool open(IODevice::OpenMode) override;