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>
27 #include <drm-oem-intel.h>
29 #include <FBaseErrorDefine.h>
31 #include <FIoDirectory.h>
32 #include <FBase_StringConverter.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::App;
42 using namespace Tizen::Io;
44 InstallerUtil::InstallerUtil(void)
48 InstallerUtil::~InstallerUtil(void)
53 InstallerUtil::Remove(const Tizen::Base::String& filePath)
59 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
60 TryReturn(pFilePath, false, "pFilePath is null");
62 err = lstat(pFilePath.get(), &fileinfo);
65 AppLog("Remove(): [%s] - %s[errno(%d)]: skip", pFilePath.get(), strerror(errno), errno);
69 if (S_ISLNK(fileinfo.st_mode))
71 AppLog("Remove(): symlink, path=[%s]", pFilePath.get());
72 err = unlink(pFilePath.get());
73 TryReturn(err >= 0, false, "unlink() failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
75 else if (S_ISDIR(fileinfo.st_mode))
77 AppLog("Remove(): directory, path=[%ls]", filePath.GetPointer());
78 r = Directory::Remove(filePath, true);
79 TryReturn(!IsFailed(r), false, "Directory::Remove() failed, filePath=%ls", filePath.GetPointer());
83 AppLog("Remove(): file, path=[%ls]", filePath.GetPointer());
84 r = File::Remove(filePath);
85 TryReturn(!IsFailed(r), false, "File::Remove() failed, filePath=%ls", filePath.GetPointer());
92 InstallerUtil::Copy(const String& srcFilePath, const String& destFilePath)
98 // AppLog("+ Copy(): src=[%ls], dest=[%ls]", srcFilePath.GetPointer(), destFilePath.GetPointer());
103 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
104 TryReturn(pBuf, false, "pBuf is null");
106 r = srcFile.Construct(srcFilePath, L"r");
107 TryReturn(!IsFailed(r), false, "srcFile.Construct is failed");
109 r = destFile.Construct(destFilePath, L"w");
110 TryReturn(!IsFailed(r), false, "destFile.Construct is failed");
114 readBytes = srcFile.Read(pBuf.get(), bufSize);
117 r = destFile.Write(pBuf.get(), readBytes);
118 TryReturn(!IsFailed(r), false, "destFile.Write is failed");
121 while (readBytes > 0);
127 InstallerUtil::CopyDirectory(const String& srcFilePath, const String& destFilePath)
129 result r = E_SUCCESS;
132 res = File::IsFileExist(srcFilePath);
135 AppLog("CopyDirectory(): src=[%ls]: skip", srcFilePath.GetPointer());
139 std::unique_ptr<Directory> pDir(new (std::nothrow) Directory);
140 TryReturn(pDir, false, "pDir is null.");
142 r = pDir->Construct(srcFilePath);
143 TryReturn(!IsFailed(r), false, "pDir->Construct() failed, srcFilePath=[%ls].", srcFilePath.GetPointer());
145 std::unique_ptr<DirEnumerator> pDirEnum(pDir->ReadN());
146 TryReturn(pDirEnum, false, "pDirEnum is null.");
148 while (pDirEnum->MoveNext() == E_SUCCESS)
150 DirEntry entry = pDirEnum->GetCurrentDirEntry();
152 String entryName = entry.GetName();
153 String srcEntryDir = srcFilePath;
155 srcEntryDir += entryName;
157 if (entryName == L"." || entryName == L"..")
162 // if file or directory is symbolic link, skip this.
163 if (InstallerUtil::IsSymlink(srcEntryDir) == true)
168 String destEntryDir = destFilePath;
169 destEntryDir += L"/";
170 destEntryDir += entryName;
172 if (entry.IsDirectory() == false)
175 Directory::Create(destFilePath, true);
176 InstallerUtil::Copy(srcEntryDir, destEntryDir);
180 Directory::Create(destEntryDir, true);
181 CopyDirectory(srcEntryDir, destEntryDir);
185 AppLog("CopyDirectory(): src=[%ls], dest=[%ls]", srcFilePath.GetPointer(), destFilePath.GetPointer());
190 InstallerUtil::IsSymlink(const Tizen::Base::String& filePath)
193 struct stat fileinfo;
195 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
196 TryReturn(pFilePath, false, "pFilePath is null");
198 err = lstat(pFilePath.get(), &fileinfo);
199 TryReturn(err >= 0, false, "lstat() failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
201 if (S_ISLNK(fileinfo.st_mode))
210 InstallerUtil::GetRealPath(const String& filePath, String& realPath)
212 char* pRealPath = null;
214 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
215 TryReturn(pFilePath, false, "pFilePath is null");
217 char tmpPath[PATH_MAX] = {0};
218 pRealPath = realpath(pFilePath.get(), tmpPath);
219 TryReturn(pRealPath, false, "pRealPath is null");
223 AppLog("GetRealPath(): path=[%ls], realPath=[%ls]", filePath.GetPointer(), realPath.GetPointer());
229 InstallerUtil::CreateSymlink(const String& oldPath, const String& newPath)
234 res = File::IsFileExist(oldPath);
237 AppLog("CreateSymlink(): oldPath=[%ls] not found", oldPath.GetPointer());
241 std::unique_ptr<char[]> pOldPath(_StringConverter::CopyToCharArrayN(oldPath));
242 TryReturn(pOldPath, false, "pOldPath is null");
244 std::unique_ptr<char[]> pNewPath(_StringConverter::CopyToCharArrayN(newPath));
245 TryReturn(pNewPath, false, "pNewPath is null");
247 err = symlink(pOldPath.get(), pNewPath.get());
248 TryReturn(err == 0, false, "symlink() is failed(%s), oldpath=[%s], newpath=[%s]", strerror(errno), pOldPath.get(), pNewPath.get());
250 SmackManager smackManager;
252 smackManager.AddLabelDir(label, newPath);
254 AppLog("CreateSymlink(): [%ls] -> [%ls]", newPath.GetPointer(), oldPath.GetPointer());
260 InstallerUtil::ChangeMode(const String& filePath, int mode)
264 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
265 TryReturn(pFilePath, false, "pFilePath is null");
267 err = chmod(pFilePath.get(), mode);
268 TryReturn(err == 0, false, "chmod() is failed(%s), filepath=[%s], mode=[%o]", strerror(errno), pFilePath.get(), mode);
274 InstallerUtil::ChangeOwner(const String& filePath)
278 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
279 TryReturn(pFilePath, false, "pFilePath is null");
281 err = chown(pFilePath.get(), APP_OWNER_ID, APP_GROUP_ID);
282 TryReturn(err == 0, false, "chown() is failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
288 InstallerUtil::ChangeDirectoryPermission(const String& filePath, int mode, bool appOwner)
290 result r = E_SUCCESS;
293 res = File::IsFileExist(filePath);
296 AppLog("path=[%ls]: skip", filePath.GetPointer());
300 std::unique_ptr<Directory> pDir(new (std::nothrow) Directory);
301 TryReturn(pDir, false, "pDir is null.");
303 r = pDir->Construct(filePath);
304 TryReturn(!IsFailed(r), false, "pDir->Construct() failed, filePath=[%ls]", filePath.GetPointer());
306 std::unique_ptr<DirEnumerator> pDirEnum(pDir->ReadN());
307 TryReturn(pDirEnum, false, "pDirEnum is null.");
309 while (pDirEnum->MoveNext() == E_SUCCESS)
311 DirEntry entry = pDirEnum->GetCurrentDirEntry();
312 String entryName = entry.GetName();
313 if (entryName.IsEmpty() == true)
315 AppLog("entryName is empty.", entryName.GetPointer());
319 String entryDir = filePath;
321 entryDir += entryName;
323 if (entryName == L".")
325 if (appOwner == true)
327 InstallerUtil::ChangeOwner(entryDir);
329 InstallerUtil::ChangeMode(entryDir, mode | PERM_EXECUTE);
332 else if (entryName == L"..")
337 if (entry.IsDirectory() == false)
339 if (appOwner == true)
341 InstallerUtil::ChangeOwner(entryDir);
343 InstallerUtil::ChangeMode(entryDir, mode);
347 ChangeDirectoryPermission(entryDir, mode, appOwner);
348 if (appOwner == true)
350 InstallerUtil::ChangeOwner(entryDir);
352 InstallerUtil::ChangeMode(entryDir, mode | PERM_EXECUTE);
356 AppLog("path=[%ls], mode=[%04o], appOwner=[%s]",
357 filePath.GetPointer(), mode, appOwner?"true":"false");
363 InstallerUtil::IsDrmFile(const Tizen::Base::String& path)
366 char* pFilePath = null;
367 result r = E_SUCCESS;
370 pFilePath = _StringConverter::CopyToCharArrayN(path);
371 TryCatch(pFilePath, r = GetLastResult(), "pFilePath is null");
373 isDrm = drm_oem_intel_isDrmFile(pFilePath);
376 AppLog("IsDrmFile() called, packagePath=[%ls] is a drm file", path.GetPointer());
389 InstallerUtil::DecryptPackage(const Tizen::Base::String& packagePath)
392 char* pFilePath = null;
393 result r = E_SUCCESS;
396 pFilePath = _StringConverter::CopyToCharArrayN(packagePath);
397 TryCatch(pFilePath, r = GetLastResult(), "pFilePath is null");
399 result = drm_oem_intel_decrypt_package(pFilePath, pFilePath);
402 AppLog("DecryptPackage() called, packagePath=%ls, decrpyt success", packagePath.GetPointer());
406 AppLog("DecryptPackage() called, packagePath=%ls, decrypt failed", packagePath.GetPointer());
416 InstallerUtil::GetCategory(int categoryType)
420 if (categoryType == CATEGORY_TYPE_IME)
424 else if (categoryType == CATEGORY_TYPE_HOME_SCREEN)
426 category = L"home-screen";
428 else if (categoryType == CATEGORY_TYPE_LOCK_SCREEN)
430 category = L"lock-screen";
437 InstallerUtil::GetCategoryType(char* pCategory)
439 CategoryType category = CATEGORY_TYPE_NONE;
441 if (strcasecmp(pCategory, "Ime") == 0)
443 category = CATEGORY_TYPE_IME;
445 else if (strcasecmp(pCategory, "home-screen") == 0)
447 category = CATEGORY_TYPE_HOME_SCREEN;
449 else if (strcasecmp(pCategory, "lock-screen") == 0)
451 category = CATEGORY_TYPE_LOCK_SCREEN;
458 InstallerUtil::CreateSymlinkForAppDirectory(const String& inPath, String& outPath)
462 int length = inPath.GetLength();
463 inPath.SubString(length - PACKAGE_ID_LENGTH, PACKAGE_ID_LENGTH, appId);
466 newPath = PATH_OPT_APPS;
470 if (inPath != newPath)
472 InstallerUtil::CreateSymlink(inPath, newPath);
476 AppLog("CreateSymlinkForAppDirectory(): output path=[%ls]", outPath.GetPointer());
482 InstallerUtil::DumpLog(const char* pBuf)
484 char temp[4096] = {0};
485 TryReturn(pBuf, false, "pBuf is null");
487 int bufLen = strlen(pBuf);
488 strncpy(temp, pBuf, sizeof(temp));
490 char *pStart = &temp[0];
492 for (int i = 0; i < bufLen; i++)
497 AppLog("%s", pStart);
498 pStart = temp + i + 1;
505 #define LOG_PRINT_LINE_MAX 20
506 #define LOG_BUFFER_COUNT_MAX 4096
508 InstallerUtil::DumpLogData(char *pData, int dataLen)
510 const char *szData = (const char*)pData;
512 int i = 0, j = 0, idx = 0, idx2 = 0, high = 0, low = 0, temp = 0;
514 char buf[LOG_PRINT_LINE_MAX + 2] = {0};
515 char buf2[(LOG_PRINT_LINE_MAX + 2) * 3] = {0};
516 char buf_out[sizeof(buf) + sizeof(buf2) + 1] = {0};
519 if (dataLen > LOG_BUFFER_COUNT_MAX)
521 dataLen = LOG_BUFFER_COUNT_MAX;
524 // 16 characters by 20 line are proper. // too many logs decrease performance.
525 // if (dataLen > 16*20)
528 AppLog("------------------------------------------");
530 while (i < (int)dataLen)
534 /* make ascii table */
535 if (ch >= 32 && ch <= 128)
543 high = (ch & 0xf0)>>4;
546 buf2[idx2++] = LogChangeHexToStr(high);
547 buf2[idx2++] = LogChangeHexToStr(low);
550 if (idx >= LOG_PRINT_LINE_MAX)
552 memcpy(buf_out, buf2, idx2);
554 buf_out[idx2++] = ' ';
555 buf_out[idx2++] = ' ';
557 memcpy(buf_out + idx2, buf, idx);
558 buf_out[idx2+idx] = '\0';
563 AppLog("%s\n", buf_out);
572 memcpy(buf_out, buf2, idx2);
575 for (j = 0; j < (LOG_PRINT_LINE_MAX * 3) - temp; j++)
577 buf_out[idx2++] = ' ';
580 buf_out[idx2++] = ' ';
581 buf_out[idx2++] = ' ';
583 memcpy(buf_out+idx2, buf, idx);
584 buf_out[idx2+idx] = '\0';
586 AppLog("%s\n", buf_out);
589 AppLog("------------------------------------------");
595 InstallerUtil::LogChangeHexToStr(int hex)
599 const static char hexValues[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 0};
602 if (hex >= 0 && hex <= 0x0F)
608 AppLog("LogChangeHexToStr: Error! [Hex Val: %d]\n", hex);
615 InstallerUtil::CreateLog(const String& logFile)
619 result r = file.Construct(logFile, "w");
629 InstallerUtil::AppendLog(const char* pFunction, int lineNumber, bool fatal, const char* pFormat, ...)
633 InstallerManager *pManager = InstallerManager::GetInstance();
634 if (pManager == null)
639 if (pManager->IsFileLogOn() == false)
644 String logFile = pManager->GetLogFilePath();
645 result r = file.Construct(logFile, "a");
652 va_start(args, pFormat);
653 const int bufSize = 1024;
654 char logs[bufSize+1] = {0};
655 char logs2[bufSize+1] = {0};
658 snprintf(logs, bufSize, " | %s (%d). > %s", (char*)pFunction, lineNumber, pFormat);
662 snprintf(logs, bufSize, "[TRY]| %s (%d). > %s", (char*)pFunction, lineNumber, pFormat);
665 vsnprintf(logs2, bufSize, logs, args);
666 int length = strlen(logs2);
667 logs2[length] = '\n';
669 r = file.Write(logs2, length+1);
680 InstallerUtil::PrintLog(const String& logFile)
682 InstallerManager *pManager = InstallerManager::GetInstance();
683 if (pManager == null)
688 if (pManager->IsFileLogOn() == false)
694 FileAttributes attribute;
696 result r = File::GetAttributes(logFile, attribute);
703 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
709 r = file.Construct(logFile, "r");
718 memset(pBuf.get(), 0, bufSize);
719 readBytes = file.Read(pBuf.get(), bufSize);
722 fprintf(stderr, "%s", pBuf.get());
725 while (readBytes > 0);