patch 9.0.1962: No support for writing extended attributes
Problem: No support for writing extended attributes
Solution: Add extended attribute support for linux
It's been a long standing issue, that if you write a file with extended
attributes and backupcopy is set to no, the file will loose the extended
attributes.
So this patch adds support for retrieving the extended attributes and
copying it to the new file. It currently only works on linux, mainly
because I don't know the different APIs for other systems (BSD, MacOSX and
Solaris). On linux, this should be supported since Kernel 2.4 or
something, so this should be pretty safe to use now.
Enable the extended attribute support with normal builds.
I also added it explicitly to the :version output as well as make it
able to check using `:echo has("xattr")`, to have users easily check
that this is available.
In contrast to the similar support for SELINUX and SMACK support (which
also internally uses extended attributes), I have made this a FEAT_XATTR
define, instead of the similar HAVE_XATTR.
Add a test and change CI to include relevant packages so that CI can
test that extended attributes are correctly written.
closes: #306
closes: #13203
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/os_unix.c b/src/os_unix.c
index c5a54e4..674dd96 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -35,6 +35,11 @@
static int selinux_enabled = -1;
#endif
+#ifdef FEAT_XATTR
+# include <attr/xattr.h>
+# define XATTR_VAL_LEN 1024
+#endif
+
#ifdef HAVE_SMACK
# include <attr/xattr.h>
# include <linux/xattr.h>
@@ -3096,6 +3101,96 @@
}
#endif // HAVE_SMACK
+#ifdef FEAT_XATTR
+/*
+ * Copy extended attributes from_file to to_file
+ */
+ void
+mch_copy_xattr(char_u *from_file, char_u *to_file)
+{
+ char *xattr_buf;
+ size_t size;
+ size_t tsize;
+ ssize_t keylen, vallen, max_vallen = 0;
+ char *key;
+ char *val = NULL;
+ char *errmsg = NULL;
+
+ if (from_file == NULL)
+ return;
+
+ // get the length of the extended attributes
+ size = listxattr((char *)from_file, NULL, 0);
+ // not supported or no attributes to copy
+ if (errno == ENOTSUP || size == 0)
+ return;
+ xattr_buf = (char*)alloc(size);
+ if (xattr_buf == NULL)
+ return;
+ size = listxattr((char *)from_file, xattr_buf, size);
+ tsize = size;
+
+ errno = 0;
+
+ for (int round = 0; round < 2; round++)
+ {
+
+ key = xattr_buf;
+ if (round == 1)
+ size = tsize;
+
+ while (size > 0)
+ {
+ vallen = getxattr((char *)from_file, key,
+ val, round ? max_vallen : 0);
+ // only set the attribute in the second round
+ if (vallen >= 0 && round &&
+ setxattr((char *)to_file, key, val, vallen, 0) == 0)
+ ;
+ else if (errno)
+ {
+ switch (errno)
+ {
+ case E2BIG:
+ errmsg = e_xattr_e2big;
+ goto error_exit;
+ case ENOTSUP:
+ errmsg = e_xattr_enotsup;
+ goto error_exit;
+ case ERANGE:
+ errmsg = e_xattr_erange;
+ goto error_exit;
+ default:
+ errmsg = e_xattr_other;
+ goto error_exit;
+ }
+ }
+
+ if (round == 0 && vallen > max_vallen)
+ max_vallen = vallen;
+
+ // add one for terminating null
+ keylen = STRLEN(key) + 1;
+ size -= keylen;
+ key += keylen;
+ }
+ if (round)
+ break;
+
+ val = (char*)alloc(max_vallen + 1);
+ if (val == NULL)
+ goto error_exit;
+
+ }
+error_exit:
+ vim_free(xattr_buf);
+ vim_free(val);
+
+ if (errmsg != NULL)
+ emsg((char *)errmsg);
+}
+#endif
+
/*
* Return a pointer to the ACL of file "fname" in allocated memory.
* Return NULL if the ACL is not available for whatever reason.