2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FSys_RuntimeInfo.cpp
19 * @brief This is the implementation file for _RuntimeInfo class.
30 #include <FBaseSysLog.h>
31 #include "FSys_RuntimeInfo.h"
33 using namespace Tizen::Base;
35 namespace Tizen { namespace System
38 _RuntimeInfo* _RuntimeInfo::__pRuntimeInfo = null;
40 _RuntimeInfo::_RuntimeInfo()
42 pthread_mutex_init(&__directory, NULL);
45 _RuntimeInfo::~_RuntimeInfo()
47 pthread_mutex_destroy(&__directory);
51 _RuntimeInfo::GetInstance(void)
53 if(__pRuntimeInfo == null)
55 __pRuntimeInfo = new (std::nothrow) _RuntimeInfo();
58 return __pRuntimeInfo;
62 _RuntimeInfo::GetDirectorySize(const char* path)
66 pthread_mutex_lock(&__directory);
67 size = GetDirectorySizeIteratively(path);
68 pthread_mutex_unlock(&__directory);
74 _RuntimeInfo::IsMounted(const char* path)
79 const char* table = "/etc/mtab";
83 fp = setmntent(table, "r");
84 SysTryCatch(NID_SYS, fp != null, r = E_SYSTEM, r, "It is failed to open mount table.");
86 SysLog(NID_SYS, "path: %s", path);
87 while(getmntent_r(fp, &mnt, buf, sizeof(buf)))
89 SysLog(NID_SYS, "mnt_dir: %s", mnt.mnt_dir);
90 if(strcmp(mnt.mnt_dir, path) == 0)
106 _RuntimeInfo::GetDirectorySizeIteratively(const char* path)
110 DIR* d = opendir(path);
111 long long total_size = 0;
116 SysLog(NID_SYS, "path %s", path);
117 for (de = readdir(d); de != null; de = readdir(d))
119 char filePath[1024] = {0,};
120 int length = strlen(path) + strlen(de->d_name);
121 SysTryReturn(NID_SYS, length > 0 && length < 1023, 0, E_INVALID_ARG, "The file path is invalid");
122 sprintf(filePath, "%s%s", path, de->d_name);
124 if (lstat(filePath, &buf) == 0)
126 if (S_ISLNK(buf.st_mode) == true)
128 total_size += buf.st_size;
132 if(stat(filePath, &buf) == 0)
134 if(S_ISDIR(buf.st_mode) == true)
136 char directoryName[1024] = {0,};
137 sprintf(directoryName, "%s%s/", path, de->d_name);
138 if(strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
140 if(IsMounted(filePath) == false)
142 total_size += GetDirectorySizeIteratively(directoryName);
145 total_size += buf.st_size;
149 total_size += buf.st_size;