In Red Hat Bugzilla, there was a request to add support of preserving ext2 
extended filesystem flags (append only, immutable etc..) to cp, mv, install 
commands: 
https://bugzilla.redhat.com/show_bug.cgi?id=995249

I took a look at it and added the support into cp, using e2fsprogs-libs 
library, so it is preserving the attributes as intended.
However, I should consult this change with upstream first so I'm posting my 
patch.
Note that this is an initial version of the patch and it is not complete at 
all, It has very basic functionality and I'm just demonstrating the way I'm 
going to implement the support.
I need your feedback about this first. Eventually I will complete the patch.

I am doing it under Fedora 19 3.10.4-300.fc19.x86_64, with libattr and 
e2fsprogs-libs installed.

Steps to test it:

- apply the patch
- into Makefile, add manually:
  "LIB_EXT2ATTR = -le2p"    (for example after the line "LIB_XATTR = -lattr")
  "copy_ldadd = ..." add "$(LIB_EXT2ATTR)"
$ make
$ touch testfile
$ sudo chattr +a testfile
$ lsattr testfile
-----a-------e-- testfile
$ sudo src/cp --preserve=all testfile testfile1
$ lsattr testfile1
-----a-------e-- testfile1


From 59e236c167df4868a7fd83f714d9c5b7abd6145e Mon Sep 17 00:00:00 2001
From: Jaromir Koncicky <[email protected]>
Date: Fri, 16 Aug 2013 15:16:24 +0200
Subject: [PATCH] copy: added support for ext2 attributes preserving

---
 src/copy.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/copy.c b/src/copy.c
index f0cebb1..74318e0 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -69,6 +69,11 @@
 # include "verror.h"
 #endif
 
+#define USE_EXT2ATTR 1 // This will be added into config
+#if USE_EXT2ATTR
+#include <e2p/e2p.h>
+#endif
+
 #ifndef HAVE_FCHOWN
 # define HAVE_FCHOWN false
 # define fchown(fd, uid, gid) (-1)
@@ -550,6 +555,26 @@ copy_attr (char const *src_path _GL_UNUSED,
 }
 #endif /* USE_XATTR */
 
+#if USE_EXT2ATTR
+static bool
+copy_ext2attr (char const *src_path, char const *dst_path)
+{
+  unsigned long flags;
+  if (fgetflags(src_path, &flags) == -1)
+    return false;
+  if (fsetflags(dst_path, flags) == -1)
+    return false;
+  return true;
+}
+#else /* USE_XATTR */
+static bool
+copy_ext2attr (char const *src_path ATTRIBUTE_UNUSED,
+           char const *dst_path ATTRIBUTE_UNUSED)
+{
+  return true;
+}
+#endif /* USE_XATTR */
+
 /* Read the contents of the directory SRC_NAME_IN, and recursively
    copy the contents to DST_NAME_IN.  NEW_DST is true if
    DST_NAME_IN is a directory that was created previously in the
@@ -1170,6 +1195,13 @@ preserve_metadata:
         }
     }
 
+  /* Preserve ext2 attributes.
+     This should be done in the very last phase because setting some
+     ext2 attributes disallows changing the file properties.  */
+  if (x->preserve_xattr && ! copy_ext2attr (src_name, dst_name) // There will be new setting like x->preserve_ext2attr
+      && x->require_preserve_xattr)
+    return_val = false;
+
 close_src_and_dst_desc:
   if (close (dest_desc) < 0)
     {
-- 
1.8.3.1

Reply via email to