summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristian Brabandt <cb@256bit.org>2023-09-30 12:49:18 +0200
committerChristian Brabandt <cb@256bit.org>2023-09-30 12:49:18 +0200
commite085dfda5d8dde064b0332464040959479696d1c (patch)
treeb9d30e78eb9335de6f2db790f888f0a9bd9514d7 /src
parent1f025b01e29be6fce907d0379602b45031d6998f (diff)
patch 9.0.1962: No support for writing extended attributesv9.0.1962
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>
Diffstat (limited to 'src')
-rwxr-xr-xsrc/auto/configure28
-rw-r--r--src/bufwrite.c15
-rw-r--r--src/config.h.in1
-rw-r--r--src/configure.ac12
-rw-r--r--src/errors.h10
-rw-r--r--src/evalfunc.c7
-rw-r--r--src/feature.h10
-rw-r--r--src/os_unix.c95
-rw-r--r--src/proto/os_unix.pro1
-rw-r--r--src/testdir/test_writefile.vim23
-rw-r--r--src/version.c7
11 files changed, 206 insertions, 3 deletions
diff --git a/src/auto/configure b/src/auto/configure
index 34e9f449aa..54c1aa8159 100755
--- a/src/auto/configure
+++ b/src/auto/configure
@@ -825,6 +825,7 @@ with_global_runtime
with_modified_by
enable_smack
enable_selinux
+enable_xattr
with_features
with_compiledby
enable_xsmp
@@ -1514,6 +1515,7 @@ Optional Features:
--disable-darwin Disable Darwin (Mac OS X) support.
--disable-smack Do not check for Smack support.
--disable-selinux Do not check for SELinux support.
+ --disable-xattr Do not check for XATTR support.
--disable-xsmp Disable XSMP session management
--disable-xsmp-interact Disable XSMP interaction
--enable-luainterp=OPTS Include Lua interpreter. default=no OPTS=no/yes/dynamic
@@ -5419,6 +5421,32 @@ printf "%s\n" "yes" >&6; }
fi
fi
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking --enable-xattr argument" >&5
+printf %s "checking --enable-xattr argument... " >&6; }
+# Check whether --enable-xattr was given.
+if test ${enable_xattr+y}
+then :
+ enableval=$enable_xattr;
+else $as_nop
+ enable_xattr="yes"
+fi
+
+if test "$enable_xattr" = "yes"; then
+ { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+printf "%s\n" "yes" >&6; }
+ ac_fn_c_check_header_compile "$LINENO" "attr/xattr.h" "ac_cv_header_attr_xattr_h" "$ac_includes_default"
+if test "x$ac_cv_header_attr_xattr_h" = xyes
+then :
+ printf "%s\n" "#define HAVE_XATTR 1" >>confdefs.h
+
+fi
+
+else
+ { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+fi
+
+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking --with-features argument" >&5
printf %s "checking --with-features argument... " >&6; }
diff --git a/src/bufwrite.c b/src/bufwrite.c
index 03a83b569a..bf79ad5bf3 100644
--- a/src/bufwrite.c
+++ b/src/bufwrite.c
@@ -1471,6 +1471,9 @@ buf_write(
# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
mch_copy_sec(fname, backup);
# endif
+# ifdef FEAT_XATTR
+ mch_copy_xattr(fname, backup);
+# endif
#endif
// copy the file.
@@ -1506,6 +1509,9 @@ buf_write(
#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
mch_copy_sec(fname, backup);
#endif
+#ifdef FEAT_XATTR
+ mch_copy_xattr(fname, backup);
+#endif
#ifdef MSWIN
(void)mch_copy_file_attribute(fname, backup);
#endif
@@ -2196,11 +2202,18 @@ restore_backup:
}
#endif
-#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
+#if defined(HAVE_SELINUX) || defined(HAVE_SMACK) || defined(FEAT_XATTR)
// Probably need to set the security context.
if (!backup_copy)
+ {
+#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
mch_copy_sec(backup, wfname);
#endif
+#ifdef FEAT_XATTR
+ mch_copy_xattr(backup, wfname);
+#endif
+ }
+#endif
#ifdef UNIX
// When creating a new file, set its owner/group to that of the
diff --git a/src/config.h.in b/src/config.h.in
index 93972ca0cc..8ad9f03136 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -226,6 +226,7 @@
#undef HAVE_MBLEN
#undef HAVE_TIMER_CREATE
#undef HAVE_CLOCK_GETTIME
+#undef HAVE_XATTR
/* Define, if needed, for accessing large files. */
#undef _LARGE_FILES
diff --git a/src/configure.ac b/src/configure.ac
index bfdcfea4d2..e21e23490a 100644
--- a/src/configure.ac
+++ b/src/configure.ac
@@ -513,6 +513,18 @@ if test "x$found_smack" = "x"; then
fi
fi
+dnl enable xattr support
+AC_MSG_CHECKING(--enable-xattr argument)
+AC_ARG_ENABLE(xattr,
+ [ --disable-xattr Do not check for XATTR support.],
+ , enable_xattr="yes")
+if test "$enable_xattr" = "yes"; then
+ AC_MSG_RESULT(yes)
+ AC_CHECK_HEADER([attr/xattr.h], [AC_DEFINE(HAVE_XATTR)])
+else
+ AC_MSG_RESULT(no)
+fi
+
dnl Check user requested features.
AC_MSG_CHECKING(--with-features argument)
diff --git a/src/errors.h b/src/errors.h
index 6b4416963a..16b38cf99a 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -3552,6 +3552,14 @@ EXTERN char e_positional_arg_num_type_inconsistent_str_str[]
INIT(= N_("E1504: Positional argument %d type used inconsistently: %s/%s"));
EXTERN char e_invalid_format_specifier_str[]
INIT(= N_("E1505: Invalid format specifier: %s"));
-// E1506 - E1519 unused
+EXTERN char e_xattr_erange[]
+ INIT(= N_("E1506: Buffer too small to copy xattr value or key"));
+EXTERN char e_xattr_enotsup[]
+ INIT(= N_("E1507: Extended attributes are not supported by the filesystem"));
+EXTERN char e_xattr_e2big[]
+ INIT(= N_("E1508: size of the extended attribute value is larger than the maximum size allowed"));
+EXTERN char e_xattr_other[]
+ INIT(= N_("E1509: error occured when reading or writing extended attribute"));
+// E1509 - E1519 unused
EXTERN char e_aptypes_is_null_nr_str[]
INIT(= "E1520: Internal error: ap_types or ap_types[idx] is NULL: %d: %s");
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 2cd1985a06..501ee03582 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -6464,6 +6464,13 @@ f_has(typval_T *argvars, typval_T *rettv)
0
#endif
},
+ {"xattr",
+#ifdef FEAT_XATTR
+ 1
+#else
+ 0
+#endif
+ },
{"xim",
#ifdef FEAT_XIM
1
diff --git a/src/feature.h b/src/feature.h
index ca180dd1f0..b26dc6ccf1 100644
--- a/src/feature.h
+++ b/src/feature.h
@@ -22,7 +22,7 @@
* - Add a #define below.
* - Add a message in the table above ex_version().
* - Add a string to f_has().
- * - Add a feature to ":help feature-list" in doc/eval.txt.
+ * - Add a feature to ":help feature-list" in doc/builtin.txt.
* - Add feature to ":help +feature-list" in doc/various.txt.
* - Add comment for the documentation of commands that use the feature.
*/
@@ -1175,3 +1175,11 @@
|| defined(FEAT_TERMINAL)
# define USING_LOAD_LIBRARY
#endif
+
+/*
+ * XATTR support
+ */
+
+#if defined(FEAT_NORMAL) && defined(HAVE_XATTR)
+# define FEAT_XATTR
+#endif
diff --git a/src/os_unix.c b/src/os_unix.c
index c5a54e419e..674dd96666 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 @@ mch_copy_sec(char_u *from_file, char_u *to_file)
}
#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.
diff --git a/src/proto/os_unix.pro b/src/proto/os_unix.pro
index c3a8483f87..6e13de6caa 100644
--- a/src/proto/os_unix.pro
+++ b/src/proto/os_unix.pro
@@ -37,6 +37,7 @@ long mch_getperm(char_u *name);
int mch_setperm(char_u *name, long perm);
int mch_fsetperm(int fd, long perm);
void mch_copy_sec(char_u *from_file, char_u *to_file);
+void mch_copy_xattr(char_u *from_file, char_u *to_file);
vim_acl_T mch_get_acl(char_u *fname);
void mch_set_acl(char_u *fname, vim_acl_T aclent);
void mch_free_acl(vim_acl_T aclent);
diff --git a/src/testdir/test_writefile.vim b/src/testdir/test_writefile.vim
index 140b2ee037..a54efa7cf9 100644
--- a/src/testdir/test_writefile.vim
+++ b/src/testdir/test_writefile.vim
@@ -977,4 +977,27 @@ func Test_wq_quitpre_autocommand()
call delete('Xsomefile')
endfunc
+func Test_write_with_xattr_support()
+ CheckLinux
+ CheckExecutable setfattr
+
+ let contents = ["file with xattrs", "line two"]
+ call writefile(contents, 'Xwattr.txt', 'D')
+ " write a couple of xattr
+ call system('setfattr -n user.cookie -v chocolate Xwattr.txt')
+ call system('setfattr -n user.frieda -v bar Xwattr.txt')
+ call system('setfattr -n user.empty Xwattr.txt')
+
+ set backupcopy=no writebackup& backup&
+ sp Xwattr.txt
+ w
+ $r! getfattr -d %
+ let expected = ['file with xattrs', 'line two', '# file: Xwattr.txt', 'user.cookie="chocolate"', 'user.empty=""', 'user.frieda="bar"', '']
+ call assert_equal(expected, getline(1,'$'))
+
+ set backupcopy&
+ bw!
+
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/version.c b/src/version.c
index 5404c02b0e..ffa5afcb57 100644
--- a/src/version.c
+++ b/src/version.c
@@ -654,6 +654,11 @@ static char *(features[]) =
"-X11",
# endif
#endif
+# ifdef FEAT_XATTR
+ "+xattr",
+# else
+ "-xattr",
+# endif
#ifdef FEAT_XFONTSET
"+xfontset",
#else
@@ -700,6 +705,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1962,
+/**/
1961,
/**/
1960,