Merge changes I168c9485,Ia3df2821 into tizen
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_create_res_control_directories.cc
1 // Copyright (c) 2021 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.
4
5 #include "common/step/filesystem/step_create_res_control_directories.h"
6
7 #include <boost/filesystem/path.hpp>
8
9 #include <string>
10
11 #include "common/utils/file_util.h"
12
13 namespace bf = boost::filesystem;
14 namespace ci = common_installer;
15
16 namespace {
17
18 constexpr char kAllowedDir[] = "res/mount/allowed";
19 constexpr char kGlobalDir[] = "res/mount/global";
20 constexpr char kWorkDir[] = "res/mount/work";
21
22 bool CreateResControlDirs(const bf::path& root) {
23   if (!ci::CreateDir(root / kAllowedDir))
24     return false;
25   if (!ci::CreateDir(root / kGlobalDir))
26     return false;
27   if (!ci::CreateDir(root / kWorkDir))
28     return false;
29   return true;
30 }
31
32 bool RemoveResControlDirs(const bf::path& root) {
33   if (!ci::RemoveAll(root / kAllowedDir))
34     return false;
35   if (!ci::RemoveAll(root / kGlobalDir))
36     return false;
37   if (!ci::RemoveAll(root / kWorkDir))
38     return false;
39   return true;
40 }
41
42 }  // namespace
43
44 namespace common_installer {
45 namespace filesystem {
46
47 Step::Status StepCreateResControlDirectories::precheck() {
48   if (context_->GetPkgPath().empty()) {
49     LOG(ERROR) << "Failed to get pkg root directory";
50     return Status::APP_DIR_ERROR;
51   }
52   return Status::OK;
53 }
54
55 Step::Status StepCreateResControlDirectories::process() {
56   bf::path root = context_->GetPkgPath();
57   // TODO(jeremy.jang): check the package has res-control tag (+ update case)
58   if (!CreateResControlDirs(root)) {
59     LOG(ERROR) << "Failed to create res-control directories";
60     return Status::APP_DIR_ERROR;
61   }
62   return Status::OK;
63 }
64
65 Step::Status StepCreateResControlDirectories::undo() {
66   bf::path root = context_->GetPkgPath();
67   if (!RemoveResControlDirs(root)) {
68     LOG(ERROR) << "Failed to remove res-control directories";
69     return Status::APP_DIR_ERROR;
70   }
71   return Status::OK;
72 }
73
74 }  // namespace filesystem
75 }  // namespace common_installer