Fixed installation failure during install preload webapp
[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 <unistd.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <widget_install/task_file_manipulation.h>
26 #include <widget_install/job_widget_install.h>
27 #include <widget_install/widget_install_errors.h>
28 #include <widget_install/widget_install_context.h>
29 #include <dpl/utils/wrt_utility.h>
30 #include <dpl/foreach.h>
31 #include <dpl/log/log.h>
32 #include <dpl/assert.h>
33 #include <dpl/errno_string.h>
34 #include <dpl/utils/folder_size.h>
35 #include <dpl/wrt-dao-ro/global_config.h>
36 #include <string>
37 #include <fstream>
38 #include <widget_install_to_external.h>
39
40 #define WEBAPP_DEFAULT_UID  5000
41 #define WEBAPP_DEFAULT_GID  5000
42
43 namespace {
44 const mode_t PRIVATE_STORAGE_MODE = 0700;
45 const mode_t SHARE_MODE = 0705;
46 }
47
48 using namespace WrtDB;
49
50 namespace {
51 const char* GLIST_RES_DIR = "res";
52
53 bool _FolderCopy(std::string source, std::string dest)
54 {
55     DIR* dir = opendir(source.c_str());
56     if (NULL == dir) {
57         return false;
58     }
59
60     struct dirent dEntry;
61     struct dirent *dEntryResult;
62     int return_code;
63
64     do {
65         struct stat statInfo;
66         return_code = readdir_r(dir, &dEntry, &dEntryResult);
67         if (dEntryResult != NULL && return_code == 0) {
68             std::string fileName = dEntry.d_name;
69             std::string fullName = source + "/" + fileName;
70
71             if (stat(fullName.c_str(), &statInfo) != 0) {
72                 closedir(dir);
73                 return false;
74             }
75
76             if (S_ISDIR(statInfo.st_mode)) {
77                 if (("." == fileName) || (".." == fileName)) {
78                     continue;
79                 }
80                 std::string destFolder = dest + "/" + fileName;
81                 WrtUtilMakeDir(destFolder);
82
83                 if (!_FolderCopy(fullName, destFolder)) {
84                     closedir(dir);
85                     return false;
86                 }
87             }
88
89             std::string destFile = dest + "/" + fileName;
90             std::ifstream infile(fullName);
91             std::ofstream outfile(destFile);
92             outfile << infile.rdbuf();
93             outfile.close();
94             infile.close();
95         }
96     } while (dEntryResult != NULL && return_code == 0);
97     closedir(dir);
98     return true;
99 }
100
101 void changeOwnerForDirectory(std::string storagePath) {
102     if (euidaccess(storagePath.c_str(), F_OK) != 0) {
103         if (!WrtUtilMakeDir(storagePath, PRIVATE_STORAGE_MODE)) {
104             LogError("Failed to create directory for private storage");
105             ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
106                      "Failed to create directory for private storage");
107         }
108         // '5000' is default uid, gid for applications.
109         // So installed applications should be launched as process of uid
110         // '5000'.
111         // the process can access private directory 'data' of itself.
112         if (chown(storagePath.c_str(),
113                   WEBAPP_DEFAULT_UID,
114                   WEBAPP_DEFAULT_GID) != 0)
115         {
116             ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
117                      "Chown to invaild user");
118         }
119     } else if (euidaccess(storagePath.c_str(), W_OK | R_OK | X_OK) == 0) {
120         LogInfo("Private storage already exists.");
121         // Even if private directory already is created, private dircetory
122         // should change owner.
123         if (chown(storagePath.c_str(),
124                   WEBAPP_DEFAULT_UID,
125                   WEBAPP_DEFAULT_GID) != 0)
126         {
127             ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
128                      "Chown to invaild user");
129         }
130         if (chmod(storagePath.c_str(), PRIVATE_STORAGE_MODE) != 0) {
131             ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
132                      "chmod to 0700");
133         }
134     } else {
135         ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
136                  "No access to private storage.");
137     }
138 }
139 }
140
141 namespace Jobs {
142 namespace WidgetInstall {
143 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
144     DPL::TaskDecl<TaskFileManipulation>(this),
145     m_context(context),
146     m_extHandle(NULL)
147 {
148     if (INSTALL_LOCATION_TYPE_EXTERNAL !=
149             m_context.locationType)
150     {
151         AddStep(&TaskFileManipulation::StepCreateDirs);
152         AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
153         if (m_context.widgetConfig.packagingType !=
154             WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
155         {
156             AddStep(&TaskFileManipulation::StepRenamePath);
157             AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
158         }
159         AddStep(&TaskFileManipulation::StepLinkForPreload);
160
161     } else {
162         AddStep(&TaskFileManipulation::StepPrepareExternalDir);
163         AddStep(&TaskFileManipulation::StepInstallToExternal);
164         AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
165
166         AddAbortStep(&TaskFileManipulation::StepAbortCreateExternalDir);
167     }
168 }
169
170 void TaskFileManipulation::StepCreateDirs()
171 {
172     std::string widgetPath;
173
174     widgetPath = m_context.locations->getPackageInstallationDir();
175
176     std::string widgetBinPath = m_context.locations->getBinaryDir();
177     std::string widgetSrcPath = m_context.locations->getSourceDir();
178
179     WrtUtilMakeDir(widgetPath);
180
181     // If package type is widget with osp service, we don't need to make bin
182     // and src directory
183     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
184         LogDebug("Doesn't need to create resource directory");
185     } else {
186         LogDebug("Create resource directory");
187         WrtUtilMakeDir(widgetBinPath);
188         WrtUtilMakeDir(widgetSrcPath);
189         if (m_context.job->getInstallerStruct().m_installMode
190                 == InstallMode::INSTALL_MODE_PRELOAD)
191         {
192             std::string userWidgetDir = m_context.locations->getUserDataRootDir();
193             WrtUtilMakeDir(userWidgetDir);
194         }
195     }
196
197     m_context.job->UpdateProgress(
198         InstallerContext::INSTALL_DIR_CREATE,
199         "Widget Directory Created");
200 }
201
202 void TaskFileManipulation::StepCreatePrivateStorageDir()
203 {
204     std::string storagePath = m_context.locations->getPrivateStorageDir();
205     LogDebug("Create private storage directory : " <<
206             m_context.locations->getPrivateStorageDir());
207     changeOwnerForDirectory(storagePath);
208 }
209
210 void TaskFileManipulation::StepRenamePath()
211 {
212     std::string instDir;
213
214     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
215         instDir = m_context.locations->getPackageInstallationDir();
216     } else {
217         instDir = m_context.locations->getSourceDir();
218     }
219
220     LogDebug("Copy file from temp directory to " << instDir);
221     if (!WrtUtilRemove(instDir)) {
222         ThrowMsg(Exceptions::RemovingFolderFailure,
223                  "Error occurs during removing existing folder");
224     }
225
226     if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(),
227                  instDir.c_str()) == 0))
228     {
229         ThrowMsg(Exceptions::FileOperationFailed,
230                  "Error occurs during renaming widget folder");
231     }
232     m_context.job->UpdateProgress(
233         InstallerContext::INSTALL_RENAME_PATH,
234         "Widget Rename path Finished");
235 }
236
237 void TaskFileManipulation::StepLinkForPreload()
238 {
239     if (m_context.job->getInstallerStruct().m_installMode
240             == InstallMode::INSTALL_MODE_PRELOAD)
241     {
242         std::string srcDir = m_context.locations->getUserDataRootDir() +
243             WrtDB::GlobalConfig::GetWidgetSrcPath();
244
245         if (0 != access(srcDir.c_str(), F_OK)) {
246             LogDebug("Make symbolic name for preaload app" <<
247                     m_context.locations->getSourceDir() << " to " << srcDir);
248             std::string resDir = m_context.locations->getUserDataRootDir() +
249                 "/res";
250
251             WrtUtilMakeDir(resDir);
252             if (symlink(m_context.locations->getSourceDir().c_str(), srcDir.c_str()) != 0)
253             {
254                 int error = errno;
255                 if (error)
256                     LogPedantic("Failed to make a symbolic name for a file "
257                             << "[" <<  DPL::GetErrnoString(error) << "]");
258                 ThrowMsg(Exceptions::FileOperationFailed,
259                         "Symbolic link creating is not done.");
260             }
261         }
262
263         /* link for data directory */
264         std::string storagePath = m_context.locations->getPrivateStorageDir();
265         std::string dataDir = m_context.locations->getPackageInstallationDir() +
266             "/" + WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
267         if (0 != access(dataDir.c_str(), F_OK)) {
268             LogDebug("Make symbolic name for preaload app " <<
269                     storagePath << " to " << dataDir);
270
271             if (symlink(storagePath.c_str(), dataDir.c_str()) != 0)
272             {
273                 int error = errno;
274                 if (error)
275                     LogPedantic("Failed to make a symbolic name for a file "
276                             << "[" <<  DPL::GetErrnoString(error) << "]");
277                 ThrowMsg(Exceptions::FileOperationFailed,
278                         "Symbolic link creating is not done.");
279             }
280             changeOwnerForDirectory(dataDir);
281         }
282
283         if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
284             std::string widgetBinPath = m_context.locations->getBinaryDir();
285             std::string userBinPath = m_context.locations->getUserBinaryDir();
286             LogDebug("Make symbolic link for preload app " << widgetBinPath <<
287                     " to " << userBinPath);
288             if (symlink(widgetBinPath.c_str(), userBinPath.c_str()) != 0)
289             {
290                 int error = errno;
291                 if (error)
292                     LogPedantic("Failed to make a symbolic name for a file "
293                             << "[" <<  DPL::GetErrnoString(error) << "]");
294                 ThrowMsg(Exceptions::FileOperationFailed,
295                         "Symbolic link creating is not done.");
296             }
297
298         }
299     }
300 }
301
302 void TaskFileManipulation::StepAbortRenamePath()
303 {
304     LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
305     std::string widgetPath;
306     if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
307         widgetPath = m_context.locations->getPackageInstallationDir();
308         if (!WrtUtilRemove(widgetPath)) {
309             ThrowMsg(Exceptions::RemovingFolderFailure,
310                      "Error occurs during removing existing folder");
311         }
312     }
313     LogDebug("Rename widget path sucessful!");
314 }
315
316 void TaskFileManipulation::StepPrepareExternalDir()
317 {
318     LogDebug("Step prepare to install in exernal directory");
319     Try {
320         std::string pkgid =
321             DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
322
323         WidgetInstallToExtSingleton::Instance().initialize(pkgid);
324
325         size_t totalSize =
326             Utils::getFolderSize(m_context.locations->getTemporaryPackageDir());
327
328         int folderSize = (int)(totalSize / (1024 * 1024)) + 1;
329
330         GList *list = NULL;
331         app2ext_dir_details* dirDetail = NULL;
332
333         dirDetail = (app2ext_dir_details*) calloc(1,
334                 sizeof(
335                     app2ext_dir_details));
336         if (NULL == dirDetail) {
337             ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
338                     "error in app2ext");
339         }
340         dirDetail->name = strdup(GLIST_RES_DIR);
341         dirDetail->type = APP2EXT_DIR_RO;
342         list = g_list_append(list, dirDetail);
343
344         if (m_context.isUpdateMode) {
345             WidgetInstallToExtSingleton::Instance().preUpgrade(list,
346                                                                folderSize);
347         } else {
348             WidgetInstallToExtSingleton::Instance().preInstallation(list,
349                                                                     folderSize);
350         }
351         free(dirDetail);
352         g_list_free(list);
353
354         /* make bin directory */
355         std::string widgetBinPath = m_context.locations->getBinaryDir();
356         WrtUtilMakeDir(widgetBinPath);
357     }
358     Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
359     {
360         ReThrowMsg(Exceptions::ErrorExternalInstallingFailure,
361                    "Error during \
362                 create external folder ");
363     }
364 }
365
366 void TaskFileManipulation::StepInstallToExternal()
367 {
368     LogDebug("StepInstallExternal");
369     if (!WrtUtilMakeDir(m_context.locations->getSourceDir())) {
370         ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
371                  "To make src \
372                 directory failed");
373     }
374
375     LogDebug("Resource move to external storage " <<
376              m_context.locations->getSourceDir());
377     if (!_FolderCopy(m_context.locations->getTemporaryPackageDir(),
378                      m_context.locations->getSourceDir()))
379     {
380         ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
381                  "Error occurs during renaming widget folder");
382     }
383 }
384
385 void TaskFileManipulation::StepAbortCreateExternalDir()
386 {
387     LogError("Abort StepAbortCreateExternalDir");
388     if (m_context.isUpdateMode) {
389         WidgetInstallToExtSingleton::Instance().postUpgrade(false);
390     } else {
391         WidgetInstallToExtSingleton::Instance().postInstallation(false);
392     }
393     WidgetInstallToExtSingleton::Instance().deinitialize();
394 }
395 } //namespace WidgetInstall
396 } //namespace Jobs