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