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.
6 #include "common/step/filesystem/step_copy.h"
12 #include <system_error>
14 #include "common/shared_dirs.h"
15 #include "common/utils/file_util.h"
17 namespace ci = common_installer;
18 namespace fs = std::filesystem;
20 namespace common_installer {
21 namespace filesystem {
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;
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;
35 if (context_->unpacked_dir_path.get().empty()) {
36 LOG(ERROR) << "unpacked_dir_path attribute is empty";
37 return Step::Status::INVALID_VALUE;
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;
46 if (!context_->manifest_data.get()) {
47 LOG(ERROR) << "manifest_data attribute is empty";
48 return Step::Status::MANIFEST_NOT_FOUND;
51 if (context_->pkgid.get().empty()) {
52 LOG(ERROR) << "pkgid attribute is empty";
53 return Step::Status::PACKAGE_NOT_FOUND;
56 return Step::Status::OK;
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();
65 install_path = context_->GetPkgPath();
68 ci::RemoveRWDirectories(context_->unpacked_dir_path.get());
70 std::error_code error;
71 fs::create_directories(install_path.parent_path(), error);
73 LOG(ERROR) << "Cannot create directory: "
74 << install_path.parent_path().string();
75 return Step::Status::APP_DIR_ERROR;
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;
83 LOG(INFO) << "Successfully move: " << context_->unpacked_dir_path.get()
84 << " to: " << install_path << " directory";
86 if (context_->storage.get() == Storage::EXTENDED) {
87 fs::create_symlink(install_path, context_->GetPkgPath(), error);
89 LOG(ERROR) << "Failed to create symlink for extended path: "
96 Step::Status StepCopy::clean() {
97 if (context_->external_storage)
98 context_->external_storage->Commit();
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);
112 if (!RemoveAll(context_->GetPkgPath()))
113 return Status::APP_DIR_ERROR;
117 } // namespace filesystem
118 } // namespace common_installer