Remove duplicated code in filesystem manager
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Mon, 19 Nov 2012 10:55:35 +0000 (11:55 +0100)
committerGerrit Code Review <gerrit2@kim11>
Thu, 29 Nov 2012 13:41:12 +0000 (22:41 +0900)
[Issue#] N/A
[Bug] Duplicated code in Filesystem Manager.
[Cause] N/A
[Solution] Move code to new functions.
[Verification] Run deviceapis Filesystem example from Tizen IDE.

Change-Id: I7ab7ec46a6ebbf5a7dc05acf63b6a5eb94b306b7

src/modules/tizen/Filesystem/Manager.cpp
src/modules/tizen/Filesystem/Manager.h

index c43044d..68d533f 100755 (executable)
@@ -343,6 +343,52 @@ void Manager::OnRequestReceived(const EventResolvePtr& event)
     event->setCancelAllowed(true);
 }
 
+void Manager::checkPaths(
+        Api::IPathPtr &src,
+        Api::IPathPtr &dest) {
+    Assert(dest);
+    Assert(src);
+    if (!dest->isAbsolute()) {
+        dest = src->getPath() + *dest;
+    }
+
+    if (src == dest) {
+        ThrowMsg(Commons::PlatformException,
+                 "Destination is same as source: " << src->getFullPath());
+    }
+
+    INodePtr parent;
+    Try {
+        parent = Node::resolve(IPath::create(dest->getPath()));
+    }
+    Catch(Commons::PlatformException) {
+        ReThrowMsg(Commons::PlatformException,
+                   "Could not get destination's parent node.");
+    }
+
+    if (parent->getType() != NT_DIRECTORY) {
+        ThrowMsg(Commons::PlatformException,
+                 "Destination's parent node is not directory.");
+    }
+
+    if (!access(parent->getPath(), AT_WRITE)) {
+        ThrowMsg(Commons::SecurityException,
+            "Not enough permissions to write to destination.");
+    }
+}
+
+bool Manager::pathExists(const std::string &path) {
+    errno = 0;
+    struct stat info;
+    memset(&info, 0, sizeof(struct stat));
+    int status = lstat(path.c_str(), &info);
+    if ((status != 0) && (errno != ENOENT)) {
+        ThrowMsg(Commons::PlatformException,
+                 "No access to platform destination node.");
+    }
+    return 0 == status;
+}
+
 void Manager::OnRequestReceived(const EventCopyPtr& event)
 {
     Try {
@@ -364,49 +410,12 @@ void Manager::OnRequestReceived(const EventCopyPtr& event)
         IPathPtr src = event->getSource();
         IPathPtr dest = event->getDestination();
 
-        Assert(dest);
-        if (!dest->isAbsolute()) {
-            dest = src->getPath() + *dest;
-        }
-
-        if (src == dest) {
-            ThrowMsg(Commons::PlatformException,
-                     "Destination is same as source: " <<
-                     srcNode->getPath()->getFullPath());
-        }
-
-        INodePtr parent;
-        Try {
-            parent = Node::resolve(IPath::create(dest->getPath()));
-        }
-        Catch(Commons::PlatformException) {
-            ReThrowMsg(Commons::PlatformException,
-                       "Could not get destination's parent node.");
-        }
-
-        if (parent->getType() != NT_DIRECTORY) {
-            ThrowMsg(Commons::PlatformException,
-                     "Destination's parent node is not directory.");
-        }
-
-        if (!access(parent->getPath(), AT_WRITE)) {
-            ThrowMsg(Commons::SecurityException,
-                "Not enough permissions to copy source node to destination.");
-        }
+        checkPaths(src, dest);
 
         std::string realSrc = src->getFullPath();
         std::string realDest = dest->getFullPath();
 
-        errno = 0;
-        struct stat info;
-        memset(&info, 0, sizeof(struct stat));
-        int status = lstat(realDest.c_str(), &info);
-        if ((status != 0) && (errno != ENOENT)) {
-            ThrowMsg(Commons::PlatformException,
-                     "No access to platform destination node.");
-        }
-
-        if (0 == status) {
+        if (pathExists(realDest)) {
             //no owerwrite flag setted -> exception
             if ((event->getOptions() & OPT_OVERWRITE) == 0) {
                 ThrowMsg(Commons::PlatformException, "Overwrite is not set.");
@@ -462,46 +471,11 @@ void Manager::OnRequestReceived(const EventMovePtr& event)
                 "Not enough permissions to move source node.");
         }
 
-        Assert(dest);
-        if (!dest->isAbsolute()) {
-            dest = src->getPath() + *dest;
-        }
-
-        if (src == dest) {
-            ThrowMsg(Commons::PlatformException,
-                     "Destination is same as source: " <<
-                     srcNode->getPath()->getFullPath());
-        }
-
-        INodePtr parent;
-        Try {
-            parent = Node::resolve(IPath::create(dest->getPath()));
-        }
-        Catch(Commons::PlatformException) {
-            ReThrowMsg(Commons::PlatformException,
-                       "Could not get destination's parent node.");
-        }
-
-        if (parent->getType() != NT_DIRECTORY) {
-            ThrowMsg(Commons::PlatformException,
-                     "Destination's parent node is not directory.");
-        }
-
-        if (!access(parent->getPath(), AT_WRITE)) {
-            ThrowMsg(Commons::SecurityException,
-                "Not enough permissions to move source node to destination.");
-        }
+        checkPaths(src, dest);
 
-        errno = 0;
-        struct stat info;
-        memset(&info, 0, sizeof(info));
-        int status = lstat(dest->getFullPath().c_str(), &info);
-        if ((status != 0) && (errno != ENOENT)) {
-            ThrowMsg(Commons::PlatformException,
-                     "No access to platform destination node.");
-        }
+        bool destExists = pathExists(dest->getFullPath());
 
-        if ((0 == (event->getOptions() & OPT_OVERWRITE)) && (0 == status)) {
+        if (destExists && (0 == (event->getOptions() & OPT_OVERWRITE))) {
             ThrowMsg(Commons::PlatformException, "Overwrite is not set.");
         }
 
@@ -520,7 +494,7 @@ void Manager::OnRequestReceived(const EventMovePtr& event)
             {
             case EXDEV:
                 {
-                    if (0 == status) {
+                    if (destExists) {
                         //destination exist. Need to be removed
                         Try {
                             INodePtr node = Node::resolve(
index 9699285..e582a89 100644 (file)
@@ -119,6 +119,14 @@ class Manager : public Api::IManager
                      const std::string &dest,
                      bool recursive = true) const;
 
+    /**
+     * Check two paths to copy/move. Checks if they are not the same and
+     * required permissions.
+     * @param src
+     * @param dest
+     */
+    void checkPaths(Api::IPathPtr &src, Api::IPathPtr &dest);
+    bool pathExists(const std::string &path);
   private:
     static Locations m_locations; ///< Paths to default locations.
     static const std::size_t m_maxPathLength; ///< Maximum path length.