]> granicus.if.org Git - vim/commitdiff
patch 7.4.1516 v7.4.1516
authorBram Moolenaar <Bram@vim.org>
Tue, 8 Mar 2016 16:08:53 +0000 (17:08 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 8 Mar 2016 16:08:53 +0000 (17:08 +0100)
Problem:    Cannot change file permissions.
Solution:   Add setfperm().

runtime/doc/eval.txt
src/eval.c
src/testdir/test_alot.vim
src/testdir/test_file_perm.vim [new file with mode: 0644]
src/version.c

index 4251eb45adf63aac73a7afb05c13e8a28c616af8..8910f2ff4cfbcc82736047b827e24d8134a4c2eb 100644 (file)
@@ -2067,6 +2067,7 @@ serverlist()                      String  get a list of available servers
 setbufvar( {expr}, {varname}, {val})   set {varname} in buffer {expr} to {val}
 setcharsearch( {dict})         Dict    set character search from {dict}
 setcmdpos( {pos})              Number  set cursor position in command-line
+setfperm( {fname}, {mode})     Number  set {fname} file permissions to {mode}
 setline( {lnum}, {line})       Number  set line {lnum} to {line}
 setloclist( {nr}, {list}[, {action}])
                                Number  modify location list using {list}
@@ -3830,6 +3831,8 @@ getfperm({fname})                                 *getfperm()*
 <              This will hopefully (from a security point of view) display
                the string "rw-r--r--" or even "rw-------".
 
+               For setting permissins use |setfperm()|.
+
 getftime({fname})                                      *getftime()*
                The result is a Number, which is the last modification time of
                the given file {fname}.  The value is measured as seconds
@@ -6001,6 +6004,24 @@ setcmdpos({pos})                                 *setcmdpos()*
                Returns 0 when successful, 1 when not editing the command
                line.
 
+setfperm({fname}, {mode})                              *setfperm()* *chmod*
+               Set the file permissions for {fname} to {mode}.
+               {mode} must be a string with 9 characters.  It is of the form
+               "rwxrwxrwx", where each group of "rwx" flags represent, in
+               turn, the permissions of the owner of the file, the group the
+               file belongs to, and other users.  A '-' character means the
+               permission is off, any other character means on.  Multi-byte
+               characters are not supported.
+
+               For example "rw-r-----" means read-write for the user,
+               readable by the group, not accessible by others.  "xx-x-----"
+               would do the same thing.
+
+               Returns non-zero for success, zero for failure.
+
+               To read permissions see |getfperm()|.
+
+
 setline({lnum}, {text})                                        *setline()*
                Set line {lnum} of the current buffer to {text}.  To insert
                lines use |append()|.
index 70a1daa44d706834e26882ee9917414292369ef4..1422dc225d32b1fe4c444644c182567d73afddba 100644 (file)
@@ -735,6 +735,7 @@ static void f_serverlist(typval_T *argvars, typval_T *rettv);
 static void f_setbufvar(typval_T *argvars, typval_T *rettv);
 static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
 static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
+static void f_setfperm(typval_T *argvars, typval_T *rettv);
 static void f_setline(typval_T *argvars, typval_T *rettv);
 static void f_setloclist(typval_T *argvars, typval_T *rettv);
 static void f_setmatches(typval_T *argvars, typval_T *rettv);
@@ -8446,6 +8447,7 @@ static struct fst
     {"setbufvar",      3, 3, f_setbufvar},
     {"setcharsearch",  1, 1, f_setcharsearch},
     {"setcmdpos",      1, 1, f_setcmdpos},
+    {"setfperm",       2, 2, f_setfperm},
     {"setline",                2, 2, f_setline},
     {"setloclist",     2, 3, f_setloclist},
     {"setmatches",     1, 1, f_setmatches},
@@ -18421,6 +18423,42 @@ f_setcmdpos(typval_T *argvars, typval_T *rettv)
        rettv->vval.v_number = set_cmdline_pos(pos);
 }
 
+/*
+ * "setfperm({fname}, {mode})" function
+ */
+    static void
+f_setfperm(typval_T *argvars, typval_T *rettv)
+{
+    char_u     *fname;
+    char_u     modebuf[NUMBUFLEN];
+    char_u     *mode_str;
+    int                i;
+    int                mask;
+    int                mode = 0;
+
+    rettv->vval.v_number = 0;
+    fname = get_tv_string_chk(&argvars[0]);
+    if (fname == NULL)
+       return;
+    mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
+    if (mode_str == NULL)
+       return;
+    if (STRLEN(mode_str) != 9)
+    {
+       EMSG2(_(e_invarg2), mode_str);
+       return;
+    }
+
+    mask = 1;
+    for (i = 8; i >= 0; --i)
+    {
+       if (mode_str[i] != '-')
+           mode |= mask;
+       mask = mask << 1;
+    }
+    rettv->vval.v_number = mch_setperm(fname, mode) == OK;
+}
+
 /*
  * "setline()" function
  */
index d7718ff6d09196aac7b8d56fef04254d82b3b4e1..857fe9abaa387b58cd3f40362cde47be9f6920dd 100644 (file)
@@ -5,6 +5,7 @@ source test_backspace_opt.vim
 source test_cursor_func.vim
 source test_delete.vim
 source test_expand.vim
+source test_file_perm.vim
 source test_glob2regpat.vim
 source test_join.vim
 source test_lispwords.vim
diff --git a/src/testdir/test_file_perm.vim b/src/testdir/test_file_perm.vim
new file mode 100644 (file)
index 0000000..44778ee
--- /dev/null
@@ -0,0 +1,20 @@
+" Test getting and setting file permissions.
+
+func Test_file_perm()
+  call assert_equal('', getfperm('Xtest'))
+  call assert_equal(0, setfperm('Xtest', 'r--------'))
+
+  call writefile(['one'], 'Xtest')
+  call assert_true(len(getfperm('Xtest')) == 9)
+
+  call assert_equal(1, setfperm('Xtest', 'rwx------'))
+  call assert_equal('rwx------', getfperm('Xtest'))
+
+  call assert_equal(1, setfperm('Xtest', 'r--r--r--'))
+  call assert_equal('r--r--r--', getfperm('Xtest'))
+
+  call assert_fails("setfperm('Xtest', '---')")
+
+  call assert_equal(1, setfperm('Xtest', 'rwx------'))
+  call delete('Xtest')
+endfunc
index 8b5385e928a2f1db92e4e5c7dcb801e9b43ad980..f39635e707c11aa5ede82b7b88568122996600df 100644 (file)
@@ -743,6 +743,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1516,
 /**/
     1515,
 /**/