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.
26 #include <unique_ptr.h>
28 #include <FBaseErrorDefine.h>
30 #include <FIoDirectory.h>
31 #include <FBase_StringConverter.h>
32 #include <FSecCryptoSha2Hash.h>
34 #include "InstallerDefs.h"
35 #include "InstallerUtil.h"
36 #include "InstallerManager.h"
37 #include "SmackManager.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Utility;
42 using namespace Tizen::App;
43 using namespace Tizen::Io;
44 using namespace Tizen::Security::Crypto;
46 InstallerUtil::InstallerUtil(void)
50 InstallerUtil::~InstallerUtil(void)
55 InstallerUtil::Remove(const Tizen::Base::String& filePath)
61 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
62 TryReturn(pFilePath, false, "pFilePath is null");
64 err = lstat(pFilePath.get(), &fileinfo);
67 AppLog("Remove(): [%s] - %s[errno(%d)]: skip", pFilePath.get(), strerror(errno), errno);
71 if (S_ISLNK(fileinfo.st_mode))
73 AppLog("Remove(): symlink, path=[%s]", pFilePath.get());
74 err = unlink(pFilePath.get());
75 TryReturn(err >= 0, false, "unlink() failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
77 else if (S_ISDIR(fileinfo.st_mode))
79 AppLog("Remove(): directory, path=[%ls]", filePath.GetPointer());
80 r = Directory::Remove(filePath, true);
81 TryReturn(!IsFailed(r), false, "Directory::Remove() failed, filePath=%ls", filePath.GetPointer());
85 AppLog("Remove(): file, path=[%ls]", filePath.GetPointer());
86 r = File::Remove(filePath);
87 TryReturn(!IsFailed(r), false, "File::Remove() failed, filePath=%ls", filePath.GetPointer());
94 InstallerUtil::Copy(const String& srcFilePath, const String& destFilePath)
100 // AppLog("+ Copy(): src=[%ls], dest=[%ls]", srcFilePath.GetPointer(), destFilePath.GetPointer());
105 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
106 TryReturn(pBuf, false, "pBuf is null");
108 r = srcFile.Construct(srcFilePath, L"r");
109 TryReturn(!IsFailed(r), false, "srcFile.Construct is failed");
111 r = destFile.Construct(destFilePath, L"w");
112 TryReturn(!IsFailed(r), false, "destFile.Construct is failed");
116 readBytes = srcFile.Read(pBuf.get(), bufSize);
119 r = destFile.Write(pBuf.get(), readBytes);
120 TryReturn(!IsFailed(r), false, "destFile.Write is failed");
123 while (readBytes > 0);
129 InstallerUtil::CopyDirectory(const String& srcFilePath, const String& destFilePath)
131 result r = E_SUCCESS;
134 res = File::IsFileExist(srcFilePath);
137 AppLog("CopyDirectory(): src=[%ls]: skip", srcFilePath.GetPointer());
141 std::unique_ptr<Directory> pDir(new (std::nothrow) Directory);
142 TryReturn(pDir, false, "pDir is null.");
144 r = pDir->Construct(srcFilePath);
145 TryReturn(!IsFailed(r), false, "pDir->Construct() failed, srcFilePath=[%ls].", srcFilePath.GetPointer());
147 std::unique_ptr<DirEnumerator> pDirEnum(pDir->ReadN());
148 TryReturn(pDirEnum, false, "pDirEnum is null.");
150 while (pDirEnum->MoveNext() == E_SUCCESS)
152 DirEntry entry = pDirEnum->GetCurrentDirEntry();
154 String entryName = entry.GetName();
155 String srcEntryDir = srcFilePath;
157 srcEntryDir += entryName;
159 if (entryName == L"." || entryName == L"..")
164 // if file or directory is symbolic link, skip this.
165 if (InstallerUtil::IsSymlink(srcEntryDir) == true)
170 String destEntryDir = destFilePath;
171 destEntryDir += L"/";
172 destEntryDir += entryName;
174 if (entry.IsDirectory() == false)
177 Directory::Create(destFilePath, true);
178 InstallerUtil::Copy(srcEntryDir, destEntryDir);
182 Directory::Create(destEntryDir, true);
183 CopyDirectory(srcEntryDir, destEntryDir);
187 AppLog("CopyDirectory(): src=[%ls], dest=[%ls]", srcFilePath.GetPointer(), destFilePath.GetPointer());
192 InstallerUtil::IsSymlink(const Tizen::Base::String& filePath)
195 struct stat fileinfo;
197 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
198 TryReturn(pFilePath, false, "pFilePath is null");
200 err = lstat(pFilePath.get(), &fileinfo);
201 TryReturn(err >= 0, false, "lstat() failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
203 if (S_ISLNK(fileinfo.st_mode))
212 InstallerUtil::GetRealPath(const String& filePath, String& realPath)
214 char* pRealPath = null;
216 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
217 TryReturn(pFilePath, false, "pFilePath is null");
219 char tmpPath[PATH_MAX] = {0};
220 pRealPath = realpath(pFilePath.get(), tmpPath);
221 TryReturn(pRealPath, false, "pRealPath is null");
225 AppLog("GetRealPath(): path=[%ls], realPath=[%ls]", filePath.GetPointer(), realPath.GetPointer());
231 InstallerUtil::CreateSymlink(const String& oldPath, const String& newPath)
236 res = File::IsFileExist(oldPath);
239 AppLog("CreateSymlink(): oldPath=[%ls] not found", oldPath.GetPointer());
243 std::unique_ptr<char[]> pOldPath(_StringConverter::CopyToCharArrayN(oldPath));
244 TryReturn(pOldPath, false, "pOldPath is null");
246 std::unique_ptr<char[]> pNewPath(_StringConverter::CopyToCharArrayN(newPath));
247 TryReturn(pNewPath, false, "pNewPath is null");
249 err = symlink(pOldPath.get(), pNewPath.get());
250 TryReturn(err == 0, false, "symlink() is failed(%s), oldpath=[%s], newpath=[%s]", strerror(errno), pOldPath.get(), pNewPath.get());
252 SmackManager smackManager;
254 smackManager.AddLabelDir(label, newPath);
256 AppLog("\n >> CreateSymlink(): [%ls] -> [%ls]", newPath.GetPointer(), oldPath.GetPointer());
262 InstallerUtil::ChangeMode(const String& filePath, int mode)
266 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
267 TryReturn(pFilePath, false, "pFilePath is null");
269 err = chmod(pFilePath.get(), mode);
270 TryReturn(err == 0, false, "chmod() is failed(%s), filepath=[%s], mode=[%o]", strerror(errno), pFilePath.get(), mode);
276 InstallerUtil::ChangeOwner(const String& filePath)
280 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
281 TryReturn(pFilePath, false, "pFilePath is null");
283 err = chown(pFilePath.get(), APP_OWNER_ID, APP_GROUP_ID);
284 TryReturn(err == 0, false, "chown() is failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
290 InstallerUtil::ChangeDirectoryPermission(const String& filePath, int mode, bool appOwner)
292 result r = E_SUCCESS;
295 res = File::IsFileExist(filePath);
298 AppLog("path=[%ls]: skip", filePath.GetPointer());
302 std::unique_ptr<Directory> pDir(new (std::nothrow) Directory);
303 TryReturn(pDir, false, "pDir is null.");
305 r = pDir->Construct(filePath);
306 TryReturn(!IsFailed(r), false, "pDir->Construct() failed, filePath=[%ls]", filePath.GetPointer());
308 std::unique_ptr<DirEnumerator> pDirEnum(pDir->ReadN());
309 TryReturn(pDirEnum, false, "pDirEnum is null.");
311 while (pDirEnum->MoveNext() == E_SUCCESS)
313 DirEntry entry = pDirEnum->GetCurrentDirEntry();
314 String entryName = entry.GetName();
315 if (entryName.IsEmpty() == true)
317 AppLog("entryName is empty.", entryName.GetPointer());
321 String entryDir = filePath;
323 entryDir += entryName;
325 if (entryName == L".")
327 if (appOwner == true)
329 InstallerUtil::ChangeOwner(entryDir);
331 InstallerUtil::ChangeMode(entryDir, mode | PERM_EXECUTE);
334 else if (entryName == L"..")
339 if (entry.IsDirectory() == false)
341 if (appOwner == true)
343 InstallerUtil::ChangeOwner(entryDir);
345 InstallerUtil::ChangeMode(entryDir, mode);
349 ChangeDirectoryPermission(entryDir, mode, appOwner);
350 if (appOwner == true)
352 InstallerUtil::ChangeOwner(entryDir);
354 InstallerUtil::ChangeMode(entryDir, mode | PERM_EXECUTE);
358 AppLog("\n >> path=[%ls], mode=[%04o], appOwner=[%s]",
359 filePath.GetPointer(), mode, appOwner?"true":"false");
365 InstallerUtil::IsDrmFile(const String& path)
371 InstallerUtil::DecryptPackage(const String& packagePath)
377 InstallerUtil::GetCategory(int categoryType)
381 if (categoryType == CATEGORY_TYPE_IME)
385 else if (categoryType == CATEGORY_TYPE_HOME_SCREEN)
387 category = L"home-screen";
389 else if (categoryType == CATEGORY_TYPE_LOCK_SCREEN)
391 category = L"lock-screen";
398 InstallerUtil::GetCategoryType(char* pCategory)
400 CategoryType category = CATEGORY_TYPE_NONE;
402 if (strcasecmp(pCategory, "Ime") == 0)
404 category = CATEGORY_TYPE_IME;
406 else if (strcasecmp(pCategory, "home-screen") == 0)
408 category = CATEGORY_TYPE_HOME_SCREEN;
410 else if (strcasecmp(pCategory, "lock-screen") == 0)
412 category = CATEGORY_TYPE_LOCK_SCREEN;
419 InstallerUtil::CreateSymlinkForAppDirectory(const String& inPath, String& outPath)
423 int length = inPath.GetLength();
424 inPath.SubString(length - PACKAGE_ID_LENGTH, PACKAGE_ID_LENGTH, appId);
427 newPath = PATH_OPT_APPS;
431 if (inPath != newPath)
433 InstallerUtil::CreateSymlink(inPath, newPath);
437 AppLog("CreateSymlinkForAppDirectory(): output path=[%ls]", outPath.GetPointer());
443 InstallerUtil::CreateInfoFile(const String& filePath, const String* pContext)
445 result r = E_SUCCESS;
448 r = file.Construct(filePath, "w");
449 TryReturn(!IsFailed(r), false, "file.Construct() failed, filePath=[%ls]", filePath.GetPointer());
451 AppLog("------------------------------------------");
452 AppLog("CreateInfoFile(), filePath = [%ls]", filePath.GetPointer());
456 r = file.Write(*pContext);
457 TryReturn(!IsFailed(r), false, "file.Write() failed, filePath=[%ls]", filePath.GetPointer());
458 AppLog("string = [%ls]", pContext->GetPointer());
460 AppLog("------------------------------------------");
466 InstallerUtil::DumpLog(const char* pBuf)
468 char temp[4096] = {0};
469 TryReturn(pBuf, false, "pBuf is null");
471 int bufLen = strlen(pBuf);
472 strncpy(temp, pBuf, sizeof(temp));
474 char *pStart = &temp[0];
476 for (int i = 0; i < bufLen; i++)
481 AppLog("%s", pStart);
482 pStart = temp + i + 1;
489 #define LOG_PRINT_LINE_MAX 20
490 #define LOG_BUFFER_COUNT_MAX 4096
492 InstallerUtil::DumpLogData(char *pData, int dataLen)
494 const char *szData = (const char*)pData;
496 int i = 0, j = 0, idx = 0, idx2 = 0, high = 0, low = 0, temp = 0;
498 char buf[LOG_PRINT_LINE_MAX + 2] = {0};
499 char buf2[(LOG_PRINT_LINE_MAX + 2) * 3] = {0};
500 char buf_out[sizeof(buf) + sizeof(buf2) + 1] = {0};
503 if (dataLen > LOG_BUFFER_COUNT_MAX)
505 dataLen = LOG_BUFFER_COUNT_MAX;
508 // 16 characters by 20 line are proper. // too many logs decrease performance.
509 // if (dataLen > 16*20)
512 AppLog("------------------------------------------");
514 while (i < (int)dataLen)
518 /* make ascii table */
519 if (ch >= 32 && ch <= 128)
527 high = (ch & 0xf0)>>4;
530 buf2[idx2++] = LogChangeHexToStr(high);
531 buf2[idx2++] = LogChangeHexToStr(low);
534 if (idx >= LOG_PRINT_LINE_MAX)
536 memcpy(buf_out, buf2, idx2);
538 buf_out[idx2++] = ' ';
539 buf_out[idx2++] = ' ';
541 memcpy(buf_out + idx2, buf, idx);
542 buf_out[idx2+idx] = '\0';
547 AppLog("%s\n", buf_out);
556 memcpy(buf_out, buf2, idx2);
559 for (j = 0; j < (LOG_PRINT_LINE_MAX * 3) - temp; j++)
561 buf_out[idx2++] = ' ';
564 buf_out[idx2++] = ' ';
565 buf_out[idx2++] = ' ';
567 memcpy(buf_out+idx2, buf, idx);
568 buf_out[idx2+idx] = '\0';
570 AppLog("%s\n", buf_out);
573 AppLog("------------------------------------------");
579 InstallerUtil::LogChangeHexToStr(int hex)
583 const static char hexValues[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 0};
586 if (hex >= 0 && hex <= 0x0F)
592 AppLog("LogChangeHexToStr: Error! [Hex Val: %d]\n", hex);
599 InstallerUtil::CreateLog(const String& logFile)
603 result r = file.Construct(logFile, "w");
613 InstallerUtil::AppendLog(const char* pFunction, int lineNumber, bool fatal, const char* pFormat, ...)
617 InstallerManager *pManager = InstallerManager::GetInstance();
618 if (pManager == null)
623 if (pManager->IsFileLogOn() == false)
628 String logFile = pManager->GetLogFilePath();
629 result r = file.Construct(logFile, "a");
636 va_start(args, pFormat);
637 const int bufSize = 1024;
638 char logs[bufSize+1] = {0};
639 char logs2[bufSize+1] = {0};
642 snprintf(logs, bufSize, " | %s (%d). > %s", (char*)pFunction, lineNumber, pFormat);
646 snprintf(logs, bufSize, "[TRY]| %s (%d). > %s", (char*)pFunction, lineNumber, pFormat);
649 vsnprintf(logs2, bufSize, logs, args);
650 int length = strlen(logs2);
651 logs2[length] = '\n';
653 r = file.Write(logs2, length+1);
665 InstallerUtil::PrintLog(const String& logFile)
667 InstallerManager *pManager = InstallerManager::GetInstance();
668 if (pManager == null)
673 if (pManager->IsFileLogOn() == false)
679 FileAttributes attribute;
681 result r = File::GetAttributes(logFile, attribute);
688 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
694 r = file.Construct(logFile, "r");
703 memset(pBuf.get(), 0, bufSize);
704 readBytes = file.Read(pBuf.get(), bufSize);
707 fprintf(stderr, "%s", pBuf.get());
710 while (readBytes > 0);
716 InstallerUtil::GetRdsList(const PackageId& packageId, IList* pDeletedList, IList* pAddedList, IList* pModifiedList)
720 char rdsFilePath[1024] = {0};
721 char buffer[1024] = {0};
722 InstallerRdsState state = INSTALLER_RDS_STATE_NONE;
724 snprintf(rdsFilePath, sizeof(rdsFilePath), "%s/%ls/%s", DIR_APPLICATIONS_TMP, packageId.GetPointer(), INSTALLER_RDS_FILE_NAME);
726 fp = fopen(rdsFilePath, "r");
727 TryReturn(fp, false, "fp is null.");
728 AppLog(".rds_delta file");
731 while (fgets(buffer, sizeof(buffer), fp) != null)
733 bool isMetadata = false;
735 if (buffer[0] == '#')
737 if (strcasestr(buffer, INSTALLER_RDS_DELETE_STR))
739 state = INSTALLER_RDS_STATE_DELETE;
741 else if (strcasestr(buffer, INSTALLER_RDS_ADD_STR))
743 state = INSTALLER_RDS_STATE_ADD;
745 else if (strcasestr(buffer, INSTALLER_RDS_MODIFY_STR))
747 state = INSTALLER_RDS_STATE_MODIFY;
753 if (state == INSTALLER_RDS_STATE_NONE)
755 AppLog("Unknown RDS State, INSTALLER_RDS_STATE_NONE");
759 std::unique_ptr<String> pStr(new (std::nothrow) String(buffer));
760 TryCatch(pStr, res = false, "pStr is null.");
761 TryCatch(pStr->IsEmpty() == false, res = false, "pStr is empty.");
764 AppLog(".rds_delta: line(%03d)=[%ls]", line, pStr->GetPointer());
767 if (isMetadata == true)
770 if (state == INSTALLER_RDS_STATE_DELETE)
772 pDeletedList->Add(pStr.release());
774 else if (state == INSTALLER_RDS_STATE_ADD)
776 pAddedList->Add(pStr.release());
778 else if (state == INSTALLER_RDS_STATE_MODIFY)
780 pModifiedList->Add(pStr.release());
783 memset(buffer, 0, sizeof(buffer));
792 InstallerUtil::GetInstallerOperationString(int operation)
794 if (operation == INSTALLER_OPERATION_INSTALL)
798 else if (operation == INSTALLER_OPERATION_UNINSTALL)
802 else if (operation == INSTALLER_OPERATION_REINSTALL)
811 InstallerUtil::GetFileDigest(const String& filePath, String& digestValue)
813 const int bufSize = 64*1024;
815 result r = E_SUCCESS;
818 std::unique_ptr<Sha2Hash> pHash(new (std::nothrow) Sha2Hash());
820 r = pHash->SetAlgorithm("SHA2/256");
821 TryReturn(!IsFailed(r), false, "pHash->SetAlgorithm() is failed.");
823 r = pHash->Initialize();
824 TryReturn(!IsFailed(r), false, "pHash->Initialize() is failed.");
826 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
827 TryReturn(pBuf, false, "pBuf is null");
829 r = file.Construct(filePath, L"r");
830 TryReturn(!IsFailed(r), false, "file.Construct() is failed.");
834 readBytes = file.Read(pBuf.get(), bufSize);
835 AppLog("readBytes for Hash=[%d]", readBytes);
840 r = buffer.Construct((const byte*)pBuf.get(), 0, readBytes, bufSize);
841 TryReturn(!IsFailed(r), false, "buffer.Construct() is failed.");
843 r = pHash->Update(buffer);
844 TryReturn(!IsFailed(r), false, "pHash->Update() is failed.");
847 while (readBytes > 0);
849 std::unique_ptr<ByteBuffer> pResultBuf(pHash->FinalizeN());
850 TryReturn(pResultBuf, false, "pResultBuf is null.");
852 r = StringUtil::EncodeToBase64String(*pResultBuf, digestValue);
853 TryReturn(!IsFailed(r), false, "EncodeToBase64String() is failed.");