Fix directory size count issue on mounted directory
authorJoohyun Kim <joohyune.kim@samsung.com>
Mon, 26 Aug 2013 01:45:19 +0000 (10:45 +0900)
committerJoohyun Kim <joohyune.kim@samsung.com>
Mon, 26 Aug 2013 01:45:19 +0000 (10:45 +0900)
Change-Id: I03d801f5236c0817ce704f9ad65c4c094f5ef6bf
Signed-off-by: Joohyun Kim <joohyune.kim@samsung.com>
src/system-server/inc/FSys_RuntimeInfo.h
src/system-server/runtime/FSys_RuntimeInfo.cpp

index b653559..6d7e033 100644 (file)
@@ -22,6 +22,8 @@
 #ifndef _FSYS_SERVICE_SYS_RUNTIME_INFO__H_
 #define _FSYS_SERVICE_SYS_RUNTIME_INFO__H_
 
+#include <pthread.h>
+
 #include <FApp.h>
 #include <FBase.h>
 
@@ -31,14 +33,21 @@ class _OSP_EXPORT_ _RuntimeInfo
 {
 public:
        static _RuntimeInfo* GetInstance(void);
-       static long long GetDirectorySize(const char* path);
+
+public:
+       long long GetDirectorySize(const char* path);
 
 private:
        _RuntimeInfo();
        virtual ~_RuntimeInfo();
 
+       bool IsMounted(const char* path);
+       long long GetDirectorySizeIteratively(const char* path);
+
 private:
        static _RuntimeInfo*                    __pRuntimeInfo;
+
+       pthread_mutex_t                         __directory;
 };
 
 } } // Tizen::System
index 80b6bc9..0c58c60 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <dirent.h>
 #include <sys/stat.h>
+#include <mntent.h>
 
 #include <FBaseSysLog.h>
 #include "FSys_RuntimeInfo.h"
@@ -34,17 +35,76 @@ using namespace Tizen::Base;
 namespace Tizen { namespace System
 {
 
+_RuntimeInfo* _RuntimeInfo::__pRuntimeInfo = null;
+
 _RuntimeInfo::_RuntimeInfo()
 {
+       pthread_mutex_init(&__directory, NULL);
 }
 
 _RuntimeInfo::~_RuntimeInfo()
 {
+       pthread_mutex_destroy(&__directory);
+}
+
+_RuntimeInfo*
+_RuntimeInfo::GetInstance(void)
+{
+       if(__pRuntimeInfo == null)
+       {
+               __pRuntimeInfo = new (std::nothrow) _RuntimeInfo();
+       }
+
+       return __pRuntimeInfo;
 }
 
 long long
 _RuntimeInfo::GetDirectorySize(const char* path)
 {
+       long long size = 0;
+
+       pthread_mutex_lock(&__directory);
+       size = GetDirectorySizeIteratively(path);
+       pthread_mutex_unlock(&__directory);
+
+       return size;
+}
+
+bool
+_RuntimeInfo::IsMounted(const char* path)
+{
+       FILE* fp = null;
+       result r = E_SUCCESS;
+       bool mounted = false;
+       const char* table = "/etc/mtab";
+       struct mntent* mnt = null;
+
+       
+       fp = setmntent(table, "r");
+       SysTryCatch(NID_SYS, fp != null, r = E_SYSTEM, r, "It is failed to open mount table.");
+
+       SysLog(NID_SYS, "path: %s", path);
+       while(mnt=getmntent(fp))
+       {
+               SysLog(NID_SYS, "mnt_dir: %s", mnt->mnt_dir);
+               if(strcmp(mnt->mnt_dir, path) == 0)
+               {
+                       mounted = true;
+                       break;
+               }
+       }
+
+CATCH:
+       if(fp != null)
+       {
+               endmntent(fp);
+       }
+       return mounted;
+}
+
+long long
+_RuntimeInfo::GetDirectorySizeIteratively(const char* path)
+{
        struct dirent *de;
        struct stat buf;
        DIR* d = opendir(path);
@@ -75,7 +135,10 @@ _RuntimeInfo::GetDirectorySize(const char* path)
                                                sprintf(directoryName, "%s%s/", path, de->d_name);
                                                if(strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
                                                {
-                                                       total_size += GetDirectorySize(directoryName);
+                                                       if(IsMounted(filePath) == false)
+                                                       {
+                                                               total_size += GetDirectorySizeIteratively(directoryName);
+                                                       }
                                                }
                                                total_size += buf.st_size;