2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
20 #include <tzplatform_config.h>
24 #include "pkgmgr_parser_plugin_interface.h"
43 #include "ni_common.h"
44 #include "db_manager.h"
45 #include "tac_common.h"
46 #include "path_manager.h"
47 #include "plugin_manager.h"
52 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
55 #error "CROSSGEN_PATH is missed"
59 #define __STR(x) __XSTR(x)
60 #if defined(__arm__) || defined(__aarch64__)
61 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
63 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
64 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
65 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
67 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
68 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
74 static int __interval = 0;
75 static PathManager* __pm = nullptr;
77 static void waitInterval()
79 // by the recommand, ignore small value for performance.
80 if (__interval > 10000) {
81 _SOUT("sleep %d usec", __interval);
86 static std::string getNIFilePath(const std::string& dllPath)
88 size_t index = dllPath.find_last_of(".");
89 if (index == std::string::npos) {
90 _SERR("File doesnot contain extension. fail to get NI file name");
93 std::string fName = dllPath.substr(0, index);
94 std::string fExt = dllPath.substr(index, dllPath.length());
96 // crossgen generate file with lower case extension only
97 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
98 std::string niPath = fName + ".ni" + fExt;
104 * @brief create the directory including parents directory, and
105 * copy ownership and smack labels to the created directory.
106 * @param[in] target directory path
107 * @param[in] source directory path to get ownership and smack label
108 * @return if directory created successfully, return true otherwise false
110 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
113 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
115 for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
116 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
117 std::string newPath = std::string(target_path.begin(), newIter);
119 if (!newPath.empty()) {
120 if (stat(newPath.c_str(), &st) != 0) {
121 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
122 _SERR("Fail to create app ni directory (%s)", newPath.c_str());
125 if (!source.empty()) {
126 copySmackAndOwnership(source, newPath);
129 if (!S_ISDIR(st.st_mode)) {
130 _SERR("Fail. path is not a dir (%s)", newPath.c_str());
136 if(newIter != target_path.end()) {
144 static std::string getAppNIFilePath(const std::string& absDllPath, DWORD flags)
146 std::string niDirPath;
147 std::string prevPath;
149 prevPath = getBaseName(absDllPath);
150 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
152 if (flags & NI_FLAGS_APP_UNDER_RO_AREA) {
153 niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
154 _SERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
155 _ERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
158 if (!isDirectory(niDirPath)) {
159 if (!createDirsAndCopyOwnerShip(niDirPath, prevPath)) {
160 niDirPath = prevPath;
161 _SERR("fail to create dir (%s)", niDirPath.c_str());
165 return getNIFilePath(concatPath(niDirPath, getFileName(absDllPath)));
168 static bool checkNIExistence(const std::string& absDllPath, const std::string& absNIPath)
170 if (absNIPath.empty()) {
174 if (isFile(absNIPath)) {
178 // native image of System.Private.CoreLib.dll should have to overwrite
179 // original file to support new coreclr
180 if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
181 std::string coreLibBackup = absDllPath + ".Backup";
182 if (isFile(coreLibBackup)) {
190 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
191 static uintptr_t getFileSize(const std::string& path)
195 if (stat(path.c_str(), &sb) == 0) {
202 // Get next base address to be used for system ni image from file
203 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
204 static uintptr_t getNextBaseAddrFromFile()
206 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
208 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
215 while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
223 // Get next base address to be used for system ni image
224 static uintptr_t getNextBaseAddr()
226 uintptr_t baseAddr = 0;
228 if (!isFile(__SYSTEM_BASE_FILE)) {
229 // This is the starting address for all default base addresses
230 baseAddr = DEFAULT_BASE_ADDR_START;
232 baseAddr = getNextBaseAddrFromFile();
234 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
235 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
236 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
242 // Save base address of system ni image to file
243 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
245 uintptr_t niSize = getFileSize(absNIPath);
247 _SERR("File %s doesn't exist", absNIPath.c_str());
251 // Write new entry to the file
252 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
254 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
258 fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
262 // check if dll is listed in TPA
263 static bool isTPADll(const std::string& dllPath)
265 std::string absPath = getBaseName(getAbsolutePath(dllPath));
267 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
268 for (unsigned int i = 0; i < paths.size(); i++) {
269 if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
278 // baseAddr should be checked in file before getting here
279 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
281 if (!isFile(dllPath)) {
282 _SERR("dll file is not exist : %s", dllPath.c_str());
283 return NI_ERROR_NO_SUCH_FILE;
286 if (!isManagedAssembly(dllPath)) {
287 //_SERR("Input file is not a dll file : %s", dllPath.c_str());
288 return NI_ERROR_INVALID_PARAMETER;
291 std::string absDllPath = getAbsolutePath(dllPath);
292 std::string absNIPath;
294 bool isAppNI = flags & NI_FLAGS_APPNI;
295 if (isAppNI && strstr(absDllPath.c_str(), __DOTNET_DIR) == NULL) {
296 absNIPath = getAppNIFilePath(absDllPath, flags);
298 absNIPath = getNIFilePath(absDllPath);
301 if (absNIPath.empty()) {
302 _SERR("Fail to get ni file name");
303 return NI_ERROR_UNKNOWN;
306 if (checkNIExistence(absDllPath, absNIPath)) {
307 //_SERR("Already ni file is exist for %s", absNIPath.c_str());
308 return NI_ERROR_ALREADY_EXIST;
311 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
312 uintptr_t baseAddr = 0;
314 if (isTPADll(dllPath)) {
315 baseAddr = getNextBaseAddr();
321 return NI_ERROR_UNKNOWN;
325 waitpid(pid, &status, 0);
326 if (WIFEXITED(status)) {
327 // Do not use checkNIExistence() function to check whether ni file created or not.
328 // checkNIExistence() return false for System.Private.Corelib.dll
329 if (isFile(absNIPath)) {
330 copySmackAndOwnership(absDllPath, absNIPath);
331 std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
332 std::string pdbFilePath = replaceAll(absNIPath, ".ni.dll", ".pdb");
333 if (isFile(absPdbPath) && (absPdbPath != pdbFilePath)) {
334 if (!copyFile(absPdbPath, pdbFilePath)) {
335 _SERR("Failed to copy a .pdb file");
337 copySmackAndOwnership(absPdbPath, pdbFilePath);
340 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
342 updateBaseAddrFile(absNIPath, baseAddr);
345 return NI_ERROR_NONE;
347 _SERR("Fail to create native image for %s", dllPath.c_str());
348 return NI_ERROR_NO_SUCH_FILE;
352 std::string jitPath = __pm->getRuntimePath() + "/libclrjit.so";
353 std::vector<const char*> argv = {
356 "/JITPath", jitPath.c_str()
359 bool compat = flags & NI_FLAGS_COMPATIBILITY;
360 argv.push_back(compat ? "/Platform_Assemblies_Pathes" : "/p");
361 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
362 std::string platformAssembliesPaths;
363 for (const auto &path : paths) {
364 if (!platformAssembliesPaths.empty()) {
365 platformAssembliesPaths += ":";
367 platformAssembliesPaths += path;
369 argv.push_back(platformAssembliesPaths.c_str());
371 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
373 argv.push_back("/FragileNonVersionable");
376 if (flags & NI_FLAGS_VERBOSE) {
377 argv.push_back("/verbose");
380 if (flags & NI_FLAGS_INSTRUMENT) {
381 argv.push_back("/Tuning");
384 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
385 std::string baseAddrString;
387 argv.push_back("/BaseAddress");
388 std::stringstream ss;
389 ss << "0x" << std::hex << baseAddr;
390 baseAddrString = ss.str();
391 argv.push_back(baseAddrString.c_str());
395 argv.push_back("/App_Paths");
396 std::string absAppPath;
397 if (!appPath.empty()) {
398 absAppPath = appPath;
400 absAppPath = getBaseName(absDllPath);
402 argv.push_back(absAppPath.c_str());
404 argv.push_back("/out");
405 argv.push_back(absNIPath.c_str());
407 argv.push_back(absDllPath.c_str());
408 argv.push_back(nullptr);
410 _SOUT("+ %s (%s)", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
412 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
416 return NI_ERROR_NONE;
419 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
420 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
424 DWORD *pFlags = (DWORD*)userData;
426 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
427 if (ret != PMINFO_R_OK) {
428 _SERR("Failed to get pkgid");
432 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
433 _SERR("Failed to remove previous dlls from [%s]", pkgId);
437 if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
438 _SERR("Failed to generate NI file [%s]", pkgId);
441 _SOUT("Complete make application to native image");
447 static bool isCoreLibPrepared(DWORD flags)
449 if (flags & NI_FLAGS_ENABLER2R) {
453 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
454 if (isFile(coreLibBackup)) {
457 _SERR("The native image of System.Private.CoreLib does not exist\n"
458 "Run the command to create the native image\n"
459 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n");
464 static bool hasCoreLibNI()
466 std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
467 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
468 std::string cmd = ildasm + " " + coreLib + " -noil -stats | grep '\\.xdata'";
471 fp = popen(cmd.c_str(), "r");
474 if (fgets(buff, sizeof(buff), fp) != NULL) {
483 static ni_error_e createCoreLibNI(DWORD flags)
485 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
486 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
487 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
489 if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
490 if (!crossgen(coreLib, std::string(), flags)) {
491 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
492 _SERR("Failed to rename System.Private.CoreLib.dll");
493 return NI_ERROR_CORE_NI_FILE;
495 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
496 _SERR("Failed to rename System.Private.CoreLib.ni.dll");
497 return NI_ERROR_CORE_NI_FILE;
500 _SERR("Failed to create native image for %s", coreLib.c_str());
501 return NI_ERROR_CORE_NI_FILE;
504 return NI_ERROR_NONE;
507 ni_error_e initNICommon()
509 #if defined(__arm__) || defined(__aarch64__)
510 // get interval value
511 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
512 std::ifstream inFile(intervalFile);
514 _SOUT("crossgen_interval.txt is found");
515 inFile >> __interval;
518 if (initializePluginManager("normal")) {
519 _SERR("Fail to initialize PluginManager");
520 return NI_ERROR_UNKNOWN;
524 __pm = new PathManager();
525 } catch (const std::exception& e) {
526 _SERR("Failed to create PathManager");
527 return NI_ERROR_UNKNOWN;
530 char* pluginDllPaths = pluginGetDllPath();
531 if (pluginDllPaths && pluginDllPaths[0] != '\0') {
532 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
535 char* pluginNativePaths = pluginGetNativeDllSearchingPath();
536 if (pluginNativePaths && pluginNativePaths[0] != '\0') {
537 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
540 return NI_ERROR_NONE;
542 _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
543 return NI_ERROR_NOT_SUPPORTED;
547 void finalizeNICommon()
551 finalizePluginManager();
557 ni_error_e createNIPlatform(DWORD flags)
559 if (createCoreLibNI(flags) != NI_ERROR_NONE) {
560 return NI_ERROR_CORE_NI_FILE;
563 return createNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath(), flags);
566 ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
568 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
569 return createCoreLibNI(flags);
572 if (!isCoreLibPrepared(flags)) {
573 return NI_ERROR_CORE_NI_FILE;
576 return crossgen(dllPath, std::string(), flags);
579 ni_error_e createNIUnderDirs(const std::string& rootPaths, DWORD flags)
581 if (!isCoreLibPrepared(flags)) {
582 return NI_ERROR_CORE_NI_FILE;
585 auto convert = [&rootPaths, flags](const std::string& path, const std::string& filename) {
586 // if path is symlink, donot generate crossgen
587 if (!crossgen(path, rootPaths.c_str(), flags)) {
592 std::vector<std::string> targetPaths;
593 splitPath(rootPaths, targetPaths);
594 for (const auto &path : targetPaths) {
595 // TAC directory should be handled specially because that contains symlink of native image file.
596 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
597 if (!isDirectory(path)) {
600 // make native image symlink if not exist under tac directory
602 for (auto& symlinkAssembly : bf::recursive_directory_iterator(path)) {
603 std::string symPath = symlinkAssembly.path().string();
604 if (!isManagedAssembly(symPath)) {
608 // if there is symlink and original file for native image, skip generation
609 std::string symNIPath = changeExtension(symPath, "dll", "ni.dll");
610 if (isFile(symNIPath)) {
614 // if original native image not exist, generate native image
615 std::string originPath = bf::read_symlink(symPath).string();
616 std::string originNIPath = changeExtension(originPath, "dll", "ni.dll");
617 if (!isFile(originNIPath)) {
618 if (!crossgen(originPath, path.c_str(), flags)) {
623 // if no symlink file exist, create symlink
624 if (!isFile(symNIPath)) {
625 bf::create_symlink(originNIPath, symNIPath);
626 copySmackAndOwnership(symPath.c_str(), symNIPath.c_str(), true);
627 _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
628 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
631 } catch (const bf::filesystem_error& error) {
632 _SERR("Failed to recursive directory: %s", error.what());
633 return NI_ERROR_UNKNOWN;
636 scanFilesInDirectory(path, convert, 0);
640 return NI_ERROR_NONE;
643 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
645 std::string rootPath = getRootPath(pkgId);
646 if (rootPath.empty()) {
647 _SERR("Failed to get root path from [%s]", pkgId.c_str());
648 return NI_ERROR_INVALID_PACKAGE;
651 __pm->setAppRootPath(rootPath);
653 flags |= NI_FLAGS_APPNI;
655 if (isReadOnlyArea(rootPath)) {
656 flags |= NI_FLAGS_APP_UNDER_RO_AREA;
658 flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
661 // create native image under bin and lib directory
662 // tac directory is skipped in the createNIUnderDirs.
663 return createNIUnderDirs(__pm->getAppPaths(), flags);
666 void removeNIPlatform()
668 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
669 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
671 if (hasCoreLibNI()) {
672 if (!isFile(coreLibBackup)) {
676 if (remove(coreLib.c_str())) {
677 _SERR("Failed to remove System.Private.CoreLib native image file");
679 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
680 _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
684 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
685 if (isFile(__SYSTEM_BASE_FILE)) {
686 if (remove(__SYSTEM_BASE_FILE)) {
687 _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
692 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
695 void removeNIUnderDirs(const std::string& rootPaths)
697 auto convert = [](const std::string& path, const std::string& filename) {
698 if (isNativeImage(path)) {
699 if (remove(path.c_str())) {
700 _SERR("Failed to remove %s", path.c_str());
705 std::vector<std::string> paths;
706 splitPath(rootPaths, paths);
707 for (const auto &path : paths) {
708 scanFilesInDirectory(path, convert, -1);
712 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
714 std::string rootPath = getRootPath(pkgId);
715 if (rootPath.empty()) {
716 _SERR("Failed to get root path from [%s]", pkgId.c_str());
717 return NI_ERROR_INVALID_PACKAGE;
720 __pm->setAppRootPath(rootPath);
722 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
723 std::string appNIPaths = __pm->getAppNIPaths();
724 std::vector<std::string> paths;
725 splitPath(appNIPaths, paths);
726 for (const auto &path : paths) {
727 if (!isReadOnlyArea(path)) {
728 // Only the native image inside the TAC should be removed.
729 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
730 removeNIUnderDirs(path);
732 if (isDirectory(path)) {
733 if (!removeAll(path.c_str())) {
734 _SERR("Failed to remove app ni dir [%s]", path.c_str());
741 return NI_ERROR_NONE;
744 ni_error_e regenerateAppNI(DWORD flags)
746 if (!isCoreLibPrepared(flags)) {
747 return NI_ERROR_CORE_NI_FILE;
751 pkgmgrinfo_appinfo_metadata_filter_h handle;
753 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
754 if (ret != PMINFO_R_OK)
755 return NI_ERROR_UNKNOWN;
757 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
758 if (ret != PMINFO_R_OK) {
759 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
760 return NI_ERROR_UNKNOWN;
763 ret = pkgmgrMDFilterForeach(handle, appAotCb, &flags);
765 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
766 return NI_ERROR_UNKNOWN;
769 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
770 return NI_ERROR_NONE;
773 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
774 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
777 DWORD *pFlags = (DWORD*)userData;
779 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
780 if (ret != PMINFO_R_OK || pkgId == NULL) {
781 _SERR("Failed to get pkgid");
785 sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
787 _SERR("Sqlite open error");
790 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
792 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
793 std::vector<std::string> nugets = selectDB(tac_db, sql);
801 std::string nugetPaths;
802 for (const auto &nuget : nugets) {
803 if (!nugetPaths.empty()) {
806 nugetPaths += concatPath(__DOTNET_DIR, nuget);
809 auto convert = [&nugetPaths, pFlags](const std::string& path, const std::string& filename) {
810 if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
812 if (!crossgen(path, nugetPaths.c_str(), *pFlags)) {
817 for (auto& nuget : nugets) {
818 scanFilesInDirectory(concatPath(__DOTNET_DIR, nuget), convert, -1);
824 ni_error_e regenerateTACNI(DWORD flags)
826 if (!isCoreLibPrepared(flags)) {
827 return NI_ERROR_CORE_NI_FILE;
830 removeNIUnderDirs(__DOTNET_DIR);
832 pkgmgrinfo_appinfo_metadata_filter_h handle;
833 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
834 if (ret != PMINFO_R_OK) {
835 return NI_ERROR_UNKNOWN;
838 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
839 if (ret != PMINFO_R_OK) {
840 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
841 return NI_ERROR_UNKNOWN;
844 ret = pkgmgrMDFilterForeach(handle, regenTacCb, &flags);
846 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
847 return NI_ERROR_UNKNOWN;
850 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
852 return NI_ERROR_NONE;
855 static std::vector<uid_t> getUserIds()
857 std::vector<uid_t> list;
860 errno = 0; // so we can distinguish errors from no more entries
861 passwd* entry = getpwent();
864 _SERR("Error while getting userIDs");
870 list.push_back(entry->pw_uid);
877 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
879 std::string pDataFile;
881 tzplatform_set_user(uid);
883 const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
884 if (tzUserApp != NULL) {
885 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
888 tzplatform_reset_user();
893 ni_error_e removeAppProfileData(const std::string& pkgId)
896 return NI_ERROR_INVALID_PARAMETER;
899 std::vector<uid_t> uidList = getUserIds();
900 for (auto& uid : uidList) {
901 // get data path from pkgid
902 std::string dataPath = getAppDataPath(pkgId, uid);
903 if (!dataPath.empty() && exist(dataPath)) {
904 std::string pDataFile = dataPath + PROFILE_BASENAME;
906 if (exist(pDataFile)) {
907 if (!removeFile(pDataFile)) {
908 _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
909 return NI_ERROR_UNKNOWN;
911 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
916 return NI_ERROR_NONE;
919 static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
922 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
923 if (ret != PMINFO_R_OK || pkgId == NULL) {
924 _SERR("Fail to get pkgid");
928 if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
929 _SERR("Fail to remove profile data for (%s)", pkgId);
935 static ni_error_e removeAppProfileByAppType(const char* type)
939 pkgmgrinfo_appinfo_filter_h filter;
941 ret = pkgmgrinfo_appinfo_filter_create(&filter);
942 if (ret != PMINFO_R_OK) {
943 _SERR("Fail to create appinfo filter");
944 return NI_ERROR_UNKNOWN;
947 ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
948 if (ret != PMINFO_R_OK) {
949 pkgmgrinfo_appinfo_filter_destroy(filter);
950 _SERR("Fail to add appinfo filter (%s)", type);
951 return NI_ERROR_UNKNOWN;
954 ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
955 if (ret != PMINFO_R_OK) {
956 _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
957 pkgmgrinfo_appinfo_filter_destroy(filter);
958 return NI_ERROR_UNKNOWN;
961 pkgmgrinfo_appinfo_filter_destroy(filter);
963 return NI_ERROR_NONE;
966 void removeAllAppProfileData()
968 std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
970 for (auto& type : appTypeList) {
971 if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
972 _SERR("Fail to removeAppProfileByAppType for type (%s)", type);