Fix RecoverStorageDirectories
[platform/core/appfw/app-installers.git] / src / common / shared_dirs.cc
index d9379b9..52dd7b5 100644 (file)
@@ -979,7 +979,28 @@ bool BackupPerUserSharedDataDir(const std::string& pkgid) {
 
 bool RestoreSharedDataDir(const std::string& pkgid, uid_t uid) {
   bf::path apps_rw = ci::GetRootAppPath(false, uid);
-  return RestoreSharedDataDirectories(apps_rw, pkgid);
+  if (!RestoreSharedDataDirectories(apps_rw, pkgid))
+    return false;
+
+  if (!::SetPackageDirectoryOwnerAndPermissions(apps_rw / pkgid, uid))
+    return false;
+
+  std::vector<std::string> shared_dirs(kSharedDataEntries);
+  for (auto entry : shared_dirs) {
+    bf::path shared_dst = apps_rw / entry / pkgid;
+    if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
+      return false;
+  }
+
+  std::string error_message;
+  if (!RegisterSecurityContextForPath(pkgid, apps_rw / pkgid, uid, false,
+                                      &error_message)) {
+    LOG(ERROR) << "Failed to register security context for path: " << apps_rw
+               << ", error_message: " << error_message;
+    return false;
+  }
+
+  return true;
 }
 
 bool RestorePerUserSharedDataDir(const std::string& pkgid) {
@@ -988,6 +1009,7 @@ bool RestorePerUserSharedDataDir(const std::string& pkgid) {
   if (!RestoreSharedDataDirectories(skel_apps_rw, pkgid))
     return false;
 
+  std::string error_message;
   ci::UserList list = ci::GetUserList();
   for (auto l : list) {
     uid_t uid = std::get<0>(l);
@@ -1001,6 +1023,23 @@ bool RestorePerUserSharedDataDir(const std::string& pkgid) {
     for (auto& apps_rw : apps_rw_paths) {
       if (!RestoreSharedDataDirectories(apps_rw, pkgid))
         return false;
+
+      if (!::SetPackageDirectoryOwnerAndPermissions(apps_rw / pkgid, uid))
+        return false;
+
+      std::vector<std::string> shared_dirs(kSharedDataEntries);
+      for (auto entry : shared_dirs) {
+        bf::path shared_dst = apps_rw / entry / pkgid;
+        if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
+          return false;
+      }
+
+      if (!RegisterSecurityContextForPath(pkgid, apps_rw / pkgid, uid,
+              false, &error_message)) {
+        LOG(ERROR) << "Failed to register security context for path: "
+                   << apps_rw / pkgid << ", error_message: " << error_message;
+        return false;
+      }
     }
   }