Fix PackageParser for bug
[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;
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
117         for (de = readdir(d); de != null; de = readdir(d))
118         {
119                 char filePath[1024] = {0,};
120                 sprintf(filePath, "%s%s", path, de->d_name);
121
122                 if (lstat(filePath, &buf) == 0)
123                 {
124                         if (S_ISLNK(buf.st_mode) == true)
125                         {
126                                 total_size += buf.st_size;
127                         }
128                         else // reg file
129                         {
130                                 if(stat(filePath, &buf) == 0)
131                                 {
132                                         if(S_ISDIR(buf.st_mode) == true)
133                                         {
134                                                 char directoryName[1024] = {0,};
135                                                 sprintf(directoryName, "%s%s/", path, de->d_name);
136                                                 if(strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
137                                                 {
138                                                         if(IsMounted(filePath) == false)
139                                                         {
140                                                                 total_size += GetDirectorySizeIteratively(directoryName);
141                                                         }
142                                                 }
143                                                 total_size += buf.st_size;
144
145                                         }
146                                         else
147                                         {
148                                                 total_size += buf.st_size;
149                                         }
150                                 }
151                         }
152                 }
153         }
154         closedir(d);
155         return total_size;
156 }
157 } } // Tizen::System