]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1962: No support for writing extended attributes v9.0.1962
authorChristian Brabandt <cb@256bit.org>
Sat, 30 Sep 2023 10:49:18 +0000 (12:49 +0200)
committerChristian Brabandt <cb@256bit.org>
Sat, 30 Sep 2023 10:49:18 +0000 (12:49 +0200)
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>
16 files changed:
.github/workflows/ci.yml
runtime/doc/builtin.txt
runtime/doc/editing.txt
runtime/doc/tags
runtime/doc/various.txt
src/auto/configure
src/bufwrite.c
src/config.h.in
src/configure.ac
src/errors.h
src/evalfunc.c
src/feature.h
src/os_unix.c
src/proto/os_unix.pro
src/testdir/test_writefile.vim
src/version.c

index 8c2e5396b7c680bd3e2825fa8c2e42c6927e7adb..cd1615f9f447fe7fe3a70088ae72e98b826efd37 100644 (file)
@@ -110,6 +110,8 @@ jobs:
               tcl-dev \
               cscope \
               libsodium-dev \
+              attr \
+              libattr1-dev
             )
           fi
           sudo apt-get update && sudo apt-get install -y "${PKGS[@]}"
index 8a92ff6ecfe1c2d6a2b23fca6b9291cbeba5c29d..b4ea216f31e8046c7bf11d9663c08b60a01d1f95 100644 (file)
@@ -1,4 +1,4 @@
-*builtin.txt*  For Vim version 9.0.  Last change: 2023 Aug 09
+*builtin.txt*  For Vim version 9.0.  Last change: 2023 Sep 27
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -11082,6 +11082,8 @@ winaltkeys              Compiled with 'winaltkeys' option.
 windows                        Compiled with support for more than one window.
                        (always true)
 writebackup            Compiled with 'writebackup' default on.
+xattr                  Compiled with extended attributes support |xattr|
+                       (currently only supported on Linux).
 xfontset               Compiled with X fontset support |xfontset|.
 xim                    Compiled with X input method support |xim|.
 xpm                    Compiled with pixmap support.
index 46279110a7238ca32d6a274a2693a5b6fbbfa4b9..a015c8462aaf9c0bd6775dc6ebd60df7304a8ce8 100644 (file)
@@ -1,4 +1,4 @@
-*editing.txt*   For Vim version 9.0.  Last change: 2023 Sep 22
+*editing.txt*   For Vim version 9.0.  Last change: 2023 Sep 27
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -1097,6 +1097,13 @@ will get the ACL info of the original file.
    The ACL info is also used to check if a file is read-only (when opening the
 file).
 
+                               *xattr* *E1506* *E1507* *E1508* *E1509*
+xattr stands for Extended Attributes  It is an advanced way to save metadata
+alongside the file in the filesystem.  It depends on the actual filesystem
+being used and Vim supports it only on a Linux system.
+   Vim attempts to preserve the extended attribute info when writing a file.
+The backup file will get the extended attribute of the original file.
+
                                                *read-only-share*
 When MS-Windows shares a drive on the network it can be marked as read-only.
 This means that even if the file read-only attribute is absent, and the ACL
index 1df34d3a3d5a55ae784ad72c990b9c65b2801118..249800fd2928929a61f5a8755088227b752ea4b5 100644 (file)
@@ -1483,6 +1483,7 @@ $quote    eval.txt        /*$quote*
 +wildmenu      various.txt     /*+wildmenu*
 +windows       various.txt     /*+windows*
 +writebackup   various.txt     /*+writebackup*
++xattr various.txt     /*+xattr*
 +xfontset      various.txt     /*+xfontset*
 +xim   various.txt     /*+xim*
 +xpm   various.txt     /*+xpm*
@@ -4506,6 +4507,10 @@ E1502    builtin.txt     /*E1502*
 E1503  builtin.txt     /*E1503*
 E1504  builtin.txt     /*E1504*
 E1505  builtin.txt     /*E1505*
+E1506  editing.txt     /*E1506*
+E1507  editing.txt     /*E1507*
+E1508  editing.txt     /*E1508*
+E1509  editing.txt     /*E1509*
 E151   helphelp.txt    /*E151*
 E152   helphelp.txt    /*E152*
 E153   helphelp.txt    /*E153*
@@ -11224,6 +11229,7 @@ x-resources     version5.txt    /*x-resources*
 x11-clientserver       remote.txt      /*x11-clientserver*
 x11-cut-buffer gui_x11.txt     /*x11-cut-buffer*
 x11-selection  gui_x11.txt     /*x11-selection*
+xattr  editing.txt     /*xattr*
 xf86conf.vim   syntax.txt      /*xf86conf.vim*
 xfontset       mbyte.txt       /*xfontset*
 xfree-xterm    syntax.txt      /*xfree-xterm*
index e478c8266ea6a3b63e4b65996a1888df0d0c98a5..b2b79039358f5ba775e99583d23a2c07c65be8f3 100644 (file)
@@ -1,4 +1,4 @@
-*various.txt*   For Vim version 9.0.  Last change: 2022 Dec 13
+*various.txt*   For Vim version 9.0.  Last change: 2023 Sep 27
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -503,6 +503,7 @@ T  *+windows*               more than one window; Always enabled since 8.0.1118.
 m  *+writebackup*      |'writebackup'| is default on
 m  *+xim*              X input method |xim|
    *+xfontset*         X fontset support |xfontset|
+N  *+xattr*            compiled with extended attribute support (Linux only)
    *+xpm*              pixmap support
 m  *+xpm_w32*          Win32 GUI only: pixmap support |w32-xpm-support|
    *+xsmp*             XSMP (X session management) support
index 34e9f449aa60c1e2dfcff5f9b345e858888574e3..54c1aa8159944f8be4fa92f0bb4f276726f9c3d6 100755 (executable)
@@ -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; }
index 03a83b569a44fac079f1b030374b61dc322b977c..bf79ad5bf3fcb2f6478ff434de55ddd4ef7ab88c 100644 (file)
@@ -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
index 93972ca0cc723a4797c40387ad0a74f14b8cc10a..8ad9f03136ea60d77c461b25a5a7ecd0e69e9af1 100644 (file)
 #undef HAVE_MBLEN
 #undef HAVE_TIMER_CREATE
 #undef HAVE_CLOCK_GETTIME
+#undef HAVE_XATTR
 
 /* Define, if needed, for accessing large files. */
 #undef _LARGE_FILES
index bfdcfea4d24a253d404bb8efad6bfb45ef2d2b12..e21e23490af12866155c87a4cef2362e4eb77534 100644 (file)
@@ -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)
index 6b4416963a9464e537f5b761bbaa6387451f188f..16b38cf99aed3c13bdc80a9838fafd0bc287898d 100644 (file)
@@ -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");
index 2cd1985a0643202d4f44d61c9cc24d5cba1488fe..501ee03582fa737f5cecac2ede0a0a2a76d230c4 100644 (file)
@@ -6462,6 +6462,13 @@ f_has(typval_T *argvars, typval_T *rettv)
                1
 #else
                0
+#endif
+               },
+       {"xattr",
+#ifdef FEAT_XATTR
+               1
+#else
+               0
 #endif
                },
        {"xim",
index ca180dd1f053904970618e87e4c74c8795a5cc63..b26dc6ccf187c42b8971f67c6001f645afd813c8 100644 (file)
@@ -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.
  */
        || defined(FEAT_TERMINAL)
 # define USING_LOAD_LIBRARY
 #endif
+
+/*
+ * XATTR support
+ */
+
+#if defined(FEAT_NORMAL) && defined(HAVE_XATTR)
+# define FEAT_XATTR
+#endif
index c5a54e419e7cb92677d0b38b7ce33b1a957cae93..674dd96666e32c8309374837e3dc40d3889527a3 100644 (file)
 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.
index c3a8483f876e83d8ea06bca290e55af6ce021ae5..6e13de6caa79d09067069aec09afc15b2d907f9e 100644 (file)
@@ -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);
index 140b2ee0375cca47c6aa2d15b1542ff9c3a9faef..a54efa7cf967f670173b636c1f030e7d5bbab6d0 100644 (file)
@@ -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
index 5404c02b0ed03ea9b86060c08fda1ae59e4f2f2d..ffa5afcb5767c8c4981937701d48a59f3bbd3ce1 100644 (file)
@@ -654,6 +654,11 @@ static char *(features[]) =
        "-X11",
 # endif
 #endif
+# ifdef FEAT_XATTR
+       "+xattr",
+# else
+       "-xattr",
+# endif
 #ifdef FEAT_XFONTSET
        "+xfontset",
 #else
@@ -699,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1962,
 /**/
     1961,
 /**/