2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file ConfigurationManager.cpp
19 * @brief This is the implementation file for %ConfigurationManager class.
25 #include <unique_ptr.h>
27 #include <pkgmgr_parser.h>
28 #include <pkgmgr_installer.h>
29 #include <app2ext_interface.h>
31 #include <FBaseUtilStringUtil.h>
33 #include <FIoDirectory.h>
34 #include <FSecCertX509CertificatePath.h>
36 #include <FBase_StringConverter.h>
38 #include "ConfigurationManager.h"
39 #include "ManifestGenerator.h"
40 #include "InstallerUtil.h"
41 #include "SmackManager.h"
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Base::Utility;
46 using namespace Tizen::Security::Cert;
47 using namespace Tizen::App;
48 using namespace Tizen::Io;
50 ConfigurationManager::ConfigurationManager(void)
54 ConfigurationManager::~ConfigurationManager(void)
59 ConfigurationManager::CreateFile(InstallationContext* pContext)
64 rootPath = pContext->__rootPath;
67 apiVersion = pContext->__apiVersion;
68 String versionInfoFile;
69 versionInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), VERSION_INFO_FILE);
70 InstallerUtil::CreateInfoFile(versionInfoFile, &apiVersion);
72 if (pContext->__isOspCompat == true)
74 AppLog("[OspCompat] is detected");
76 String compatInfoFile;
77 compatInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), COMPAT_INFO_FILE);
78 InstallerUtil::CreateInfoFile(compatInfoFile, null);
81 String webServicePrivilege(TIZEN_PRIVILEGE_WEB_SERVICE);
82 if (FindPrivilege(pContext, webServicePrivilege) == true)
84 AppLog("WEB_SERVICE privilege is detected. rootPath=[%ls]", rootPath.GetPointer());
86 String webServiceInfoFile;
87 webServiceInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), WEBSERVICE_INFO_FILE);
88 InstallerUtil::CreateInfoFile(webServiceInfoFile, null);
91 IListT<AppData*>* pAppDataList = pContext->__pAppDataList;
92 TryReturn(pAppDataList, false, "pAppDataList is null");
94 for (int i = 0 ; i < pAppDataList->GetCount(); i++)
96 AppData* pAppData = null;
97 pAppDataList->GetAt(i, pAppData);
98 TryReturn(pAppData, false, "pAppData is null");
100 String appName = pAppData->__name;
101 String appType = pAppData->__type;
102 AppId appId = pAppData->__appId;
105 binaryPath.Format(1024, L"%ls%ls/%ls", rootPath.GetPointer(), DIR_BIN, appName.GetPointer());
107 if (File::IsFileExist(binaryPath) == true)
109 InstallerUtil::Remove(binaryPath);
112 if (appType == L"UiApp")
114 //err = symlink(UIAPP_LOADER_PATH, pBinaryPath);
116 AppLog("copy ui app loader");
117 InstallerUtil::Copy(UIAPP_LOADER_PATH, binaryPath);
119 HashMap* pFeatureList = pAppData->__pFeatureList;
120 TryReturn(pFeatureList, false, "pFeatureList is null");
122 String coordinateSystem;
123 String baseScreenSize;
124 String logicalCoordinate;
126 std::unique_ptr< IMapEnumerator > pEnum(pFeatureList->GetMapEnumeratorN());
127 TryReturn(pEnum, false, "GetMapEnumeratorN() failed. [%s]", GetErrorMessage(GetLastResult()));
129 while (pEnum->MoveNext() == E_SUCCESS)
131 String* pKey = static_cast< String* > (pEnum->GetKey());
132 TryReturn(pEnum, false, "GetKey() failed. [%s]", GetErrorMessage(GetLastResult()));
134 String* pValue = static_cast< String* > (pEnum->GetValue());
135 TryReturn(pEnum, false, "GetValue() failed. [%s]", GetErrorMessage(GetLastResult()));
137 if ((*pKey) == L"CoordinateSystem")
139 coordinateSystem = (*pValue);
142 if ((*pKey) == L"BaseScreenSize")
144 baseScreenSize = (*pValue);
147 if ((*pKey) == L"LogicalCoordinate")
149 logicalCoordinate = (*pValue);
153 String uiScalability;
154 uiScalability.Format(1024, UISCALABILITY_INFO, coordinateSystem.GetPointer(), baseScreenSize.GetPointer(), logicalCoordinate.GetPointer());
156 String uiScalabilityInfoFile;
157 uiScalabilityInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), UISCALABILITY_INFO_FILE);
159 InstallerUtil::CreateInfoFile(uiScalabilityInfoFile, &uiScalability);
161 int categoryType = pAppData->__feature;
162 if (categoryType != CATEGORY_TYPE_NONE)
164 String category = InstallerUtil::GetCategory(categoryType);
165 category.ToLowerCase();
167 int type = _Aul::GetAppType(category);
169 if (category == L"ime")
171 CreateImeSymlink(binaryPath, appId);
175 typeInfo.Format(1024, L"%d", type);
178 typeInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), TYPE_INFO_FILE);
180 InstallerUtil::CreateInfoFile(typeInfoFile, &typeInfo);
183 else if (appType == L"ServiceApp")
185 if (pContext->__isPreloaded == true || pAppData->__isSystemService == true)
187 AppLog("copy system service loader");
188 InstallerUtil::Copy(SYSTEMSERIVCE_LOADER_PATH, binaryPath);
192 AppLog("copy service app loader");
193 InstallerUtil::Copy(SERVICEAPP_LOADER_PATH, binaryPath);
197 InstallerUtil::ChangeMode(binaryPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
200 ArrayList* pContentDataList = pContext->GetContentDataList();
201 if (pContentDataList)
203 int contentCount = pContentDataList->GetCount();
204 for (int i = 0 ; i < contentCount; i++)
206 ContentData* pContentData = static_cast<ContentData*>(pContentDataList->GetAt(i));
209 String contentId = pContentData->GetContentId();
210 String oldPath = rootPath + DIR_CONTENTS + L"/" + contentId;
212 if (File::IsFileExist(SLP_FONT_PATH) == false)
214 Directory::Create(SLP_FONT_PATH, false);
215 InstallerUtil::ChangeMode(SLP_FONT_PATH, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
218 String newPath = SLP_FONT_PATH;
219 newPath += L"/" + pContext->__packageId;
220 Directory::Create(newPath, false);
221 InstallerUtil::ChangeMode(newPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
224 newPath += contentId;
225 InstallerUtil::CreateSymlink(oldPath, newPath);
230 if (pContext->__isPreloaded == true)
232 pContext->__coreXmlPath.Format(1024, DIR_RO_PACKAGE_SYSTEM_MANIFEST, pContext->__packageId.GetPointer());
236 pContext->__coreXmlPath.Format(1024, DIR_RW_PACKAGE_SYSTEM_MANIFEST, pContext->__packageId.GetPointer());
239 std::unique_ptr<char[]> pXmlPath(_StringConverter::CopyToCharArrayN(pContext->__coreXmlPath));
240 TryReturn(pXmlPath != null, false, "pXmlPath is null");
242 if (pContext->__isHybridService == true)
244 String webXmlPath = pXmlPath.get();
245 webXmlPath += L".wgt";
246 InstallerUtil::Copy(pXmlPath.get(), webXmlPath);
248 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() - START");
249 pkgmgr_parser_parse_manifest_for_uninstallation(pXmlPath.get(), null);
250 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() - END");
251 CreateCoreXmlFile(pContext);
253 String serviceXmlPath = pXmlPath.get();
254 serviceXmlPath += L".tpk";
255 InstallerUtil::Copy(pXmlPath.get(), serviceXmlPath);
256 InstallerUtil::Remove(pXmlPath.get());
258 MergeToSystemXmlFile(pXmlPath.get(), webXmlPath, serviceXmlPath);
259 InstallerUtil::Remove(webXmlPath);
260 InstallerUtil::Remove(serviceXmlPath);
264 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() - START");
265 pkgmgr_parser_parse_manifest_for_uninstallation(pXmlPath.get(), null);
266 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() - END");
267 CreateCoreXmlFile(pContext);
270 AppLog("------------------------------------------");
271 AppLog("sync() - START");
273 AppLog("sync() - END");
274 AppLog("------------------------------------------");
276 err = pkgmgr_parser_check_manifest_validation(pXmlPath.get());
277 TryReturn(err == 0, false, "pkgmgr_parser_check_manifest_validation() is failed. error = [%d][%s]", err, pXmlPath.get());
283 ConfigurationManager::RemoveFile(InstallationContext* pContext)
285 if (pContext->__isPreloaded == true)
287 pContext->__coreXmlPath.Format(1024, DIR_RO_PACKAGE_SYSTEM_MANIFEST, pContext->__packageId.GetPointer());
291 pContext->__coreXmlPath.Format(1024, DIR_RW_PACKAGE_SYSTEM_MANIFEST, pContext->__packageId.GetPointer());
294 std::unique_ptr<char[]> pXmlPath(_StringConverter::CopyToCharArrayN(pContext->__coreXmlPath));
295 TryReturn(pXmlPath != null, false, "pXmlPath is null");
297 if (pContext->__isHybridService == true)
299 AppLog("Uninstallation for HybridService - skip");
304 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() START");
305 if (pkgmgr_parser_parse_manifest_for_uninstallation(pXmlPath.get(), null) != 0)
307 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() is failed.[%s]", pXmlPath.get());
309 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() END");
311 InstallerUtil::Remove(pXmlPath.get());
314 // _PackageInfoImpl* pPackageInfoImpl = pContext->GetPackageInfoImpl();
315 // TryReturn(pPackageInfoImpl, false, "[osp-installer] pPackageInfoImpl is null.");
317 // ArrayList* pAppList = pPackageInfoImpl->GetAppInfoList();
318 // for (int i = 0 ; i < pAppList->GetCount(); i++)
320 // _PackageAppInfoImpl* pAppInfoImpl = dynamic_cast<_PackageAppInfoImpl*>(pAppList->GetAt(i));
323 // String packageName = pAppInfoImpl->GetPackageName();
325 // char dest[772] = {0};
327 // destPath.Format(1024, L"%ls/%ls", SLP_APP_PATH, packageName.GetPointer());
328 // sprintf(dest, "%ls", destPath.GetPointer());
337 ConfigurationManager::PostInstall(InstallationContext* pContext, bool error) const
339 app2ext_handle* pHandle = (app2ext_handle*)pContext->__pApp2ExtHandle;
341 PackageId packageId = pContext->__packageId;
342 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
343 TryReturn(pPackageId, false, "pPackageId is null");
347 std::unique_ptr<char[]> pXmlPath(_StringConverter::CopyToCharArrayN(pContext->__coreXmlPath));
348 TryReturn(pXmlPath != null, false, "pXmlPath is null");
350 AppLog("------------------------------------------");
351 AppLog("pkgmgr_parser_parse_manifest_for_installation() - START");
352 int err = pkgmgr_parser_parse_manifest_for_installation(pXmlPath.get(), null);
353 TryReturn(err == 0, false, "pkgmgr_parser_parse_manifest_for_installation() is failed. error = [%d][%s]", err, pXmlPath.get());
355 AppLog("pkgmgr_parser_parse_manifest_for_installation() - END");
356 AppLog("------------------------------------------");
358 SmackManager smackManager;
359 smackManager.Construct(pContext);
360 smackManager.AddPermissions(packageId);
367 pHandle->interface.post_install(pPackageId.get(), APP2EXT_STATUS_FAILED);
368 AppLog("[app2sd] post_install(%s, APP2EXT_STATUS_FAILED)", pPackageId.get());
372 pHandle->interface.post_install(pPackageId.get(), APP2EXT_STATUS_SUCCESS);
373 AppLog("[app2sd] post_install(%s, APP2EXT_STATUS_SUCCESS)", pPackageId.get());
376 app2ext_deinit(pHandle);
383 ConfigurationManager::PostUninstall(InstallationContext* pContext) const
385 app2ext_handle* pHandle = (app2ext_handle*)pContext->__pApp2ExtHandle;
387 PackageId packageId = pContext->__packageId;
388 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
389 TryReturn(pPackageId, false, "pPackageId is null.");
393 pHandle->interface.post_uninstall(pPackageId.get());
394 AppLog("[app2sd] post_uninstall(%s)", pPackageId.get());
396 app2ext_deinit(pHandle);
399 SmackManager smackManager;
400 smackManager.Construct(pContext);
401 smackManager.RevokePermissions(packageId);
407 ConfigurationManager::CreateCoreXmlFile(InstallationContext* pContext)
409 AppLog("------------------------------------------");
410 AppLog("CreateCoreXmlFile() - START");
413 ManifestGenerator manifestGenerator;
414 manifestGenerator.Construct(pContext);
415 ret = manifestGenerator.Write();
419 AppLog("manifestGenerator.Write() is failed.");
421 AppLog("CreateCoreXmlFile() - END");
422 AppLog("------------------------------------------");
428 //ConfigurationManager::CreateHybridServiceDesktopFile(InstallationContext* pContext)
430 // AppLog("HybridServiceDesktopFile - START");
433 // _PackageInfoImpl *pPackageInfoImpl = pContext->GetPackageInfoImpl();
434 // _PackageAppInfoImpl* pAppInfoImpl = null;
435 // ArrayList* pAppList = null;
436 // pAppList = pPackageInfoImpl->GetAppInfoList();
438 // for (int i = 0 ; i < pAppList->GetCount(); i++)
440 // pAppInfoImpl = dynamic_cast<_PackageAppInfoImpl*>(pAppList->GetAt(i));
445 // name.Format(1024, L"%ls", pPackageInfoImpl->GetAppName().GetPointer());
447 // String binaryPath;
448 // binaryPath.Format(1024, L"%ls%ls/%ls", pPackageInfoImpl->GetAppRootPath().GetPointer(), DIR_BIN, pAppInfoImpl->GetName().GetPointer());
451 // iconPath.Format(1024, L"%ls%ls/%ls", pPackageInfoImpl->GetAppRootPath().GetPointer(), DIR_ICONS, pAppInfoImpl->GetMainmenuIcon().GetPointer());
454 // version.Format(1024, L"%ls", pPackageInfoImpl->GetVersion().GetPointer());
457 // desktop.Format(1024, L"[Desktop Entry]\n"
459 // "Name[en_GB]=%ls\n"
460 // "Type=Application\n"
464 // "NoDisplay=true\n"
465 // "X-TIZEN-TaskManage=False\n"
466 // "X-TIZEN-PackageType=tpk\n",
467 // name.GetPointer(), name.GetPointer(), binaryPath.GetPointer(), iconPath.GetPointer(), version.GetPointer());
469 // String desktopPath;
470 // desktopPath.Format(1024, L"/opt/share/applications/%ls.desktop", pAppInfoImpl->GetPackageName().GetPointer());
472 // if (File::IsFileExist(desktopPath) == true)
474 // AppLog("removing desktop filePath=[%ls]", desktopPath.GetPointer());
475 // InstallerUtil::Remove(desktopPath);
478 // InstallerUtil::CreateInfoFile(desktopPath, &desktop);
482 // AppLog("HybridServiceDesktopFile - END");
488 ConfigurationManager::MergeToSystemXmlFile(const String& systemXmlPath, const String& webXmlPath, const String& serviceXmlPath)
490 result r = E_SUCCESS;
492 FileAttributes webXmlAttr;
493 r = File::GetAttributes(webXmlPath, webXmlAttr);
494 TryReturn(!IsFailed(r), false, "File::GetAttributes() failed, webXmlPath=%ls", webXmlPath.GetPointer());
496 FileAttributes serviceAttr;
497 r = File::GetAttributes(serviceXmlPath, serviceAttr);
498 TryReturn(!IsFailed(r), false, "File::GetAttributes() failed, serviceXmlPath=%ls", serviceXmlPath.GetPointer());
500 long long webXmlFileSize = webXmlAttr.GetFileSize();
501 long long serviceXmlFileSize = serviceAttr.GetFileSize();
502 long long mergedSize = webXmlFileSize + serviceXmlFileSize;
505 r = webXml.Construct(webXmlPath, L"r");
506 TryReturn(!IsFailed(r), false, "webXmlPath.Construct is failed");
508 std::unique_ptr<char[]> pMergedBuf(new (std::nothrow) char[mergedSize + 1]);
509 TryReturn(pMergedBuf, false, "pMergedBuf is null");
510 memset(pMergedBuf.get(), 0, mergedSize + 1);
512 int readBytes = webXml.Read(pMergedBuf.get(), webXmlFileSize);
513 TryReturn(readBytes >= 0, false, "webXml.Read is failed");
516 r = serviceXml.Construct(serviceXmlPath, L"r");
517 TryReturn(!IsFailed(r), false, "serviceXmlPath.Construct is failed");
519 std::unique_ptr<char[]> pServiceBuf(new (std::nothrow) char[serviceXmlFileSize + 1]);
520 TryReturn(pServiceBuf, false, "pServiceBuf is null");
521 memset(pServiceBuf.get(), 0, serviceXmlFileSize + 1);
523 readBytes = serviceXml.Read(pServiceBuf.get(), serviceXmlFileSize);
524 TryReturn(readBytes >= 0, false, "serviceXml.Read is failed");
526 char* pManifestTag = strcasestr(pMergedBuf.get(), "</manifest>");
527 TryReturn(pManifestTag, false, "pManifestTag is null");
529 char* pAppTagStart = strcasestr(pServiceBuf.get(), "<ui-application");
530 TryReturn(pAppTagStart, false, "pAppTagStart is null");
532 char* pAppTagEnd = strcasestr(pServiceBuf.get(), "</ui-application>");
533 TryReturn(pAppTagEnd, false, "pAppTagEnd is null");
535 int serviceAppLen = pAppTagEnd - pAppTagStart + strlen("</ui-application>");
537 memcpy(pManifestTag, pAppTagStart, serviceAppLen);
539 char* pManifestEndTag = pManifestTag + serviceAppLen;
540 strcpy(pManifestEndTag, "\n</manifest>");
542 int fileSize = pManifestEndTag - pMergedBuf.get() + strlen("\n</manifest>");
545 r = systemXml.Construct(systemXmlPath, L"w");
546 TryReturn(!IsFailed(r), false, "systemXmlPath.Construct() is failed.");
548 r = systemXml.Write(pMergedBuf.get(), fileSize);
549 TryReturn(!IsFailed(r), false, "systemXmlPath.Write() is failed.");
551 AppLog("pMergedBuf.get()=0x%0x, length=%d", (unsigned int)pMergedBuf.get(), fileSize);
552 InstallerUtil::DumpLogData(pMergedBuf.get(), fileSize);
558 ConfigurationManager::CreateImeSymlink(const String& binaryPath, const String& appId)
562 const char* pExePath = null;
563 const char* pSymlinkPath = null;
565 Directory::Create(IME_PATH, true);
568 exePath.Format(1024, L"%ls.exe", binaryPath.GetPointer());
571 symlinkPath.Format(1024, L"%s/%ls.so", IME_PATH, appId.GetPointer());
573 pExePath = _StringConverter::CopyToCharArrayN(exePath);
574 TryCatch(pExePath, res = false, "pExePath is null");
576 pSymlinkPath = _StringConverter::CopyToCharArrayN(symlinkPath);
577 TryCatch(pSymlinkPath, res = false, "pSymlinkPath is null");
579 err = symlink(pExePath, pSymlinkPath);
581 AppLog("[%s] -> [%s]", pSymlinkPath, pExePath);
585 delete[] pSymlinkPath;
591 ConfigurationManager::FindPrivilege(InstallationContext* pContext, const String& privilege) const
593 TryReturn(pContext, false, "pContext is null.");
596 const ArrayList* pPrivilegeList = pContext->GetPrivilegeList();
600 if (pPrivilegeList->Contains(privilege) == true)
602 AppLog("Privilege = [%ls]", privilege.GetPointer());