2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_ecnrypt_resource.cpp
18 * @author Soyoung Kim (sy037.kim@samsung.com)
20 * @brief Implementation file for installer task encrypt resource
22 #include "task_encrypt_resource.h"
29 #include <dpl/log/log.h>
30 #include <dpl/errno_string.h>
31 #include <dpl/foreach.h>
32 #include <dpl/wrt-dao-ro/global_config.h>
34 #include <widget_install/job_widget_install.h>
35 #include <widget_install/widget_install_context.h>
36 #include <widget_install/widget_install_errors.h>
38 using namespace WrtDB;
39 using namespace WRTEncryptor;
42 std::set<std::string>& getSupportedForEncryption()
44 static std::set<std::string> encryptSet;
45 if (encryptSet.empty()) {
46 encryptSet.insert(".html");
47 encryptSet.insert(".css");
48 encryptSet.insert(".js");
53 bool isSupportedForEncryption(std::string file)
55 size_t foundKey = file.rfind(".");
56 if (std::string::npos != foundKey) {
57 std::string mimeType = file.substr(foundKey);
58 return getSupportedForEncryption().count(mimeType) > 0;
65 namespace WidgetInstall {
66 TaskEncryptResource::TaskEncryptResource(InstallerContext& context) :
67 DPL::TaskDecl<TaskEncryptResource>(this),
70 AddStep(&TaskEncryptResource::StepEncryptResource);
73 void TaskEncryptResource::StepEncryptResource()
75 LogDebug("Step Encrypt resource");
76 m_resEnc = new ResourceEncryptor;
77 m_resEnc->CreateEncryptionKey(DPL::ToUTF8String(*m_context.
78 widgetConfig.pkgname));
80 EncryptDirectory(m_context.locations->getTemporaryRootDir());
83 void TaskEncryptResource::EncryptDirectory(std::string path)
85 DIR* pkgDir = opendir(path.c_str());
87 LogDebug("Package directory doesn't exist");
88 ThrowMsg(Exceptions::InternalError, "Error occurs during read \
92 struct dirent* dirent;
95 if ((dirent = readdir(pkgDir))) {
96 std::string dirName = dirent->d_name;
97 std::string absFileName = path + "/" + dirName;
98 if (stat(absFileName.c_str(), &statInfo) != 0) {
99 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
102 if (S_ISDIR(statInfo.st_mode)) {
103 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name,
107 EncryptDirectory(absFileName);
109 if (isSupportedForEncryption(absFileName)) {
110 EncryptFile(absFileName);
116 if (closedir(pkgDir)) {
117 LogError("Fail to close directory : " << path);
121 void TaskEncryptResource::EncryptFile(const std::string &fileName)
125 LogDebug("Need to ecnrypt file Name " << fileName);
126 std::string encFile = fileName + ".enc";
129 int ret = stat(fileName.c_str(), &buf);
131 size_t fileSize = buf.st_size;
133 FILE* resFp = fopen(fileName.c_str(), "r");
134 if ( NULL == resFp) {
135 LogDebug("Couldnot open file : " << fileName);
139 int blockSize = m_resEnc->GetBlockSize(fileSize);
140 LogDebug("Get block size : " << blockSize);
142 unsigned char readBuf[fileSize];
143 unsigned char outEncBuf[blockSize];
144 memset(readBuf, 0, fileSize);
145 memset(outEncBuf, 0, blockSize);
147 fread(readBuf, sizeof(unsigned char), fileSize, resFp);
149 m_resEnc->EncryptChunk(readBuf, outEncBuf, fileSize);
151 FILE* encFp = fopen(encFile.c_str(), "w");
153 LogError("Failed to open ecryption file");
156 fwrite(outEncBuf, sizeof(unsigned char), blockSize, encFp);
161 LogDebug("Success to encrypt file");
162 LogDebug("Remove unecrypted file : " << fileName);
164 unlink(fileName.c_str());
165 if ((rename(encFile.c_str(), fileName.c_str())) != 0) {
166 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
169 std::string realPath = fileName;
170 realPath.replace(0, m_context.locations->getTemporaryRootDir().length(),
171 m_context.locations->getSourceDir());
173 WrtDB::EncryptedFileInfo info;
174 info.fileName = DPL::FromUTF8String(realPath);
175 info.fileSize = fileSize;
177 m_context.widgetConfig.encryptedFiles.insert(info);
180 Catch(ResourceEncryptor::Exception::Base)
182 ReThrowMsg(Exceptions::ExtractFileFailed, fileName);
185 } //namespace WidgetInstall