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