+
+static std::vector<uid_t> getUserIds()
+{
+ std::vector<uid_t> list;
+
+ while (true) {
+ errno = 0; // so we can distinguish errors from no more entries
+ passwd* entry = getpwent();
+ if (!entry) {
+ if (errno) {
+ _SERR("Error while getting userIDs");
+ list.clear();
+ return list;
+ }
+ break;
+ }
+ list.push_back(entry->pw_uid);
+ }
+ endpwent();
+
+ return list;
+}
+
+static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
+{
+ std::string pDataFile;
+
+ tzplatform_set_user(uid);
+
+ const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
+ if (tzUserApp != NULL) {
+ pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
+ }
+
+ tzplatform_reset_user();
+
+ return pDataFile;
+}
+
+ni_error_e removeAppProfileData(const std::string& pkgId)
+{
+ if (pkgId.empty()) {
+ return NI_ERROR_INVALID_PARAMETER;
+ }
+
+ std::vector<uid_t> uidList = getUserIds();
+ for (auto& uid : uidList) {
+ // get data path from pkgid
+ std::string dataPath = getAppDataPath(pkgId, uid);
+ if (!dataPath.empty() && exist(dataPath)) {
+ std::string pDataFile = dataPath + PROFILE_BASENAME;
+
+ if (exist(pDataFile)) {
+ if (!removeFile(pDataFile)) {
+ _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
+ return NI_ERROR_UNKNOWN;
+ }
+ _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
+ }
+ }
+ }
+
+ return NI_ERROR_NONE;
+}
+
+static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
+{
+ char *pkgId = NULL;
+ int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+ if (ret != PMINFO_R_OK || pkgId == NULL) {
+ _SERR("Fail to get pkgid");
+ return 0;
+ }
+
+ if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
+ _SERR("Fail to remove profile data for (%s)", pkgId);
+ }
+
+ return 0;
+}
+
+static ni_error_e removeAppProfileByAppType(const char* type)
+{
+ int ret;
+
+ pkgmgrinfo_appinfo_filter_h filter;
+
+ ret = pkgmgrinfo_appinfo_filter_create(&filter);
+ if (ret != PMINFO_R_OK) {
+ _SERR("Fail to create appinfo filter");
+ return NI_ERROR_UNKNOWN;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
+ if (ret != PMINFO_R_OK) {
+ pkgmgrinfo_appinfo_filter_destroy(filter);
+ _SERR("Fail to add appinfo filter (%s)", type);
+ return NI_ERROR_UNKNOWN;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
+ if (ret != PMINFO_R_OK) {
+ _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
+ pkgmgrinfo_appinfo_filter_destroy(filter);
+ return NI_ERROR_UNKNOWN;
+ }
+
+ pkgmgrinfo_appinfo_filter_destroy(filter);
+
+ return NI_ERROR_NONE;
+}
+
+void removeAllAppProfileData()
+{
+ std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
+
+ for (auto& type : appTypeList) {
+ if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
+ _SERR("Fail to removeAppProfileByAppType for type (%s)", type);
+ }
+ }
+}