Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_create_storage_directories.cc
1 // Copyright (c) 2015 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_storage_directories.h"
6
7 #include <pkgmgrinfo_basic.h>
8
9 #include <filesystem>
10 #include <string>
11 #include <vector>
12
13 #include "common/privileges.h"
14 #include "common/shared_dirs.h"
15 #include "common/utils/file_util.h"
16 #include "common/utils/glist_range.h"
17
18 namespace ci = common_installer;
19 namespace fs = std::filesystem;
20
21 namespace common_installer {
22 namespace filesystem {
23
24 bool StepCreateStorageDirectories::CreatePerUserStorageDirs(
25     bool trusted, bool shareddata) {
26   LOG(INFO) << "Creating per-user directories for package: "
27             << context_->pkgid.get();
28
29   bool is_readonly = context_->is_readonly_package.get();
30   if (!CreatePerUserStorageDirectories(context_->pkgid.get(), trusted,
31         shareddata, is_readonly, additional_shared_dirs_)) {
32     LOG(ERROR) << "Failed to create per-user storage directories";
33     return false;
34   }
35
36   return true;
37 }
38
39 bool StepCreateStorageDirectories::CreateStorageDirs(
40     bool trusted, bool shareddata) {
41   if (!CreateStorageDirectories(context_->root_application_path.get(),
42                                 context_->pkgid.get(), context_->uid.get(),
43                                 trusted, shareddata)) {
44     LOG(ERROR) << "Failed to create storage directories";
45     return false;
46   }
47   return true;
48 }
49
50 bool StepCreateStorageDirectories::DeletePerUserStorageDirs() {
51   return ci::DeletePerUserStorageDirectories(context_->pkgid.get());
52 }
53
54 Step::Status StepCreateStorageDirectories::process() {
55   bool trusted = false;
56   std::string author_id = context_->certificate_info.get().author_id.get();
57   if (!author_id.empty())
58     trusted = true;
59
60   manifest_x* manifest = context_->manifest_data.get();
61   bool shareddata = ShouldSupportLegacySharedDataDir(manifest->api_version);
62   if (!shareddata) {
63     for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
64       if (!strcmp(priv->value, privileges::kPrivForSharedData)) {
65         shareddata = true;
66         break;
67       }
68     }
69   }
70
71   if (context_->request_mode.get() == RequestMode::GLOBAL) {
72     if (!CreatePerUserStorageDirs(trusted, shareddata))
73       return Status::APP_DIR_ERROR;
74   } else {
75     if (!CreateStorageDirs(trusted, shareddata))
76       return Status::APP_DIR_ERROR;
77   }
78   return Status::OK;
79 }
80
81 Step::Status StepCreateStorageDirectories::undo() {
82   if (context_->request_mode.get() != RequestMode::GLOBAL)
83     return Step::Status::OK;
84
85   if (!DeletePerUserStorageDirs()) {
86     LOG(ERROR) << "Failed to delete per user storage directories";
87     return Status::APP_DIR_ERROR;
88   }
89
90   return Status::OK;
91 }
92
93 }  // namespace filesystem
94 }  // namespace common_installer