Use multi-user support pkgmgrinfo API
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <dirent.h>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <limits.h>
22 #include <strings.h>
23 #include <pkgmgr_installer_info.h>
24 #include <sys/smack.h>
25 #include <sys/prctl.h>
26 #include <openssl/sha.h>
27
28 #include <cstdlib>
29 #include <cstring>
30 #include <algorithm>
31 #include <unordered_map>
32 #include <vector>
33 #include <iterator>
34 #include <fstream>
35 #include <sstream>
36 #include <map>
37 #include <iomanip>
38
39 #include "log.h"
40 #include "utils.h"
41 #include "path_manager.h"
42
43 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
44 {
45         return static_cast<int>(a.length()) - length >= aOffset &&
46                 static_cast<int>(b.length()) - length >= bOffset &&
47                 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
48                         [](unsigned char a, unsigned char b)
49                         { return std::tolower(a) == std::tolower(b); });
50 }
51
52 bool isManagedAssembly(const std::string& fileName)
53 {
54         return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
55                         iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
56                         !isNativeImage(fileName);
57 }
58
59 bool isNativeImage(const std::string& fileName)
60 {
61         return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
62 }
63
64 std::string concatPath(const std::string& path1, const std::string& path2)
65 {
66         std::string path(path1);
67         if (path.back() == PATH_SEPARATOR) {
68                 path.append(path2);
69         } else {
70                 path += PATH_SEPARATOR;
71                 path.append(path2);
72         }
73
74         return path;
75 }
76
77 void splitPath(const std::string& path, std::vector<std::string>& out)
78 {
79         std::istringstream ss(path);
80         std::string token;
81
82         while (std::getline(ss, token, ':')) {
83                 out.push_back(token);
84         }
85 }
86
87 std::string getAbsolutePath(const std::string& path)
88 {
89         std::string absPath;
90         char *realPath = realpath(path.c_str(), NULL);
91         if (realPath) {
92                 absPath.assign(realPath);
93                 free(realPath);
94         }
95
96         return absPath;
97 }
98
99 std::string getRootPath(const std::string& pkgId)
100 {
101         int ret = 0;
102         char *path = 0;
103         std::string rootPath;
104
105         pkgmgrinfo_pkginfo_h pkg_handle;
106         ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
107         if (ret != 0) {
108                 return rootPath;
109         }
110
111         ret = pkgmgrinfo_pkginfo_get_root_path(pkg_handle, &path);
112         if (ret != PMINFO_R_OK) {
113                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
114                 return rootPath;
115         }
116         rootPath = path;
117         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
118
119         return rootPath;
120 }
121
122 std::string getExecName(const std::string& pkgId)
123 {
124         char *exec = NULL;
125         char *appId = 0;
126         std::string execName;
127
128         pkgmgrinfo_pkginfo_h pkg_handle;
129         int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
130         if (ret != 0) {
131                 return execName;
132         }
133
134         ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
135         if (ret != PMINFO_R_OK) {
136                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
137                 return execName;
138         }
139
140         pkgmgrinfo_appinfo_h app_handle;
141         ret = pkgmgrGetAppInfo(appId, &app_handle);
142         if (ret != 0) {
143                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
144                 return execName;
145         }
146
147         ret = pkgmgrinfo_appinfo_get_exec(app_handle, &exec);
148         if (ret != PMINFO_R_OK) {
149                 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
150                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
151                 return execName;
152         }
153         execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
154
155         pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
156         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
157
158         return execName;
159 }
160
161 std::string getAppType(const std::string& pkgId)
162 {
163         char *appId = 0;
164         char *type = 0;
165         std::string appType;
166
167         pkgmgrinfo_pkginfo_h pkg_handle;
168         int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
169         if (ret != 0) {
170                 return appType;
171         }
172
173         ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
174         if (ret != PMINFO_R_OK) {
175                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
176                 return appType;
177         }
178
179         pkgmgrinfo_appinfo_h app_handle;
180         ret = pkgmgrGetAppInfo(appId, &app_handle);
181         if (ret != 0) {
182                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
183                 return appType;
184         }
185
186         ret = pkgmgrinfo_appinfo_get_apptype(app_handle, &type);
187         if (ret != PMINFO_R_OK) {
188                 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
189                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
190                 return appType;
191         }
192         appType = type;
193
194         pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
195         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
196
197         return appType;
198 }
199
200 std::string getMetadataValue(const std::string& pkgId, const std::string& key)
201 {
202         char *value = NULL;
203         char *appId = 0;
204         std::string metadataValue;
205
206         pkgmgrinfo_pkginfo_h pkg_handle;
207         int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
208         if (ret != 0) {
209                 return metadataValue;
210         }
211
212         ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
213         if (ret != PMINFO_R_OK) {
214                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
215                 return metadataValue;
216         }
217
218         pkgmgrinfo_appinfo_h app_handle;
219         ret = pkgmgrGetAppInfo(appId, &app_handle);
220         if (ret != 0) {
221                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
222                 return metadataValue;
223         }
224
225         ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, key.c_str(), &value);
226         if (ret != PMINFO_R_OK) {
227                 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
228                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
229                 return metadataValue;
230         }
231         metadataValue = std::string(value);
232
233         pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
234         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
235
236         return metadataValue;
237 }
238
239 std::string getBaseName(const std::string& path)
240 {
241         auto pos = path.find_last_of(PATH_SEPARATOR);
242         if (pos != std::string::npos)
243                 return path.substr(0, pos);
244         else
245                 return std::string(".");
246         return path;
247 }
248
249 std::string replaceAll(const std::string& str, const std::string& pattern, const std::string& replace)
250 {
251         std::string result = str;
252         std::string::size_type pos = 0;
253         std::string::size_type offset = 0;
254
255         while ((pos = result.find(pattern, offset)) != std::string::npos) {
256                 result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
257                 offset = pos + replace.size();
258         }
259
260         return result;
261 }
262
263 bool isFile(const std::string& path)
264 {
265         struct stat sb;
266         return lstat(path.c_str(), &sb) == 0;
267 }
268
269 bool isDirectory(const std::string& path)
270 {
271         struct stat sb;
272         if (stat(path.c_str(), &sb) != 0) {
273                 return false;
274         }
275         if (sb.st_mode & S_IFDIR) {
276                 return true;
277         } else {
278                 return false;
279         }
280 }
281
282 std::string getAssemblyNameFromPath(const std::string& path)
283 {
284         std::string ret(getFileName(path));
285
286         if (ret.find_last_of(".") == std::string::npos)
287                 return ret;
288         ret.erase(ret.find_last_of("."));
289
290         if (ret.size() > 3 && std::equal(ret.begin() + ret.size() - 3, ret.end(), ".ni"))
291                 ret.erase(ret.size() - 3);
292
293         return ret;
294 }
295
296 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list)
297 {
298         std::vector<std::string> assems;
299         std::unordered_map<std::string, std::string> assemPaths;
300
301         auto reader = [&assems, &assemPaths](const std::string& path, const std::string& filename) {
302                 if (isManagedAssembly(filename) || isNativeImage(filename)) {
303                         std::string assem = getAssemblyNameFromPath(filename);
304
305                         if (assemPaths.count(assem) == 0) {
306                                 assems.push_back(assem);
307                                 assemPaths[assem] = path;
308                         } else if (isManagedAssembly(assemPaths[assem]) && isNativeImage(filename)) {
309                                 // Update only if a native image is found in the same directory.
310                                 // For example, if we have two directories = { X, Y } where X contains A.dll and
311                                 // Y contains both A.dll and A.ni.dll, always A.dll in X will be used.
312                                 if (getBaseName(assemPaths[assem]).compare(getBaseName(path)) == 0)
313                                         assemPaths[assem] = path;
314                         }
315                 }
316         };
317         for (auto& directory : directories)
318                 scanFilesInDirectory(directory, reader, 0);
319
320         if (!list.empty() && list.back() != ':')
321                 list.push_back(':');
322
323         for (auto& assem : assems)
324                 list += assemPaths[assem] + ":";
325
326         if (list.back() == ':')
327                 list.pop_back();
328 }
329
330 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth)
331 {
332         DIR *dir;
333         struct dirent* entry;
334         bool isDir;
335
336         dir = opendir(directory.c_str());
337
338         if (dir == nullptr)
339                 return;
340
341         std::vector<std::string> innerDirectories;
342
343         while ((entry = readdir(dir)) != nullptr) {
344                 isDir = false;
345                 std::string path = concatPath(directory, entry->d_name);
346                 switch (entry->d_type) {
347                         case DT_REG: break;
348                         case DT_DIR:
349                                 isDir = true;
350                                 break;
351                         // symlink is added to the list even if there is no original file.
352                         // It used to remove broken symlinks related to TAC
353                         case DT_LNK:
354                                 break;
355                         case DT_UNKNOWN:
356                                 continue;
357                         default:
358                                 continue;
359                 }
360                 if (!isDir)
361                         reader(path, entry->d_name);
362                 else if (depth > 0 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
363                         innerDirectories.push_back(path);
364         }
365
366         if (depth > 0)
367                 for (auto& d : innerDirectories)
368                         scanFilesInDirectory(d, reader, depth - 1);
369
370         closedir(dir);
371 }
372
373 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink)
374 {
375         char* label = NULL;
376         struct stat info;
377
378         if (isSymlink) {
379                 // change smack label for symbolic link.
380                 if (smack_lgetlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
381                         if (smack_lsetlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
382                                 fprintf(stderr, "Fail to set smack label\n");
383                         }
384                         free(label);
385                 }
386
387                 // change owner and groups for symbolic link.
388                 if (!lstat(fromPath.c_str(), &info)) {
389                         if (lchown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
390                                 fprintf(stderr, "Failed to change owner and group name\n");
391                 }
392         } else {
393                 // change smack label
394                 if (smack_getlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
395                         if (smack_setlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
396                                 fprintf(stderr, "Fail to set smack label\n");
397                         }
398                         free(label);
399                 }
400
401                 // change owner and groups for generated ni file.
402                 if (!stat(fromPath.c_str(), &info)) {
403                         if (chown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
404                                 fprintf(stderr, "Failed to change owner and group name\n");
405                 }
406         }
407 }
408
409 static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid)
410 {
411         int fd = open(path.c_str(), O_RDONLY);
412         if (fd < 0) {
413                 _ERR("Can't open directory: %s", path.c_str());
414                 return false;
415         }
416         int ret = fchown(fd, uid, gid);
417         close(fd);
418         if (ret != 0) {
419                 _ERR("Failed to change owner of: %s", path.c_str());
420                 return false;
421         }
422         return true;
423 }
424
425 static bool setDirPermissions(const bf::path& path, bf::perms permissions)
426 {
427         bs::error_code error;
428         bf::permissions(path, permissions, error);
429         if (error) {
430                 _ERR("Failed to set permissions for directory: %s, %s", path.c_str(), error.message().c_str());
431                 return false;
432         }
433         return true;
434 }
435
436 static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid)
437 {
438         if (!setOwnership(path, uid, gid)) {
439                 _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
440                 return false;
441         }
442         if (!setDirPermissions(path, permissions)) {
443                 _ERR("Failed to change permission: %s, (%d)", path.c_str(), permissions);
444                 return false;
445         }
446         return true;
447 }
448
449 static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2)
450 {
451         if (!exist(path)) {
452                 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
453                 return false;
454         }
455         bf::perms permissions = bf::status(path).permissions();
456         struct stat stats;
457         if (stat(path.c_str(), &stats) != 0) {
458                 return false;
459         }
460         if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
461                 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
462                 return false;
463         }
464         return true;
465 }
466
467 bool exist(const bf::path& path)
468 {
469         bs::error_code error;
470         int ret = bf::exists(path, error);
471         if (error) {
472                 if ((error.value() != bs::errc::success) && (error.value() != bs::errc::no_such_file_or_directory)) {
473                         _ERR("Failed to check %s exists : %s", path.c_str(), error.message().c_str());
474                 }
475         }
476         return ret;
477 }
478
479 bool createDir(const bf::path& path)
480 {
481         if (exist(path)) {
482                 return true;
483         }
484         bs::error_code error;
485         bf::create_directories(path, error);
486         if (error) {
487                 _ERR("Failed to create directory: %s", error.message().c_str());
488                 return false;
489         }
490         return true;
491 }
492
493 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
494 {
495         try {
496                 // Check whether the function call is valid
497                 if (!exist(path1) || !bf::is_directory(path1)) {
498                         _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
499                         return false;
500                 }
501                 if (!exist(path2)) {
502                         // Create the destination directory
503                         if (!createDir(path2)) {
504                                 _ERR("Unable to create destination directory %s", path2.c_str());
505                                 return false;
506                         }
507                         if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
508                                 copyOwnershipAndPermissions(path1, path2);
509                         }
510                 } else {
511                         if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
512                                 _ERR("Destination directory %s already exists", path2.c_str());
513                                 return false;
514                         }
515                         if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
516                                 copyOwnershipAndPermissions(path1, path2);
517                         }
518                 }
519         } catch (const bf::filesystem_error& error) {
520                 _ERR("Failed to copy directory: %s", error.what());
521                 return false;
522         }
523
524         // Iterate through the source directory
525         for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
526                 try {
527                         bf::path current(file->path());
528                         bf::path target = path2 / current.filename();
529                         if (bf::is_symlink(symlink_status(current))) {
530                                 if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && exist(target)) {
531                                         continue;
532                                 }
533                                 bs::error_code error;
534                                 bf::copy_symlink(current, target, error);
535                                 if (error) {
536                                         _ERR("Failed to copy symlink: %s, %s", current.c_str(), error.message().c_str());
537                                         return false;
538                                 }
539                         } else if (bf::is_directory(current)) {
540                                 // Found directory: Recursion
541                                 if (!copyDir(current, target, flags)) {
542                                         return false;
543                                 }
544                         } else {
545                                 if ((flags & FS_MERGE_SKIP) && exist(target)) {
546                                         continue;
547                                 }
548                                 bf::path destination = target;
549                                 if (flags & FS_COMMIT_COPY_FILE) {
550                                         destination = bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
551                                 }
552                                 if (flags & FS_MERGE_OVERWRITE) {
553                                         bf::copy_file(current, destination, bf::copy_option::overwrite_if_exists);
554                                 } else {
555                                         bf::copy_file(current, destination);
556                                 }
557                                 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
558                                         copyOwnershipAndPermissions(current, destination);
559                                 }
560                                 if (flags & FS_COMMIT_COPY_FILE) {
561                                         if (flags & FS_MERGE_OVERWRITE) {
562                                                 bf::remove(target);
563                                         }
564                                         bf::rename(destination, target);
565                                 }
566                         }
567                 } catch (const bf::filesystem_error& error) {
568                         _ERR("Failed to copy directory: %s", error.what());
569                         return false;
570                 }
571         }
572         return true;
573 }
574
575 bool copyFile(const bf::path& path1, const bf::path& path2)
576 {
577         bs::error_code error;
578         if (!exist(path1)) {
579                 return false;
580         }
581         bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
582         if (error) {
583                 _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
584                 return false;
585         }
586         return true;
587 }
588
589 bool moveFile(const bf::path& path1, const bf::path& path2)
590 {
591         if (!exist(path1) || exist(path2)) {
592                 return false;
593         }
594         bs::error_code error;
595         bf::rename(path1, path2, error);
596         if (error) {
597                 _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
598                 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
599                 if (error) {
600                         _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
601                         return false;
602                 }
603                 bf::remove_all(path1, error);
604                 if (error) {
605                         _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
606                         return false;
607                 }
608         }
609         return true;
610 }
611
612 bool removeFile(const bf::path& path)
613 {
614         if (!exist(path)) {
615                 return true;
616         }
617         bs::error_code error;
618         bf::remove(path, error);
619         if (error) {
620                 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
621                 return false;
622         }
623         return true;
624 }
625
626 bool removeAll(const bf::path& path)
627 {
628         if (!exist(path)) {
629                 return true;
630         }
631         bs::error_code error;
632         bf::remove_all(path, error);
633         if (error) {
634                 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
635                 return false;
636         }
637         return true;
638 }
639
640 void setCmdName(const std::string& name)
641 {
642         #define PRC_NAME_LENGTH         16
643
644         char processName[PRC_NAME_LENGTH] = {0, };
645
646         if (name.empty())
647                 return;
648
649         memset(processName, '\0', PRC_NAME_LENGTH);
650         snprintf(processName, PRC_NAME_LENGTH, "%s", name.c_str());
651         prctl(PR_SET_NAME, processName);
652 }
653
654 std::string getFileName(const std::string& path)
655 {
656         std::string ret(path);
657         size_t index = ret.find_last_of(PATH_SEPARATOR);
658         return index == std::string::npos ? ret : ret.substr(index + 1);
659 }
660
661 std::string SHA256(const std::string& path)
662 {
663         std::string output = "";
664         FILE *file = fopen(path.c_str(), "rb");
665         if (!file) {
666                 return output;
667         }
668
669         unsigned char hash[SHA256_DIGEST_LENGTH];
670         SHA256_CTX sha256;
671         SHA256_Init(&sha256);
672         int bytesRead = 0;
673         const int bufSize = 32768;
674         char *buffer = (char*)malloc(bufSize);
675         if (!buffer) {
676                 fclose(file);
677                 return output;
678         }
679
680         while ((bytesRead = fread(buffer, 1, bufSize, file))) {
681                 SHA256_Update(&sha256, buffer, bytesRead);
682         }
683         SHA256_Final(hash, &sha256);
684
685         std::stringstream ss;
686         for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
687                 ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
688         }
689         output = ss.str();
690
691         fclose(file);
692         free(buffer);
693
694         return output;
695 }
696
697 int pkgmgrGetPkgInfo(const std::string& pkgId, pkgmgrinfo_pkginfo_h* handle)
698 {
699         uid_t uid = 0;
700         int ret = 0;
701
702         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
703                 _ERR("Failed to get UID");
704                 return -1;
705         }
706
707         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, handle);
708         if (ret != PMINFO_R_OK) {
709                 _ERR("Failed to get pkginfo (%d)", ret);
710                 return -1;
711         }
712
713         return 0;
714 }
715
716 int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle)
717 {
718         uid_t uid = 0;
719         int ret = 0;
720
721         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
722                 _ERR("Failed to get UID");
723                 return -1;
724         }
725
726         ret = pkgmgrinfo_appinfo_get_usr_appinfo(appId.c_str(), uid, handle);
727         if (ret != PMINFO_R_OK) {
728                 _ERR("Failed to get appinfo (%d)", ret);
729                 return -1;
730         }
731
732         return 0;
733 }
734
735 int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
736                                          pkgmgrinfo_app_list_cb app_cb,
737                                          void *user_data)
738 {
739         uid_t uid = 0;
740         int ret = 0;
741
742         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
743                 _ERR("Failed to get UID");
744                 return -1;
745         }
746
747
748         ret = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, uid);
749         if (ret != PMINFO_R_OK) {
750                 _ERR("Failed to execute the metadata filter query (%d)", ret);
751                 return -1;
752         }
753
754         return 0;
755 }
756