[Release] wrt-installer_0.0.73
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_private_storage.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    installer_task_private_storage.cpp
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task private storage.
21  */
22 #include "task_private_storage.h"
23
24 #include <pwd.h>
25 #include <grp.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string>
31
32 #include <dpl/log/log.h>
33 #include <dpl/errno_string.h>
34
35 #include <dpl/wrt-dao-ro/widget_config.h>
36 #include <dpl/utils/wrt_utility.h>
37 #include <widget_install/job_widget_install.h>
38 #include <widget_install/widget_install_context.h>
39 #include <widget_install/widget_install_errors.h>
40
41 #define WEBAPP_DEFAULT_UID  5000
42 #define WEBAPP_DEFAULT_GID  5000
43
44 namespace {
45 const mode_t PRIVATE_STORAGE_MODE = 0700;
46 }
47
48 namespace Jobs {
49 namespace WidgetInstall {
50 TaskPrivateStorage::TaskPrivateStorage(InstallerContext& context) :
51     DPL::TaskDecl<TaskPrivateStorage>(this),
52     m_context(context)
53 {
54     AddStep(&TaskPrivateStorage::StepCreateDirectory);
55 }
56
57 void TaskPrivateStorage::StepCreateDirectory()
58 {
59     using namespace WrtDB;
60
61     LogInfo("Step: Creating private storage directory.");
62
63     m_context.installStep =
64         InstallerContext::INSTALL_CREATE_PRIVATE_STORAGE;
65
66     std::ostringstream widgetPath;
67     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
68     if(!pkgname.IsNull()) {
69         widgetPath << m_context.locations->getPackageInstallationDir() << "/";
70     } else {
71         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
72     }
73
74     if (euidaccess(widgetPath.str().c_str(), W_OK | X_OK) != 0) {
75         ThrowMsg(Exceptions::InternalError, DPL::GetErrnoString());
76     }
77
78     std::ostringstream storagePath;
79     storagePath << widgetPath.str().c_str()
80                 << GlobalConfig::GetWidgetPrivateStoragePath();
81
82     if (euidaccess(storagePath.str().c_str(), F_OK) != 0) {
83         if(!WrtUtilMakeDir(storagePath.str(), PRIVATE_STORAGE_MODE)){
84             LogError("Failed to create directory for private storage");
85             ThrowMsg(Exceptions::InternalError,
86                     "Failed to create directory for private storage");
87         }
88         // '5000' is default uid, gid for applications.
89         // So installed applications should be launched as process of uid '5000'.
90         // the process can access private directory 'data' of itself.
91         if(chown(storagePath.str().c_str(),
92                  WEBAPP_DEFAULT_UID,
93                  WEBAPP_DEFAULT_GID) != 0)
94         {
95             ThrowMsg(Exceptions::InternalError,
96                  "Chown to invaild user");
97         }
98     } else if (euidaccess(storagePath.str().c_str(), W_OK | R_OK | X_OK) == 0) {
99         LogInfo("Private storage already exists.");
100         // Even if private directory already is created, private dircetory
101         // should change owner.
102         if(chown(storagePath.str().c_str(),
103                  WEBAPP_DEFAULT_UID,
104                  WEBAPP_DEFAULT_GID) != 0)
105         {
106             ThrowMsg(Exceptions::InternalError,
107                  "Chown to invaild user");
108         }
109         if(chmod(storagePath.str().c_str(), PRIVATE_STORAGE_MODE) != 0) {
110             ThrowMsg(Exceptions::InternalError,
111                  "chmod to 0700");
112         }
113
114     } else {
115         ThrowMsg(Exceptions::InternalError,
116                  "No access to private storage.");
117     }
118
119     m_context.job->UpdateProgress(
120         InstallerContext::INSTALL_CREATE_PRIVATE_STORAGE,
121         "Private storage created."
122         );
123 }
124 } //namespace WidgetInstall
125 } //namespace Jobs