Merge directories for copying shared/ when installing package 21/56921/6
authorTomasz Iwanek <t.iwanek@samsung.com>
Wed, 13 Jan 2016 13:29:21 +0000 (14:29 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Thu, 14 Jan 2016 12:11:07 +0000 (04:11 -0800)
Tpk package may have shared/ directory. Therefore, in update
we merge directories if possible to:
 - package supplied directories,
 - runtime created files,
 - package supplied directories from previous version.

Change-Id: I23b181ee4b37acb8c08ad53873875a74c46dfe1b

src/common/step/step_copy_storage_directories.cc
src/common/step/step_copy_storage_directories.h
src/common/utils/file_util.cc
src/common/utils/file_util.h

index d41d1b1..fa9a45d 100644 (file)
@@ -29,10 +29,13 @@ namespace filesystem {
 bool StepCopyStorageDirectories::MoveAppStorage(
     const bf::path& in_src,
     const bf::path& in_dst,
-    const char *key) {
+    const char *key,
+    bool merge_dirs) {
   bf::path src = in_src / key;
   bf::path dst = in_dst / key;
-  return common_installer::MoveDir(src, dst);
+  return common_installer::MoveDir(src, dst,
+      merge_dirs ? common_installer::FS_MERGE_DIRECTORIES
+                 : common_installer::FS_NONE);
 }
 
 common_installer::Step::Status StepCopyStorageDirectories::precheck() {
@@ -58,7 +61,7 @@ common_installer::Step::Status StepCopyStorageDirectories::process() {
 
   if (!MoveAppStorage(backup_path_,
                       context_->pkg_path.get(),
-                      kSharedLocation)) {
+                      kSharedLocation, true)) {
     LOG(ERROR) << "Failed to restore shared directory for widget in update";
     return Status::APP_DIR_ERROR;
   }
index dcd223d..2c08f34 100644 (file)
@@ -54,7 +54,7 @@ class StepCopyStorageDirectories : public common_installer::Step {
  protected:
   bool MoveAppStorage(const boost::filesystem::path& in_src,
                       const boost::filesystem::path& in_dst,
-                      const char *key);
+                      const char *key, bool merge_dirs = false);
   bool CacheDir();
   boost::filesystem::path backup_path_;
   SCOPE_LOG_TAG(CopyStorageDirectories)
index bfc9379..32d7213 100644 (file)
@@ -118,23 +118,26 @@ bool SetDirPermissions(const boost::filesystem::path& path,
   return true;
 }
 
-bool CopyDir(const bf::path& src, const bf::path& dst) {
+bool CopyDir(const bf::path& src, const bf::path& dst, FSFlag flags) {
   try {
     // Check whether the function call is valid
     if (!bf::exists(src) || !bf::is_directory(src)) {
-      LOG(ERROR) << "Source directory " << src.string()
+      LOG(ERROR) << "Source directory " << src
                  << " does not exist or is not a directory.";
       return false;
     }
-    if (bf::exists(dst)) {
-      LOG(ERROR) << "Destination directory " << dst.string()
-                 << " already exists.";
-      return false;
-    }
-    // Create the destination directory
-    if (!CreateDir(dst)) {
-      LOG(ERROR) << "Unable to create destination directory" << dst.string();
-      return false;
+    if (!bf::exists(dst)) {
+      // Create the destination directory
+      if (!CreateDir(dst)) {
+        LOG(ERROR) << "Unable to create destination directory" << dst;
+        return false;
+      }
+    } else {
+      if (!(flags & FS_MERGE_DIRECTORIES)) {
+        LOG(ERROR) << "Destination directory " << dst.string()
+                   << " already exists.";
+        return false;
+      }
     }
   } catch (const bf::filesystem_error& error) {
     LOG(ERROR) << "Failed to copy directory: " << error.what();
@@ -147,17 +150,20 @@ bool CopyDir(const bf::path& src, const bf::path& dst) {
       ++file) {
     try {
       bf::path current(file->path());
+      bf::path target = dst / current.filename();
       if (bf::is_directory(current)) {
         // Found directory: Recursion
-        if (!CopyDir(current, dst / current.filename())) {
+        if (!CopyDir(current, target, flags)) {
           return false;
         }
       } else if (bf::is_symlink(current)) {
-        // Found symlink
-        bf::copy_symlink(current, dst / current.filename());
+        if ((flags & FS_MERGE_DIRECTORIES) && bf::exists(target))
+          continue;
+        bf::copy_symlink(current, target);
       } else {
-        // Found file: Copy
-        bf::copy_file(current, dst / current.filename());
+        if ((flags & FS_MERGE_DIRECTORIES) && bf::exists(target))
+          continue;
+        bf::copy_file(current, target);
       }
     } catch (const bf::filesystem_error& error) {
       LOG(ERROR) << "Failed to copy directory: " << error.what();
@@ -178,9 +184,9 @@ bool CopyFile(const bf::path& src, const bf::path& dst) {
   return true;
 }
 
-bool MoveDir(const bf::path& src, const bf::path& dst) {
-  if (bf::exists(dst)) {
-    LOG(ERROR) << "Destination directory does not exist: " << dst;
+bool MoveDir(const bf::path& src, const bf::path& dst, FSFlag flags) {
+  if (bf::exists(dst) && !(flags & FS_MERGE_DIRECTORIES)) {
+    LOG(ERROR) << "Destination directory does exist: " << dst;
     return false;
   }
 
@@ -188,7 +194,7 @@ bool MoveDir(const bf::path& src, const bf::path& dst) {
   bf::rename(src, dst, error);
   if (error) {
     LOG(WARNING) << "Cannot move directory: " << src << ". Will copy/remove...";
-    if (!CopyDir(src, dst)) {
+    if (!CopyDir(src, dst, flags)) {
       LOG(ERROR) << "Cannot copy directory: " << src;
       return false;
     }
index e546c29..b857db1 100644 (file)
@@ -9,16 +9,21 @@
 
 namespace common_installer {
 
+enum FSFlag {
+  FS_NONE              = 0,
+  FS_MERGE_DIRECTORIES = (1 << 0)
+};
+
 bool CreateDir(const boost::filesystem::path& path);
 
 bool CopyDir(const boost::filesystem::path& src,
-             const boost::filesystem::path& dst);
+             const boost::filesystem::path& dst, FSFlag flags = FS_NONE);
 
 bool CopyFile(const boost::filesystem::path& src,
              const boost::filesystem::path& dst);
 
 bool MoveDir(const boost::filesystem::path& src,
-             const boost::filesystem::path& dst);
+             const boost::filesystem::path& dst, FSFlag flags = FS_NONE);
 
 bool MoveFile(const boost::filesystem::path& src,
               const boost::filesystem::path& dst);