Merge "Fixed certificate of authentication value to base64."
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_file_manipulation.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    task_db_update.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task database updating
21  */
22 #include <sys/stat.h>
23 #include <widget_install/task_file_manipulation.h>
24 #include <widget_install/job_widget_install.h>
25 #include <widget_install/widget_install_errors.h>
26 #include <widget_install/widget_install_context.h>
27 #include <dpl/utils/wrt_utility.h>
28 #include <dpl/foreach.h>
29 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
31 #include <string>
32
33 using namespace WrtDB;
34
35 namespace Jobs {
36 namespace WidgetInstall {
37 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
38     DPL::TaskDecl<TaskFileManipulation>(this),
39     m_context(context)
40 {
41     AddStep(&TaskFileManipulation::StepCreateDirs);
42     AddStep(&TaskFileManipulation::StepRenamePath);
43
44     AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
45 }
46
47 void TaskFileManipulation::StepCreateDirs()
48 {
49     std::string widgetPath;
50     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
51     if (pkgname.IsNull()) {
52         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
53     }
54
55     widgetPath = m_context.locations->getPackageInstallationDir();
56
57     std::string widgetBinPath = m_context.locations->getBinaryDir();
58     std::string widgetSrcPath = m_context.locations->getSourceDir();
59
60     WrtUtilMakeDir(widgetPath);
61
62     if (m_context.job->getInstallerStruct().m_preload) {
63         std::string privateDir = m_context.locations->getBasePrivateDir();
64         WrtUtilMakeDir(privateDir);
65     }
66
67     // If package type is widget with osp service, we don't need to make bin
68     // and src directory
69     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
70         LogDebug("Doesn't need to create resource directory");
71     } else {
72         LogDebug("Create resource directory");
73         WrtUtilMakeDir(widgetBinPath);
74         WrtUtilMakeDir(widgetSrcPath);
75     }
76
77     m_context.job->UpdateProgress(
78         InstallerContext::INSTALL_DIR_CREATE,
79         "Widget Directory Created");
80 }
81
82 void TaskFileManipulation::StepRenamePath()
83 {
84     std::string instDir;
85     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
86     if (pkgname.IsNull()) {
87         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
88     }
89
90     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
91         instDir = m_context.locations->getPackageInstallationDir();
92     } else {
93         instDir = m_context.locations->getSourceDir();
94     }
95
96     LogDebug("Copy file from temp directory to " << instDir);
97     if (!WrtUtilRemove(instDir)) {
98         ThrowMsg(Exceptions::RemovingFolderFailure,
99                 "Error occurs during removing existing folder");
100     }
101
102     if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(), instDir.c_str()) == 0)) {
103         ThrowMsg(Exceptions::UnknownError,
104                 "Error occurs during renaming widget folder");
105     }
106     m_context.job->UpdateProgress(
107         InstallerContext::INSTALL_RENAME_PATH,
108         "Widget Rename path Finished");
109 }
110
111 void TaskFileManipulation::StepAbortRenamePath()
112 {
113     LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
114     std::string widgetPath;
115     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
116         widgetPath = m_context.locations->getPackageInstallationDir();
117     } else {
118         widgetPath = m_context.locations->getSourceDir();
119     }
120     struct stat fileInfo;
121     if (stat(widgetPath.c_str(), &fileInfo) != 0) {
122         LogError("Failed to get widget file path : " << widgetPath);
123         return;
124     }
125
126     if (!(rename(widgetPath.c_str(), m_context.locations->getTemporaryPackageDir().c_str()) == 0)) {
127         LogError("Failed to rename");
128     }
129     LogDebug("Rename widget path sucessful!");
130 }
131 } //namespace WidgetInstall
132 } //namespace Jobs