New FSFlag for preserve ownership and permissions 84/123084/4
authorDamian Pietruchowski <d.pietruchow@samsung.com>
Tue, 4 Apr 2017 10:24:20 +0000 (12:24 +0200)
committerjongmyeong ko <jongmyeong.ko@samsung.com>
Thu, 8 Jun 2017 10:33:29 +0000 (10:33 +0000)
Change-Id: I35124a78ea4564ae1a4b70132fbe5b94a764b53c
Signed-off-by: Damian Pietruchowski <d.pietruchow@samsung.com>
src/common/utils/file_util.cc
src/common/utils/file_util.h

index 004df24..1e7658e 100644 (file)
@@ -173,6 +173,25 @@ bool SetDirOwnershipAndPermissions(const boost::filesystem::path& path,
   return true;
 }
 
+bool CopyOwnershipAndPermissions(const boost::filesystem::path& src,
+                                 const boost::filesystem::path& dst) {
+  if(!bf::exists(src)) {
+    LOG(ERROR) << "Failed to copy ownership and permissions"
+               << " from " << src << " to " << dst;
+    return false;
+  }
+  bf::perms permissions = bf::status(src).permissions();
+  struct stat stats;
+  stat(src.c_str(), &stats);
+  if(!SetDirOwnershipAndPermissions(dst, permissions, stats.st_uid,
+                                    stats.st_gid)) {
+    LOG(ERROR) << "Failed to copy ownership and permissions"
+               << " from " << src << " to " << dst;
+    return false;
+  }
+  return true;
+}
+
 bool CopyDir(const bf::path& src, const bf::path& dst,
              FSFlag flags, bool skip_symlink) {
   try {
@@ -188,12 +207,16 @@ bool CopyDir(const bf::path& src, const bf::path& dst,
         LOG(ERROR) << "Unable to create destination directory" << dst;
         return false;
       }
+      if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
+        CopyOwnershipAndPermissions(src, dst);
     } else {
       if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
         LOG(ERROR) << "Destination directory " << dst.string()
                    << " already exists.";
         return false;
       }
+      if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS))
+        CopyOwnershipAndPermissions(src, dst);
     }
   } catch (const bf::filesystem_error& error) {
     LOG(ERROR) << "Failed to copy directory: " << error.what();
@@ -241,6 +264,9 @@ bool CopyDir(const bf::path& src, const bf::path& dst,
         else
           bf::copy_file(current, destination);
 
+        if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)
+          CopyOwnershipAndPermissions(current, destination);
+
         if (flags & FS_COMMIT_COPY_FILE) {
           if (flags & FS_MERGE_OVERWRITE)
             bf::remove(target);
index 9814d8b..b2ed7a7 100644 (file)
@@ -15,7 +15,8 @@ enum FSFlag : int {
   FS_NONE              = 0,
   FS_MERGE_SKIP        = (1 << 0),
   FS_MERGE_OVERWRITE   = (1 << 1),
-  FS_COMMIT_COPY_FILE  = (1 << 2)
+  FS_COMMIT_COPY_FILE  = (1 << 2),
+  FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
 };
 
 FSFlag operator|(FSFlag a, FSFlag b);
@@ -50,6 +51,9 @@ bool SetDirOwnershipAndPermissions(const boost::filesystem::path& path,
                       boost::filesystem::perms permissions, uid_t uid,
                       gid_t gid);
 
+bool CopyOwnershipAndPermissions(const boost::filesystem::path& src,
+                                 const boost::filesystem::path& dst);
+
 int64_t GetUnpackedPackageSize(const boost::filesystem::path& path);
 
 int64_t GetDirectorySize(const boost::filesystem::path& path);