Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_copy_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_copy_storage_directories.h"
6
7 #include <filesystem>
8 #include <string>
9 #include <system_error>
10
11 #include "common/utils/paths.h"
12 #include "utils/file_util.h"
13
14 namespace fs = std::filesystem;
15
16 namespace {
17
18 const char kCache[] = "cache";
19 const char kDataLocation[] = "data";
20 const char kSharedResLocation[] = "shared";
21
22 }  // namespace
23
24 namespace common_installer {
25 namespace filesystem {
26
27 bool StepCopyStorageDirectories::CopyAppStorage(
28     const fs::path& in_src,
29     const fs::path& in_dst,
30     const char *key,
31     bool merge_dirs) {
32   fs::path src = in_src / key;
33   fs::path dst = in_dst / key;
34   return common_installer::CopyDir(src, dst,
35       merge_dirs ? common_installer::FS_MERGE_SKIP |
36                    common_installer::FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS
37                  : common_installer::FS_NONE, false);
38 }
39
40 common_installer::Step::Status StepCopyStorageDirectories::precheck() {
41   backup_path_ =
42       common_installer::GetBackupPathForPackagePath(context_->GetPkgPath());
43
44   std::error_code error_code;
45   if (!fs::exists(backup_path_, error_code)) {
46     LOG(DEBUG) << "Cannot restore storage directories from: " << backup_path_;
47     return Status::INVALID_VALUE;
48   }
49
50   return Status::OK;
51 }
52
53 common_installer::Step::Status StepCopyStorageDirectories::process() {
54   if (context_->request_mode.get() == RequestMode::GLOBAL)
55     return Status::OK;
56   if (!CopyAppStorage(backup_path_,
57                       context_->GetPkgPath(),
58                       kDataLocation, true)) {
59     LOG(ERROR) << "Failed to restore private directory for widget in update";
60     return Status::APP_DIR_ERROR;
61   }
62
63   if (!CopyAppStorage(backup_path_,
64                       context_->GetPkgPath(),
65                       kSharedResLocation, true)) {
66     LOG(ERROR) << "Failed to restore shared directory for widget in update";
67     return Status::APP_DIR_ERROR;
68   }
69
70   if (!CacheDir())
71     return Status::APP_DIR_ERROR;
72
73   return Status::OK;
74 }
75
76 bool StepCopyStorageDirectories::CacheDir() {
77   std::error_code error_code;
78   fs::path cache_path = context_->GetPkgPath() / kCache;
79   fs::create_directory(cache_path, error_code);
80   if (error_code) {
81     LOG(ERROR) << "Failed to create cache directory for package";
82     return false;
83   }
84   return true;
85 }
86
87 }  // namespace filesystem
88 }  // namespace common_installer