[Libreoffice-commits] online.git: common/FileUtil.cpp common/FileUtil.hpp common/JailUtil.cpp gtk/mobile.cpp kit/Kit.cpp

Ashod Nakashian (via logerrit) logerrit at kemper.freedesktop.org
Mon Jul 6 11:55:14 UTC 2020


 common/FileUtil.cpp |   14 ++++++++++++++
 common/FileUtil.hpp |    7 +++++++
 common/JailUtil.cpp |   10 +++-------
 gtk/mobile.cpp      |    2 +-
 kit/Kit.cpp         |    7 +++----
 5 files changed, 28 insertions(+), 12 deletions(-)

New commits:
commit 9f7f6dca6ab13a671228a07547c16fbed8cfc0a7
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Sun Jul 5 17:51:02 2020 -0400
Commit:     Ashod Nakashian <ashnakash at gmail.com>
CommitDate: Mon Jul 6 13:54:54 2020 +0200

    wsd: cleanup realpath call
    
    The new utility is safer and more readable.
    
    Change-Id: I3a86675378d458cb004e5534dbf2b401936d0e57
    Reviewed-on: https://gerrit.libreoffice.org/c/online/+/98183
    Tested-by: Jenkins
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice at gmail.com>
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/common/FileUtil.cpp b/common/FileUtil.cpp
index ac980b5f3..560a1b659 100644
--- a/common/FileUtil.cpp
+++ b/common/FileUtil.cpp
@@ -238,6 +238,20 @@ namespace FileUtil
 #endif
     }
 
+    std::string realpath(const char* path)
+    {
+        char* resolved = ::realpath(path, nullptr);
+        if (resolved)
+        {
+            std::string real = resolved;
+            free(resolved);
+            return real;
+        }
+
+        LOG_SYS("Failed to get the realpath of [" << path << "]");
+        return path;
+    }
+
 } // namespace FileUtil
 
 namespace
diff --git a/common/FileUtil.hpp b/common/FileUtil.hpp
index 1ef60a843..79342a46d 100644
--- a/common/FileUtil.hpp
+++ b/common/FileUtil.hpp
@@ -89,6 +89,13 @@ namespace FileUtil
     /// Link source to target, and copy if linking fails.
     bool linkOrCopyFile(const char* source, const char* target);
 
+    /// Returns the realpath(3) of the provided path.
+    std::string realpath(const char* path);
+    inline std::string realpath(const std::string& path)
+    {
+        return realpath(path.c_str());
+    }
+
     /// File/Directory stat helper.
     class Stat
     {
diff --git a/common/JailUtil.cpp b/common/JailUtil.cpp
index 95927c476..a104b91b1 100644
--- a/common/JailUtil.cpp
+++ b/common/JailUtil.cpp
@@ -312,13 +312,9 @@ void setupLoSymlink(const std::string& sysTemplate, const std::string& loTemplat
     symlinkPathToJail(sysTemplate, loTemplate, loSubPath);
 
     // Font paths can end up as realpaths so match that too.
-    char* resolved = realpath(loTemplate.c_str(), nullptr);
-    if (resolved)
-    {
-        if (strcmp(loTemplate.c_str(), resolved) != 0)
-            symlinkPathToJail(sysTemplate, std::string(resolved), loSubPath);
-        free(resolved);
-    }
+    const std::string resolved = FileUtil::realpath(loTemplate);
+    if (loTemplate != resolved)
+        symlinkPathToJail(sysTemplate, resolved, loSubPath);
 }
 
 void setupRandomDeviceLink(const std::string& sysTemplate, const std::string& name)
diff --git a/gtk/mobile.cpp b/gtk/mobile.cpp
index 929e07d17..e467a71c2 100644
--- a/gtk/mobile.cpp
+++ b/gtk/mobile.cpp
@@ -324,7 +324,7 @@ int main(int argc, char* argv[])
 
     gtk_container_add(GTK_CONTAINER(mainWindow), GTK_WIDGET(webView));
 
-    fileURL = "file://" + std::string(realpath(argv[1], nullptr));
+    fileURL = "file://" + FileUtil::realpath(argv[1]);
 
     std::string urlAndQuery =
         "file://" TOPSRCDIR "/loleaflet/dist/loleaflet.html"
diff --git a/kit/Kit.cpp b/kit/Kit.cpp
index 5eefa30f9..d14a28a81 100644
--- a/kit/Kit.cpp
+++ b/kit/Kit.cpp
@@ -338,13 +338,12 @@ namespace
                     const Path& destination,
                     LinkOrCopyType type)
     {
-        char* resolved = realpath(source.c_str(), nullptr);
-        if (resolved && resolved != source)
+        std::string resolved = FileUtil::realpath(source);
+        if (resolved != source)
         {
             LOG_DBG("linkOrCopy: Using real path [" << resolved << "] instead of original link ["
                                                     << source << "].");
-            source = resolved;
-            free(resolved);
+            source = std::move(resolved);
         }
 
         LOG_INF("linkOrCopy " << linkOrCopyTypeString(type) << " from [" << source << "] to ["


More information about the Libreoffice-commits mailing list