libtar: backup and restore posix capabilities

This patch also allows libtar to combine data from multiple extended
tar headers into a single header.

Change-Id: I82d13e89a3622ea665b60062b1904ddbedfa41b3
diff --git a/libtar/append.c b/libtar/append.c
index 4388297..7c679f6 100644
--- a/libtar/append.c
+++ b/libtar/append.c
@@ -22,6 +22,10 @@
 #include <sys/types.h>
 #include <stdbool.h>
 
+#include <sys/capability.h>
+#include <sys/xattr.h>
+#include <linux/xattr.h>
+
 #ifdef STDC_HEADERS
 # include <stdlib.h>
 # include <string.h>
@@ -154,6 +158,24 @@
 	}
 #endif
 
+	/* get posix file capabilities */
+	if (TH_ISREG(t) && t->options & TAR_STORE_POSIX_CAP)
+	{
+		if (t->th_buf.has_cap_data)
+		{
+			memset(&t->th_buf.cap_data, 0, sizeof(struct vfs_cap_data));
+			t->th_buf.has_cap_data = 0;
+		}
+
+		if (getxattr(realname, XATTR_NAME_CAPS, &t->th_buf.cap_data, sizeof(struct vfs_cap_data)) >= 0)
+		{
+			t->th_buf.has_cap_data = 1;
+#if 1 //def DEBUG
+			print_caps(&t->th_buf.cap_data);
+#endif
+		}
+	}
+
 	/* check if it's a hardlink */
 #ifdef DEBUG
 	puts("tar_append_file(): checking inode cache for hardlink...");