Remove white space
[platform/framework/native/appfw.git] / src / system-server / 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 = null;
81
82         fp = setmntent(table, "r");
83         SysTryCatch(NID_SYS, fp != null, r = E_SYSTEM, r, "It is failed to open mount table.");
84
85         SysLog(NID_SYS, "path: %s", path);
86         while(mnt=getmntent(fp))
87         {
88                 SysLog(NID_SYS, "mnt_dir: %s", mnt->mnt_dir);
89                 if(strcmp(mnt->mnt_dir, path) == 0)
90                 {
91                         mounted = true;
92                         break;
93                 }
94         }
95
96 CATCH:
97         if(fp != null)
98         {
99                 endmntent(fp);
100         }
101         return mounted;
102 }
103
104 long long
105 _RuntimeInfo::GetDirectorySizeIteratively(const char* path)
106 {
107         struct dirent *de;
108         struct stat buf;
109         DIR* d = opendir(path);
110         long long total_size = 0;
111         if(d == null)
112         {
113                 return 0;
114         }
115
116         for (de = readdir(d); de != null; de = readdir(d))
117         {
118                 char filePath[1024] = {0,};
119                 sprintf(filePath, "%s%s", path, de->d_name);
120
121                 if (lstat(filePath, &buf) == 0)
122                 {
123                         if (S_ISLNK(buf.st_mode) == true)
124                         {
125                                 total_size += buf.st_size;
126                         }
127                         else // reg file
128                         {
129                                 if(stat(filePath, &buf) == 0)
130                                 {
131                                         if(S_ISDIR(buf.st_mode) == true)
132                                         {
133                                                 char directoryName[1024] = {0,};
134                                                 sprintf(directoryName, "%s%s/", path, de->d_name);
135                                                 if(strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
136                                                 {
137                                                         if(IsMounted(filePath) == false)
138                                                         {
139                                                                 total_size += GetDirectorySizeIteratively(directoryName);
140                                                         }
141                                                 }
142                                                 total_size += buf.st_size;
143
144                                         }
145                                         else
146                                         {
147                                                 total_size += buf.st_size;
148                                         }
149                                 }
150                         }
151                 }
152         }
153         closedir(d);
154         return total_size;
155 }
156 } } // Tizen::System