1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/shared_dirs.h"
7 #include <manifest_parser/utils/logging.h>
8 #include <manifest_parser/utils/version_number.h>
10 #include <boost/filesystem/operations.hpp>
11 #include <boost/filesystem/path.hpp>
12 #include <boost/program_options.hpp>
13 #include <boost/system/error_code.hpp>
17 #include <vcore/Certificate.h>
18 #include <pkgmgr-info.h>
19 #include <sys/types.h>
22 #include <tzplatform_config.h>
23 #include <sys/xattr.h>
37 #include "common/utils/paths.h"
38 #include "common/security_registration.h"
39 #include "common/utils/pkgmgr_query.h"
40 #include "common/utils/base64.h"
41 #include "common/utils/file_util.h"
42 #include "common/utils/user_util.h"
43 #include "common/utils/glist_range.h"
45 namespace bf = boost::filesystem;
46 namespace bpo = boost::program_options;
47 namespace bs = boost::system;
48 namespace ci = common_installer;
52 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
53 const utils::VersionNumber ver30("3.0");
54 const char kDisableLegacySharedDataDirSupport[] =
55 "/usr/share/app-installers/disable_legacy_shareddata_support";
57 const std::vector<const char*> kEntries = {
62 const std::vector<std::string> kReadOnlyEntries = {
68 const std::vector<std::string> kSharedDataEntries = {
73 const char kSharedResDir[] = "shared/res";
74 const char kSharedCacheDir[] = "shared/cache";
75 const char kSharedDataDir[] = "shared/data";
76 const char kShared[] = "shared";
77 const char kData[] = "data";
78 const char kCache[] = "cache";
79 const char kSharedDir[] = ".shared";
80 const char kSharedTmpDir[] = ".shared_tmp";
81 const char kSharedTrustedDir[] = "shared/trusted";
82 const char kSkelAppDir[] = "skel/apps_rw";
83 const char kExternalStoragePrivilege[] =
84 "http://tizen.org/privilege/externalstorage.appdata";
85 const char kSystemShareGroupName[] = "system_share";
87 // the input path should be root directory of package.
88 // for example: "../apps_rw/pkgid" or "../.shared/pkgid"
89 bool SetPackageDirectoryOwnerAndPermissions(const bf::path& path, uid_t uid) {
90 boost::optional<gid_t> gid = ci::GetGidByUid(uid);
94 bf::perms perms755 = bf::all_all ^ bf::group_write ^ bf::others_write;
96 bf::owner_read | bf::owner_write | bf::group_read | bf::others_read;
97 bf::perms perms_setgid = perms755 | bf::set_gid_on_exe;
98 boost::optional<gid_t> system_share =
99 ci::GetGidByGroupName(kSystemShareGroupName);
101 if (!ci::SetDirOwnershipAndPermissions(path, perms755, uid, *gid))
104 for (bf::recursive_directory_iterator iter(path);
105 iter != bf::recursive_directory_iterator(); ++iter) {
106 if (bf::is_symlink(symlink_status(iter->path()))) {
109 } else if (bf::is_directory(iter->path()) && iter.level() == 0 &&
110 (iter->path().filename() == ".mmc" ||
111 iter->path().filename() == ".pkg" ||
112 iter->path().filename() == "tep")) {
113 // skip path which is related to mount or directory installer creates
115 } else if (bf::is_directory(iter->path())) {
117 if (iter.level() == 0 &&
118 (iter->path().filename() == kData ||
119 iter->path().filename() == kCache))
121 if (!ci::SetDirOwnershipAndPermissions(
122 iter->path(), is_rw ? perms_setgid : perms755, uid,
123 is_rw ? *system_share : *gid))
127 if (iter.level() == 1 && iter->path().parent_path().filename() == "bin")
129 if (!ci::SetDirOwnershipAndPermissions(
130 iter->path(), is_bin ? perms755 : perms644, uid, *gid))
137 bf::path GetDirectoryPathForStorage(uid_t user, std::string apps_prefix) {
138 std::string username = ci::GetUsernameByUid(user);
139 if (username.empty())
143 apps_rw = bf::path(apps_prefix.c_str()) / username / "apps_rw";
147 bool DeleteSharedDataDirectories(const bf::path& path,
148 const std::string& pkgid) {
149 if (!ci::RemoveAll(path / pkgid / kSharedDataDir))
152 std::vector<std::string> shared_dirs(kSharedDataEntries);
153 for (auto entry : shared_dirs) {
154 if (!ci::RemoveAll(path / entry / pkgid))
161 bool CreateSharedDataDirectories(const bf::path& path,
162 const std::string& pkgid) {
163 if (!ci::CreateDir(path / kSharedDir / pkgid / kData) ||
164 !ci::CreateDir(path / kSharedTmpDir / pkgid))
167 bf::current_path(path / pkgid / kShared);
168 if (bf::exists(path / pkgid / kShared / kData))
170 bf::path relative_path = ci::RelativePath(path / kSharedDir / pkgid / kData,
171 bf::current_path() / kData);
172 bs::error_code error;
173 bf::create_symlink(relative_path, kData, error);
175 LOG(ERROR) << "Failed to create symlink : " << error.message();
182 bool DeleteDirectories(const bf::path& app_dir, const std::string& pkgid) {
183 bf::path base_dir = app_dir / pkgid;
184 if (!ci::RemoveAll(base_dir))
186 if (!DeleteSharedDataDirectories(app_dir, pkgid))
191 bool CreateSymlinkFiles(const bf::path& src_dir, const bf::path& dst_dir) {
192 std::vector<std::string> rofiles(kReadOnlyEntries);
193 for (bf::directory_iterator file(src_dir);
194 file != bf::directory_iterator();
196 if (bf::is_regular_file(file->path())) {
197 bf::path current(file->path());
198 bf::path file_name = current.filename();
199 LOG(DEBUG) << "file_name: " << file_name;
200 rofiles.push_back(file_name.string());
204 bs::error_code error;
205 for (auto& entry : rofiles) {
206 bf::path src_path = src_dir / entry;
207 bf::path dst_path = dst_dir / entry;
208 if (!bf::exists(src_path)) {
209 // check if symlink for .mmc/bin,lib,res, then do not skip
210 if (!bf::is_symlink(symlink_status(src_path))) {
211 LOG(INFO) << "src_path not exist : " << src_path;
215 if (bf::exists(dst_path) || bf::is_symlink(symlink_status(dst_path))) {
216 LOG(WARNING) << "dst_path exist, skip : " << dst_path;
219 bf::create_symlink(src_path, dst_path, error);
221 LOG(ERROR) << "Symlink creation failure src_path: " << src_path
222 << " dst_path: " << dst_path;
223 LOG(ERROR) << "error: " << error.message();
230 bool DeleteSymlinkFiles(const bf::path& src_dir, const bf::path& dst_dir) {
231 bs::error_code error;
232 for (bf::directory_iterator file(dst_dir);
233 file != bf::directory_iterator();
235 bf::path current(file->path());
236 if (!bf::is_symlink(symlink_status(current)))
238 bf::path resolved_path = bf::read_symlink(current, error);
240 LOG(ERROR) << "Failed to get resolved path of symlink: " << current
241 << ", error: " << error.message();
245 LOG(DEBUG) << "resolved_path: " << resolved_path;
246 bf::path parent = resolved_path.parent_path();
247 if (parent.empty() || (parent != src_dir)) {
248 LOG(WARNING) << "Parent is empty or not equal to src, parent: ("
252 bf::remove(current, error);
254 LOG(ERROR) << "Symlink deletion failure for: " << current
255 << ", error: " << error.message();
258 LOG(DEBUG) << "removed: " << current;
260 bf::path shared_res = dst_dir / kSharedResDir;
261 if (bf::is_symlink(symlink_status(shared_res))) {
262 bf::remove(shared_res, error);
264 LOG(ERROR) << "Symlink deletion failure for: " << shared_res
265 << ", error: " << error.message();
272 bool CreateStorageDirectories(const boost::filesystem::path& root_path,
273 const std::string& pkgid,
274 bool trusted, bool shareddata,
275 const std::vector<const char*> additional_dirs) {
276 bf::path path(root_path / pkgid);
277 if (!ci::CreateDir(path)) {
278 LOG(ERROR) << "Failed to create dir: " << path;
282 std::vector<const char*> dirs(kEntries);
283 dirs.insert(dirs.end(), additional_dirs.begin(), additional_dirs.end());
285 dirs.push_back(kSharedTrustedDir);
287 bs::error_code error;
288 for (auto& entry : dirs) {
289 bf::path subpath = path / entry;
290 if (!ci::CreateDir(subpath)) {
291 LOG(ERROR) << "Failed to create directory: " << subpath;
297 if (!CreateSharedDataDirectories(root_path, pkgid))
300 if (!DeleteSharedDataDirectories(root_path, pkgid))
304 bf::path shared_cache_path = path / kSharedCacheDir;
305 // remove shared/cache (do not support)
306 if (!ci::RemoveAll(shared_cache_path))
312 bool BackupSharedDataDirectories(const bf::path& apps_rw,
313 const std::string& pkgid) {
314 if (!ci::MakeBackup(apps_rw / pkgid / kSharedDataDir))
316 if (!ci::MakeBackup(apps_rw / kSharedDir / pkgid))
318 if (!ci::MakeBackup(apps_rw / kSharedTmpDir / pkgid))
323 bool RestoreSharedDataDirectories(const bf::path& apps_rw,
324 const std::string& pkgid) {
325 if (!ci::RestoreBackup(apps_rw / pkgid / kSharedDataDir))
327 if (!ci::RestoreBackup(apps_rw / kSharedDir / pkgid))
329 if (!ci::RestoreBackup(apps_rw / kSharedTmpDir / pkgid))
334 bool RemoveBackupSharedDataDirectories(const bf::path& apps_rw,
335 const std::string& pkgid) {
336 if (!ci::RemoveBackup(apps_rw / pkgid / kSharedDataDir))
338 if (!ci::RemoveBackup(apps_rw / kSharedDir / pkgid))
340 if (!ci::RemoveBackup(apps_rw / kSharedTmpDir / pkgid))
345 bool CreateExternalUserDirectories(uid_t user, const std::string& pkgid,
346 const std::string& apps_prefix) {
347 boost::optional<gid_t> gid = ci::GetGidByUid(user);
351 std::string group_name = ci::GetGroupNameByGid(*gid);
352 if (group_name != tzplatform_getenv(TZ_SYS_USER_GROUP)) {
353 LOG(ERROR) << "Failed to get group name of gid: " << *gid;
357 LOG(DEBUG) << "Creating directories for uid: " << user << ", gid: "
360 bf::path apps_rw = GetDirectoryPathForStorage(user, apps_prefix);
361 if (apps_rw.empty()) {
362 LOG(DEBUG) << "Directory not exists: " << apps_rw;
366 if (!CreateStorageDirectories(apps_rw, pkgid, true, false,
367 std::vector<const char*>()))
370 if (!::SetPackageDirectoryOwnerAndPermissions(apps_rw / pkgid, user))
378 namespace common_installer {
380 std::string GetDirectoryPathForInternalStorage() {
381 const char* internal_storage_prefix = tzplatform_getenv(TZ_SYS_HOME);
382 if (internal_storage_prefix)
383 return std::string(internal_storage_prefix);
384 return tzplatform_getenv(TZ_SYS_HOME);
387 std::string GetDirectoryPathForExternalStorage() {
388 return GetExternalCardPath().string();
391 bool PerformExternalDirectoryCreationForUser(uid_t user,
392 const std::string& pkgid) {
393 bf::path storage_path = GetExternalCardPath();
395 if (!bf::exists(storage_path)) {
396 LOG(WARNING) << "External storage (SD Card) is not mounted.";
400 bf::path storage_apps_path = storage_path / "apps";
401 if (!bf::exists(storage_apps_path)) {
402 bs::error_code error;
403 bf::create_directories(storage_apps_path, error);
405 LOG(ERROR) << "Failed to create directory: "
406 << storage_apps_path.c_str();
411 if (CreateExternalUserDirectories(user, pkgid, storage_apps_path.string()))
417 bool PerformExternalDirectoryDeletionForUser(uid_t user,
418 const std::string& pkgid) {
419 bf::path storage_path = GetExternalCardPath();
420 if (!bf::exists(storage_path)) {
421 LOG(WARNING) << "External storage (SD Card) is not mounted. "
422 << "It will be ignored";
426 bf::path storage_apps_path = bf::path(storage_path) / "apps";
427 return DeleteDirectories(
428 GetDirectoryPathForStorage(user, storage_apps_path.string()), pkgid);
431 bool PerformExternalDirectoryCreationForAllUsers(const std::string& pkgid) {
432 UserList list = ci::GetUserList();
433 for (auto l : list) {
434 if (!PerformExternalDirectoryCreationForUser(std::get<0>(l),
436 LOG(WARNING) << "Could not create external storage directories for user: "
442 bool PerformExternalDirectoryCreationForAllPkgs() {
443 UserList list = ci::GetUserList();
444 for (auto l : list) {
445 uid_t uid = std::get<0>(l);
446 pkgmgrinfo_pkginfo_filter_h filter_handle = nullptr;
447 int ret = pkgmgrinfo_pkginfo_filter_create(&filter_handle);
448 if (ret != PMINFO_R_OK)
450 ret = pkgmgrinfo_pkginfo_filter_add_string(filter_handle,
451 PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, kExternalStoragePrivilege);
452 if (ret != PMINFO_R_OK) {
453 pkgmgrinfo_pkginfo_filter_destroy(filter_handle);
457 ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(filter_handle,
458 [](const pkgmgrinfo_pkginfo_h handle, void* user_data) -> int {
460 static_cast<uid_t>(reinterpret_cast<uintptr_t>(user_data));
461 char* pkgid = nullptr;
463 int ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
464 if (ret != PMINFO_R_OK)
466 if (!PerformExternalDirectoryCreationForUser(uid, pkgid))
471 reinterpret_cast<void*>(static_cast<uintptr_t>(uid)));
472 if (ret != PMINFO_R_OK) {
473 LOG(DEBUG) << "Failed to create external directoy";
474 pkgmgrinfo_pkginfo_filter_destroy(filter_handle);
477 pkgmgrinfo_pkginfo_filter_destroy(filter_handle);
483 bool PerformExternalDirectoryDeletionForAllUsers(const std::string& pkgid) {
484 UserList list = ci::GetUserList();
485 for (auto l : list) {
486 uid_t uid = std::get<0>(l);
487 ci::PkgQueryInterface pkg_query(pkgid, uid);
488 LOG(DEBUG) << "Deleting directories for user: " << uid;
489 if (pkg_query.IsPackageInstalled()) {
490 LOG(DEBUG) << "Package: " << pkgid << " for uid: " << uid
491 << " still exists. Skipping";
495 if (!PerformExternalDirectoryDeletionForUser(uid, pkgid))
496 LOG(WARNING) << "Could not delete external storage directories for user: "
502 bool CreatePerUserStorageDirectories(const std::string& pkgid, bool trusted,
503 bool shareddata, bool is_readonly,
504 const std::vector<const char*> additional_dirs) {
506 bf::path skel_apps_rw = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
507 bf::path(kSkelAppDir);
508 if (!::CreateStorageDirectories(skel_apps_rw, pkgid, trusted, shareddata,
510 LOG(ERROR) << "Failed to create skeleton storage directories";
514 std::string error_message;
515 if (!RegisterSecurityContextForPath(pkgid, skel_apps_rw / pkgid,
516 kGlobalUserUid, is_readonly, &error_message)) {
517 LOG(ERROR) << "Failed to register security context for path: "
518 << skel_apps_rw / pkgid << ", error_message: " << error_message;
522 // create per user dir
523 UserList list = ci::GetUserList();
524 for (auto l : list) {
525 uid_t uid = std::get<0>(l);
526 bf::path apps_rw = std::get<2>(l) / "apps_rw";
527 LOG(DEBUG) << "Creating directories for user: " << uid;
528 if (!::CreateStorageDirectories(apps_rw, pkgid, trusted, shareddata,
530 LOG(ERROR) << "Failed to create storage directory for user: " << uid;
534 if (!::SetPackageDirectoryOwnerAndPermissions(apps_rw / pkgid, uid))
538 std::vector<std::string> shared_dirs(kSharedDataEntries);
539 for (auto entry : shared_dirs) {
540 bf::path shared_dst = apps_rw / entry / pkgid;
541 if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
546 if (!RegisterSecurityContextForPath(pkgid, apps_rw / pkgid, uid,
547 false, &error_message)) {
548 LOG(ERROR) << "Failed to register security context for path: "
549 << apps_rw / pkgid << ", error_message: " << error_message;
557 bool DeletePerUserStorageDirectories(const std::string& pkgid,
560 bf::path skel_apps_rw = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
561 bf::path(kSkelAppDir);
562 if (!ci::RemoveAll(skel_apps_rw / pkgid)) {
563 LOG(ERROR) << "Failed to delete skeleton storage directories";
570 // delete per user dir
571 UserList list = ci::GetUserList();
572 for (auto l : list) {
573 uid_t uid = std::get<0>(l);
574 bf::path apps_rw = std::get<2>(l) / "apps_rw";
575 LOG(DEBUG) << "Deleting directories for user: " << uid;
576 if (!ci::RemoveAll(apps_rw / pkgid)) {
577 LOG(ERROR) << "Failed to delete storage directory for user: " << uid;
581 if (!ci::DeletePerUserSharedDataDir(pkgid)) {
582 LOG(ERROR) << "Failed to delete per user shared data dir";
590 bool CreateStorageDirectories(const boost::filesystem::path& path,
591 const std::string& pkgid, uid_t uid,
592 bool trusted, bool shareddata) {
593 if (!::CreateStorageDirectories(path, pkgid, trusted,
594 shareddata, std::vector<const char*>())) {
595 LOG(ERROR) << "Failed to create storage directory for path: " << path;
599 if (!::SetPackageDirectoryOwnerAndPermissions(path / pkgid, uid))
603 std::vector<std::string> shared_dirs(kSharedDataEntries);
604 for (auto entry : shared_dirs) {
605 bf::path shared_dst = path / entry / pkgid;
606 if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
611 std::string error_message;
612 if (!RegisterSecurityContextForPath(pkgid, path / pkgid, uid, false,
614 LOG(ERROR) << "Failed to register security context for path: " << path
615 << ", error_message: " << error_message;
622 void RemoveRWDirectories(const boost::filesystem::path& root) {
623 if (!RemoveAll(root / kCache))
624 LOG(ERROR) << "Failed to remove packaged cache directory";
625 if (!RemoveAll(root / kData))
626 LOG(ERROR) << "Failed to remove packaged data directory";
627 if (!RemoveAll(root / kSharedCacheDir))
628 LOG(ERROR) << "Failed to remove packaged shared/cache directory";
629 if (!RemoveAll(root / kSharedDataDir))
630 LOG(ERROR) << "Failed to remove packaged shared/data directory";
631 if (!RemoveAll(root / kSharedTrustedDir))
632 LOG(ERROR) << "Failed to remove packaged shared/trusted directory";
635 bool DeleteSharedDirectories(const bf::path& path,
636 const std::string& pkgid) {
637 return DeleteSharedDataDirectories(path, pkgid);
640 bool DeleteUserExternalDirectories(const std::string& pkgid) {
641 UserList list = ci::GetUserList();
642 for (auto l : list) {
643 uid_t uid = std::get<0>(l);
644 ci::PkgQueryInterface pkg_query(pkgid, uid);
645 if (pkg_query.IsPackageInstalled()) {
646 LOG(INFO) << pkgid << " is installed for user " << uid;
650 LOG(DEBUG) << "Deleting external directories of " << pkgid
651 << ", for uid: " << uid;
652 bf::path apps_rw(std::get<2>(l) / "apps_rw");
653 if (!DeleteDirectories(apps_rw, pkgid)) {
660 bool CreateGlobalAppSymlinksForAllUsers(const std::string& pkgid) {
661 bf::path src_dir = bf::path(tzplatform_getenv(TZ_SYS_RW_APP)) / pkgid;
662 if (!bf::exists(src_dir)) {
663 LOG(ERROR) << "src_dir not exists";
668 UserList list = ci::GetUserList();
669 for (auto l : list) {
670 uid_t uid = std::get<0>(l);
671 LOG(DEBUG) << "Creating symlinks for uid: " << uid;
672 // check installed user private app.
673 ci::PkgQueryInterface pkg_query(pkgid, uid);
674 if (pkg_query.IsPackageInstalled())
676 bf::path apps_rw(std::get<2>(l) / "apps_rw");
677 bf::path dst_dir = apps_rw / pkgid;
678 if (!bf::exists(dst_dir)) {
679 LOG(WARNING) << "dst_dir not exists";
682 result = CreateSymlinkFiles(src_dir, dst_dir);
687 bool CreateGlobalAppSymlinksForUser(const std::string& pkgid, uid_t uid) {
688 bf::path src_dir = bf::path(tzplatform_getenv(TZ_SYS_RW_APP)) / pkgid;
689 if (!bf::exists(src_dir)) {
690 LOG(WARNING) << "src_dir(" << src_dir << ") not exists";
694 tzplatform_set_user(uid);
695 bf::path dst_dir = bf::path(tzplatform_getenv(TZ_USER_APP)) / pkgid;
696 tzplatform_reset_user();
697 if (!bf::exists(dst_dir)) {
698 LOG(WARNING) << "dst_dir not exists";
701 bool result = CreateSymlinkFiles(src_dir, dst_dir);
706 bool DeleteGlobalAppSymlinksForAllUsers(const std::string& pkgid) {
707 bf::path src_dir = bf::path(tzplatform_getenv(TZ_SYS_RW_APP)) / pkgid;
708 if (!bf::exists(src_dir)) {
709 LOG(WARNING) << "src_dir(" << src_dir << ") not exists";
714 UserList list = ci::GetUserList();
715 for (auto l : list) {
716 uid_t uid = std::get<0>(l);
717 LOG(DEBUG) << "Deleting symlinks for uid: " << uid;
718 // check installed user private app.
719 ci::PkgQueryInterface pkg_query(pkgid, uid);
720 if (pkg_query.IsPackageInstalled())
722 bf::path apps_rw(std::get<2>(l) / "apps_rw");
723 bf::path dst_dir = apps_rw / pkgid;
724 if (!bf::exists(dst_dir)) {
725 LOG(WARNING) << "dst_dir not exists";
728 result = DeleteSymlinkFiles(src_dir, dst_dir);
733 bool DeleteGlobalAppSymlinksForUser(const std::string& pkgid, uid_t uid) {
734 bf::path src_dir = bf::path(tzplatform_getenv(TZ_SYS_RW_APP)) / pkgid;
735 if (!bf::exists(src_dir)) {
736 LOG(ERROR) << "src_dir not exists";
740 tzplatform_set_user(uid);
741 bf::path dst_dir = bf::path(tzplatform_getenv(TZ_USER_APP)) / pkgid;
742 tzplatform_reset_user();
743 if (!bf::exists(dst_dir)) {
744 LOG(WARNING) << "dst_dir not exists";
747 bool result = DeleteSymlinkFiles(src_dir, dst_dir);
751 bool SetPackageDirectoryOwnerAndPermissions(const bf::path& path, uid_t uid) {
752 return ::SetPackageDirectoryOwnerAndPermissions(path, uid);
755 bool ShouldSupportLegacySharedDataDir(const std::string& api_version) {
756 if (bf::exists(kDisableLegacySharedDataDirSupport))
758 utils::VersionNumber api_ver(api_version);
765 bool CreateSharedDataDir(const std::string& pkgid, uid_t uid) {
766 bf::path apps_rw = ci::GetRootAppPath(false, uid);
767 if (!CreateSharedDataDirectories(apps_rw, pkgid))
770 bf::path path = apps_rw / pkgid;
771 std::string error_message;
772 if (!ci::RegisterSecurityContextForPath(pkgid, path, uid, false,
774 LOG(ERROR) << "Failed to register security context for path: " << path
775 << ", error_message: " << error_message;
782 bool CreatePerUserSharedDataDir(const std::string& pkgid) {
784 bf::path skel_apps_rw = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
785 bf::path(kSkelAppDir);
786 LOG(DEBUG) << "Creating directory : " << skel_apps_rw;
787 if (!CreateSharedDataDirectories(skel_apps_rw, pkgid))
790 std::string error_message;
791 if (!ci::RegisterSecurityContextForPath(pkgid, skel_apps_rw / pkgid,
792 kGlobalUserUid, false, &error_message)) {
793 LOG(ERROR) << "Failed to register security context for path: "
794 << skel_apps_rw / pkgid << ", error_message: " << error_message;
798 // create per user dir
799 ci::UserList list = ci::GetUserList();
800 for (auto l : list) {
801 uid_t uid = std::get<0>(l);
802 LOG(DEBUG) << "Adding shareddata directory for uid: " << uid;
804 bf::path apps_rw = ci::GetRootAppPath(false, uid);
805 if (!CreateSharedDataDirectories(apps_rw, pkgid))
808 std::vector<std::string> shared_dirs(kSharedDataEntries);
809 for (auto entry : shared_dirs) {
810 bf::path shared_dst = apps_rw / entry / pkgid;
811 if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
815 std::string error_message;
816 if (!ci::RegisterSecurityContextForPath(pkgid, apps_rw / pkgid, uid,
817 false, &error_message)) {
818 LOG(ERROR) << "Failed to register security context for path: "
819 << apps_rw / pkgid << ", error_message: " << error_message;
827 bool DeleteSharedDataDir(const std::string& pkgid, uid_t uid) {
828 bf::path apps_rw = ci::GetRootAppPath(false, uid);
829 return DeleteSharedDataDirectories(apps_rw, pkgid);
832 bool DeletePerUserSharedDataDir(const std::string& pkgid) {
833 bf::path skel_apps_rw = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
834 bf::path(kSkelAppDir);
835 if (!DeleteSharedDataDirectories(skel_apps_rw, pkgid))
838 ci::UserList list = ci::GetUserList();
839 for (auto l : list) {
840 uid_t uid = std::get<0>(l);
841 bf::path apps_rw = ci::GetRootAppPath(false, uid);
842 if (!DeleteSharedDataDirectories(apps_rw, pkgid))
849 bool BackupSharedDataDir(const std::string& pkgid, uid_t uid) {
850 bf::path apps_rw = ci::GetRootAppPath(false, uid);
851 return BackupSharedDataDirectories(apps_rw, pkgid);
854 bool BackupPerUserSharedDataDir(const std::string& pkgid) {
855 bf::path skel_apps_rw = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
856 bf::path(kSkelAppDir);
857 if (!BackupSharedDataDirectories(skel_apps_rw, pkgid))
860 ci::UserList list = ci::GetUserList();
861 for (auto l : list) {
862 uid_t uid = std::get<0>(l);
863 if (!BackupSharedDataDir(pkgid, uid))
870 bool RestoreSharedDataDir(const std::string& pkgid, uid_t uid) {
871 bf::path apps_rw = ci::GetRootAppPath(false, uid);
872 return RestoreSharedDataDirectories(apps_rw, pkgid);
875 bool RestorePerUserSharedDataDir(const std::string& pkgid) {
876 bf::path skel_apps_rw = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
877 bf::path(kSkelAppDir);
878 if (!RestoreSharedDataDirectories(skel_apps_rw, pkgid))
881 ci::UserList list = ci::GetUserList();
882 for (auto l : list) {
883 uid_t uid = std::get<0>(l);
884 if (!RestoreSharedDataDir(pkgid, uid))
891 bool RemoveBackupSharedDataDir(const std::string& pkgid, uid_t uid) {
892 bf::path apps_rw = ci::GetRootAppPath(false, uid);
893 return RemoveBackupSharedDataDirectories(apps_rw, pkgid);
896 bool RemoveBackupPerUserSharedDataDir(const std::string& pkgid) {
897 bf::path skel_apps_rw = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
898 bf::path(kSkelAppDir);
899 if (!RemoveBackupSharedDataDirectories(skel_apps_rw, pkgid))
902 ci::UserList list = ci::GetUserList();
903 for (auto l : list) {
904 uid_t uid = std::get<0>(l);
905 if (!RemoveBackupSharedDataDir(pkgid, uid))
912 } // namespace common_installer