b90c6c044b286effc398c762527ea4d1cb44f036
[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 <string>
11
12 #include "common/shared_dirs.h"
13 #include "common/utils/file_util.h"
14
15 namespace bf = boost::filesystem;
16 namespace bs = boost::system;
17 namespace ci = common_installer;
18
19 namespace common_installer {
20 namespace filesystem {
21
22 Step::Status StepCopy::precheck() {
23   if (context_->root_application_path.get().empty()) {
24     LOG(ERROR) << "root_application_path attribute is empty";
25     return Step::Status::INVALID_VALUE;
26   }
27   if (!boost::filesystem::exists(context_->root_application_path.get())) {
28     LOG(ERROR) << "root_application_path ("
29                << context_->root_application_path.get()
30                << ") path does not exist";
31     return Step::Status::INVALID_VALUE;
32   }
33
34   if (context_->unpacked_dir_path.get().empty()) {
35     LOG(ERROR) << "unpacked_dir_path attribute is empty";
36     return Step::Status::INVALID_VALUE;
37   }
38   if (!boost::filesystem::exists(context_->unpacked_dir_path.get())) {
39     LOG(ERROR) << "unpacked_dir_path ("
40                << context_->unpacked_dir_path.get()
41                << ") path does not exist";
42     return Step::Status::INVALID_VALUE;
43   }
44
45   if (!context_->manifest_data.get()) {
46     LOG(ERROR) << "manifest_data attribute is empty";
47     return Step::Status::MANIFEST_NOT_FOUND;
48   }
49
50   if (context_->pkgid.get().empty()) {
51     LOG(ERROR) << "pkgid attribute is empty";
52     return Step::Status::PACKAGE_NOT_FOUND;
53   }
54
55   return Step::Status::OK;
56 }
57
58 Step::Status StepCopy::process() {
59   bf::path install_path;
60   if (context_->storage.get() == Storage::EXTENDED) {
61     install_path = bf::path(GetExtendedRootAppPath(context_->uid.get())) /
62         context_->pkgid.get();
63   } else {
64     install_path = context_->GetPkgPath();
65   }
66
67   ci::RemoveRWDirectories(context_->unpacked_dir_path.get());
68
69   bs::error_code error;
70   bf::create_directories(install_path.parent_path(), error);
71   if (error) {
72     LOG(ERROR) << "Cannot create directory: "
73                << install_path.parent_path().string();
74     return Step::Status::APP_DIR_ERROR;
75   }
76   if (!MoveDir(context_->unpacked_dir_path.get(), install_path,
77                FSFlag::FS_MERGE_SKIP)) {
78     LOG(ERROR) << "Cannot move widget directory to install path, from "
79         << context_->unpacked_dir_path.get() << " to " << install_path;
80     return Status::APP_DIR_ERROR;
81   }
82   LOG(INFO) << "Successfully move: " << context_->unpacked_dir_path.get()
83             << " to: " << install_path << " directory";
84
85   if (context_->storage.get() == Storage::EXTENDED) {
86     bf::create_symlink(install_path, context_->GetPkgPath(), error);
87     if (error) {
88       LOG(ERROR) << "Failed to create symlink for extended path: "
89                  << error.message();
90     }
91   }
92   return Status::OK;
93 }
94
95 Step::Status StepCopy::clean() {
96   if (context_->external_storage)
97     context_->external_storage->Commit();
98   return Status::OK;
99 }
100
101 Step::Status StepCopy::undo() {
102   if (context_->external_storage) {
103     context_->external_storage->Abort();
104   } else if (context_->storage.get() == Storage::EXTENDED) {
105     bf::path install_path =
106         bf::path(GetExtendedRootAppPath(context_->uid.get())) /
107             context_->pkgid.get();
108     RemoveAll(install_path);
109   }
110
111   if (!RemoveAll(context_->GetPkgPath()))
112     return Status::APP_DIR_ERROR;
113   return Status::OK;
114 }
115
116 }  // namespace filesystem
117 }  // namespace common_installer