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>
33 #include "InstallerDefs.h"
34 #include "InstallerUtil.h"
36 #include <drm-oem-intel.h>
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::App;
41 using namespace Tizen::Io;
43 InstallerUtil::InstallerUtil(void)
47 InstallerUtil::~InstallerUtil(void)
52 InstallerUtil::Remove(const Tizen::Base::String& filePath)
58 AppLogTag(OSP_INSTALLER, "+ Remove(): path=[%ls]", filePath.GetPointer());
60 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
61 TryReturn(pFilePath, false, "[osp-installer] pFilePath is null");
63 err = lstat(pFilePath.get(), &fileinfo);
66 AppLogTag(OSP_INSTALLER, "Remove(): lstat(%s): %s[errno:%d]: skip", pFilePath.get(), strerror(errno), errno);
70 if (S_ISLNK(fileinfo.st_mode))
72 AppLogTag(OSP_INSTALLER, "Remove(): symlink, path=[%s]", pFilePath.get());
73 err = unlink(pFilePath.get());
74 TryReturn(err >= 0, false, "[osp-installer] unlink() failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
76 else if (S_ISDIR(fileinfo.st_mode))
78 AppLogTag(OSP_INSTALLER, "Remove(): directory, path=[%ls]", filePath.GetPointer());
79 r = Directory::Remove(filePath, true);
80 TryReturn(!IsFailed(r), false, "[osp-installer] Directory::Remove() failed, filePath=%ls", filePath.GetPointer());
84 AppLogTag(OSP_INSTALLER, "Remove(): file, path=[%ls]", filePath.GetPointer());
85 r = File::Remove(filePath);
86 TryReturn(!IsFailed(r), false, "[osp-installer] File::Remove() failed, filePath=%ls", filePath.GetPointer());
93 InstallerUtil::Copy(const String& srcFilePath, const String& destFilePath)
99 // AppLogTag(OSP_INSTALLER, "+ Copy(): src=[%ls], dest=[%ls]", srcFilePath.GetPointer(), destFilePath.GetPointer());
104 std::unique_ptr<char[]> pBuf(new (std::nothrow) char[bufSize]);
105 TryReturn(pBuf, false, "[osp-installer] pBuf is null");
107 r = srcFile.Construct(srcFilePath, L"r");
108 TryReturn(!IsFailed(r), false, "[osp-installer] srcFile.Construct is failed");
110 r = destFile.Construct(destFilePath, L"w");
111 TryReturn(!IsFailed(r), false, "[osp-installer] destFile.Construct is failed");
115 readBytes = srcFile.Read(pBuf.get(), bufSize);
118 r = destFile.Write(pBuf.get(), readBytes);
119 TryReturn(!IsFailed(r), false, "[osp-installer] destFile.Write is failed");
122 while (readBytes > 0);
128 InstallerUtil::CopyDirectory(const String& srcFilePath, const String& destFilePath)
130 result r = E_SUCCESS;
132 AppLogTag(OSP_INSTALLER, "+ CopyDirectory(): src=[%ls], dest=[%ls]", srcFilePath.GetPointer(), destFilePath.GetPointer());
134 std::unique_ptr<Directory> pDir(new (std::nothrow) Directory);
135 TryReturn(pDir, false, "[osp-installer] pDir is null.");
137 r = pDir->Construct(srcFilePath);
138 TryReturn(!IsFailed(r), false, "[osp-installer] pDir->Construct() failed, srcFilePath=[%ls].", srcFilePath.GetPointer());
140 std::unique_ptr<DirEnumerator> pDirEnum(pDir->ReadN());
141 TryReturn(pDirEnum, false, "[osp-installer] pDirEnum is null.");
143 while (pDirEnum->MoveNext() == E_SUCCESS)
145 DirEntry entry = pDirEnum->GetCurrentDirEntry();
147 String entryName = entry.GetName();
148 String srcEntryDir = srcFilePath;
150 srcEntryDir += entryName;
152 if (entryName == L"." || entryName == L"..")
157 // if file or directory is symbolic link, skip this.
158 if (InstallerUtil::IsSymlink(srcEntryDir) == true)
163 String destEntryDir = destFilePath;
164 destEntryDir += L"/";
165 destEntryDir += entryName;
167 if (entry.IsDirectory() == false)
170 Directory::Create(destFilePath, true);
171 InstallerUtil::Copy(srcEntryDir, destEntryDir);
175 Directory::Create(destEntryDir, true);
176 CopyDirectory(srcEntryDir, destEntryDir);
184 InstallerUtil::IsSymlink(const Tizen::Base::String& filePath)
187 struct stat fileinfo;
189 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
190 TryReturn(pFilePath, false, "[osp-installer] pFilePath is null");
192 err = lstat(pFilePath.get(), &fileinfo);
193 TryReturn(err >= 0, false, "[osp-installer] lstat() failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
195 if (S_ISLNK(fileinfo.st_mode))
204 InstallerUtil::GetRealPath(const String& filePath, String& realPath)
206 char* pRealPath = null;
208 AppLogTag(OSP_INSTALLER, "+ GetRealPath(): path=[%ls], realPath=[%ls]", filePath.GetPointer(), realPath.GetPointer());
210 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
211 TryReturn(pFilePath, false, "[osp-installer] pFilePath is null");
213 char tmpPath[PATH_MAX] = {0};
214 pRealPath = realpath(pFilePath.get(), tmpPath);
215 TryReturn(pRealPath, false, "[osp-installer] pRealPath is null");
223 InstallerUtil::CreateSymlink(const String& oldPath, const String& newPath)
228 AppLogTag(OSP_INSTALLER, "+ CreateSymlink(): oldPath=[%ls], newPath=[%ls]", oldPath.GetPointer(), newPath.GetPointer());
230 res = File::IsFileExist(oldPath);
231 TryReturn(res == true, false, "[osp-installer] file not found, oldPath=[%ls]", oldPath.GetPointer());
233 std::unique_ptr<char[]> pOldPath(_StringConverter::CopyToCharArrayN(oldPath));
234 TryReturn(pOldPath, false, "[osp-installer] pOldPath is null");
236 std::unique_ptr<char[]> pNewPath(_StringConverter::CopyToCharArrayN(newPath));
237 TryReturn(pNewPath, false, "[osp-installer] pNewPath is null");
239 err = symlink(pOldPath.get(), pNewPath.get());
240 TryReturn(err == 0, false, "[osp-installer] symlink() is failed(%s), oldpath=[%s], newpath=[%s]", strerror(errno), pOldPath.get(), pNewPath.get());
242 AppLogTag(OSP_INSTALLER, "CreateSymlink(): [%ls] -> [%ls]", newPath.GetPointer(), oldPath.GetPointer());
248 InstallerUtil::ChangeMode(const String& filePath, int mode)
252 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
253 TryReturn(pFilePath, false, "[osp-installer] pFilePath is null");
255 err = chmod(pFilePath.get(), mode);
256 TryReturn(err == 0, false, "[osp-installer] chmod() is failed(%s), filepath=[%s], mode=[%o]", strerror(errno), pFilePath.get(), mode);
262 InstallerUtil::ChangeOwner(const String& filePath)
266 std::unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(filePath));
267 TryReturn(pFilePath, false, "[osp-installer] pFilePath is null");
269 err = chown(pFilePath.get(), APP_OWNER_ID, APP_GROUP_ID);
270 TryReturn(err == 0, false, "[osp-installer] chown() is failed(%s), filepath=[%s]", strerror(errno), pFilePath.get());
276 InstallerUtil::ChangeDirectoryPermission(const String& filePath, int mode, bool appOwner)
278 result r = E_SUCCESS;
281 AppLogTag(OSP_INSTALLER, "+ ChangeDirectoryPermission(): path=[%ls], mode=[%04o], appOwner=[%s]",
282 filePath.GetPointer(), mode, appOwner?"true":"false");
284 res = File::IsFileExist(filePath);
287 AppLogTag(OSP_INSTALLER, "ChangeDirectoryPermission(): path=[%ls]: skip", filePath.GetPointer());
291 std::unique_ptr<Directory> pDir(new (std::nothrow) Directory);
292 TryReturn(pDir, false, "[osp-installer] pDir is null.");
294 r = pDir->Construct(filePath);
295 TryReturn(!IsFailed(r), false, "[osp-installer] pDir->Construct() failed, filePath=[%ls]", filePath.GetPointer());
297 std::unique_ptr<DirEnumerator> pDirEnum(pDir->ReadN());
298 TryReturn(pDirEnum, false, "[osp-installer] pDirEnum is null.");
300 while (pDirEnum->MoveNext() == E_SUCCESS)
302 DirEntry entry = pDirEnum->GetCurrentDirEntry();
304 String entryName = entry.GetName();
305 String entryDir = filePath;
307 entryDir += entryName;
309 if (entryName == L".")
311 if (appOwner == true)
313 InstallerUtil::ChangeOwner(entryDir);
315 InstallerUtil::ChangeMode(entryDir, mode | PERM_EXECUTE);
318 else if (entryName == L"..")
323 if (entry.IsDirectory() == false)
325 if (appOwner == true)
327 InstallerUtil::ChangeOwner(entryDir);
329 InstallerUtil::ChangeMode(entryDir, mode);
333 ChangeDirectoryPermission(entryDir, mode, appOwner);
334 if (appOwner == true)
336 InstallerUtil::ChangeOwner(entryDir);
338 InstallerUtil::ChangeMode(entryDir, mode | PERM_EXECUTE);
346 InstallerUtil::IsDrmFile(const Tizen::Base::String& path)
349 char* pFilePath = null;
350 result r = E_SUCCESS;
353 pFilePath = _StringConverter::CopyToCharArrayN(path);
354 TryCatch(pFilePath, r = GetLastResult(), "[osp-installer] pFilePath is null");
356 isDrm = drm_oem_intel_isDrmFile(pFilePath);
359 AppLogTag(OSP_INSTALLER, "IsDrmFile() called, packagePath=%ls is drm file", path.GetPointer());
364 AppLogTag(OSP_INSTALLER, "IsDrmFile() called, packagePath=%ls isn't drm file", path.GetPointer());
373 InstallerUtil::DecryptPackage(const Tizen::Base::String& packagePath)
376 char* pFilePath = null;
377 result r = E_SUCCESS;
380 pFilePath = _StringConverter::CopyToCharArrayN(packagePath);
381 TryCatch(pFilePath, r = GetLastResult(), "[osp-installer] pFilePath is null");
383 result = drm_oem_intel_decrypt_package(pFilePath, pFilePath);
386 AppLogTag(OSP_INSTALLER, "DecryptPackage() called, packagePath=%ls, decrpyt success", packagePath.GetPointer());
390 AppLogTag(OSP_INSTALLER, "DecryptPackage() called, packagePath=%ls, decrypt failed", packagePath.GetPointer());
400 InstallerUtil::GetCategory(int categoryType)
404 if (categoryType == CATEGORY_TYPE_IME)
408 else if (categoryType == CATEGORY_TYPE_HOME_SCREEN)
410 category = L"home-screen";
412 else if (categoryType == CATEGORY_TYPE_LOCK_SCREEN)
414 category = L"lock-screen";
421 InstallerUtil::GetCategoryType(char* pCategory)
423 CategoryType category = CATEGORY_TYPE_NONE;
425 if (strcasecmp(pCategory, "Ime") == 0)
427 category = CATEGORY_TYPE_IME;
429 else if (strcasecmp(pCategory, "home-screen") == 0)
431 category = CATEGORY_TYPE_HOME_SCREEN;
433 else if (strcasecmp(pCategory, "lock-screen") == 0)
435 category = CATEGORY_TYPE_LOCK_SCREEN;
442 InstallerUtil::CreateSymlinkForAppDirectory(const String& inPath, String& outPath)
446 AppLogTag(OSP_INSTALLER, "+ CreateSymlinkForAppDirectory(): path=[%ls]", inPath.GetPointer());
448 int length = inPath.GetLength();
449 inPath.SubString(length - APPID_LENGTH, APPID_LENGTH, appId);
452 newPath = PATH_OPT_APPS;
456 if (inPath != newPath)
458 InstallerUtil::CreateSymlink(inPath, newPath);
462 AppLogTag(OSP_INSTALLER, "CreateSymlinkForAppDirectory(): output path=[%ls]", outPath.GetPointer());
468 InstallerUtil::DumpLog(const char* pBuf)
470 char temp[4096] = {0};
471 TryReturn(pBuf, false, "[osp-installer] pBuf is null");
473 int bufLen = strlen(pBuf);
474 strncpy(temp, pBuf, sizeof(temp));
476 char *pStart = &temp[0];
478 for (int i = 0; i < bufLen; i++)
483 AppLogTag(OSP_INSTALLER, "%s", pStart);
484 pStart = temp + i + 1;
491 #define LOG_PRINT_LINE_MAX 20
492 #define LOG_BUFFER_COUNT_MAX 4096
494 InstallerUtil::DumpLogData(char *pData, int dataLen)
496 const char *szData = (const char*)pData;
498 int i = 0, j = 0, idx = 0, idx2 = 0, high = 0, low = 0, temp = 0;
500 char buf[LOG_PRINT_LINE_MAX + 2] = {0};
501 char buf2[(LOG_PRINT_LINE_MAX + 2) * 3] = {0};
502 char buf_out[sizeof(buf) + sizeof(buf2) + 1] = {0};
505 if (dataLen > LOG_BUFFER_COUNT_MAX)
507 dataLen = LOG_BUFFER_COUNT_MAX;
510 // 16 characters by 20 line are proper. // too many logs decrease performance.
511 // if (dataLen > 16*20)
514 AppLogTag(OSP_INSTALLER, "------------------------------------------");
516 while (i < (int)dataLen)
520 /* make ascii table */
521 if (ch >= 32 && ch <= 128)
529 high = (ch & 0xf0)>>4;
532 buf2[idx2++] = LogChangeHexToStr(high);
533 buf2[idx2++] = LogChangeHexToStr(low);
536 if (idx >= LOG_PRINT_LINE_MAX)
538 memcpy(buf_out, buf2, idx2);
540 buf_out[idx2++] = ' ';
541 buf_out[idx2++] = ' ';
543 memcpy(buf_out + idx2, buf, idx);
544 buf_out[idx2+idx] = '\0';
549 AppLogTag(OSP_INSTALLER, "%s\n", buf_out);
558 memcpy(buf_out, buf2, idx2);
561 for (j = 0; j < (LOG_PRINT_LINE_MAX * 3) - temp; j++)
563 buf_out[idx2++] = ' ';
566 buf_out[idx2++] = ' ';
567 buf_out[idx2++] = ' ';
569 memcpy(buf_out+idx2, buf, idx);
570 buf_out[idx2+idx] = '\0';
572 AppLogTag(OSP_INSTALLER, "%s\n", buf_out);
575 AppLogTag(OSP_INSTALLER, "------------------------------------------");
581 InstallerUtil::LogChangeHexToStr(int hex)
585 const static char hexValues[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 0};
588 if (hex >= 0 && hex <= 0x0F)
594 AppLogTag(OSP_INSTALLER, "LogChangeHexToStr: Error! [Hex Val: %d]\n", hex);