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