Browse Source

LibCore: Add Core::File::real_path_for()

A slightly convenient wrapper around realpath(3).
Andreas Kling 5 years ago
parent
commit
d54ace5f04
2 changed files with 12 additions and 0 deletions
  1. 11 0
      Libraries/LibCore/File.cpp
  2. 1 0
      Libraries/LibCore/File.h

+ 11 - 0
Libraries/LibCore/File.cpp

@@ -119,4 +119,15 @@ bool File::exists(const String& filename)
     return stat(filename.characters(), &st) == 0;
     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;
+}
+
+
 }
 }

+ 1 - 0
Libraries/LibCore/File.h

@@ -46,6 +46,7 @@ public:
     static bool is_directory(const String& filename);
     static bool is_directory(const String& filename);
 
 
     static bool exists(const String& filename);
     static bool exists(const String& filename);
+    static String real_path_for(const String& filename);
 
 
     virtual bool open(IODevice::OpenMode) override;
     virtual bool open(IODevice::OpenMode) override;