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 InstallerUtil.cpp
19 * @brief This is the implementation file for %InstallerUtil class.
27 #include <unique_ptr.h>
30 #include <FBaseErrorDefine.h>
32 #include <FIoDirectory.h>
33 #include <FIoRegistry.h>
34 #include <FAppPkgPackageAppInfo.h>
35 #include <FAppPkgPackageInfo.h>
36 #include <FBase_StringConverter.h>
37 #include <FSecCryptoSha2Hash.h>
39 #include <FAppPkg_PackageManagerImpl.h>
40 #include <FAppPkg_PackageInfoImpl.h>
41 #include <FAppPkg_PackageAppInfoImpl.h>
43 #include "InstallerDefs.h"
44 #include "InstallerUtil.h"
45 #include "InstallerManager.h"
46 #include "SmackManager.h"
47 #include "DatabaseManager.h"
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Collection;
51 using namespace Tizen::Base::Utility;
52 using namespace Tizen::App;
53 using namespace Tizen::App::Package;
54 using namespace Tizen::Io;
55 using namespace Tizen::Security::Crypto;
57 InstallerUtil::InstallerUtil(void)
61 InstallerUtil::~InstallerUtil(void)
66 InstallerUtil::Remove(const Tizen::Base::String& filePath)
72 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
73 TryReturn(pFilePath, false, "pFilePath is null");
75 err = lstat(pFilePath.get(), &fileinfo);
78 AppLog("Remove(): skip, path=[%s][%s](%d)", pFilePath.get(), strerror(errno), errno);
82 if (S_ISLNK(fileinfo.st_mode))
84 AppLog("Remove(): symlink=[%s]", pFilePath.get());
85 err = unlink(pFilePath.get());
86 TryReturn(err >= 0, false, "unlink() failed(%s), file=[%s]", strerror(errno), pFilePath.get());
88 else if (S_ISDIR(fileinfo.st_mode))
90 AppLog("Remove(): directory=[%ls]", filePath.GetPointer());
91 r = Directory::Remove(filePath, true);
92 TryReturn(!IsFailed(r), false, "Directory::Remove() failed, filePath=%ls", filePath.GetPointer());
96 AppLog("Remove(): file=[%ls]", filePath.GetPointer());
97 r = File::Remove(filePath);
98 TryReturn(!IsFailed(r), false, "File::Remove() failed, filePath=%ls", filePath.GetPointer());
105 InstallerUtil::Copy(const String& srcFilePath, const String& destFilePath, bool failIfExist)
109 result r = E_SUCCESS;
111 if (failIfExist == true)
113 if (File::IsFileExist(destFilePath) == true)
115 AppLog("Copy(): des=[%ls]: skip", destFilePath.GetPointer());
123 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
124 TryReturn(pBuf, false, "pBuf is null");
126 r = srcFile.Construct(srcFilePath, L"r");
127 TryReturn(!IsFailed(r), false, "srcFile.Construct is failed");
129 r = destFile.Construct(destFilePath, L"w");
130 TryReturn(!IsFailed(r), false, "destFile.Construct is failed");
134 readBytes = srcFile.Read(pBuf.get(), bufSize);
137 r = destFile.Write(pBuf.get(), readBytes);
138 TryReturn(!IsFailed(r), false, "destFile.Write is failed");
141 while (readBytes > 0);
147 InstallerUtil::CopyDirectory(const String& srcFilePath, const String& destFilePath, bool failIfExist)
149 result r = E_SUCCESS;
152 res = File::IsFileExist(srcFilePath);
155 AppLog("CopyDirectory(): src=[%ls]: skip", srcFilePath.GetPointer());
159 std::unique_ptr<Directory> pDir(new (std::nothrow) Directory);
160 TryReturn(pDir, false, "pDir is null.");
162 r = pDir->Construct(srcFilePath);
163 TryReturn(!IsFailed(r), false, "pDir->Construct() failed, srcFilePath=[%ls].", srcFilePath.GetPointer());
165 std::unique_ptr<DirEnumerator> pDirEnum(pDir->ReadN());
166 TryReturn(pDirEnum, false, "pDirEnum is null.");
168 while (pDirEnum->MoveNext() == E_SUCCESS)
170 DirEntry entry = pDirEnum->GetCurrentDirEntry();
172 String entryName = entry.GetName();
173 String srcEntryDir = srcFilePath;
175 srcEntryDir += entryName;
177 if (entryName == L"." || entryName == L"..")
182 // if file or directory is symbolic link, skip this.
183 if (InstallerUtil::IsSymlink(srcEntryDir) == true)
188 String destEntryDir = destFilePath;
189 destEntryDir += L"/";
190 destEntryDir += entryName;
192 if (entry.IsDirectory() == false)
195 Directory::Create(destFilePath, true);
196 InstallerUtil::Copy(srcEntryDir, destEntryDir, failIfExist);
200 Directory::Create(destEntryDir, true);
201 CopyDirectory(srcEntryDir, destEntryDir, failIfExist);
205 AppLog("CopyDirectory(): src=[%ls], dest=[%ls]", srcFilePath.GetPointer(), destFilePath.GetPointer());
210 InstallerUtil::IsSymlink(const Tizen::Base::String& filePath)
213 struct stat fileinfo;
215 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
216 TryReturn(pFilePath, false, "pFilePath is null");
218 err = lstat(pFilePath.get(), &fileinfo);
219 TryReturn(err >= 0, false, "lstat() failed(%s), file=[%s]", strerror(errno), pFilePath.get());
221 if (S_ISLNK(fileinfo.st_mode))
230 InstallerUtil::GetRealPath(const String& filePath, String& realPath)
232 char* pRealPath = null;
234 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
235 TryReturn(pFilePath, false, "pFilePath is null");
237 char tmpPath[PATH_MAX] = {0};
238 pRealPath = realpath(pFilePath.get(), tmpPath);
239 TryReturn(pRealPath, false, "pRealPath is null");
243 AppLog("GetRealPath(): path=[%ls], realPath=[%ls]", filePath.GetPointer(), realPath.GetPointer());
249 InstallerUtil::CreateSymlink(const String& oldPath, const String& newPath, bool SmackLabelToRealPath)
254 res = File::IsFileExist(oldPath);
257 AppLog("CreateSymlink(): oldPath=[%ls] not found.", oldPath.GetPointer());
261 res = File::IsFileExist(newPath);
264 AppLog("CreateSymlink(): newPath=[%ls] is alreay exist.", oldPath.GetPointer());
268 std::unique_ptr<char[]> pOldPath(_StringConverter::CopyToCharArrayN(oldPath));
269 TryReturn(pOldPath, false, "pOldPath is null");
271 std::unique_ptr<char[]> pNewPath(_StringConverter::CopyToCharArrayN(newPath));
272 TryReturn(pNewPath, false, "pNewPath is null");
274 err = symlink(pOldPath.get(), pNewPath.get());
275 TryReturn(err == 0, false, "symlink() is failed(%s), oldpath=[%s], newpath=[%s]", strerror(errno), pOldPath.get(), pNewPath.get());
277 SmackManager smackManager;
280 if (SmackLabelToRealPath == true)
282 smackManager.AddLabelDir(label, newPath);
286 smackManager.AddLabelSymlink(newPath);
289 AppLog("CreateSymlink(): [%ls] -> [%ls]", newPath.GetPointer(), oldPath.GetPointer());
295 InstallerUtil::ChangeMode(const String& filePath, int mode)
299 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
300 TryReturn(pFilePath, false, "pFilePath is null");
302 err = chmod(pFilePath.get(), mode);
303 TryReturn(err == 0, false, "chmod() is failed(%s), file=[%s], mode=[%o]", strerror(errno), pFilePath.get(), mode);
309 InstallerUtil::ChangeOwner(const String& filePath)
313 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
314 TryReturn(pFilePath, false, "pFilePath is null");
316 err = chown(pFilePath.get(), APP_OWNER_ID, APP_GROUP_ID);
317 TryReturn(err == 0, false, "chown() is failed(%s), file=[%s]", strerror(errno), pFilePath.get());
323 InstallerUtil::ChangeDirectoryPermission(const String& file, int mode, bool appOwner)
325 result r = E_SUCCESS;
328 res = File::IsFileExist(file);
331 AppLog("path=[%ls]: skip", file.GetPointer());
335 std::unique_ptr<Directory> pDir(new (std::nothrow) Directory);
336 TryReturn(pDir, false, "pDir is null.");
338 r = pDir->Construct(file);
339 TryReturn(!IsFailed(r), false, "pDir->Construct() failed, file=[%ls]", file.GetPointer());
341 std::unique_ptr<DirEnumerator> pDirEnum(pDir->ReadN());
342 TryReturn(pDirEnum, false, "pDirEnum is null.");
344 while (pDirEnum->MoveNext() == E_SUCCESS)
346 DirEntry entry = pDirEnum->GetCurrentDirEntry();
347 String entryName = entry.GetName();
348 if (entryName.IsEmpty() == true)
350 AppLog("entryName is empty.", entryName.GetPointer());
354 String entryDir = file;
356 entryDir += entryName;
358 if (entryName == L".")
360 if (appOwner == true)
362 InstallerUtil::ChangeOwner(entryDir);
364 InstallerUtil::ChangeMode(entryDir, mode | PERM_EXECUTE);
367 else if (entryName == L"..")
372 if (entry.IsDirectory() == false)
374 if (appOwner == true)
376 InstallerUtil::ChangeOwner(entryDir);
378 InstallerUtil::ChangeMode(entryDir, mode);
382 ChangeDirectoryPermission(entryDir, mode, appOwner);
383 if (appOwner == true)
385 InstallerUtil::ChangeOwner(entryDir);
387 InstallerUtil::ChangeMode(entryDir, mode | PERM_EXECUTE);
391 AppLog("path=[%ls], mode=[%04o], appOwner=[%s]",
392 file.GetPointer(), mode, appOwner?"true":"false");
398 InstallerUtil::IsDrmFile(const String& path)
401 void* pHandle = null;
402 char* pErrorMsg = null;
403 int (*drm_oem_sapps_is_drm_file)(const char* pDcfPath, int dcfPathLen);
405 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(path));
406 TryReturn(pFilePath, false, "pFilePath is null.");
408 pHandle = dlopen("/usr/lib/libdrm-service-core-tizen.so", RTLD_LAZY | RTLD_GLOBAL);
411 AppLog("dlopen() failed. [%ls][%s]", path.GetPointer(), dlerror());
415 drm_oem_sapps_is_drm_file = reinterpret_cast <int (*)(const char*, int)>(dlsym(pHandle, "drm_oem_sapps_is_drm_file"));
416 pErrorMsg = dlerror();
417 if ((pErrorMsg != null) || (drm_oem_sapps_is_drm_file == null))
419 AppLog("dlsym() failed. [%ls][%s]", path.GetPointer(), pErrorMsg);
424 AppLog("[drm] drm_oem_sapps_is_drm_file(%s, %d)", pFilePath.get(), strlen(pFilePath.get()));
425 ret = drm_oem_sapps_is_drm_file(pFilePath.get(), strlen(pFilePath.get()));
426 AppLog("[drm] drm_oem_sapps_is_drm_file(), result = [%d]", ret);
432 AppLog("file[%ls] is DRM file.", path.GetPointer());
442 InstallerUtil::DecryptPackage(const String& path, const String& decryptedPath)
445 void* pHandle = null;
446 char* pErrorMsg = null;
447 int (*drm_oem_sapps_decrypt_package)(const char* pDcfPath, int dcfPathLen, const char* pDecryptedFile, int decryptedFileLen);
449 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(path));
450 TryReturn(pFilePath, false, "pFilePath is null.");
452 std::unique_ptr<char[]> pDecryptedPath(_StringConverter::CopyToCharArrayN(decryptedPath));
453 TryReturn(pDecryptedPath, false, "pDecryptedPath is null.");
455 pHandle = dlopen("/usr/lib/libdrm-service-core-tizen.so", RTLD_LAZY | RTLD_GLOBAL);
458 AppLog("dlopen() failed. [%ls][%s]", path.GetPointer(), dlerror());
462 drm_oem_sapps_decrypt_package = reinterpret_cast <int (*)(const char*, int, const char*, int)>(dlsym(pHandle, "drm_oem_sapps_decrypt_package"));
463 pErrorMsg = dlerror();
464 if ((pErrorMsg != null) || (drm_oem_sapps_decrypt_package == null))
466 AppLog("dlsym() failed. [%ls][%s]", path.GetPointer(), pErrorMsg);
471 AppLog("[drm] drm_oem_sapps_decrypt_package(%s, %d, %s, %d)", pFilePath.get(), strlen(pFilePath.get()), pDecryptedPath.get(), strlen(pDecryptedPath.get()));
472 ret = drm_oem_sapps_decrypt_package(pFilePath.get(), strlen(pFilePath.get()), pDecryptedPath.get(), strlen(pDecryptedPath.get()));
473 AppLog("[drm] drm_oem_sapps_decrypt_package(), result = [%d]", ret);
479 AppLog("[%ls] -> [%ls] is decrypted.", path.GetPointer(), decryptedPath.GetPointer());
489 InstallerUtil::GetCategory(int categoryType)
493 if (categoryType == CATEGORY_TYPE_IME)
497 else if (categoryType == CATEGORY_TYPE_HOME_SCREEN)
499 category = L"home-screen";
501 else if (categoryType == CATEGORY_TYPE_LOCK_SCREEN)
503 category = L"lock-screen";
510 InstallerUtil::GetCategoryType(char* pCategory)
512 CategoryType category = CATEGORY_TYPE_NONE;
514 if (strcasecmp(pCategory, "Ime") == 0)
516 category = CATEGORY_TYPE_IME;
518 else if (strcasecmp(pCategory, "home-screen") == 0)
520 category = CATEGORY_TYPE_HOME_SCREEN;
522 else if (strcasecmp(pCategory, "lock-screen") == 0)
524 category = CATEGORY_TYPE_LOCK_SCREEN;
531 InstallerUtil::CreateSymlinkForAppDirectory(const String& inPath, String& outPath)
535 int length = inPath.GetLength();
536 inPath.SubString(length - PACKAGE_ID_LENGTH, PACKAGE_ID_LENGTH, appId);
539 newPath = PATH_OPT_APPS;
543 if (inPath != newPath)
545 InstallerUtil::CreateSymlink(inPath, newPath);
549 AppLog("CreateSymlinkForAppDirectory(): output path=[%ls]", outPath.GetPointer());
555 InstallerUtil::CreateInfoFile(const String& filePath, const String* pContext)
557 result r = E_SUCCESS;
560 r = file.Construct(filePath, "w");
561 TryReturn(!IsFailed(r), false, "file.Construct() failed, filePath=[%ls]", filePath.GetPointer());
563 AppLog("------------------------------------------");
564 AppLog("CreateInfoFile(), filePath = [%ls]", filePath.GetPointer());
568 r = file.Write(*pContext);
569 TryReturn(!IsFailed(r), false, "file.Write() failed, filePath=[%ls]", filePath.GetPointer());
570 AppLog("string = [%ls]", pContext->GetPointer());
572 AppLog("------------------------------------------");
578 InstallerUtil::CreateFlagFile(const String& filePath)
585 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
586 TryCatch(pFilePath, res = false, "pFilePath is null.");
588 pFile = fopen(pFilePath.get(), "w");
589 TryCatch(pFile, res = false, "fopen(%s, w) failed.", pFilePath.get());
592 TryCatch(fd != -1, res = false, "fileno(%s) failed.", pFilePath.get());
595 AppLog("CreateFlagFile(), filePath = [%ls]", filePath.GetPointer());
607 InstallerUtil::DumpLog(const char* pBuf)
609 TryReturn(pBuf, false, "pBuf is null");
611 char temp[4096] = {0};
612 int bufLen = strlen(pBuf);
613 strncpy(temp, pBuf, sizeof(temp)-1);
615 char* pStart = &temp[0];
617 for (int i = 0; i < bufLen; i++)
622 AppLog("%s", pStart);
623 pStart = temp + i + 1;
630 #define LOG_PRINT_LINE_MAX 20
631 #define LOG_BUFFER_COUNT_MAX 4096
633 InstallerUtil::DumpLogData(char *pData, int dataLen)
635 const char *szData = (const char*)pData;
637 int i = 0, j = 0, idx = 0, idx2 = 0, high = 0, low = 0, temp = 0;
639 char buf[LOG_PRINT_LINE_MAX + 2] = {0};
640 char buf2[(LOG_PRINT_LINE_MAX + 2) * 3] = {0};
641 char buf_out[sizeof(buf) + sizeof(buf2) + 1] = {0};
644 if (dataLen > LOG_BUFFER_COUNT_MAX)
646 dataLen = LOG_BUFFER_COUNT_MAX;
649 // 16 characters by 20 line are proper. // too many logs decrease performance.
650 // if (dataLen > 16*20)
653 AppLog("------------------------------------------");
655 while (i < (int)dataLen)
659 /* make ascii table */
660 if (ch >= 32 && ch <= 128)
668 high = (ch & 0xf0)>>4;
671 buf2[idx2++] = LogChangeHexToStr(high);
672 buf2[idx2++] = LogChangeHexToStr(low);
675 if (idx >= LOG_PRINT_LINE_MAX)
677 memcpy(buf_out, buf2, idx2);
679 buf_out[idx2++] = ' ';
680 buf_out[idx2++] = ' ';
682 memcpy(buf_out + idx2, buf, idx);
683 buf_out[idx2+idx] = '\0';
688 AppLog("%s\n", buf_out);
697 memcpy(buf_out, buf2, idx2);
700 for (j = 0; j < (LOG_PRINT_LINE_MAX * 3) - temp; j++)
702 buf_out[idx2++] = ' ';
705 buf_out[idx2++] = ' ';
706 buf_out[idx2++] = ' ';
708 memcpy(buf_out+idx2, buf, idx);
709 buf_out[idx2+idx] = '\0';
711 AppLog("%s\n", buf_out);
714 AppLog("------------------------------------------");
720 InstallerUtil::LogChangeHexToStr(int hex)
724 const static char hexValues[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 0};
727 if (hex >= 0 && hex <= 0x0F)
733 AppLog("LogChangeHexToStr: Error! [Hex Val: %d]\n", hex);
740 InstallerUtil::CreateLog(const String& logFile)
744 result r = file.Construct(logFile, "w");
754 InstallerUtil::AppendLog(const char* pFunction, int lineNumber, bool fatal, const char* pFormat, ...)
758 InstallerManager *pManager = InstallerManager::GetInstance();
759 if (pManager == null)
764 if (pManager->IsFileLogOn() == false)
769 String logFile = pManager->GetLogFilePath();
770 result r = file.Construct(logFile, "a");
777 va_start(args, pFormat);
778 const int bufSize = 1024;
779 char logs[bufSize+1] = {0};
780 char logs2[bufSize+1] = {0};
783 snprintf(logs, bufSize, " | %s (%d). > %s", (char*)pFunction, lineNumber, pFormat);
787 snprintf(logs, bufSize, "[TRY]| %s (%d). > %s", (char*)pFunction, lineNumber, pFormat);
790 vsnprintf(logs2, bufSize, logs, args);
791 int length = strlen(logs2);
792 logs2[length] = '\n';
794 r = file.Write(logs2, length+1);
801 if (pManager->IsHistoryFileLogOn() == true)
804 String historyLogFilePath = pManager->GetHistoryLogFilePath();
805 r = historyLogFile.Construct(historyLogFilePath, "a");
808 r = historyLogFile.Write(logs2, length+1);
822 InstallerUtil::PrintLog(const String& logFile)
824 InstallerManager *pManager = InstallerManager::GetInstance();
825 if (pManager == null)
830 if (pManager->IsFileLogOn() == false)
836 FileAttributes attribute;
838 result r = File::GetAttributes(logFile, attribute);
845 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
851 r = file.Construct(logFile, "r");
860 memset(pBuf.get(), 0, bufSize);
861 readBytes = file.Read(pBuf.get(), bufSize);
864 fprintf(stderr, "%s", pBuf.get());
867 while (readBytes > 0);
873 InstallerUtil::GetRdsList(const PackageId& packageId, IList* pDeletedList, IList* pAddedList, IList* pModifiedList)
877 char rdsFilePath[1024] = {0};
878 char buffer[1024] = {0};
879 InstallerRdsState state = INSTALLER_RDS_STATE_NONE;
881 snprintf(rdsFilePath, sizeof(rdsFilePath), "%s/%ls/%s", DIR_APPLICATIONS_TMP, packageId.GetPointer(), INSTALLER_RDS_FILE_NAME);
883 fp = fopen(rdsFilePath, "r");
884 TryReturn(fp, false, "fp is null.");
885 AppLog(".rds_delta file");
888 while (fgets(buffer, sizeof(buffer), fp) != null)
890 bool isMetadata = false;
892 if (buffer[0] == '#')
894 if (strcasestr(buffer, INSTALLER_RDS_DELETE_STR))
896 state = INSTALLER_RDS_STATE_DELETE;
898 else if (strcasestr(buffer, INSTALLER_RDS_ADD_STR))
900 state = INSTALLER_RDS_STATE_ADD;
902 else if (strcasestr(buffer, INSTALLER_RDS_MODIFY_STR))
904 state = INSTALLER_RDS_STATE_MODIFY;
910 if (state == INSTALLER_RDS_STATE_NONE)
912 AppLog("Unknown RDS State, INSTALLER_RDS_STATE_NONE");
916 std::unique_ptr<String> pStr(new (std::nothrow) String(buffer));
917 TryCatch(pStr, res = false, "pStr is null.");
918 TryCatch(pStr->IsEmpty() == false, res = false, "pStr is empty.");
921 AppLog(".rds_delta: line(%03d)=[%ls]", line, pStr->GetPointer());
924 if (isMetadata == true)
927 if (state == INSTALLER_RDS_STATE_DELETE)
929 pDeletedList->Add(pStr.release());
931 else if (state == INSTALLER_RDS_STATE_ADD)
933 pAddedList->Add(pStr.release());
935 else if (state == INSTALLER_RDS_STATE_MODIFY)
937 pModifiedList->Add(pStr.release());
940 memset(buffer, 0, sizeof(buffer));
949 InstallerUtil::GetInstallerOperationString(int operation)
951 if (operation == INSTALLER_OPERATION_INSTALL)
955 else if (operation == INSTALLER_OPERATION_UNINSTALL)
959 else if (operation == INSTALLER_OPERATION_REINSTALL)
968 InstallerUtil::GetFileDigest(const String& filePath, String& digestValue)
970 const int bufSize = 64*1024;
972 result r = E_SUCCESS;
975 std::unique_ptr<Sha2Hash> pHash(new (std::nothrow) Sha2Hash());
977 r = pHash->SetAlgorithm("SHA2/256");
978 TryReturn(!IsFailed(r), false, "pHash->SetAlgorithm() is failed.");
980 r = pHash->Initialize();
981 TryReturn(!IsFailed(r), false, "pHash->Initialize() is failed.");
983 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
984 TryReturn(pBuf, false, "pBuf is null");
986 r = file.Construct(filePath, L"r");
987 TryReturn(!IsFailed(r), false, "file.Construct() is failed.");
991 readBytes = file.Read(pBuf.get(), bufSize);
992 AppLog("readBytes for Hash=[%d]", readBytes);
997 r = buffer.Construct((const byte*)pBuf.get(), 0, readBytes, bufSize);
998 TryReturn(!IsFailed(r), false, "buffer.Construct() is failed.");
1000 r = pHash->Update(buffer);
1001 TryReturn(!IsFailed(r), false, "pHash->Update() is failed.");
1004 while (readBytes > 0);
1006 std::unique_ptr<ByteBuffer> pResultBuf(pHash->FinalizeN());
1007 TryReturn(pResultBuf, false, "pResultBuf is null.");
1009 r = StringUtil::EncodeToBase64String(*pResultBuf, digestValue);
1010 TryReturn(!IsFailed(r), false, "EncodeToBase64String() is failed.");
1016 InstallerUtil::ParseN(const String& str, const String& tokenDelimiter)
1018 TryReturn(str.IsEmpty() == false, null, "str is empty.");
1019 TryReturn(tokenDelimiter.IsEmpty() == false, null, "tokenDelimiter is empty.");
1021 std::unique_ptr< HashMap > pMap(new (std::nothrow) HashMap);
1022 TryReturn(pMap, null, "pMap is null.");
1024 result r = pMap->Construct();
1025 TryReturn(!IsFailed(r), null, "pMap->Construct() is failed.");
1027 StringTokenizer strTok(str, tokenDelimiter);
1028 while(strTok.HasMoreTokens() == true)
1031 r = strTok.GetNextToken(token);
1032 TryReturn(!IsFailed(r), null, "strTok.GetNextToken() is failed.");
1034 AppLog("token = [%ls]", token.GetPointer());
1036 StringTokenizer infoTok(token, L"=");
1038 if (infoTok.GetTokenCount() != 2)
1040 AppLog("'=' is not existed.");
1044 std::unique_ptr< String > pKey(new (std::nothrow) String);
1045 r = infoTok.GetNextToken(*pKey);
1046 TryReturn(!IsFailed(r), null, "infoTok.GetNextToken(*pKey) is failed.");
1047 AppLog(" - key = [%ls]", pKey->GetPointer());
1049 std::unique_ptr< String > pValue(new (std::nothrow) String);
1050 r = infoTok.GetNextToken(*pValue);
1051 TryReturn(!IsFailed(r), null, "infoTok.GetNextToken(*pValue) is failed.");
1052 AppLog(" - value = [%ls]", pValue->GetPointer());
1054 r = pMap->Add(pKey.release(), pValue.release());
1055 TryReturn(!IsFailed(r), null, "pMap->Add() is failed.");
1058 if (pMap->GetCount() <= 0)
1060 AppLog("pMap->GetCount() is invalid.");
1064 return pMap.release();
1068 InstallerUtil::TerminateApp(const AppId& appId)
1072 if (_Aul::IsRunning(appId) == true)
1074 AppLog("App(%ls) is running.", appId.GetPointer());
1076 result r = _Aul::TerminateApplication(appId);
1077 TryReturn(r == E_SUCCESS, false, "TerminateApplication() failed. [%ls]", appId.GetPointer());
1079 for (int j = 0; j < TERMINATE_RETRY_COUNT; j++)
1081 res = _Aul::IsRunning(appId);
1084 AppLog("App(%ls) is terminated.", appId.GetPointer());
1089 AppLog("App(%ls) is not terminated yet. wait count = [%d]", appId.GetPointer(), j);
1096 AppLog("App(%ls) can't be terminated.", appId.GetPointer());
1102 AppLog("App(%ls) is not running.", appId.GetPointer());
1109 InstallerUtil::TerminateApps(const PackageId& packageId, bool DisableAutoRestart)
1111 std::unique_ptr< PackageInfo > pPackageInfo(_PackageManagerImpl::GetInstance()->GetPackageInfoN(packageId));
1112 TryReturn(pPackageInfo, false, "GetPackageInfoN() failed.");
1114 _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
1115 TryReturn(pPackageInfoImpl, false, "GetInstance() failed.");
1117 std::unique_ptr< IList > pPackageAppList(pPackageInfoImpl->GetPackageAppInfoListN());
1118 TryReturn(pPackageAppList, false, "GetPackageAppInfoListN() failed.");
1120 for (int i = 0; i < pPackageAppList->GetCount(); i++)
1122 PackageAppInfo* pPackageAppInfo = dynamic_cast < PackageAppInfo* >(pPackageAppList->GetAt(i));
1123 TryReturn(pPackageAppInfo, false, "pPackageAppList->GetAt(%d) failed.", i);
1125 AppId appId = pPackageAppInfo->GetAppId();
1127 if (DisableAutoRestart == true)
1129 _PackageAppInfoImpl* pAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo);
1130 TryReturn(pAppInfoImpl, false, "pAppInfoImpl is null.");
1132 String key("AutoRestart");
1133 if (pAppInfoImpl->GetAppFeature(key) == L"True")
1135 AppLog("AutoRestart = [True]");
1137 DatabaseManager databaseManager;
1138 databaseManager.DisableAutoRestart(appId);
1140 AppLog("AutoRestart[%ls] is updated.", pAppInfoImpl->GetAppFeature(key).GetPointer());
1144 TerminateApp(appId);
1151 InstallerUtil::IsUninstallable(const PackageId& packageId)
1155 std::unique_ptr< PackageInfo > pPackageInfo(_PackageManagerImpl::GetInstance()->GetPackageInfoN(packageId));
1156 TryReturn(pPackageInfo, false, "GetPackageInfoN() failed.");
1158 _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
1159 TryReturn(pPackageInfoImpl, false, "GetInstance() failed.");
1161 res = pPackageInfoImpl->IsUninstallable();
1163 AppLog("packageId[%ls]: Uninstallable = [%s]", packageId.GetPointer(), res?"true":"false");
1169 InstallerUtil::IsCscPackage(const PackageId& packageId, String& cscInfo)
1174 pkgmgrinfo_pkginfo_h handle = null;
1176 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
1177 TryReturn(pPackageId, false, "pPackageId is null.");
1179 result = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &handle);
1180 TryReturn(result == PMINFO_R_OK, false, "pkgmgrinfo_pkginfo_get_pkginfo() failed. result=[%d], package=[%s]", result, pPackageId.get());
1182 result = pkgmgrinfo_pkginfo_get_csc_path(handle, &pPath);
1183 TryReturn(result == PMINFO_R_OK, false, "pkgmgrinfo_pkginfo_get_csc_path() failed. result=[%d], package=[%s]", result, pPackageId.get());
1185 AppLog("csc_path = [%s]", pPath);
1189 if (cscInfo.IsEmpty() == false)
1192 AppLog("packageId[%ls]: cscInfo = [%ls]", packageId.GetPointer(), cscInfo.GetPointer());
1197 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1204 InstallerUtil::IsDefaultExternalStorage()
1208 int mmcStatus = VCONFKEY_SYSMAN_MMC_REMOVED;
1210 res = vconf_get_int("db/setting/default_memory/download_application", &storage);
1211 TryReturn(res == 0, false, "vconf_get_int(db/setting/default_memory/download_application) failed.");
1213 AppLog("Storage = [%d]", storage);
1217 res = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmcStatus);
1218 TryReturn(res == 0, false, "vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) failed.");
1220 if ((mmcStatus == VCONFKEY_SYSMAN_MMC_REMOVED) || (mmcStatus == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED))
1222 AppLog("mmcStatus is MMC_REMOVED or NOT_MOUNTED.");
1226 AppLog("mmcStatus is MMC_MOUNTED.");
1235 InstallerUtil::IsSignatureVerificationEnabled()
1239 String section(L"feature");
1240 String entry(L"signature");
1243 r = reg.Construct(CONFIG_PATH, "r");
1244 TryReturn(!IsFailed(r), false, "CONFIG file is not found.");
1246 r = reg.GetValue(section, entry, value);
1247 TryReturn(!IsFailed(r), false, "GetValue is failed. entry = [%ls]", entry.GetPointer());
1249 AppLog("[%ls is %ls.]", entry.GetPointer(), value.GetPointer());
1260 InstallerUtil::IsAuthorSignatureVerificationEnabled()
1264 String section(L"feature");
1265 String entry(L"author-signature");
1268 r = reg.Construct(CONFIG_PATH, "r");
1269 TryReturn(!IsFailed(r), false, "CONFIG file is not found.");
1271 r = reg.GetValue(section, entry, value);
1272 TryReturn(!IsFailed(r), false, "GetValue is failed. entry = [%ls]", entry.GetPointer());
1274 AppLog("[%ls is %ls.]", entry.GetPointer(), value.GetPointer());
1285 InstallerUtil::GetDirectorySize(const String& rootPath)
1287 long long total = 0;
1292 struct dirent *ep = NULL;
1293 struct stat fileinfo;
1294 char file[FILENAME_MAX] = { 0, };
1295 char *pDirName = null;
1297 pDirName = _StringConverter::CopyToCharArrayN(rootPath);
1298 TryCatch(pDirName, total = 0, "[osp-installer][libtpk] pDirName is null");
1300 dp = opendir(pDirName);
1301 TryCatch(dp, total = 0, "[osp-installer][libtpk] dp is null");
1303 while ((ep = readdir(dp)) != NULL)
1305 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
1310 snprintf(file, FILENAME_MAX, "%s/%s", pDirName, ep->d_name);
1312 if (lstat(file, &fileinfo) < 0)
1317 if (S_ISLNK(fileinfo.st_mode))
1319 AppLog("SYMLINK=%s", file);
1320 total += INSTALLER_BLOCK_SIZE;
1323 else if (S_ISDIR(fileinfo.st_mode))
1325 ret = GetDirectorySize(file);
1326 ret += fileinfo.st_size;
1328 AppLog("DIR=%s, size=%d[%d KB],", file, (int)ret, (int)(ret/1024));
1332 /*It is a file. Calculate the actual size occupied (in terms of 4096 blocks)*/
1333 q = (fileinfo.st_size / INSTALLER_BLOCK_SIZE);
1334 r = (fileinfo.st_size % INSTALLER_BLOCK_SIZE);
1339 total += q * INSTALLER_BLOCK_SIZE;
1343 // AppLog("File=%s, size=%d[%d KB]", file, (q * INSTALLER_BLOCK_SIZE), (q * INSTALLER_BLOCK_SIZE)/1024);
1358 InstallerUtil::GetDisplaySize(const Tizen::Base::String& path)
1360 long long int size = 0;
1361 float sizeRes = 0.0;
1362 String unitStr = L"0 B";
1363 result r = E_SUCCESS;
1365 FileAttributes attr;
1366 r = File::GetAttributes(path, attr);
1367 TryReturn(!IsFailed(r), L"0.0KB", "File::GetAttributes() failed");
1369 if (attr.IsDirectory())
1371 size = GetDirectorySize(path);
1374 size += INSTALLER_BLOCK_SIZE;
1375 AppLog("DIR=%ls, size=%d[%dKB],", path.GetPointer(), (int)size, (int)(size/1024));
1379 size = attr.GetFileSize();
1382 sizeRes = size / 1024.0;
1385 resStr.Format(128, L"%.1f", sizeRes);
1387 int strLen = resStr.GetLength();
1389 for (int i = strLen - 5; i > 0; i = i - 3)
1391 resStr.Insert(L",", i);
1399 Tizen::Base::_ApiVisibility
1400 InstallerUtil::ConvertToNativeApiVisibility(int apiVisibility)
1402 switch(apiVisibility)
1404 case API_VISIBILITY_PUBLIC:
1405 return Tizen::Base::_API_VISIBILITY_PUBLIC;
1407 case API_VISIBILITY_PARTNER:
1408 return Tizen::Base::_API_VISIBILITY_PARTNER;
1410 case API_VISIBILITY_PLATFORM:
1411 return Tizen::Base::_API_VISIBILITY_PLATFORM;
1414 return Tizen::Base::_API_VISIBILITY_NONE;
1419 InstallerUtil::GetPrivilegeLevel(int apiVisibility)
1421 if (apiVisibility == _API_VISIBILITY_PLATFORM)
1423 return PRIVILEGE_LEVEL_PLATFORM;
1425 else if (apiVisibility == _API_VISIBILITY_PARTNER)
1427 return PRIVILEGE_LEVEL_PARTNER;
1430 return PRIVILEGE_LEVEL_PUBLIC;