Fix the crash while installing of ServiceApp
[platform/framework/native/installer.git] / src / Manager / ConfigurationManager.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        ConfigurationManager.cpp
19  * @brief       This is the implementation file for %ConfigurationManager class.
20  */
21
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <unique_ptr.h>
26
27 #include <pkgmgr_parser.h>
28 #include <pkgmgr_installer.h>
29 #include <app2ext_interface.h>
30
31 #include <FBaseUtilStringUtil.h>
32 #include <FIoFile.h>
33 #include <FIoDirectory.h>
34 #include <FSecCertX509CertificatePath.h>
35 #include <FApp_Aul.h>
36 #include <FBase_StringConverter.h>
37
38 #include "ConfigurationManager.h"
39 #include "ManifestGenerator.h"
40 #include "InstallerUtil.h"
41 #include "SmackManager.h"
42
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;
49
50 ConfigurationManager::ConfigurationManager(void)
51 {
52 }
53
54 ConfigurationManager::~ConfigurationManager(void)
55 {
56 }
57
58 bool
59 ConfigurationManager::CreateFile(InstallationContext* pContext)
60 {
61         int err = 0;
62
63         String rootPath;
64         rootPath = pContext->__rootPath;
65
66         String apiVersion;
67         apiVersion = pContext->__apiVersion;
68         String versionInfoFile;
69         versionInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), VERSION_INFO_FILE);
70         CreateInfoFile(versionInfoFile, &apiVersion);
71
72         if (pContext->__isOspCompat == true)
73         {
74                 AppLog("[OspCompat] is detected");
75
76                 String compatInfoFile;
77                 compatInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), COMPAT_INFO_FILE);
78                 CreateInfoFile(compatInfoFile, null);
79         }
80
81         String webServicePrivilege(TIZEN_PRIVILEGE_WEB_SERVICE);
82         if (FindPrivilege(pContext, webServicePrivilege) == true)
83         {
84                 AppLog("WEB_SERVICE privilege is detected. rootPath=[%ls]", rootPath.GetPointer());
85
86                 String webServiceInfoFile;
87                 webServiceInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), WEBSERVICE_INFO_FILE);
88                 CreateInfoFile(webServiceInfoFile, null);
89         }
90
91         IListT<AppData*>* pAppDataList = pContext->__pAppDataList;
92         TryReturn(pAppDataList, false, "pAppDataList is null");
93
94         for (int i = 0 ; i < pAppDataList->GetCount(); i++)
95         {
96                 AppData* pAppData = null;
97                 pAppDataList->GetAt(i, pAppData);
98                 TryReturn(pAppData, false, "pAppData is null");
99
100                 String appName = pAppData->__name;
101                 String appType = pAppData->__type;
102                 AppId appId = pAppData->__appId;
103
104                 String binaryPath;
105                 binaryPath.Format(1024, L"%ls%ls/%ls", rootPath.GetPointer(), DIR_BIN, appName.GetPointer());
106
107                 if (File::IsFileExist(binaryPath) == true)
108                 {
109                         InstallerUtil::Remove(binaryPath);
110                 }
111
112                 if (appType == L"UiApp")
113                 {
114                         //err = symlink(UIAPP_LOADER_PATH, pBinaryPath);
115
116                         AppLog("copy ui app loader");
117                         InstallerUtil::Copy(UIAPP_LOADER_PATH, binaryPath);
118
119                         HashMap* pFeatureList = pAppData->__pFeatureList;
120                         TryReturn(pFeatureList, false, "pFeatureList is null");
121
122                         String coordinateSystem;
123                         String baseScreenSize;
124                         String logicalCoordinate;
125
126                         std::unique_ptr< IMapEnumerator > pEnum(pFeatureList->GetMapEnumeratorN());
127                         TryReturn(pEnum, false, "GetMapEnumeratorN() failed. [%s]", GetErrorMessage(GetLastResult()));
128
129                         while (pEnum->MoveNext() == E_SUCCESS)
130                         {
131                                 String* pKey = static_cast< String* > (pEnum->GetKey());
132                                 TryReturn(pEnum, false, "GetKey() failed. [%s]", GetErrorMessage(GetLastResult()));
133
134                                 String* pValue = static_cast< String* > (pEnum->GetValue());
135                                 TryReturn(pEnum, false, "GetValue() failed. [%s]", GetErrorMessage(GetLastResult()));
136
137                                 if ((*pKey) == L"CoordinateSystem")
138                                 {
139                                         coordinateSystem = (*pValue);
140                                 }
141
142                                 if ((*pKey) == L"BaseScreenSize")
143                                 {
144                                         baseScreenSize = (*pValue);
145                                 }
146
147                                 if ((*pKey) == L"LogicalCoordinate")
148                                 {
149                                         logicalCoordinate = (*pValue);
150                                 }
151                         }
152
153                         String uiScalability;
154                         uiScalability.Format(1024, UISCALABILITY_INFO, coordinateSystem.GetPointer(), baseScreenSize.GetPointer(), logicalCoordinate.GetPointer());
155
156                         String uiScalabilityInfoFile;
157                         uiScalabilityInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), UISCALABILITY_INFO_FILE);
158
159                         CreateInfoFile(uiScalabilityInfoFile, &uiScalability);
160
161                         int categoryType = pAppData->__feature;
162                         if (categoryType != CATEGORY_TYPE_NONE)
163                         {
164                                 String category = InstallerUtil::GetCategory(categoryType);
165                                 category.ToLowerCase();
166
167                                 int type = _Aul::GetAppType(category);
168
169                                 if (category == L"ime")
170                                 {
171                                         CreateImeSymlink(binaryPath, appId);
172                                 }
173
174                                 String typeInfo;
175                                 typeInfo.Format(1024, L"%d", type);
176
177                                 String typeInfoFile;
178                                 typeInfoFile.Format(1024, L"%ls%ls", rootPath.GetPointer(), TYPE_INFO_FILE);
179
180                                 CreateInfoFile(typeInfoFile, &typeInfo);
181                         }
182                 }
183                 else if (appType == L"ServiceApp")
184                 {
185                         if (pContext->__isPreloaded == true || pAppData->__isSystemService == true)
186                         {
187                                 AppLog("copy system service loader");
188                                 InstallerUtil::Copy(SYSTEMSERIVCE_LOADER_PATH, binaryPath);
189                         }
190                         else
191                         {
192                                 AppLog("copy service app loader");
193                                 InstallerUtil::Copy(SERVICEAPP_LOADER_PATH, binaryPath);
194                         }
195                 }
196
197                 InstallerUtil::ChangeMode(binaryPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
198         }
199
200         ArrayList* pContentDataList = pContext->GetContentDataList();
201         if (pContentDataList)
202         {
203                 int contentCount = pContentDataList->GetCount();
204                 for (int i = 0 ; i < contentCount; i++)
205                 {
206                         ContentData* pContentData = static_cast<ContentData*>(pContentDataList->GetAt(i));
207                         if (pContentData)
208                         {
209                                 String contentId = pContentData->GetContentId();
210                                 String oldPath = rootPath + DIR_CONTENTS + L"/" + contentId;
211
212                                 if (File::IsFileExist(SLP_FONT_PATH) == false)
213                                 {
214                                         Directory::Create(SLP_FONT_PATH, false);
215                                         InstallerUtil::ChangeMode(SLP_FONT_PATH, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
216                                 }
217
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);
222
223                                 newPath += L"/";
224                                 newPath += contentId;
225                                 InstallerUtil::CreateSymlink(oldPath, newPath);
226                         }
227                 }
228         }
229
230         if (pContext->__isPreloaded == true)
231         {
232                 pContext->__coreXmlPath.Format(1024, DIR_RO_PACKAGE_SYSTEM_MANIFEST, pContext->__packageId.GetPointer());
233         }
234         else
235         {
236                 pContext->__coreXmlPath.Format(1024, DIR_RW_PACKAGE_SYSTEM_MANIFEST, pContext->__packageId.GetPointer());
237         }
238
239         std::unique_ptr<char[]> pXmlPath(_StringConverter::CopyToCharArrayN(pContext->__coreXmlPath));
240         TryReturn(pXmlPath != null, false, "pXmlPath is null");
241
242         if (pContext->__isHybridService == true)
243         {
244                 String webXmlPath = pXmlPath.get();
245                 webXmlPath += L".wgt";
246                 InstallerUtil::Copy(pXmlPath.get(), webXmlPath);
247
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);
252
253                 String serviceXmlPath = pXmlPath.get();
254                 serviceXmlPath += L".tpk";
255                 InstallerUtil::Copy(pXmlPath.get(), serviceXmlPath);
256                 InstallerUtil::Remove(pXmlPath.get());
257
258                 MergeToSystemXmlFile(pXmlPath.get(), webXmlPath, serviceXmlPath);
259                 InstallerUtil::Remove(webXmlPath);
260                 InstallerUtil::Remove(serviceXmlPath);
261         }
262         else
263         {
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);
268         }
269         
270         AppLog("------------------------------------------");
271         AppLog("sync() - START");
272         sync();
273         AppLog("sync() - END");
274         AppLog("------------------------------------------");
275         
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());
278
279         return true;
280 }
281
282 bool
283 ConfigurationManager::RemoveFile(InstallationContext* pContext)
284 {
285         if (pContext->__isPreloaded == true)
286         {
287                 pContext->__coreXmlPath.Format(1024, DIR_RO_PACKAGE_SYSTEM_MANIFEST, pContext->__packageId.GetPointer());
288         }
289         else
290         {
291                 pContext->__coreXmlPath.Format(1024, DIR_RW_PACKAGE_SYSTEM_MANIFEST, pContext->__packageId.GetPointer());
292         }
293
294         std::unique_ptr<char[]> pXmlPath(_StringConverter::CopyToCharArrayN(pContext->__coreXmlPath));
295         TryReturn(pXmlPath != null, false, "pXmlPath is null");
296
297         if (pContext->__isHybridService == true)
298         {
299                 AppLog("Uninstallation for HybridService - skip");
300
301         }
302         else
303         {
304                 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() START");
305                 if (pkgmgr_parser_parse_manifest_for_uninstallation(pXmlPath.get(), null) != 0)
306                 {
307                         AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() is failed.[%s]", pXmlPath.get());
308                 }
309                 AppLog("pkgmgr_parser_parse_manifest_for_uninstallation() END");
310
311                 InstallerUtil::Remove(pXmlPath.get());
312         }
313
314 //      _PackageInfoImpl* pPackageInfoImpl = pContext->GetPackageInfoImpl();
315 //      TryReturn(pPackageInfoImpl, false, "[osp-installer] pPackageInfoImpl is null.");
316 //
317 //      ArrayList* pAppList = pPackageInfoImpl->GetAppInfoList();
318 //      for (int i = 0 ; i < pAppList->GetCount(); i++)
319 //      {
320 //              _PackageAppInfoImpl* pAppInfoImpl = dynamic_cast<_PackageAppInfoImpl*>(pAppList->GetAt(i));
321 //              if (pAppInfoImpl)
322 //              {
323 //                      String packageName = pAppInfoImpl->GetPackageName();
324 //                      String destPath;
325 //                      char    dest[772] = {0};
326 //
327 //                      destPath.Format(1024, L"%ls/%ls", SLP_APP_PATH, packageName.GetPointer());
328 //                      sprintf(dest, "%ls", destPath.GetPointer());
329 //                      unlink(dest);
330 //              }
331 //      }
332
333         return true;
334 }
335
336 bool
337 ConfigurationManager::RegisterCertInfo(InstallationContext* pContext) const
338 {
339         if (pContext->__isHybridService == true)
340         {
341                 AppLog("Skip - HybridService");
342                 return true;
343         }
344
345         AppLog("START");
346
347         int res = 0;
348         bool result = true;
349         pkgmgr_instcertinfo_h handle = null;
350         String appId = pContext->__packageId;
351         X509CertificatePath* pAuthorCertPath = pContext->GetAuthorCertPath();
352         X509CertificatePath* pDistributorCertPath = pContext->GetDistributorCertPath();
353
354         char* pAppId = _StringConverter::CopyToCharArrayN(appId);
355         TryCatch(pAppId, result = false, "pAppId is null");
356
357         res = pkgmgr_installer_create_certinfo_set_handle(&handle);
358         TryCatch(res == 0, result = false, "pkgmgr_installer_create_certinfo_set_handle() failed.[%d]", res);
359
360         if (pAuthorCertPath)
361         {
362                 AppLog("[AuthorCert]");
363                 result = SetCertHashValue(handle, pAuthorCertPath, PM_SET_AUTHOR_SIGNER_CERT);
364                 TryCatch(result == true, , "RegisterCertHashValue() failed. [pAuthorCertPath]");
365         }
366
367         if (pDistributorCertPath)
368         {
369                 AppLog("[DistributorCert]");
370                 result = SetCertHashValue(handle, pDistributorCertPath, PM_SET_DISTRIBUTOR_SIGNER_CERT);
371                 TryCatch(result == true, , "RegisterCertHashValue() failed. [pDistributorCertPath]");
372         }
373
374         res = pkgmgr_installer_save_certinfo(pAppId, handle);
375         TryCatch(res == 0, result = false, "pkgmgr_installer_save_certinfo(%s) failed.[%d]", pAppId, res);
376
377         AppLog("RegisterCertInfo - END");
378         AppLog("------------------------------------------");
379
380 CATCH:
381         if (handle)
382         {
383                 pkgmgr_installer_destroy_certinfo_set_handle(handle);
384         }
385
386         delete[] pAppId;
387         return result;
388 }
389
390 bool
391 ConfigurationManager::SetCertHashValue(void* pHandle, X509CertificatePath* pCertPath, int certType) const
392 {
393         TryReturn(pCertPath, false, "pCertPath is null.");
394
395         int res = 0;
396         bool result = true;
397         ICertificate* pCert = null;
398         char* pCertValue = null;
399
400         for (int i = 0; i < pCertPath->GetLength(); i++)
401         {
402                 pCert = pCertPath->GetCertificateN(i);
403                 TryCatch(pCert, result = false, "pCert is null.[%i]", i);
404
405                 pCertValue = GetCertValueN(pCert);
406                 TryCatch(pCertValue, result = false, "pCertValue is null.[%i]", i);
407
408                 AppLog("------------------------------------------");
409                 AppLog("CertValue(%d), certType[%d]", i, certType);
410                 AppLog("[%s]", pCertValue);
411
412                 res = pkgmgr_installer_set_cert_value(pHandle, (pkgmgr_instcert_type)certType, pCertValue);
413                 TryCatch(res == 0, result = false, "pkgmgr_installer_set_cert_value(%d) failed.[%d]", i, res);
414
415                 delete pCert;
416                 pCert = null;
417                 delete[] pCertValue;
418                 pCertValue = null;
419
420                 certType--;
421         }
422
423 CATCH:
424         delete pCert;
425         delete[] pCertValue;
426
427         return result;
428 }
429
430 char*
431 ConfigurationManager::GetCertValueN(ICertificate* pCert) const
432 {
433         result r = E_SUCCESS;
434         ByteBuffer* pEncodedData = null;
435         String base64Value;
436         char* pEncodedValue = null;
437
438         pEncodedData = pCert->GetEncodedDataN();
439         TryCatch(pEncodedData, , "pEncodedData is null.");
440
441         r = StringUtil::EncodeToBase64String(*pEncodedData, base64Value);
442         TryCatch(!IsFailed(r), , "StringUtil::EncodeToBase64String is failed.");
443
444         pEncodedValue = _StringConverter::CopyToCharArrayN(base64Value);
445         TryCatch(pEncodedValue, , "pEncodedValue is null");
446
447 CATCH:
448         delete pEncodedData;
449
450         return pEncodedValue;
451 }
452
453 bool
454 ConfigurationManager::UnregisterCertInfo(InstallationContext* pContext) const
455 {
456         if (pContext->__isHybridService == true)
457         {
458                 AppLog("Skip - HybridService");
459                 return true;
460         }
461
462         AppLog("START");
463         int res = 0;
464         bool result = true;
465         String appId = pContext->__packageId;
466
467         char* pAppId = _StringConverter::CopyToCharArrayN(appId);
468         TryCatch(pAppId, result = false, "pAppId is null");
469
470         res = pkgmgr_installer_delete_certinfo(pAppId);
471         TryCatch(res == 0, result = false, "pkgmgr_installer_delete_certinfo(%s) failed.[%d]", pAppId, res);
472
473         AppLog("UnregisterCertInfo - END");
474         AppLog("------------------------------------------");
475
476 CATCH:
477         delete[] pAppId;
478         return result;
479 }
480
481 bool
482 ConfigurationManager::PostInstall(InstallationContext* pContext, bool error) const
483 {
484         app2ext_handle* pHandle = (app2ext_handle*)pContext->__pApp2ExtHandle;
485
486         PackageId packageId = pContext->__packageId;
487         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
488         TryReturn(pPackageId, false, "pPackageId is null");
489
490         if (error == false)
491         {
492                 std::unique_ptr<char[]> pXmlPath(_StringConverter::CopyToCharArrayN(pContext->__coreXmlPath));
493                 TryReturn(pXmlPath != null, false, "pXmlPath is null");
494
495                 AppLog("------------------------------------------");
496                 AppLog("pkgmgr_parser_parse_manifest_for_installation() - START");
497                 int err = pkgmgr_parser_parse_manifest_for_installation(pXmlPath.get(), null);
498                 TryReturn(err == 0, false, "pkgmgr_parser_parse_manifest_for_installation() is failed. error = [%d][%s]", err, pXmlPath.get());
499
500                 AppLog("pkgmgr_parser_parse_manifest_for_installation() - END");
501                 AppLog("------------------------------------------");
502
503                 SmackManager smackManager;
504                 smackManager.Construct(pContext);
505                 smackManager.AddPermissions(packageId);
506         }
507
508         if (pHandle)
509         {
510                 if (error == true)
511                 {
512                         pHandle->interface.post_install(pPackageId.get(), APP2EXT_STATUS_FAILED);
513                         AppLog("[app2sd] post_install(%s, APP2EXT_STATUS_FAILED)", pPackageId.get());
514                 }
515                 else
516                 {
517                         pHandle->interface.post_install(pPackageId.get(), APP2EXT_STATUS_SUCCESS);
518                         AppLog("[app2sd] post_install(%s, APP2EXT_STATUS_SUCCESS)", pPackageId.get());
519                 }
520
521                 app2ext_deinit(pHandle);
522         }
523
524         return true;
525 }
526
527 bool
528 ConfigurationManager::PostUninstall(InstallationContext* pContext) const
529 {
530         app2ext_handle* pHandle = (app2ext_handle*)pContext->__pApp2ExtHandle;
531
532         PackageId packageId = pContext->__packageId;
533         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
534         TryReturn(pPackageId, false, "pPackageId is null.");
535
536         if (pHandle)
537         {
538                 pHandle->interface.post_uninstall(pPackageId.get());
539                 AppLog("[app2sd] post_uninstall(%s)", pPackageId.get());
540
541                 app2ext_deinit(pHandle);
542         }
543
544         SmackManager smackManager;
545         smackManager.Construct(pContext);
546         smackManager.RevokePermissions(packageId);
547
548         return true;
549 }
550
551 bool
552 ConfigurationManager::CreateCoreXmlFile(InstallationContext* pContext)
553 {
554         AppLog("------------------------------------------");
555         AppLog("CreateCoreXmlFile() - START");
556         bool ret = false;
557
558         ManifestGenerator manifestGenerator;
559         manifestGenerator.Construct(pContext);
560         ret = manifestGenerator.Write();
561
562         if (ret == false)
563         {
564                 AppLog("manifestGenerator.Write() is failed.");
565         }
566         AppLog("CreateCoreXmlFile() - END");
567         AppLog("------------------------------------------");
568
569         return ret;
570 }
571
572 //bool
573 //ConfigurationManager::CreateHybridServiceDesktopFile(InstallationContext* pContext)
574 //{
575 //      AppLog("HybridServiceDesktopFile - START");
576 //
577 //      bool ret = true;
578 //      _PackageInfoImpl *pPackageInfoImpl = pContext->GetPackageInfoImpl();
579 //      _PackageAppInfoImpl* pAppInfoImpl = null;
580 //      ArrayList* pAppList = null;
581 //      pAppList = pPackageInfoImpl->GetAppInfoList();
582 //
583 //      for (int i = 0 ; i < pAppList->GetCount(); i++)
584 //      {
585 //              pAppInfoImpl = dynamic_cast<_PackageAppInfoImpl*>(pAppList->GetAt(i));
586 //
587 //              if (pAppInfoImpl)
588 //              {
589 //                      String name;
590 //                      name.Format(1024, L"%ls", pPackageInfoImpl->GetAppName().GetPointer());
591 //
592 //                      String binaryPath;
593 //                      binaryPath.Format(1024, L"%ls%ls/%ls", pPackageInfoImpl->GetAppRootPath().GetPointer(), DIR_BIN, pAppInfoImpl->GetName().GetPointer());
594 //
595 //                      String iconPath;
596 //                      iconPath.Format(1024, L"%ls%ls/%ls", pPackageInfoImpl->GetAppRootPath().GetPointer(), DIR_ICONS, pAppInfoImpl->GetMainmenuIcon().GetPointer());
597 //
598 //                      String version;
599 //                      version.Format(1024, L"%ls", pPackageInfoImpl->GetVersion().GetPointer());
600 //
601 //                      String desktop;
602 //                      desktop.Format(1024, L"[Desktop Entry]\n"
603 //                                                                                                        "Name=%ls\n"
604 //                                                                                                              "Name[en_GB]=%ls\n"
605 //                                                                                                              "Type=Application\n"
606 //                                                                                                      "Exec=%ls\n"
607 //                                                                                                        "Icon=%ls\n"
608 //                                                                                                        "Version=%ls\n"
609 //                                                                                                        "NoDisplay=true\n"
610 //                                                                                                        "X-TIZEN-TaskManage=False\n"
611 //                                                                                                        "X-TIZEN-PackageType=tpk\n",
612 //                                                                                                        name.GetPointer(), name.GetPointer(), binaryPath.GetPointer(), iconPath.GetPointer(), version.GetPointer());
613 //
614 //                      String desktopPath;
615 //                      desktopPath.Format(1024, L"/opt/share/applications/%ls.desktop", pAppInfoImpl->GetPackageName().GetPointer());
616 //
617 //                      if (File::IsFileExist(desktopPath) == true)
618 //                      {
619 //                              AppLog("removing desktop filePath=[%ls]", desktopPath.GetPointer());
620 //                              InstallerUtil::Remove(desktopPath);
621 //                      }
622 //
623 //                      CreateInfoFile(desktopPath, &desktop);
624 //              }
625 //      }
626 //
627 //      AppLog("HybridServiceDesktopFile - END");
628 //
629 //      return ret;
630 //}
631
632 bool
633 ConfigurationManager::MergeToSystemXmlFile(const String& systemXmlPath, const String& webXmlPath, const String& serviceXmlPath)
634 {
635         result r = E_SUCCESS;
636
637         FileAttributes webXmlAttr;
638         r = File::GetAttributes(webXmlPath, webXmlAttr);
639         TryReturn(!IsFailed(r), false, "File::GetAttributes() failed, webXmlPath=%ls", webXmlPath.GetPointer());
640
641         FileAttributes serviceAttr;
642         r = File::GetAttributes(serviceXmlPath, serviceAttr);
643         TryReturn(!IsFailed(r), false, "File::GetAttributes() failed, serviceXmlPath=%ls", serviceXmlPath.GetPointer());
644
645         long long webXmlFileSize = webXmlAttr.GetFileSize();
646         long long serviceXmlFileSize = serviceAttr.GetFileSize();
647         long long mergedSize = webXmlFileSize + serviceXmlFileSize;
648
649         File webXml;
650         r = webXml.Construct(webXmlPath, L"r");
651         TryReturn(!IsFailed(r), false, "webXmlPath.Construct is failed");
652
653         std::unique_ptr<char[]> pMergedBuf(new (std::nothrow) char[mergedSize + 1]);
654         TryReturn(pMergedBuf, false, "pMergedBuf is null");
655         memset(pMergedBuf.get(), 0, mergedSize + 1);
656
657         int readBytes = webXml.Read(pMergedBuf.get(), webXmlFileSize);
658         TryReturn(readBytes >= 0, false, "webXml.Read is failed");
659
660         File serviceXml;
661         r = serviceXml.Construct(serviceXmlPath, L"r");
662         TryReturn(!IsFailed(r), false, "serviceXmlPath.Construct is failed");
663
664         std::unique_ptr<char[]> pServiceBuf(new (std::nothrow) char[serviceXmlFileSize + 1]);
665         TryReturn(pServiceBuf, false, "pServiceBuf is null");
666         memset(pServiceBuf.get(), 0, serviceXmlFileSize + 1);
667
668         readBytes = serviceXml.Read(pServiceBuf.get(), serviceXmlFileSize);
669         TryReturn(readBytes >= 0, false, "serviceXml.Read is failed");
670
671         char* pManifestTag = strcasestr(pMergedBuf.get(), "</manifest>");
672         TryReturn(pManifestTag, false, "pManifestTag is null");
673
674         char* pAppTagStart = strcasestr(pServiceBuf.get(), "<ui-application");
675         TryReturn(pAppTagStart, false, "pAppTagStart is null");
676
677         char* pAppTagEnd = strcasestr(pServiceBuf.get(), "</ui-application>");
678         TryReturn(pAppTagEnd, false, "pAppTagEnd is null");
679
680         int serviceAppLen = pAppTagEnd - pAppTagStart + strlen("</ui-application>");
681
682         memcpy(pManifestTag, pAppTagStart, serviceAppLen);
683
684         char* pManifestEndTag = pManifestTag + serviceAppLen;
685         strcpy(pManifestEndTag, "\n</manifest>");
686
687         int fileSize = pManifestEndTag - pMergedBuf.get() + strlen("\n</manifest>");
688
689         File systemXml;
690         r = systemXml.Construct(systemXmlPath, L"w");
691         TryReturn(!IsFailed(r), false, "systemXmlPath.Construct is failed");
692
693         systemXml.Write(pMergedBuf.get(), fileSize);
694
695         AppLog("pMergedBuf.get()=0x%0x, length=%d", (unsigned int)pMergedBuf.get(), fileSize);
696         InstallerUtil::DumpLogData(pMergedBuf.get(), fileSize);
697
698         return true;
699 }
700
701 bool
702 ConfigurationManager::CreateInfoFile(const String& filePath, const String* pContext)
703 {
704         result r = E_SUCCESS;
705         File file;
706
707         r = file.Construct(filePath, "w");
708         TryReturn(!IsFailed(r), false, "file.Construct() failed, filePath=[%ls]", filePath.GetPointer());
709
710         AppLog("------------------------------------------");
711         AppLog("CreateInfoFile(), filePath = [%ls]", filePath.GetPointer());
712
713         if (pContext)
714         {
715                 r = file.Write(*pContext);
716                 TryReturn(!IsFailed(r), false, "file.Write() failed, filePath=[%ls]", filePath.GetPointer());
717                 AppLog("string = [%ls]", pContext->GetPointer());
718         }
719         AppLog("------------------------------------------");
720
721         return true;
722 }
723
724 bool
725 ConfigurationManager::CreateImeSymlink(const String& binaryPath, const String& appId)
726 {
727         bool res = true;
728         int err = 0;
729         const char* pExePath = null;
730         const char* pSymlinkPath = null;
731
732         Directory::Create(IME_PATH, true);
733
734         String exePath;
735         exePath.Format(1024, L"%ls.exe", binaryPath.GetPointer());
736
737         String symlinkPath;
738         symlinkPath.Format(1024, L"%s/%ls.so", IME_PATH, appId.GetPointer());
739
740         pExePath = _StringConverter::CopyToCharArrayN(exePath);
741         TryCatch(pExePath, res = false, "pExePath is null");
742
743         pSymlinkPath = _StringConverter::CopyToCharArrayN(symlinkPath);
744         TryCatch(pSymlinkPath, res = false, "pSymlinkPath is null");
745
746         err = symlink(pExePath, pSymlinkPath);
747
748         AppLog("[%s] -> [%s]", pSymlinkPath, pExePath);
749
750 CATCH:
751         delete[] pExePath;
752         delete[] pSymlinkPath;
753
754         return res;
755 }
756
757 bool
758 ConfigurationManager::FindPrivilege(InstallationContext* pContext, const String& privilege) const
759 {
760         TryReturn(pContext, false, "pContext is null.");
761
762         bool ret = false;
763         const ArrayList* pPrivilegeList = pContext->GetPrivilegeList();
764
765         if (pPrivilegeList)
766         {
767                 if (pPrivilegeList->Contains(privilege) == true)
768                 {
769                         AppLog("Privilege = [%ls]", privilege.GetPointer());
770                         ret = true;
771                 }
772         }
773
774         return ret;
775 }