Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_change_ownership_and_permission.cc
index 7192f00..2b86ee9 100644 (file)
 #include <sys/types.h>
 #include <fcntl.h>
 #include <pkgmgr-info.h>
-#include <cassert>
 
+#include <cassert>
 #include <cstring>
+#include <filesystem>
+#include <optional>
 #include <string>
 #include <vector>
 
-#include <boost/range/iterator_range.hpp>
-
-#include "common/paths.h"
-#include "common/request.h"
+#include "common/utils/paths.h"
 #include "common/shared_dirs.h"
 #include "common/utils/file_util.h"
 #include "common/utils/glist_range.h"
 #include "common/utils/user_util.h"
+#include "common/utils/request.h"
 
-namespace bf = boost::filesystem;
 namespace ci = common_installer;
+namespace fs = std::filesystem;
 
 namespace {
 
 const char kSystemShareGroupName[] = "system_share";
-const char kDataDir[] = "data";
-const char kSharedDataDir[] = "shared/data";
-
-bool GrantPermission755(const bf::path& path) {
-  auto permission = bf::perms::owner_all |
-      bf::perms::group_read | bf::perms::group_exe |
-      bf::perms::others_read | bf::perms::others_exe;
+const std::vector<const char*> kDataDirEntries = {
+  {"data"},
+  {"shared/data"},
+  {"cache"},
+};
+
+bool GrantPermission755(const fs::path& path) {
+  auto permission = fs::perms::owner_all |
+      fs::perms::group_read | fs::perms::group_exec |
+      fs::perms::others_read | fs::perms::others_exec;
   if (!ci::SetDirPermissions(path, permission)) {
     LOG(ERROR) << "Grant permission error" << " path: " << path
-               << " permission: " << permission;
+               << " permission: 755";
     return false;
   }
   return true;
 }
 
-bool GrantPermission644(const bf::path& path) {
-  auto permission = bf::perms::owner_read | bf::perms::owner_write |
-      bf::perms::group_read | bf::perms::others_read;
+bool GrantPermission644(const fs::path& path) {
+  auto permission = fs::perms::owner_read | fs::perms::owner_write |
+      fs::perms::group_read | fs::perms::others_read;
   if (!ci::SetDirPermissions(path, permission)) {
     LOG(ERROR) << "Grant permission error" << " path: " << path
-               << " permission: " << permission;
+               << " permission: 644";
     return false;
   }
   return true;
 }
 
-bool ChangeDataDir(const bf::path& pkg_path, uid_t uid) {
+bool ChangeDataDir(const fs::path& pkg_path, uid_t uid) {
   if (ci::GetRequestMode(uid) == ci::RequestMode::GLOBAL)
     return true;
-  boost::optional<gid_t> gid = ci::GetGidByGroupName(kSystemShareGroupName);
+  std::optional<gid_t> gid = ci::GetGidByGroupName(kSystemShareGroupName);
   if (!gid) {
     LOG(ERROR) << "Failed to get gid of " << kSystemShareGroupName;
     return false;
   }
 
-  bf::perms prms = bf::add_perms | bf::group_write | bf::set_gid_on_exe;
-  bf::path data = pkg_path / kDataDir;
-  if (!ci::SetOwnership(data, uid, *gid)) {
-    LOG(ERROR) << "Failed to change owner: " << data
-               << "(" << uid << ", " << *gid << ")";
-    return false;
-  }
-  if (!ci::SetDirPermissions(data, prms)) {
-    LOG(ERROR) << "Failed to change permission: " << data
-               << std::oct << prms;
-    return false;
-  }
-  bf::path shareddata = pkg_path / kSharedDataDir;
-  if (!bf::exists(shareddata))
-    return true;
-  if (!ci::SetOwnership(shareddata, uid, *gid)) {
-    LOG(ERROR) << "Failed to change owner: " << shareddata
-               << "(" << uid << ", " << *gid << ")";
-    return false;
-  }
-  if (!ci::SetDirPermissions(shareddata, prms)) {
-    LOG(ERROR) << "Failed to change permission: " << shareddata
-               << std::oct << prms;
-    return false;
+  fs::perms prms = fs::perms::group_write | fs::perms::set_gid;
+  for (auto& entry : kDataDirEntries) {
+    fs::path path = pkg_path / entry;
+    if (!fs::exists(path))
+      continue;
+    if (!ci::SetOwnership(path, uid, *gid)) {
+      LOG(ERROR) << "Failed to change owner: " << path
+                 << "(" << uid << ", " << *gid << ")";
+      return false;
+    }
+    if (!ci::SetDirPermissions(path, prms, true)) {
+      LOG(ERROR) << "Failed to add data dir permission: " << path;
+      return false;
+    }
   }
 
   return true;
 }
 
-bool GrantDefaultPermissions(bf::path pkg_path) {
-  if (bf::is_directory(pkg_path)) {
+bool GrantDefaultPermissions(fs::path pkg_path, bool skip_symlink) {
+  if (fs::is_directory(pkg_path)) {
     if (!GrantPermission755(pkg_path))
       return false;
   }
-  for (auto& entry :
-      boost::make_iterator_range(bf::directory_iterator(pkg_path), {})) {
-    auto path = entry.path();
-
-    // symlink will be skipped
-    if (bf::is_symlink(symlink_status(path)))
+  for (auto& entry : fs::directory_iterator(pkg_path)) {
+    if (skip_symlink && fs::is_symlink(symlink_status(entry)))
       continue;
 
     // skip path, which is related to mount or directory installer creates
-    if (bf::is_directory(path) &&
-        (path.filename() == ".mmc" || path.filename() == ".pkg" ||
-        path.filename() == "tep"))
+    if (fs::is_directory(entry) &&
+        (entry.path().filename() == ".mmc" || entry.path().filename() == ".pkg" ||
+        entry.path().filename() == "tep"))
       continue;
 
-    if (bf::is_directory(path) && path.filename() == "bin") {
-      if (!GrantPermission755(path))
+    if (fs::is_directory(entry) && entry.path().filename() == "bin") {
+      if (!GrantPermission755(entry))
         return false;
-      for (auto& entry :
-          boost::make_iterator_range(bf::directory_iterator(path), {})) {
-        auto path = entry.path();
-        if (bf::is_regular_file(path)) {
-          if (!GrantPermission755(path))
+      for (auto& bin_entry : fs::directory_iterator(entry)) {
+        if (fs::is_symlink(symlink_status(bin_entry)))
+          continue;
+
+        if (fs::is_regular_file(bin_entry)) {
+          if (!GrantPermission755(bin_entry))
             return false;
         }
       }
       continue;
     }
 
-    if (bf::is_directory(path) && path.filename() == "lib") {
-      if (!GrantPermission755(path))
+    if (fs::is_directory(entry) && entry.path().filename() == "lib") {
+      if (!GrantPermission755(entry))
         return false;
-      for (auto& entry :
-          boost::make_iterator_range(bf::directory_iterator(path), {})) {
-        auto path = entry.path();
-        if (bf::is_regular_file(path)) {
-          if (!GrantPermission644(path))
+      for (auto& lib_entry : fs::directory_iterator(entry)) {
+        if (fs::is_symlink(fs::symlink_status(lib_entry)))
+          continue;
+
+        if (fs::is_regular_file(lib_entry)) {
+          if (!GrantPermission644(lib_entry))
             return false;
         }
       }
       continue;
     }
 
-    if (bf::is_directory(path)) {
-      if (!GrantPermission755(path))
+    if (fs::is_directory(entry)) {
+      if (!GrantPermission755(entry))
         return false;
       continue;
     }
 
-    if (bf::is_regular_file(path)) {
-      if (!GrantPermission644(path))
+    if (fs::is_regular_file(entry)) {
+      if (!GrantPermission644(entry))
         return false;
       continue;
     }
@@ -162,13 +153,19 @@ bool GrantDefaultPermissions(bf::path pkg_path) {
 namespace common_installer {
 namespace filesystem {
 
+StepChangeOwnershipAndPermission::StepChangeOwnershipAndPermission(
+    InstallerContext* context, bool skip_symlink = false)
+    : Step(context),
+      skip_symlink_(skip_symlink) {
+}
+
 Step::Status StepChangeOwnershipAndPermission::precheck() {
   if (context_->root_application_path.get().empty()) {
     LOG(ERROR) << "root_application_path attribute is empty";
     return Step::Status::INVALID_VALUE;
   }
 
-  if (!boost::filesystem::exists(context_->root_application_path.get())) {
+  if (!std::filesystem::exists(context_->root_application_path.get())) {
     LOG(ERROR) << "root_application_path ("
                << context_->root_application_path.get()
                << ") path does not exist";
@@ -185,19 +182,19 @@ Step::Status StepChangeOwnershipAndPermission::precheck() {
 
 Step::Status StepChangeOwnershipAndPermission::process() {
   uid_t uid = context_->uid.get();
-  boost::optional<gid_t> gid = ci::GetGidByUid(uid);
+  std::optional<gid_t> gid = ci::GetGidByUid(uid);
   if (!gid)
     return Status::ERROR;
 
   // Grant default permissions
-  if (!GrantDefaultPermissions(context_->pkg_path.get()))
+  if (!GrantDefaultPermissions(context_->GetPkgPath(), skip_symlink_))
     return Status::GRANT_PERMISSION_ERROR;
 
   // Change owner of files at root path
-  if (!ci::SetOwnershipAll(context_->pkg_path.get(), uid, *gid))
+  if (!ci::SetOwnershipAll(context_->GetPkgPath(), uid, *gid))
     return Status::ERROR;
 
-  if (!ChangeDataDir(context_->pkg_path.get(), uid))
+  if (!ChangeDataDir(context_->GetPkgPath(), uid))
     return Status::ERROR;
 
   // For icon files
@@ -210,8 +207,8 @@ Step::Status StepChangeOwnershipAndPermission::process() {
         continue;
 
       icon_x* icon = reinterpret_cast<icon_x*>(app->icon->data);
-      bf::path icon_path = icon->text;
-      bf::path found_icon_path = GetIconPath(iconpath,
+      fs::path icon_path = icon->text;
+      fs::path found_icon_path = GetIconPath(iconpath,
           context_->pkgid.get(), icon_path.filename(),
           context_->root_application_path.get());
       if (!found_icon_path.empty()) {