Add OOM exception handler
[platform/core/appfw/wgt-backend.git] / src / wgt / step / filesystem / step_wgt_patch_storage_directories.cc
1 // Copyright (c) 2015 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 "wgt/step/filesystem/step_wgt_patch_storage_directories.h"
6
7 #include <boost/filesystem/operations.hpp>
8 #include <boost/filesystem/path.hpp>
9 #include <boost/system/error_code.hpp>
10
11 #include <common/utils/file_util.h>
12
13 namespace bf = boost::filesystem;
14 namespace bs = boost::system;
15 namespace ci = common_installer;
16
17 namespace {
18
19 const char kSharedLocation[] = "shared";
20 const char kSharedResLocation[] = "shared/res";
21 const char kResWgtSubPath[] = "res/wgt";
22 const char kTemporaryData[] = "tmp";
23
24 }  // namespace
25
26 namespace wgt {
27 namespace filesystem {
28
29 common_installer::Step::Status StepWgtPatchStorageDirectories::process() {
30   if (!CreatePrivateTmpDir())
31     return Status::APP_DIR_ERROR;
32
33   int version = std::stoi(context_->manifest_data.get()->api_version);
34   if (version >= 3) {
35     LOG(DEBUG) <<
36         "Moving and linking widget's shared/res directory content in "
37         << context_->pkg_path.get();
38     if (!ShareDirFor3x())
39       return Status::APP_DIR_ERROR;
40   }
41
42   return Status::OK;
43 }
44
45 bool StepWgtPatchStorageDirectories::ShareDirFor3x() {
46   // check if ${pkg_path}/res/wgt/shared/res exists
47   bf::path wgt_shared_res_dir =
48       context_->pkg_path.get() / kResWgtSubPath / kSharedResLocation;
49   if (!bf::exists(wgt_shared_res_dir))
50     return true;
51   // create ${pkg_path}/shared/res
52   bf::path shared_dir = context_->pkg_path.get() / kSharedLocation;
53   if (!bf::exists(shared_dir)) {
54     bs::error_code error;
55     bf::create_directory(shared_dir, error);
56     if (error) {
57       LOG(ERROR) << "Failed to create directory: " << shared_dir;
58       return false;
59     }
60   }
61   bf::path shared_res_dir = context_->pkg_path.get() / kSharedResLocation;
62   if (!bf::exists(shared_res_dir)) {
63     bs::error_code error;
64     bf::create_directory(shared_res_dir, error);
65     if (error) {
66       LOG(ERROR) << "Failed to create directory: " << shared_res_dir;
67       return false;
68     }
69   }
70
71   // move and link all contents of ${pkg_path}res/wgt/shared/res
72   // to ${pkg_path}shared/res
73   bf::directory_iterator end_itr;
74   for (bf::directory_iterator itr(wgt_shared_res_dir); itr != end_itr; ++itr) {
75     bf::path current = itr->path();
76     if (bf::is_symlink(current)) {
77       continue;
78     }
79     bf::path dest = shared_res_dir / current.filename();
80     if (bf::is_directory(current)) {
81       if (!ci::MoveDir(current, dest, ci::FS_MERGE_OVERWRITE))
82         return false;
83     } else {
84       if (!ci::MoveFile(current, dest, true))
85         return false;
86     }
87     bs::error_code error;
88     bf::create_symlink(dest, current, error);
89     if (error) {
90       LOG(ERROR) << "linking failed for " << dest
91                  << ": " << boost::system::system_error(error).what();
92       return false;
93     }
94   }
95   return true;
96 }
97
98 bool StepWgtPatchStorageDirectories::CreatePrivateTmpDir() {
99   bf::path tmp_path = context_->pkg_path.get() / kTemporaryData;
100   if (bf::exists(tmp_path) && bf::is_directory(tmp_path)) {
101     return true;
102   }
103   bs::error_code error;
104   bf::create_directory(tmp_path, error);
105   if (error) {
106     LOG(ERROR) << "Failed to create private temporary directory for package";
107     return false;
108   }
109   return true;
110 }
111
112 }  // namespace filesystem
113 }  // namespace wgt