Merge "add to create share directory"
[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 #define WEBAPP_DEFAULT_UID  5000
34 #define WEBAPP_DEFAULT_GID  5000
35
36 namespace {
37 const mode_t PRIVATE_STORAGE_MODE = 0700;
38 const mode_t SHARE_MODE = 0705;
39 }
40
41 using namespace WrtDB;
42
43 namespace Jobs {
44 namespace WidgetInstall {
45 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
46     DPL::TaskDecl<TaskFileManipulation>(this),
47     m_context(context)
48 {
49     AddStep(&TaskFileManipulation::StepCreateDirs);
50     AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
51     AddStep(&TaskFileManipulation::StepCreateShareDir);
52     AddStep(&TaskFileManipulation::StepRenamePath);
53
54     AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
55 }
56
57 void TaskFileManipulation::StepCreateDirs()
58 {
59     std::string widgetPath;
60     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
61     if (pkgname.IsNull()) {
62         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
63     }
64
65     widgetPath = m_context.locations->getPackageInstallationDir();
66
67     std::string widgetBinPath = m_context.locations->getBinaryDir();
68     std::string widgetSrcPath = m_context.locations->getSourceDir();
69
70     WrtUtilMakeDir(widgetPath);
71
72     // If package type is widget with osp service, we don't need to make bin
73     // and src directory
74     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
75         LogDebug("Doesn't need to create resource directory");
76     } else {
77         LogDebug("Create resource directory");
78         WrtUtilMakeDir(widgetBinPath);
79         WrtUtilMakeDir(widgetSrcPath);
80     }
81
82     m_context.job->UpdateProgress(
83         InstallerContext::INSTALL_DIR_CREATE,
84         "Widget Directory Created");
85 }
86
87 void TaskFileManipulation::StepCreatePrivateStorageDir()
88 {
89     std::string storagePath = m_context.locations->getPrivateStorageDir();
90
91     if (euidaccess(storagePath.c_str(), F_OK) != 0) {
92         if(!WrtUtilMakeDir(storagePath, PRIVATE_STORAGE_MODE)){
93             LogError("Failed to create directory for private storage");
94             ThrowMsg(Exceptions::InternalError,
95                     "Failed to create directory for private storage");
96         }
97         // '5000' is default uid, gid for applications.
98         // So installed applications should be launched as process of uid '5000'.
99         // the process can access private directory 'data' of itself.
100         if(chown(storagePath.c_str(),
101                  WEBAPP_DEFAULT_UID,
102                  WEBAPP_DEFAULT_GID) != 0)
103         {
104             ThrowMsg(Exceptions::InternalError,
105                  "Chown to invaild user");
106         }
107     } else if (euidaccess(storagePath.c_str(), W_OK | R_OK | X_OK) == 0) {
108         LogInfo("Private storage already exists.");
109         // Even if private directory already is created, private dircetory
110         // should change owner.
111         if(chown(storagePath.c_str(),
112                  WEBAPP_DEFAULT_UID,
113                  WEBAPP_DEFAULT_GID) != 0)
114         {
115             ThrowMsg(Exceptions::InternalError,
116                  "Chown to invaild user");
117         }
118         if(chmod(storagePath.c_str(), PRIVATE_STORAGE_MODE) != 0) {
119             ThrowMsg(Exceptions::InternalError,
120                  "chmod to 0700");
121         }
122
123     } else {
124         ThrowMsg(Exceptions::InternalError,
125                  "No access to private storage.");
126     }
127 }
128
129 void TaskFileManipulation::StepCreateShareDir()
130 {
131     std::string sharePath = m_context.locations->getShareDir();
132
133     if (euidaccess(sharePath.c_str(), F_OK) != 0) {
134         if(!WrtUtilMakeDir(sharePath, SHARE_MODE)){
135             LogError("Failed to create directory for share");
136             ThrowMsg(Exceptions::InternalError,
137                     "Failed to create directory for share");
138         }
139         // '5000' is default uid, gid for applications.
140         // So installed applications should be launched as process of uid '5000'.
141         // the process can access private directory 'data' of itself.
142         if(chown(sharePath.c_str(),
143                  WEBAPP_DEFAULT_UID,
144                  WEBAPP_DEFAULT_GID) != 0)
145         {
146             ThrowMsg(Exceptions::InternalError,
147                  "Chown to invaild user");
148         }
149     } else if (euidaccess(sharePath.c_str(), W_OK | R_OK | X_OK) == 0) {
150         LogInfo("Share directory already exists.");
151         // Even if share directory already is created, share dircetory
152         // should change owner.
153         if(chown(sharePath.c_str(),
154                  WEBAPP_DEFAULT_UID,
155                  WEBAPP_DEFAULT_GID) != 0)
156         {
157             ThrowMsg(Exceptions::InternalError,
158                  "Chown to invaild user");
159         }
160         if(chmod(sharePath.c_str(), SHARE_MODE) != 0) {
161             ThrowMsg(Exceptions::InternalError,
162                  "chmod to 0700");
163         }
164
165     } else {
166         ThrowMsg(Exceptions::InternalError,
167                  "No access to private storage.");
168     }
169
170 }
171
172 void TaskFileManipulation::StepRenamePath()
173 {
174     std::string instDir;
175     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
176     if (pkgname.IsNull()) {
177         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
178     }
179
180     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
181         instDir = m_context.locations->getPackageInstallationDir();
182     } else {
183         instDir = m_context.locations->getSourceDir();
184     }
185
186     LogDebug("Copy file from temp directory to " << instDir);
187     if (!WrtUtilRemove(instDir)) {
188         ThrowMsg(Exceptions::RemovingFolderFailure,
189                 "Error occurs during removing existing folder");
190     }
191
192     if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(), instDir.c_str()) == 0)) {
193         ThrowMsg(Exceptions::UnknownError,
194                 "Error occurs during renaming widget folder");
195     }
196     m_context.job->UpdateProgress(
197         InstallerContext::INSTALL_RENAME_PATH,
198         "Widget Rename path Finished");
199 }
200
201 void TaskFileManipulation::StepAbortRenamePath()
202 {
203     LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
204     std::string widgetPath;
205     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
206         widgetPath = m_context.locations->getPackageInstallationDir();
207     } else {
208         widgetPath = m_context.locations->getSourceDir();
209     }
210     struct stat fileInfo;
211     if (stat(widgetPath.c_str(), &fileInfo) != 0) {
212         LogError("Failed to get widget file path : " << widgetPath);
213         return;
214     }
215
216     if (!(rename(widgetPath.c_str(), m_context.locations->getTemporaryPackageDir().c_str()) == 0)) {
217         LogError("Failed to rename");
218     }
219     LogDebug("Rename widget path sucessful!");
220 }
221 } //namespace WidgetInstall
222 } //namespace Jobs