Fix the crash while installing of ServiceApp
[platform/framework/native/installer.git] / src / Manager / PermissionManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        PermissionManager.cpp
19  * @brief       This is the implementation file for %PermissionManager class.
20  */
21
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <unique_ptr.h>
26
27 #include <FBaseUtilStringUtil.h>
28 #include <FIoFile.h>
29 #include <FIoDirectory.h>
30 #include <FBase_StringConverter.h>
31 #include <FIo_FileImpl.h>
32
33 #include "PermissionManager.h"
34 #include "InstallerUtil.h"
35 #include "SmackManager.h"
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Utility;
40 using namespace Tizen::Io;
41 using namespace Tizen::App;
42
43 PermissionManager::PermissionManager(void)
44 {
45 }
46
47 PermissionManager::~PermissionManager(void)
48 {
49 }
50
51 bool
52 PermissionManager::SetDirectory(InstallationContext* pContext)
53 {
54         result r = E_SUCCESS;
55
56         String destPath;
57         String appRootPath;
58         SmackManager smackManager;
59         smackManager.Construct(pContext);
60
61         appRootPath = pContext->__rootPath;
62         PackageId packageId = pContext->__packageId;
63
64         // appRoot
65         // InstallerUtil::ChangeOwner(appRootPath);
66         InstallerUtil::ChangeMode(appRootPath, PERM_BASE | PERM_EXECUTE);
67         smackManager.AddLabelDir(packageId, appRootPath, true);
68
69         // appRoot/bin
70         destPath = appRootPath + DIR_BIN;
71         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
72         smackManager.AddLabelDir(packageId, destPath);
73
74         // appRoot/info
75         destPath = appRootPath + DIR_INFO;
76         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
77         smackManager.AddLabelDir(packageId, destPath);
78
79         // appRoot/res
80         destPath = appRootPath + DIR_RES;
81         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
82         smackManager.AddLabelDir(packageId, destPath);
83
84         // appRoot/lib
85         destPath = appRootPath + DIR_LIB;
86         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
87         smackManager.AddLabelDir(packageId, destPath);
88
89         // appRoot/shared
90         destPath = appRootPath + DIR_SHARED;
91         if (File::IsFileExist(destPath) == false)
92         {
93                 r = Directory::Create(destPath, false);
94                 TryReturn(!IsFailed(r), INSTALLER_ERROR_INTERNAL_STATE, "Directory::Create() failed");
95         }
96         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
97         smackManager.AddLabelDir(packageId, destPath);
98
99         // appRoot/shared/res
100         destPath = appRootPath + DIR_SHARED_RES;
101         if (File::IsFileExist(destPath) == false)
102         {
103                 String iconPath = appRootPath + DIR_ICONS;
104                 InstallerUtil::CreateSymlink(iconPath, destPath);
105         }
106         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
107         smackManager.AddLabelSharedDir(packageId, destPath);
108
109         // appRoot/shared/data
110         destPath = appRootPath + DIR_SHARED_DATA;
111         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
112         smackManager.AddLabelSharedDir(packageId, destPath);
113
114         // appRoot/shared/trusted
115         destPath = appRootPath + DIR_SHARED_TRUSTED;
116         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
117         smackManager.AddLabelSharedDir(packageId, destPath);
118
119         // appRoot/contents
120         destPath = appRootPath + DIR_CONTENTS;
121         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
122         smackManager.AddLabelDir(packageId, destPath);
123
124         // appRoot/setting
125         destPath = appRootPath + DIR_SETTING;
126         if (pContext->__isAppSetting == true)
127         {
128                 String appVersion = pContext->__version;
129                 String srcPath;
130                 String settingXmlPath;
131
132                 srcPath = destPath + L"/setting." + appVersion + L".xml";
133                 settingXmlPath = destPath + L"/setting.xml";
134
135                 InstallerUtil::Remove(settingXmlPath);
136                 InstallerUtil::CreateSymlink(srcPath, settingXmlPath);
137
138                 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_WRITE, false);
139                 smackManager.AddLabelDir(packageId, destPath);
140         }
141
142         // appRoot/data
143         destPath = appRootPath + DIR_DATA;
144         if (File::IsFileExist(destPath) == false)
145         {
146                 r = Directory::Create(destPath, false);
147                 TryReturn(!IsFailed(r), INSTALLER_ERROR_INTERNAL_STATE, "Directory::Create() failed");
148         }
149         InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
150         smackManager.AddLabelDir(packageId, destPath);
151
152         String apiVersion = pContext->__apiVersion;
153
154         AppLog("------------------------------------------");
155         AppLog("[Tizen::Io] # path = [%ls]", appRootPath.GetPointer());
156         AppLog("[Tizen::Io] # packageId = [%ls]", packageId.GetPointer());
157         AppLog("[Tizen::Io] # apiVersion = [%ls]", apiVersion.GetPointer());
158
159         if (pContext->__isOspCompat == true)
160         {
161                 AppLog("[Tizen::Io] OSP 2.0 application");
162
163                 if (_FileImpl::PrepareDataCaging(appRootPath, packageId) == false)
164                 {
165                         AppLog("[Tizen::Io] _FileImpl::PrepareDataCaging() failed");
166                         return false;
167                 }
168
169                 SetSymLink(pContext);
170         }
171         else
172         {
173                 AppLog("[Tizen::Io] apiVersion is equal to or greater than Tizen 2.0");
174
175                 if (_FileImpl::CreateOspApplicationDirectories(appRootPath, packageId) == false)
176                 {
177                         AppLog("[Tizen::Io] _FileImpl::CreateOspApplicationDirectories() failed");
178                         return false;
179                 }
180         }
181         AppLog("------------------------------------------");
182
183         return true;
184 }
185
186 bool
187 PermissionManager::SetFile(InstallationContext* pContext)
188 {
189         String destPath;
190         String appRootPath = pContext->__rootPath;
191
192         IListT<AppData*>* pAppDataList = pContext->__pAppDataList;
193         TryReturn(pAppDataList, false, "pAppDataList is null");
194
195         for (int i = 0 ; i < pAppDataList->GetCount(); i++)
196         {
197                 AppData* pAppData = null;
198                 pAppDataList->GetAt(i, pAppData);
199                 TryReturn(pAppData, false, "pAppData is null");
200
201                 // set permission(755) to bin file.
202                 destPath = appRootPath + DIR_BIN + L"/" + pAppData->__name + L".exe";
203                 if (File::IsFileExist(destPath) == true)
204                 {
205                         InstallerUtil::ChangeMode(destPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
206                 }
207         }
208
209         return true;
210 }
211
212 bool
213 PermissionManager::SetSymLink(InstallationContext* pContext)
214 {
215         String oldPath;
216         String newPath;
217         String appRootPath = pContext->__rootPath;
218
219 #if 0
220         oldPath = appRootPath + DIR_RES;
221         newPath = appRootPath + L"/Res";
222         InstallerUtil::CreateSymlink(oldPath, newPath);
223 #else
224         newPath = appRootPath + L"/Res";
225         std::unique_ptr< char[] > pResPath(_StringConverter::CopyToCharArrayN(newPath));
226         int ret = symlink("./res", pResPath.get());
227 #endif
228
229 #if 0
230         oldPath = appRootPath + DIR_DATA;
231         newPath = appRootPath + L"/Home";
232         InstallerUtil::CreateSymlink(oldPath, newPath);
233 #else
234         newPath = appRootPath + L"/Home";
235         std::unique_ptr< char[] > pHomePath(_StringConverter::CopyToCharArrayN(newPath));
236         ret = symlink("./data", pHomePath.get());
237 #endif
238
239         oldPath = appRootPath + DIR_RES + L"/screen-size-normal";
240         newPath = appRootPath + L"/Res/ScreenSize-Normal";
241         InstallerUtil::CreateSymlink(oldPath, newPath);
242
243         oldPath = appRootPath + DIR_RES + L"/screen-density-high";
244         newPath = appRootPath + L"/Res/ScreenDensity-High";
245         InstallerUtil::CreateSymlink(oldPath, newPath);
246
247         oldPath = appRootPath + DIR_RES + L"/screen-density-middle";
248         newPath = appRootPath + L"/Res/ScreenDensity-Middle";
249         InstallerUtil::CreateSymlink(oldPath, newPath);
250
251         oldPath = appRootPath + DIR_RES + L"/screen-density-low";
252         newPath = appRootPath + L"/Res/ScreenDensity-Low";
253         InstallerUtil::CreateSymlink(oldPath, newPath);
254
255         return true;
256 }