Fix StepEncryptResources 50/218750/3
authorJunghyun Yeon <jungh.yeon@samsung.com>
Thu, 28 Nov 2019 00:18:02 +0000 (09:18 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 10 Dec 2019 04:57:15 +0000 (04:57 +0000)
- Fix typo.
- Use summarized namespace for readability.
- Remove some 1 line if statement braces.

Change-Id: Id42e04fca3516ce19c783d4cb6ef7af9a74d4891
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/hybrid/hybrid_installer.cc
src/hybrid/step/encryption/step_encrypt_hybrid_resources.cc [moved from src/hybrid/step/encryption/step_encrypt_resources.cc with 73% similarity]
src/hybrid/step/encryption/step_encrypt_hybrid_resources.h [moved from src/hybrid/step/encryption/step_encrypt_resources.h with 66% similarity]
src/wgt/step/encryption/step_encrypt_resources.cc

index 2f00998..4d18750 100644 (file)
@@ -92,7 +92,7 @@
 #include "hybrid/step/configuration/step_merge_tpk_privileges.h"
 #include "hybrid/step/configuration/step_set_mainapp.h"
 #include "hybrid/step/configuration/step_stash_tpk_config.h"
-#include "hybrid/step/encryption/step_encrypt_resources.h"
+#include "hybrid/step/encryption/step_encrypt_hybrid_resources.h"
 #include "hybrid/step/pkgmgr/step_merge_xml.h"
 #include "wgt/step/configuration/step_parse.h"
 #include "wgt/step/configuration/step_set_old_signature_files_location.h"
@@ -137,7 +137,7 @@ void HybridInstaller::InstallSteps() {
   AddStep<wgt::security::StepCheckWgtBackgroundCategory>();
   AddStep<wgt::security::StepCheckWgtNotificationCategory>();
   AddStep<wgt::security::StepCheckWgtImePrivilege>();
-  AddStep<hybrid::encryption::StepEncryptResources>();
+  AddStep<hybrid::encryption::StepEncryptHybridResources>();
   AddStep<ci::security::StepRollbackInstallationSecurity>();
   AddStep<ci::filesystem::StepRemoveGlobalAppSymlinks>();
   AddStep<ci::filesystem::StepAcquireExternalStorage>(false);
@@ -189,7 +189,7 @@ void HybridInstaller::UpdateSteps() {
   AddStep<wgt::security::StepCheckWgtBackgroundCategory>();
   AddStep<wgt::security::StepCheckWgtNotificationCategory>();
   AddStep<wgt::security::StepCheckWgtImePrivilege>();
-  AddStep<hybrid::encryption::StepEncryptResources>();
+  AddStep<hybrid::encryption::StepEncryptHybridResources>();
   AddStep<ci::configuration::StepParseManifest>(
       ci::configuration::StepParseManifest::ManifestLocation::INSTALLED,
       ci::configuration::StepParseManifest::StoreLocation::BACKUP);
@@ -287,7 +287,7 @@ void HybridInstaller::DeltaSteps() {
   AddStep<wgt::security::StepCheckWgtBackgroundCategory>();
   AddStep<wgt::security::StepCheckWgtNotificationCategory>();
   AddStep<wgt::security::StepCheckWgtImePrivilege>();
-  AddStep<hybrid::encryption::StepEncryptResources>();
+  AddStep<hybrid::encryption::StepEncryptHybridResources>();
   AddStep<ci::configuration::StepBlockCrossUpdate>();
   AddStep<ci::pkgmgr::StepKillApps>();
   AddStep<ci::backup::StepBackupManifest>();
@@ -365,7 +365,7 @@ void HybridInstaller::MountInstallSteps() {
   AddStep<wgt::security::StepCheckWgtBackgroundCategory>();
   AddStep<wgt::security::StepCheckWgtNotificationCategory>();
   AddStep<wgt::security::StepCheckWgtImePrivilege>();
-  AddStep<hybrid::encryption::StepEncryptResources>();
+  AddStep<hybrid::encryption::StepEncryptHybridResources>();
   AddStep<ci::security::StepRollbackInstallationSecurity>();
   AddStep<ci::filesystem::StepRemoveGlobalAppSymlinks>();
   AddStep<ci::mount::StepMountInstall>();
@@ -417,7 +417,7 @@ void HybridInstaller::MountUpdateSteps() {
   AddStep<wgt::security::StepCheckWgtBackgroundCategory>();
   AddStep<wgt::security::StepCheckWgtNotificationCategory>();
   AddStep<wgt::security::StepCheckWgtImePrivilege>();
-  AddStep<hybrid::encryption::StepEncryptResources>();
+  AddStep<hybrid::encryption::StepEncryptHybridResources>();
   AddStep<ci::configuration::StepParseManifest>(
       ci::configuration::StepParseManifest::ManifestLocation::INSTALLED,
       ci::configuration::StepParseManifest::StoreLocation::BACKUP);
@@ -640,7 +640,7 @@ void HybridInstaller::ReadonlyUpdateInstallSteps() {
   AddStep<wgt::security::StepCheckWgtBackgroundCategory>();
   AddStep<wgt::security::StepCheckWgtNotificationCategory>();
   AddStep<wgt::security::StepCheckWgtImePrivilege>();
-  AddStep<hybrid::encryption::StepEncryptResources>();
+  AddStep<hybrid::encryption::StepEncryptHybridResources>();
   AddStep<ci::configuration::StepSwitchReadonlyMode>();
   AddStep<ci::configuration::StepParseManifest>(
       ci::configuration::StepParseManifest::ManifestLocation::INSTALLED,
@@ -2,12 +2,12 @@
 // Use of this source code is governed by an apache-2.0 license that can be
 // found in the LICENSE file.
 
-#include "hybrid/step/encryption/step_encrypt_resources.h"
+#include "hybrid/step/encryption/step_encrypt_hybrid_resources.h"
 
 namespace hybrid {
 namespace encryption {
 
-void StepEncryptResources::SetEncryptionRoot() {
+void StepEncryptHybridResources::SetEncryptionRoot() {
   input_ = context_->unpacked_dir_path.get() / "res/wgt";
 }
 
@@ -2,8 +2,8 @@
 // Use of this source code is governed by an apache-2.0 license that can be
 // found in the LICENSE file.
 
-#ifndef HYBRID_STEP_ENCRYPTION_STEP_ENCRYPT_RESOURCES_H_
-#define HYBRID_STEP_ENCRYPTION_STEP_ENCRYPT_RESOURCES_H_
+#ifndef HYBRID_STEP_ENCRYPTION_STEP_ENCRYPT_HYBRID_RESOURCES_H_
+#define HYBRID_STEP_ENCRYPTION_STEP_ENCRYPT_HYBRID_RESOURCES_H_
 
 #include <manifest_parser/utils/logging.h>
 
@@ -17,17 +17,18 @@ namespace encryption {
  *
  * This is variant for hybrid package
  */
-class StepEncryptResources : public wgt::encryption::StepEncryptResources {
+class StepEncryptHybridResources :
+    public wgt::encryption::StepEncryptResources {
  public:
   using wgt::encryption::StepEncryptResources::StepEncryptResources;
 
  private:
   void SetEncryptionRoot() override;
 
-  STEP_NAME(EncryptResources)
+  STEP_NAME(EncryptHybridResources)
 };
 
 }  // namespace encryption
 }  // namespace hybrid
 
-#endif  // HYBRID_STEP_ENCRYPTION_STEP_ENCRYPT_RESOURCES_H_
+#endif  // HYBRID_STEP_ENCRYPTION_STEP_ENCRYPT_HYBRID_RESOURCES_H_
index dc4b6ec..dffaa89 100644 (file)
 #include <string>
 #include <memory>
 
+namespace bf = boost::filesystem;
+namespace bs = boost::system;
+namespace ci = common_installer;
+
 namespace {
 
 const std::size_t kEncryptionChunkMaxSize = 8_kB;  // bytes
 const std::set<std::string> encryptSet { ".html", ".htm", ".css", ".js"};
 
-FILE* OpenFile(const std::string& path, const std::string& mode) {
+FILE* OpenFile(const bf::path& path, const std::string& mode) {
   FILE* result = nullptr;
 
   do {
@@ -77,14 +81,11 @@ void WriteBytes(unsigned char* buffer, std::size_t count, FILE* stream) {
 namespace wgt {
 namespace encryption {
 
-namespace bf = boost::filesystem;
-namespace bs = boost::system;
-
-common_installer::Step::Status StepEncryptResources::precheck() {
+ci::Step::Status StepEncryptResources::precheck() {
   backend_data_ = static_cast<WgtBackendData*>(context_->backend_data.get());
   if (!backend_data_) {
     LOG(ERROR) << "no backend data";
-    return common_installer::Step::Status::ERROR;
+    return ci::Step::Status::ERROR;
   }
 
   SetEncryptionRoot();
@@ -98,22 +99,22 @@ common_installer::Step::Status StepEncryptResources::precheck() {
     return Step::Status::INVALID_VALUE;
   }
 
-  return common_installer::Step::Status::OK;
+  return ci::Step::Status::OK;
 }
 
-common_installer::Step::Status StepEncryptResources::process() {
+ci::Step::Status StepEncryptResources::process() {
   if (!backend_data_->settings.get().encryption_enabled()) {
     LOG(DEBUG) << "no encryption";
-    return common_installer::Step::Status::OK;
+    return ci::Step::Status::OK;
   }
   LOG(DEBUG) << "Encrypting";
 
   if (!Encrypt(input_)) {
     LOG(ERROR) << "Error during encryption";
-    return common_installer::Step::Status::ERROR;
+    return ci::Step::Status::ERROR;
   }
 
-  return common_installer::Step::Status::OK;
+  return ci::Step::Status::OK;
 }
 
 bool StepEncryptResources::Encrypt(const bf::path &src) {
@@ -124,21 +125,25 @@ bool StepEncryptResources::Encrypt(const bf::path &src) {
   for (bf::directory_iterator file(src);
        file != bf::directory_iterator();
        ++file) {
-    bs::error_code error_code;
+    bs::error_code error;
     bf::path current(file->path());
 
-    bool is_dir = bf::is_directory(current, error_code);
-    if (error_code)
+    bool is_dir = bf::is_directory(current, error);
+    if (error) {
+      LOG(ERROR) << "Failed to check directory status: " << error.message();
       return false;
+    }
     if (is_dir) {
       if (!Encrypt(current))
         return false;
       continue;
     }
 
-    bool is_sym = bf::is_symlink(symlink_status(current, error_code));
-    if (error_code)
+    bool is_sym = bf::is_symlink(symlink_status(current, error));
+    if (error) {
+      LOG(ERROR) << "Failed to check symlink status: " << error.message();
       return false;
+    }
     if (is_sym)
       continue;
 
@@ -153,51 +158,49 @@ bool StepEncryptResources::Encrypt(const bf::path &src) {
 }
 
 bool StepEncryptResources::EncryptFile(const bf::path &src) {
-  bf::path encFile(src.string() + ".enc");
   struct stat info;
   memset(&info, 0, sizeof(info));
-  if (stat(src.string().c_str(), &info) != 0) {
-    LOG(ERROR) << "Could not access file " << src.string();
+  if (stat(src.c_str(), &info) != 0) {
+    LOG(ERROR) << "Could not access file " << src;
     return false;
   }
   const std::size_t fileSize = info.st_size;
-  if (0 == fileSize) {
-    LOG(ERROR) << src.string().c_str() << " size is 0, so encryption is skiped";
+  if (!fileSize) {
+    LOG(ERROR) << src << " size is 0, so encryption will be skipped";
     return true;
   }
 
-  FILE *input = OpenFile(src.string().c_str(), "rb");
-  if (input == nullptr) {
-    LOG(ERROR) << "Cannot open file for encryption: " << src.string();
+  FILE *input = OpenFile(src, "rb");
+  if (!input) {
+    LOG(ERROR) << "Cannot open file for encryption: " << src;
     return false;
   }
 
-  FILE *output = OpenFile(encFile.string().c_str(), "wb");
-  if (output == nullptr) {
-    LOG(ERROR) << "Cannot create encrypted file: " << encFile.string();
+  bf::path encFile(src.string() + ".enc");
+  FILE *output = OpenFile(encFile, "wb");
+  if (!output) {
+    LOG(ERROR) << "Cannot create encrypted file: " << encFile;
     fclose(input);
     return false;
   }
 
-  std::size_t chunkSize = (fileSize > kEncryptionChunkMaxSize
-                                 ? kEncryptionChunkMaxSize : fileSize);
+  std::size_t chunkSize = (fileSize > kEncryptionChunkMaxSize ?
+                          kEncryptionChunkMaxSize : fileSize);
 
   std::unique_ptr<unsigned char[]> inChunk(new unsigned char[chunkSize]);
   std::size_t bytesRead = 0;
 
   do {
     bytesRead = ReadBytes(inChunk.get(), chunkSize, input);
-    if (0 != bytesRead) {
+    if (!bytesRead) {
       unsigned char* encrypted_data = nullptr;
       size_t encrypted_size = 0;
       // TODO(p.sikorski) check if it is Preloaded
       int ret;
-      if (context_->request_mode.get()
-              == common_installer::RequestMode::GLOBAL) {
+      if (context_->request_mode.get() == ci::RequestMode::GLOBAL) {
         ret = wae_encrypt_global_web_application(
                 context_->pkgid.get().c_str(),
-                context_->is_readonly_package.get() ?
-                true : false,
+                context_->is_readonly_package.get() ? true : false,
                 inChunk.get(),
                 (size_t)bytesRead,
                 &encrypted_data,
@@ -263,14 +266,12 @@ bool StepEncryptResources::EncryptFile(const bf::path &src) {
   fclose(input);
 
   LOG(DEBUG) << "File encrypted successfully";
-  if (0 != unlink(src.string().c_str())) {
+  if (!unlink(src.c_str()))
     return false;
-  }
 
   LOG(DEBUG) << "Rename encrypted file";
-  if (0 != std::rename(encFile.c_str(), src.string().c_str())) {
+  if (!std::rename(encFile.c_str(), src.c_str()))
     return false;
-  }
 
   return true;
 }