Replace pkg_path with GetPkgPath() method in InstallerContext
[platform/core/appfw/wgt-backend.git] / src / wgt / step / filesystem / step_create_symbolic_link.cc
1 /* 2014, Copyright © Eurogiciel Coporation, 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 "wgt/step/filesystem/step_create_symbolic_link.h"
7
8 #include <pkgmgr-info.h>
9 #include <unistd.h>
10
11 #include <boost/filesystem.hpp>
12 #include <boost/system/error_code.hpp>
13 #include <common/utils/file_util.h>
14 #include <common/utils/glist_range.h>
15 #include <cassert>
16 #include <cstring>
17 #include <cstdio>
18 #include <string>
19
20 namespace bf = boost::filesystem;
21 namespace bs = boost::system;
22
23 namespace {
24
25 const char kWrtServiceBinaryPath[] = "/usr/bin/wrt-service";
26 const char kWebWidgetRuntimeBinaryPath[] = "/usr/bin/web-widget-runtime";
27 const char kWRTPath[] = "/usr/bin/wrt";
28
29 }  // namespace
30
31 namespace wgt {
32 namespace filesystem {
33
34 bool StepCreateSymbolicLink::CreateSymlinksForApps() {
35   boost::system::error_code error;
36   for (application_x* app :
37        GListRange<application_x*>(context_->manifest_data.get()->application)) {
38     // filter out non-wgt apps as this step is run for hybrid backend too
39     if (strcmp("webapp", app->type) != 0)
40       continue;
41     // binary is a symbolic link named <appid> and is located in <pkgid>/<appid>
42     bf::path exec_path = context_->GetPkgPath() / bf::path("bin");
43     common_installer::CreateDir(exec_path);
44
45     exec_path /= bf::path(app->appid);
46     common_installer::RemoveAll(exec_path);
47
48     if (strcmp(app->component_type, "uiapp") == 0) {
49       bf::create_symlink(bf::path(kWRTPath), exec_path, error);
50     } else if (strcmp(app->component_type, "watchapp") == 0) {
51       bf::create_symlink(bf::path(kWRTPath), exec_path, error);
52     } else if (strcmp(app->component_type, "widgetapp") == 0) {
53       bf::create_symlink(kWebWidgetRuntimeBinaryPath, exec_path, error);
54     } else {
55       bf::create_symlink(kWrtServiceBinaryPath, exec_path, error);
56     }
57     if (error) {
58       LOG(ERROR) << "Failed to set symbolic link "
59         << boost::system::system_error(error).what();
60       return false;
61     }
62   }
63   return true;
64 }
65
66 common_installer::Step::Status StepCreateSymbolicLink::process() {
67   assert(context_->manifest_data.get());
68
69   if (!CreateSymlinksForApps())
70     return Status::APP_DIR_ERROR;
71
72   LOG(DEBUG) << "Symlinks created successfully";
73   return Status::OK;
74 }
75
76 common_installer::Step::Status StepCreateSymbolicLink::undo() {
77   for (application_x* app :
78        GListRange<application_x*>(context_->manifest_data.get()->application)) {
79     bf::path exec_path = context_->GetPkgPath() / "bin" / app->appid;
80     common_installer::RemoveAll(exec_path);
81   }
82   return Status::OK;
83 }
84
85 }  // namespace filesystem
86 }  // namespace wgt