Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_copy.cc
1 /* 2014, Copyright © Intel Coporation, license APACHE-2.0, see LICENSE file */
2 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a apache 2.0 license that can be
4 // found in the LICENSE file.
5
6 #include "common/step/filesystem/step_copy.h"
7
8 #include <cassert>
9 #include <cstring>
10 #include <filesystem>
11 #include <string>
12 #include <system_error>
13
14 #include "common/shared_dirs.h"
15 #include "common/utils/file_util.h"
16
17 namespace ci = common_installer;
18 namespace fs = std::filesystem;
19
20 namespace common_installer {
21 namespace filesystem {
22
23 Step::Status StepCopy::precheck() {
24   if (context_->root_application_path.get().empty()) {
25     LOG(ERROR) << "root_application_path attribute is empty";
26     return Step::Status::INVALID_VALUE;
27   }
28   if (!std::filesystem::exists(context_->root_application_path.get())) {
29     LOG(ERROR) << "root_application_path ("
30                << context_->root_application_path.get()
31                << ") path does not exist";
32     return Step::Status::INVALID_VALUE;
33   }
34
35   if (context_->unpacked_dir_path.get().empty()) {
36     LOG(ERROR) << "unpacked_dir_path attribute is empty";
37     return Step::Status::INVALID_VALUE;
38   }
39   if (!std::filesystem::exists(context_->unpacked_dir_path.get())) {
40     LOG(ERROR) << "unpacked_dir_path ("
41                << context_->unpacked_dir_path.get()
42                << ") path does not exist";
43     return Step::Status::INVALID_VALUE;
44   }
45
46   if (!context_->manifest_data.get()) {
47     LOG(ERROR) << "manifest_data attribute is empty";
48     return Step::Status::MANIFEST_NOT_FOUND;
49   }
50
51   if (context_->pkgid.get().empty()) {
52     LOG(ERROR) << "pkgid attribute is empty";
53     return Step::Status::PACKAGE_NOT_FOUND;
54   }
55
56   return Step::Status::OK;
57 }
58
59 Step::Status StepCopy::process() {
60   fs::path install_path;
61   if (context_->storage.get() == Storage::EXTENDED) {
62     install_path = fs::path(GetExtendedRootAppPath(context_->uid.get())) /
63         context_->pkgid.get();
64   } else {
65     install_path = context_->GetPkgPath();
66   }
67
68   ci::RemoveRWDirectories(context_->unpacked_dir_path.get());
69
70   std::error_code error;
71   fs::create_directories(install_path.parent_path(), error);
72   if (error) {
73     LOG(ERROR) << "Cannot create directory: "
74                << install_path.parent_path().string();
75     return Step::Status::APP_DIR_ERROR;
76   }
77   if (!MoveDir(context_->unpacked_dir_path.get(), install_path,
78                FSFlag::FS_MERGE_SKIP)) {
79     LOG(ERROR) << "Cannot move widget directory to install path, from "
80         << context_->unpacked_dir_path.get() << " to " << install_path;
81     return Status::APP_DIR_ERROR;
82   }
83   LOG(INFO) << "Successfully move: " << context_->unpacked_dir_path.get()
84             << " to: " << install_path << " directory";
85
86   if (context_->storage.get() == Storage::EXTENDED) {
87     fs::create_symlink(install_path, context_->GetPkgPath(), error);
88     if (error) {
89       LOG(ERROR) << "Failed to create symlink for extended path: "
90                  << error.message();
91     }
92   }
93   return Status::OK;
94 }
95
96 Step::Status StepCopy::clean() {
97   if (context_->external_storage)
98     context_->external_storage->Commit();
99   return Status::OK;
100 }
101
102 Step::Status StepCopy::undo() {
103   if (context_->external_storage) {
104     context_->external_storage->Abort();
105   } else if (context_->storage.get() == Storage::EXTENDED) {
106     fs::path install_path =
107         fs::path(GetExtendedRootAppPath(context_->uid.get())) /
108             context_->pkgid.get();
109     RemoveAll(install_path);
110   }
111
112   if (!RemoveAll(context_->GetPkgPath()))
113     return Status::APP_DIR_ERROR;
114   return Status::OK;
115 }
116
117 }  // namespace filesystem
118 }  // namespace common_installer