Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_copy_tep.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_tep.h"
7
8 #include <cassert>
9 #include <cstring>
10 #include <filesystem>
11 #include <string>
12 #include <system_error>
13
14 #include "common/utils/paths.h"
15 #include "common/utils/file_util.h"
16
17 namespace common_installer {
18 namespace filesystem {
19
20 namespace fs = std::filesystem;
21
22 Step::Status StepCopyTep::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
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_->pkgid.get().empty()) {
36     LOG(ERROR) << "pkgid attribute is empty";
37     return Step::Status::INVALID_VALUE;
38   }
39
40   return Step::Status::OK;
41 }
42
43 Step::Status StepCopyTep::process() {
44   if (context_->tep_path.get().empty())
45     return Step::Status::OK;
46   fs::path tep_path;
47   if (context_->external_storage) {
48     tep_path = GetExternalTepPath(context_->request_mode.get(),
49                                   context_->uid.get());
50   } else {
51     tep_path = GetInternalTepPath(context_->GetPkgPath());
52   }
53
54   // Keep filename of app store supplied file. Filename contains hash that
55   // appstore uses to identify version of tep on device.
56   tep_path /= context_->tep_path.get().filename();
57
58   if (!fs::exists(tep_path.parent_path())) {
59     std::error_code error;
60     fs::create_directories(tep_path.parent_path(), error);
61     if (error) {
62       LOG(ERROR) << "Cannot create tep parent directory";
63       return Status::APP_DIR_ERROR;
64     }
65   }
66
67   if (context_->is_tep_move.get()) {
68     if (!MoveFile(context_->tep_path.get(), tep_path)) {
69       LOG(ERROR) << "Cannnot move TEP file into install path";
70       return Step::Status::APP_DIR_ERROR;
71     }
72   } else {
73     if (!CopyFile(context_->tep_path.get(), tep_path)) {
74       LOG(ERROR) << "Cannot copy TEP file [" << context_->tep_path.get() <<
75           "] into install path [" << tep_path << "]";
76       return Step::Status::APP_DIR_ERROR;
77     }
78   }
79   context_->tep_path.set(tep_path);
80   context_->manifest_data.get()->tep_name =
81       strdup(context_->tep_path.get().c_str());
82
83   return Step::Status::OK;
84 }
85
86 Step::Status StepCopyTep::undo() {
87   RemoveAll(context_->tep_path.get());
88   // remove tep file is installed outside package path
89   if (context_->external_storage) {
90     fs::path tep_path = GetExternalTepPath(context_->request_mode.get(),
91                                            context_->uid.get());
92     tep_path /= context_->tep_path.get().filename();
93     RemoveAll(tep_path);
94   }
95   return Status::OK;
96 }
97
98 }  // namespace filesystem
99 }  // namespace common_installer