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