Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_change_ownership_and_permission.cc
index 7b8013c..2b86ee9 100644 (file)
@@ -5,15 +5,15 @@
 
 #include "common/step/filesystem/step_change_ownership_and_permission.h"
 
-#include <boost/range/iterator_range.hpp>
-
 #include <unistd.h>
 #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>
 
@@ -24,8 +24,8 @@
 #include "common/utils/user_util.h"
 #include "common/utils/request.h"
 
-namespace bf = boost::filesystem;
 namespace ci = common_installer;
+namespace fs = std::filesystem;
 
 namespace {
 
@@ -36,51 +36,50 @@ const std::vector<const char*> kDataDirEntries = {
   {"cache"},
 };
 
-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;
+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;
+  fs::perms prms = fs::perms::group_write | fs::perms::set_gid;
   for (auto& entry : kDataDirEntries) {
-    bf::path path = pkg_path / entry;
-    if (!bf::exists(path))
+    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)) {
-      LOG(ERROR) << "Failed to change permission: " << path
-                 << std::oct << prms;
+    if (!ci::SetDirPermissions(path, prms, true)) {
+      LOG(ERROR) << "Failed to add data dir permission: " << path;
       return false;
     }
   }
@@ -88,66 +87,59 @@ bool ChangeDataDir(const bf::path& pkg_path, uid_t uid) {
   return true;
 }
 
-bool GrantDefaultPermissions(bf::path pkg_path, bool skip_symlink) {
-  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), {})) {
-    const auto& path = entry.path();
-
-    if (skip_symlink && bf::is_symlink(symlink_status(path)))
-          continue;
+  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& bin_entry :
-          boost::make_iterator_range(bf::directory_iterator(path), {})) {
-        const auto& bin_path = bin_entry.path();
-        if (bf::is_symlink(symlink_status(bin_path)))
+      for (auto& bin_entry : fs::directory_iterator(entry)) {
+        if (fs::is_symlink(symlink_status(bin_entry)))
           continue;
 
-        if (bf::is_regular_file(bin_path)) {
-          if (!GrantPermission755(bin_path))
+        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& lib_entry :
-          boost::make_iterator_range(bf::directory_iterator(path), {})) {
-        const auto& lib_path = lib_entry.path();
-        if (bf::is_symlink(symlink_status(lib_path)))
+      for (auto& lib_entry : fs::directory_iterator(entry)) {
+        if (fs::is_symlink(fs::symlink_status(lib_entry)))
           continue;
 
-        if (bf::is_regular_file(lib_path)) {
-          if (!GrantPermission644(lib_path))
+        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;
     }
@@ -173,7 +165,7 @@ Step::Status StepChangeOwnershipAndPermission::precheck() {
     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";
@@ -190,7 +182,7 @@ 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;
 
@@ -215,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()) {