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