merge with master
[platform/framework/web/wrt-plugins-common.git] / src / modules / tizen / Filesystem / Manager.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 68d533f..5ecfe2f
@@ -46,7 +46,6 @@ const char* PATH_SDCARD = "/opt/storage/sdcard";
 
 namespace WrtDeviceApis {
 namespace Filesystem {
-
 using namespace Api;
 
 Manager::Locations Manager::m_locations;
@@ -110,8 +109,7 @@ Manager::Manager()
 }
 
 Manager::~Manager()
-{
-}
+{}
 
 IPathPtr Manager::getBasePath() const
 {
@@ -182,74 +180,79 @@ void Manager::find(const EventFindPtr& event)
 }
 
 void Manager::find(const IPathPtr& path,
-        const FiltersMap& filters,
-        NodeList& result,
-        const EventFindPtr& event)
+                   const FiltersMap& filters,
+                   NodeList& result,
+                   const EventFindPtr& event)
 {
     Try {
         Assert(path && "path is NULL");
         FTS *fts;
         FTSENT *ftsent;
-        std::string pth=path->getFullPath();
-        char * const paths[] = {const_cast<char * const>(pth.c_str()), NULL};
+        std::string pth = path->getFullPath();
+        char * const paths[] = { const_cast<char * const>(pth.c_str()), NULL };
 
-        if ((fts = fts_open(paths, FTS_PHYSICAL|FTS_NOCHDIR, NULL)) == NULL) {
+        if ((fts =
+                 fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL)
+        {
             //ERROR
             int error = errno;
             LogError(__PRETTY_FUNCTION__ << ": fts_open on "
-                    << pth
-                    << " failed with error: "
-                    << strerror(error));
+                                         << pth
+                                         << " failed with error: "
+                                         << strerror(error));
             return;
         }
 
         while ((ftsent = fts_read(fts)) != NULL) {
-            if(event && event->checkCancelled()) break;
+            if (event && event->checkCancelled()) {
+                break;
+            }
             switch (ftsent->fts_info) {
-                case FTS_DP:
-                    //directory in postorder - do nothing
-                    break;
-                case FTS_D:
-                case FTS_DC:
-                case FTS_F:
-                case FTS_SL:
-                case FTS_SLNONE:
-                case FTS_DEFAULT:
-                    //regular files, symbolic links, directories in preorder
-                    //and other file entries that can be processed further
-                    if (matchFilters(ftsent->fts_name, *ftsent->fts_statp, filters)) {
-                        IPathPtr childPath=IPath::create(ftsent->fts_path);
-                        result.push_back(Node::resolve(childPath));
-                    }
-                    break;
-                case FTS_NS:
-                case FTS_NSOK:
-                case FTS_DOT:
-                case FTS_DNR:
-                case FTS_ERR:
-                default:
-                    LogWarning(__PRETTY_FUNCTION__
-                            << ": traversal failed with error: "
-                            << strerror(ftsent->fts_errno));
-                    ThrowMsg(Commons::PlatformException,
-                            "Error reading directory");
+            case FTS_DP:
+                //directory in postorder - do nothing
+                break;
+            case FTS_D:
+            case FTS_DC:
+            case FTS_F:
+            case FTS_SL:
+            case FTS_SLNONE:
+            case FTS_DEFAULT:
+                //regular files, symbolic links, directories in preorder
+                //and other file entries that can be processed further
+                if (matchFilters(ftsent->fts_name, *ftsent->fts_statp,
+                                 filters))
+                {
+                    IPathPtr childPath = IPath::create(ftsent->fts_path);
+                    result.push_back(Node::resolve(childPath));
+                }
+                break;
+            case FTS_NS:
+            case FTS_NSOK:
+            case FTS_DOT:
+            case FTS_DNR:
+            case FTS_ERR:
+            default:
+                LogWarning(__PRETTY_FUNCTION__
+                           << ": traversal failed with error: "
+                           << strerror(ftsent->fts_errno));
+                ThrowMsg(Commons::PlatformException,
+                         "Error reading directory");
             }
         }
 
         if (fts_close(fts) == -1) {
             int error = errno;
             LogWarning(__PRETTY_FUNCTION__ << ": fts_close failed with error: "
-                    << strerror(error));
+                                           << strerror(error));
             ThrowMsg(Commons::PlatformException,
                      "Could not close platform node.");
         }
     }
-    Catch(Commons::Exception) {
-    }
+    Catch(Commons::Exception) {}
 }
 
 void Manager::copyElement(
-        const std::string &src, const std::string &dest, bool recursive) const
+    const std::string &src, const std::string &dest, bool recursive) const
 {
     LogDebug("Copying src: " << src << " to: " << dest);
 
@@ -288,44 +291,60 @@ void Manager::copyElement(
             free(data);
         }
     }
-
 }
 
 bool Manager::access(const IPathPtr& path,
-        int accessType) const
+                     int accessType) const
 {
     int amode = 0;
-    if (accessType & AT_EXISTS) { amode |= F_OK; }
-    if (accessType & AT_READ) { amode |= R_OK; }
-    if (accessType & AT_WRITE) { amode |= W_OK; }
-    if (accessType & AT_EXEC) { amode |= X_OK; }
+    if (accessType & AT_EXISTS) {
+        amode |= F_OK;
+    }
+    if (accessType & AT_READ) {
+        amode |= R_OK;
+    }
+    if (accessType & AT_WRITE) {
+        amode |= W_OK;
+    }
+    if (accessType & AT_EXEC) {
+        amode |= X_OK;
+    }
     return (::access(path->getFullPath().c_str(), amode) == 0);
 }
 
 bool Manager::matchFilters(const std::string& name,
-        const struct stat& info,
-        const FiltersMap& filters)
+                           const struct stat& info,
+                           const FiltersMap& filters)
 {
     FiltersMap::const_iterator it = filters.begin();
     for (; it != filters.end(); ++it) {
         if (it->first == FF_NAME) {
-            if (!pcrecpp::RE(it->second).PartialMatch(name)) { return false; }
+            if (!pcrecpp::RE(it->second).PartialMatch(name)) {
+                return false;
+            }
         } else if (it->first == FF_SIZE) {
             std::size_t size;
             std::stringstream ss(it->second);
             ss >> size;
             if (!ss ||
-                (size != static_cast<size_t>(info.st_size))) { return false; }
+                (size != static_cast<size_t>(info.st_size)))
+            {
+                return false;
+            }
         } else if (it->first == FF_CREATED) {
             std::time_t created;
             std::stringstream ss(it->second);
             ss >> created;
-            if (!ss || (created != info.st_ctime)) { return false; }
+            if (!ss || (created != info.st_ctime)) {
+                return false;
+            }
         } else if (it->first == FF_MODIFIED) {
             std::time_t modified;
             std::stringstream ss(it->second);
             ss >> modified;
-            if (!ss || (modified != info.st_mtime)) { return false; }
+            if (!ss || (modified != info.st_mtime)) {
+                return false;
+            }
         }
     }
     return true;
@@ -335,8 +354,7 @@ void Manager::OnRequestReceived(const EventResolvePtr& event)
 {
     try {
         event->setResult(Node::resolve(event->getPath()));
-    }
-    catch (const Commons::PlatformException& ex) {
+    } catch (const Commons::PlatformException& ex) {
         LogError("Exception: " << ex.GetMessage());
         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
     }
@@ -344,8 +362,9 @@ void Manager::OnRequestReceived(const EventResolvePtr& event)
 }
 
 void Manager::checkPaths(
-        Api::IPathPtr &src,
-        Api::IPathPtr &dest) {
+    Api::IPathPtr &src,
+    Api::IPathPtr &dest)
+{
     Assert(dest);
     Assert(src);
     if (!dest->isAbsolute()) {
@@ -373,11 +392,12 @@ void Manager::checkPaths(
 
     if (!access(parent->getPath(), AT_WRITE)) {
         ThrowMsg(Commons::SecurityException,
-            "Not enough permissions to write to destination.");
+                 "Not enough permissions to write to destination.");
     }
 }
 
-bool Manager::pathExists(const std::string &path) {
+bool Manager::pathExists(const std::string &path)
+{
     errno = 0;
     struct stat info;
     memset(&info, 0, sizeof(struct stat));
@@ -395,16 +415,16 @@ void Manager::OnRequestReceived(const EventCopyPtr& event)
         INodePtr srcNode = Node::resolve(event->getSource());
         int requiredAccess;
         switch (srcNode->getType()) {
-            case NT_DIRECTORY:
-                requiredAccess = AT_EXEC;
-                break;
-            case NT_FILE:
-                requiredAccess = AT_READ;
-                break;
+        case NT_DIRECTORY:
+            requiredAccess = AT_EXEC;
+            break;
+        case NT_FILE:
+            requiredAccess = AT_READ;
+            break;
         }
         if (!access(srcNode->getPath(), requiredAccess)) {
             ThrowMsg(Commons::SecurityException,
-                "Not enough permissions to copy source node.");
+                     "Not enough permissions to copy source node.");
         }
 
         IPathPtr src = event->getSource();
@@ -446,12 +466,10 @@ void Manager::OnRequestReceived(const EventCopyPtr& event)
         copyElement(realSrc, realDest);
 
         event->setResult(Node::resolve(dest));
-    }
-    catch (const Commons::PlatformException& ex) {
+    } catch (const Commons::PlatformException& ex) {
         LogError("Exception: " << ex.GetMessage());
         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
-    }
-    catch (const Commons::SecurityException& ex) {
+    } catch (const Commons::SecurityException& ex) {
         LogError("Exception: " << ex.GetMessage());
         event->setExceptionCode(Commons::ExceptionCodes::SecurityException);
     }
@@ -468,7 +486,7 @@ void Manager::OnRequestReceived(const EventMovePtr& event)
         INodePtr srcNode = Node::resolve(src);
         if (!access(srcNode->getParent()->getPath(), AT_WRITE)) {
             ThrowMsg(Commons::SecurityException,
-                "Not enough permissions to move source node.");
+                     "Not enough permissions to move source node.");
         }
 
         checkPaths(src, dest);
@@ -490,41 +508,40 @@ void Manager::OnRequestReceived(const EventMovePtr& event)
                           dest->getFullPath().c_str()))
         {
             int error = errno;
-            switch (error)
-            {
+            switch (error) {
             case EXDEV:
-                {
-                    if (destExists) {
-                        //destination exist. Need to be removed
-                        Try {
-                            INodePtr node = Node::resolve(
-                                event->getDestination());
-                            node->remove(event->getOptions());
-                        }
-                        Catch(Commons::PlatformException) {
-                            LogError("Exception while removing dest directory");
-                            event->setExceptionCode(
-                                Commons::ExceptionCodes::PlatformException);
-                        }
-                        Catch(Commons::SecurityException) {
-                            event->setExceptionCode(
-                                Commons::ExceptionCodes::SecurityException);
-                        }
-                    }
-
-                    copyElement(src->getFullPath(),
-                                dest->getFullPath());
-                    //remove source files
+            {
+                if (destExists) {
+                    //destination exist. Need to be removed
                     Try {
-                        INodePtr node = Node::resolve(event->getSource());
+                        INodePtr node = Node::resolve(
+                                event->getDestination());
                         node->remove(event->getOptions());
                     }
-                    Catch(Commons::Exception) {
-                        LogError("Exception: "
-                                 << _rethrown_exception.GetMessage());
+                    Catch(Commons::PlatformException) {
+                        LogError("Exception while removing dest directory");
+                        event->setExceptionCode(
+                            Commons::ExceptionCodes::PlatformException);
+                    }
+                    Catch(Commons::SecurityException) {
+                        event->setExceptionCode(
+                            Commons::ExceptionCodes::SecurityException);
                     }
-                    break;
                 }
+
+                copyElement(src->getFullPath(),
+                            dest->getFullPath());
+                //remove source files
+                Try {
+                    INodePtr node = Node::resolve(event->getSource());
+                    node->remove(event->getOptions());
+                }
+                Catch(Commons::Exception) {
+                    LogError("Exception: "
+                             << _rethrown_exception.GetMessage());
+                }
+                break;
+            }
             default:
                 ThrowMsg(Commons::PlatformException,
                          "Error on rename: " << DPL::GetErrnoString(error));
@@ -533,12 +550,10 @@ void Manager::OnRequestReceived(const EventMovePtr& event)
         }
 
         event->setResult(Node::resolve(dest));
-    }
-    catch (const Commons::PlatformException& ex) {
+    } catch (const Commons::PlatformException& ex) {
         LogError("Exception: " << ex.GetMessage());
         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
-    }
-    catch (const Commons::SecurityException& ex) {
+    } catch (const Commons::SecurityException& ex) {
         LogError("Exception: " << ex.GetMessage());
         event->setExceptionCode(Commons::ExceptionCodes::SecurityException);
     }
@@ -571,8 +586,7 @@ void Manager::OnRequestReceived(const EventFindPtr& event)
         NodeList result;
         find(event->getPath(), event->getFilters(), result, event);
         event->setResult(result);
-    }
-    catch (const Commons::Exception& ex) {
+    } catch (const Commons::Exception& ex) {
         LogError("Exception: " << ex.GetMessage());
         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
     }
@@ -593,19 +607,17 @@ bool Manager::init()
 }
 
 void Manager::setupLocation(LocationType location,
-        const char* path)
+                            const char* path)
 {
     if (!nodeExists(path)) {
         try {
             makePath(path, 0755);
-        }
-        catch (const Commons::PlatformException& ex) {
+        } catch (const Commons::PlatformException& ex) {
             LogError("Exception: " << ex.DumpToString());
             return;
         }
     }
     m_locations[location] = IPath::create(path);
 }
-
 } // Filesystem
 } // WrtDeviceApis