loolwsd/LOOLWSD.hpp |   49 ++++++++----------------------------
 loolwsd/Storage.hpp |   70 +++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 72 insertions(+), 47 deletions(-)

New commits:
commit 6c6951956269ceb232f682bac295a3204e4edeca
Author: Ashod Nakashian <ashod.nakash...@collabora.co.uk>
Date:   Sat Mar 5 10:27:30 2016 -0500

    Storage is used to manage files locally
    
    Change-Id: Id50eca8fe1136777ca99c60d78c15e1a47397993
    Reviewed-on: https://gerrit.libreoffice.org/23049
    Reviewed-by: Ashod Nakashian <ashnak...@gmail.com>
    Tested-by: Ashod Nakashian <ashnak...@gmail.com>

diff --git a/loolwsd/LOOLWSD.hpp b/loolwsd/LOOLWSD.hpp
index 0997fb2..77d0ea0 100644
--- a/loolwsd/LOOLWSD.hpp
+++ b/loolwsd/LOOLWSD.hpp
@@ -36,6 +36,8 @@ public:
                                         const std::string& jailRoot,
                                         const std::string& childId)
     {
+        Log::info("DocumentURI: url: " + url + ", jailRoot: " + jailRoot + ", 
childId: " + childId);
+
         // TODO: Sanitize the url and limit access!
         auto uriPublic = Poco::URI(url);
         uriPublic.normalize();
@@ -52,55 +54,26 @@ public:
         // The URL is the publicly visible one, not visible in the chroot jail.
         // We need to map it to a jailed path and copy the file there.
 
-        // chroot/jailId/user/doc
-        const auto jailedDocRoot = Poco::Path(jailRoot, JailedDocumentRoot);
+        // user/doc/childId
+        const auto jailPath = Poco::Path(JailedDocumentRoot, childId);
 
-        // chroot/jailId/user/doc/childId
-        const auto docPath = Poco::Path(jailedDocRoot, childId);
-        Poco::File(docPath).createDirectories();
+        Log::info("jailPath: " + jailPath.toString() + ", jailRoot: " + 
jailRoot);
 
         auto uriJailed = uriPublic;
         if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
         {
-            const auto filename = 
Poco::Path(uriPublic.getPath()).getFileName();
-
-            // chroot/jailId/user/doc/childId/file.ext
-            const auto jailedFilePath = Poco::Path(docPath, 
filename).toString();
-
-            const auto localPath = Poco::Path(JailedDocumentRoot, childId);
-            uriJailed = Poco::URI(Poco::URI("file://"), Poco::Path(localPath, 
filename).toString());
-
-            Log::info("Public URI [" + uriPublic.toString() +
-                      "] jailed to [" + uriJailed.toString() + "].");
-
-            Log::info("Linking " + publicFilePath + " to " + jailedFilePath);
-            if (!Poco::File(jailedFilePath).exists() && 
link(publicFilePath.c_str(), jailedFilePath.c_str()) == -1)
-            {
-                // Failed
-                Log::error("link(\"" + publicFilePath + "\", \"" + 
jailedFilePath + "\") failed.");
-            }
-
-            try
-            {
-                // Fallback to copying.
-                if (!Poco::File(jailedFilePath).exists())
-                {
-                    Log::info("Copying " + publicFilePath + " to " + 
jailedFilePath);
-                    Poco::File(publicFilePath).copyTo(jailedFilePath);
-                }
-            }
-            catch (const Poco::Exception& exc)
-            {
-                Log::error("copyTo(\"" + publicFilePath + "\", \"" + 
jailedFilePath + "\") failed: " + exc.displayText());
-                throw;
-            }
+            std::unique_ptr<StorageBase> storage(new LocalStorage(jailRoot, 
jailPath.toString()));
+            const auto localPath = 
storage->getFilePathFromURI(uriPublic.getPath());
+            uriJailed = Poco::URI(Poco::URI("file://"), localPath);
         }
         else
         {
             Log::info("Public URI [" + uriPublic.toString() +
                       "] assuming cloud storage.");
             //TODO: Configure the storage to use. For now, assume it's WOPI.
-            std::unique_ptr<StorageBase> storage(new 
WopiStorage(docPath.toString()));
+            std::unique_ptr<StorageBase> storage(new WopiStorage(jailRoot, 
jailPath.toString()));
+            const auto localPath = 
storage->getFilePathFromURI(uriPublic.toString());
+            uriJailed = Poco::URI(Poco::URI("file://"), localPath);
         }
 
         auto document = std::shared_ptr<DocumentURI>(new 
DocumentURI(uriPublic, uriJailed, childId));
diff --git a/loolwsd/Storage.hpp b/loolwsd/Storage.hpp
index a9db691..efda920 100644
--- a/loolwsd/Storage.hpp
+++ b/loolwsd/Storage.hpp
@@ -16,6 +16,7 @@
 
 #include <Poco/Net/HTTPResponse.h>
 
+#include "Common.hpp"
 #include "Auth.hpp"
 #include "Util.hpp"
 
@@ -24,8 +25,12 @@ class StorageBase
 {
 public:
 
-    StorageBase(const std::string& localStorePath) :
-        _localStorePath(localStorePath)
+    /// localStorePath the absolute root path of the chroot.
+    /// jailPath the path within the jail that the child uses.
+    StorageBase(const std::string& localStorePath,
+                const std::string& jailPath) :
+        _localStorePath(localStorePath),
+        _jailPath(jailPath)
     {
     }
 
@@ -38,18 +43,64 @@ public:
 
 protected:
     const std::string _localStorePath;
+    const std::string _jailPath;
 };
 
 /// Trivial implementation of local storage that does not need do anything.
 class LocalStorage : public StorageBase
 {
 public:
+    LocalStorage(const std::string& localStorePath,
+                 const std::string& jailPath) :
+        StorageBase(localStorePath, jailPath)
+    {
+    }
 
     std::string getFilePathFromURI(const std::string& uri) override
     {
-        // It's local already.
-        // TODO: Validate access?
-        return uri;
+        auto localPath = _jailPath;
+        if (localPath[0] == '/')
+        {
+            // Remove the leading /
+            localPath.erase(0, 1);
+        }
+
+        // /chroot/jailId/user/doc/childId
+        const auto rootPath = Poco::Path(_localStorePath, localPath);
+        Poco::File(rootPath).createDirectories();
+
+        // /chroot/jailId/user/doc/childId/file.ext
+        const auto filename = Poco::Path(uri).getFileName();
+        const auto jailedFilePath = Poco::Path(rootPath, filename).toString();
+
+        Log::info("Public URI [" + uri +
+                  "] jailed to [" + jailedFilePath + "].");
+
+        const auto publicFilePath = uri;
+        Log::info("Linking " + publicFilePath + " to " + jailedFilePath);
+        if (!Poco::File(jailedFilePath).exists() && 
link(publicFilePath.c_str(), jailedFilePath.c_str()) == -1)
+        {
+            // Failed
+            Log::error("link(\"" + publicFilePath + "\", \"" + jailedFilePath 
+ "\") failed.");
+        }
+
+        try
+        {
+            // Fallback to copying.
+            if (!Poco::File(jailedFilePath).exists())
+            {
+                Log::info("Copying " + publicFilePath + " to " + 
jailedFilePath);
+                Poco::File(publicFilePath).copyTo(jailedFilePath);
+            }
+        }
+        catch (const Poco::Exception& exc)
+        {
+            Log::error("copyTo(\"" + publicFilePath + "\", \"" + 
jailedFilePath + "\") failed: " + exc.displayText());
+            throw;
+        }
+
+        // Now return the jailed path.
+        return Poco::Path(_jailPath, filename).toString();
     }
 
     bool restoreFileToURI(const std::string& path, const std::string& uri) 
override
@@ -64,8 +115,9 @@ public:
 class WopiStorage : public StorageBase
 {
 public:
-    WopiStorage(const std::string& localStorePath) :
-        StorageBase(localStorePath)
+    WopiStorage(const std::string& localStorePath,
+                 const std::string& jailPath) :
+        StorageBase(localStorePath, jailPath)
     {
     }
 
@@ -109,11 +161,11 @@ public:
 class WebDAVStorage : public StorageBase
 {
 public:
-
     WebDAVStorage(const std::string& localStorePath,
+                  const std::string& jailPath,
                   const std::string& url,
                   std::unique_ptr<AuthBase> authAgent) :
-        StorageBase(localStorePath),
+        StorageBase(localStorePath, jailPath),
         _url(url),
         _authAgent(std::move(authAgent))
     {
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to