Merge "Apply string localization for web" into tizen_2.2
[platform/framework/native/appfw.git] / src / server / system / runtime / FSys_RuntimeInfo.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FSys_RuntimeInfo.cpp
19  * @brief               This is the implementation file for _RuntimeInfo class.
20  */
21
22 #include <pthread.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <mntent.h>
29
30 #include <FBaseSysLog.h>
31 #include "FSys_RuntimeInfo.h"
32
33 using namespace Tizen::Base;
34
35 namespace Tizen { namespace System
36 {
37
38 _RuntimeInfo* _RuntimeInfo::__pRuntimeInfo = null;
39
40 _RuntimeInfo::_RuntimeInfo()
41 {
42         pthread_mutex_init(&__directory, NULL);
43 }
44
45 _RuntimeInfo::~_RuntimeInfo()
46 {
47         pthread_mutex_destroy(&__directory);
48 }
49
50 _RuntimeInfo*
51 _RuntimeInfo::GetInstance(void)
52 {
53         if(__pRuntimeInfo == null)
54         {
55                 __pRuntimeInfo = new (std::nothrow) _RuntimeInfo();
56         }
57
58         return __pRuntimeInfo;
59 }
60
61 long long
62 _RuntimeInfo::GetDirectorySize(const char* path)
63 {
64         long long size = 0;
65
66         pthread_mutex_lock(&__directory);
67         size = GetDirectorySizeIteratively(path);
68         pthread_mutex_unlock(&__directory);
69
70         return size;
71 }
72
73 bool
74 _RuntimeInfo::IsMounted(const char* path)
75 {
76         FILE* fp = null;
77         result r = E_SUCCESS;
78         bool mounted = false;
79         const char* table = "/etc/mtab";
80         struct mntent mnt;
81         char buf[256];
82
83         fp = setmntent(table, "r");
84         SysTryCatch(NID_SYS, fp != null, r = E_SYSTEM, r, "It is failed to open mount table.");
85
86         SysLog(NID_SYS, "path: %s", path);
87         while(getmntent_r(fp, &mnt, buf, sizeof(buf)))
88         {
89                 SysLog(NID_SYS, "mnt_dir: %s", mnt.mnt_dir);
90                 if(strcmp(mnt.mnt_dir, path) == 0)
91                 {
92                         mounted = true;
93                         break;
94                 }
95         }
96
97 CATCH:
98         if(fp != null)
99         {
100                 endmntent(fp);
101         }
102         return mounted;
103 }
104
105 long long
106 _RuntimeInfo::GetDirectorySizeIteratively(const char* path)
107 {
108         struct dirent *de;
109         struct stat buf;
110         DIR* d = opendir(path);
111         long long total_size = 0;
112         if(d == null)
113         {
114                 return 0;
115         }
116         SysLog(NID_SYS, "path %s", path);
117         for (de = readdir(d); de != null; de = readdir(d))
118         {
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);
123
124                 if (lstat(filePath, &buf) == 0)
125                 {
126                         if (S_ISLNK(buf.st_mode) == true)
127                         {
128                                 total_size += buf.st_size;
129                         }
130                         else // reg file
131                         {
132                                 if(stat(filePath, &buf) == 0)
133                                 {
134                                         if(S_ISDIR(buf.st_mode) == true)
135                                         {
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)
139                                                 {
140                                                         if(IsMounted(filePath) == false)
141                                                         {
142                                                                 total_size += GetDirectorySizeIteratively(directoryName);
143                                                         }
144                                                 }
145                                                 total_size += buf.st_size;
146                                         }
147                                         else
148                                         {
149                                                 total_size += buf.st_size;
150                                         }
151                                 }
152                         }
153                 }
154         }
155         closedir(d);
156         return total_size;
157 }
158 } } // Tizen::System