Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / mount / step_mount_unpacked.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/mount/step_mount_unpacked.h"
6
7 #include <unistd.h>
8
9 #include <filesystem>
10 #include <string>
11
12 #include "common/utils/paths.h"
13 #include "common/utils/file_util.h"
14 #include "common/tzip_interface.h"
15 #include "common/zip_interface.h"
16
17 namespace fs = std::filesystem;
18
19 namespace {
20
21 const char kPackageUnpackDirPath[] = UNPACKDIR;
22
23 }  // namespace
24
25 namespace common_installer {
26 namespace mount {
27
28 Step::Status StepMountUnpacked::process() {
29   fs::path tmp_dir = GenerateTmpDir(kPackageUnpackDirPath);
30   context_->unpacked_dir_path.set(tmp_dir);
31   // write unpacked directory for recovery file
32   // to remove unpacked dir properly when recovery mode
33   if (context_->recovery_info.get().recovery_file) {
34     context_->recovery_info.get().recovery_file->set_unpacked_dir(tmp_dir);
35     context_->recovery_info.get().recovery_file->WriteAndCommitFileContent();
36   }
37
38   auto zip = CreateZipInterface(context_->unpacked_dir_path.get());
39   if (!zip->MountZip(context_->file_path.get())) {
40     LOG(ERROR) << "Failed to mount zip file: " << context_->file_path.get();
41     return Status::IMAGE_ERROR;
42   }
43   LOG(DEBUG) << "Zip mounted in unpacked_dir: "
44              << context_->unpacked_dir_path.get();
45   return Status::OK;
46 }
47
48 Step::Status StepMountUnpacked::undo() {
49   auto zip = CreateZipInterface(context_->unpacked_dir_path.get());
50   if (!zip->UnmountZip()) {
51     LOG(ERROR) << "Failed to unmount zip file: " << context_->file_path.get();
52     return Status::IMAGE_ERROR;
53   }
54
55   RemoveAll(context_->unpacked_dir_path.get());
56
57   if (context_->request_type.get() == RequestType::MountUpdate) {
58     fs::path mount_point = GetMountLocation(context_->GetPkgPath());
59     auto zip_final = CreateZipInterface(mount_point);
60     if (!zip_final->UnmountZip()) {
61       LOG(ERROR) << "Failed to unmount zip package after revoke";
62       return Status::APP_DIR_ERROR;
63     }
64   }
65   return Status::OK;
66 }
67
68 Step::Status StepMountUnpacked::precheck() {
69   if (context_->file_path.get().empty()) {
70     LOG(ERROR) << "file_path attribute is empty";
71     return Step::Status::INVALID_VALUE;
72   }
73   if (!fs::exists(context_->file_path.get())) {
74     LOG(ERROR) << "file_path ("
75                << context_->file_path.get()
76                << ") path does not exist";
77     return Step::Status::INVALID_VALUE;
78   }
79   if (context_->root_application_path.get().empty()) {
80     LOG(ERROR) << "root_application_path attribute is empty";
81     return Step::Status::INVALID_VALUE;
82   }
83   if (!fs::exists(context_->root_application_path.get())) {
84     LOG(ERROR) << "root_application_path ("
85                << context_->root_application_path.get()
86                << ") path does not exist";
87     return Step::Status::INVALID_VALUE;
88   }
89   return Status::OK;
90 }
91
92 std::unique_ptr<IZipInterface> StepMountUnpacked::CreateZipInterface(
93     const fs::path& mount_path) {
94   std::unique_ptr<IZipInterface> zip_interface(
95       new TzipInterface(mount_path));
96   return zip_interface;
97 }
98
99 }  // namespace mount
100 }  // namespace common_installer