Added missing exception handling for boost_filesystem
[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 isSymlinkFile(const std::string& path)
305 {
306         struct stat sb;
307         if (lstat(path.c_str(), &sb) != 0) {
308                 return false;
309         }
310         return (sb.st_mode & S_IFMT) == S_IFLNK;
311 }
312
313 bool isDirectory(const std::string& path)
314 {
315         struct stat sb;
316         if (stat(path.c_str(), &sb) != 0) {
317                 return false;
318         }
319         return (sb.st_mode & S_IFMT) == S_IFDIR;
320 }
321
322 std::string getAssemblyNameFromPath(const std::string& path)
323 {
324         std::string ret(getFileName(path));
325
326         if (ret.find_last_of(".") == std::string::npos)
327                 return ret;
328         ret.erase(ret.find_last_of("."));
329
330         if (ret.size() > 3 && std::equal(ret.begin() + ret.size() - 3, ret.end(), ".ni"))
331                 ret.erase(ret.size() - 3);
332
333         return ret;
334 }
335
336 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list)
337 {
338         std::vector<std::string> assems;
339         std::unordered_map<std::string, std::string> assemPaths;
340
341         auto reader = [&assems, &assemPaths](const std::string& path, const std::string& filename) {
342                 if (isManagedAssembly(filename) || isNativeImage(filename)) {
343                         std::string assem = getAssemblyNameFromPath(filename);
344
345                         if (assemPaths.count(assem) == 0) {
346                                 assems.push_back(assem);
347                                 assemPaths[assem] = path;
348                         } else if (isManagedAssembly(assemPaths[assem]) && isNativeImage(filename)) {
349                                 // Update only if a native image is found in the same directory.
350                                 // For example, if we have two directories = { X, Y } where X contains A.dll and
351                                 // Y contains both A.dll and A.ni.dll, always A.dll in X will be used.
352                                 if (getBaseName(assemPaths[assem]).compare(getBaseName(path)) == 0)
353                                         assemPaths[assem] = path;
354                         }
355                 }
356         };
357         for (auto& directory : directories)
358                 scanFilesInDirectory(directory, reader, 0);
359
360         if (!list.empty() && list.back() != ':')
361                 list.push_back(':');
362
363         for (auto& assem : assems)
364                 list += assemPaths[assem] + ":";
365
366         if (list.back() == ':')
367                 list.pop_back();
368 }
369
370 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth)
371 {
372         DIR *dir;
373         struct dirent* entry;
374         bool isDir;
375
376         dir = opendir(directory.c_str());
377
378         if (dir == nullptr)
379                 return;
380
381         std::vector<std::string> innerDirectories;
382
383         while ((entry = readdir(dir)) != nullptr) {
384                 isDir = false;
385                 std::string path = concatPath(directory, entry->d_name);
386                 switch (entry->d_type) {
387                         case DT_REG: break;
388                         case DT_DIR:
389                                 isDir = true;
390                                 break;
391                         // symlink is added to the list even if there is no original file.
392                         // It used to remove broken symlinks related to TAC
393                         case DT_LNK:
394                                 break;
395                         case DT_UNKNOWN:
396                                 continue;
397                         default:
398                                 continue;
399                 }
400                 if (!isDir)
401                         reader(path, entry->d_name);
402                 else if (depth > 0 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
403                         innerDirectories.push_back(path);
404         }
405
406         if (depth > 0)
407                 for (auto& d : innerDirectories)
408                         scanFilesInDirectory(d, reader, depth - 1);
409
410         closedir(dir);
411 }
412
413 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink)
414 {
415         char* label = NULL;
416         struct stat info;
417
418         if (isSymlink) {
419                 // change smack label for symbolic link.
420                 if (smack_lgetlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
421                         if (smack_lsetlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
422                                 _SERR("Fail to set smack label");
423                         }
424                         free(label);
425                 }
426
427                 // change owner and groups for symbolic link.
428                 if (!lstat(fromPath.c_str(), &info)) {
429                         if (lchown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
430                                 _SERR("Failed to change owner and group name");
431                 }
432         } else {
433                 // change smack label
434                 if (smack_getlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
435                         if (smack_setlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
436                                 _SERR("Fail to set smack label");
437                         }
438                         free(label);
439                 }
440
441                 // change owner and groups for generated ni file.
442                 if (!stat(fromPath.c_str(), &info)) {
443                         if (chown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
444                                 _SERR("Failed to change owner and group name");
445                 }
446         }
447 }
448
449 static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid)
450 {
451         int fd = open(path.c_str(), O_RDONLY);
452         if (fd < 0) {
453                 _ERR("Can't open directory: %s", path.c_str());
454                 return false;
455         }
456         int ret = fchown(fd, uid, gid);
457         close(fd);
458         if (ret != 0) {
459                 _ERR("Failed to change owner of: %s", path.c_str());
460                 return false;
461         }
462         return true;
463 }
464
465 static bool setDirPermissions(const bf::path& path, bf::perms permissions)
466 {
467         bs::error_code error;
468         bf::permissions(path, permissions, error);
469         if (error) {
470                 _ERR("Failed to set permissions for directory: %s, %s", path.c_str(), error.message().c_str());
471                 return false;
472         }
473         return true;
474 }
475
476 static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid)
477 {
478         if (!setOwnership(path, uid, gid)) {
479                 _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
480                 return false;
481         }
482         if (!setDirPermissions(path, permissions)) {
483                 _ERR("Failed to change permission: %s, (%d)", path.c_str(), permissions);
484                 return false;
485         }
486         return true;
487 }
488
489 static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2)
490 {
491         if (!exist(path)) {
492                 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
493                 return false;
494         }
495         bs::error_code error;
496         bf::perms permissions = bf::status(path, error).permissions();
497         if (error) {
498                 _ERR("Failed to copy ownership and permissions : %s", error.message().c_str());
499                 return false;
500         }
501         struct stat stats;
502         if (stat(path.c_str(), &stats) != 0) {
503                 return false;
504         }
505         if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
506                 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
507                 return false;
508         }
509         return true;
510 }
511
512 bool exist(const bf::path& path)
513 {
514         bs::error_code error;
515         int ret = bf::exists(path, error);
516         if (error) {
517                 if ((error.value() != bs::errc::success) && (error.value() != bs::errc::no_such_file_or_directory)) {
518                         _ERR("Failed to check %s exists : %s", path.c_str(), error.message().c_str());
519                 }
520         }
521         return ret;
522 }
523
524 bool createDir(const bf::path& path)
525 {
526         if (exist(path)) {
527                 return true;
528         }
529         bs::error_code error;
530         bf::create_directories(path, error);
531         if (error) {
532                 _ERR("Failed to create directory: %s", error.message().c_str());
533                 return false;
534         }
535         return true;
536 }
537
538 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
539 {
540         try {
541                 // Check whether the function call is valid
542                 if (!exist(path1) || !bf::is_directory(path1)) {
543                         _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
544                         return false;
545                 }
546                 if (!exist(path2)) {
547                         // Create the destination directory
548                         if (!createDir(path2)) {
549                                 _ERR("Unable to create destination directory %s", path2.c_str());
550                                 return false;
551                         }
552                         if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
553                                 copyOwnershipAndPermissions(path1, path2);
554                         }
555                 } else {
556                         if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
557                                 _ERR("Destination directory %s already exists", path2.c_str());
558                                 return false;
559                         }
560                         if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
561                                 copyOwnershipAndPermissions(path1, path2);
562                         }
563                 }
564         } catch (const bf::filesystem_error& error) {
565                 _ERR("Failed to copy directory: %s", error.what());
566                 return false;
567         }
568
569         // Iterate through the source directory
570         try {
571                 for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
572                         bf::path current(file->path());
573                         bf::path target = path2 / current.filename();
574                         if (bf::is_symlink(bf::symlink_status(current))) {
575                                 if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && exist(target)) {
576                                         continue;
577                                 }
578                                 bs::error_code error;
579                                 bf::copy_symlink(current, target, error);
580                                 if (error) {
581                                         _ERR("Failed to copy symlink: %s, %s", current.c_str(), error.message().c_str());
582                                         return false;
583                                 }
584                         } else if (bf::is_directory(current)) {
585                                 // Found directory: Recursion
586                                 if (!copyDir(current, target, flags)) {
587                                         return false;
588                                 }
589                         } else {
590                                 if ((flags & FS_MERGE_SKIP) && exist(target)) {
591                                         continue;
592                                 }
593                                 bf::path destination = target;
594                                 if (flags & FS_COMMIT_COPY_FILE) {
595                                         destination = bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
596                                 }
597                                 if (flags & FS_MERGE_OVERWRITE) {
598                                         bf::copy_file(current, destination, bf::copy_option::overwrite_if_exists);
599                                 } else {
600                                         bf::copy_file(current, destination);
601                                 }
602                                 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
603                                         copyOwnershipAndPermissions(current, destination);
604                                 }
605                                 if (flags & FS_COMMIT_COPY_FILE) {
606                                         if (flags & FS_MERGE_OVERWRITE) {
607                                                 bf::remove(target);
608                                         }
609                                         bf::rename(destination, target);
610                                 }
611                         }
612                 }
613         } catch (const bf::filesystem_error& error) {
614                 _ERR("Failed to copy directory: %s", error.what());
615                 return false;
616         }
617
618         return true;
619 }
620
621 bool copyFile(const bf::path& path1, const bf::path& path2)
622 {
623         bs::error_code error;
624         if (!exist(path1)) {
625                 return false;
626         }
627         bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
628         if (error) {
629                 _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
630                 return false;
631         }
632         return true;
633 }
634
635 bool moveFile(const bf::path& path1, const bf::path& path2)
636 {
637         if (!exist(path1) || exist(path2)) {
638                 return false;
639         }
640         bs::error_code error;
641         bf::rename(path1, path2, error);
642         if (error) {
643                 _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
644                 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
645                 if (error) {
646                         _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
647                         return false;
648                 }
649                 bf::remove_all(path1, error);
650                 if (error) {
651                         _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
652                         return false;
653                 }
654         }
655         return true;
656 }
657
658 bool removeFile(const bf::path& path)
659 {
660         if (!exist(path)) {
661                 return true;
662         }
663         bs::error_code error;
664         bf::remove(path, error);
665         if (error) {
666                 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
667                 return false;
668         }
669         return true;
670 }
671
672 bool removeAll(const bf::path& path)
673 {
674         if (!exist(path)) {
675                 return true;
676         }
677         bs::error_code error;
678         bf::remove_all(path, error);
679         if (error) {
680                 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
681                 return false;
682         }
683         return true;
684 }
685
686 void setCmdName(const std::string& name)
687 {
688         #define PRC_NAME_LENGTH         16
689
690         char processName[PRC_NAME_LENGTH] = {0, };
691
692         if (name.empty())
693                 return;
694
695         memset(processName, '\0', PRC_NAME_LENGTH);
696         snprintf(processName, PRC_NAME_LENGTH, "%s", name.c_str());
697         prctl(PR_SET_NAME, processName);
698 }
699
700 std::string getFileName(const std::string& path)
701 {
702         std::string ret(path);
703         size_t index = ret.find_last_of(PATH_SEPARATOR);
704         return index == std::string::npos ? ret : ret.substr(index + 1);
705 }
706
707 std::string SHA256(const std::string& path)
708 {
709         std::string output = "";
710         FILE *file = fopen(path.c_str(), "rb");
711         if (!file) {
712                 return output;
713         }
714
715         unsigned char hash[SHA256_DIGEST_LENGTH];
716         SHA256_CTX sha256;
717         SHA256_Init(&sha256);
718         int bytesRead = 0;
719         const int bufSize = 32768;
720         char *buffer = (char*)malloc(bufSize);
721         if (!buffer) {
722                 fclose(file);
723                 return output;
724         }
725
726         while ((bytesRead = fread(buffer, 1, bufSize, file))) {
727                 SHA256_Update(&sha256, buffer, bytesRead);
728         }
729         SHA256_Final(hash, &sha256);
730
731         std::stringstream ss;
732         for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
733                 ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
734         }
735         output = ss.str();
736
737         fclose(file);
738         free(buffer);
739
740         return output;
741 }
742
743 int pkgmgrGetPkgInfo(const std::string& pkgId, pkgmgrinfo_pkginfo_h* handle)
744 {
745         uid_t uid = 0;
746         int ret = 0;
747
748         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
749                 _ERR("Failed to get UID");
750                 return -1;
751         }
752
753         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, handle);
754         if (ret != PMINFO_R_OK) {
755                 _ERR("Failed to get pkginfo (%d)", ret);
756                 return -1;
757         }
758
759         return 0;
760 }
761
762 int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle)
763 {
764         uid_t uid = 0;
765         int ret = 0;
766
767         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
768                 _ERR("Failed to get UID");
769                 return -1;
770         }
771
772         ret = pkgmgrinfo_appinfo_get_usr_appinfo(appId.c_str(), uid, handle);
773         if (ret != PMINFO_R_OK) {
774                 _ERR("Failed to get appinfo (%d)", ret);
775                 return -1;
776         }
777
778         return 0;
779 }
780
781 int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
782                                          pkgmgrinfo_app_list_cb app_cb,
783                                          void *user_data)
784 {
785         uid_t uid = 0;
786         int ret = 0;
787
788         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
789                 _ERR("Failed to get UID");
790                 return -1;
791         }
792
793
794         ret = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, uid);
795         if (ret != PMINFO_R_OK) {
796                 _ERR("Failed to execute the metadata filter query (%d)", ret);
797                 return -1;
798         }
799
800         return 0;
801 }
802