Merge pull request #4367 from jbradaric/vim-7.4.1107

vim-patch:7.4.{1107,1114,1116,1117,1120}
This commit is contained in:
Justin M. Keyes
2016-04-22 04:11:45 -04:00
22 changed files with 325 additions and 188 deletions

View File

@@ -29,7 +29,6 @@
#include "nvim/path.h"
#include "nvim/screen.h"
#include "nvim/strings.h"
#include "nvim/tempfile.h"
#include "nvim/undo.h"
#include "nvim/window.h"
#include "nvim/os/os.h"

View File

@@ -66,7 +66,6 @@
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tag.h"
#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/mouse.h"
#include "nvim/terminal.h"
@@ -6702,7 +6701,7 @@ static struct fst {
{ "cscope_connection", 0, 3, f_cscope_connection },
{ "cursor", 1, 3, f_cursor },
{ "deepcopy", 1, 2, f_deepcopy },
{ "delete", 1, 1, f_delete },
{ "delete", 1, 2, f_delete },
{ "dictwatcheradd", 3, 3, f_dictwatcheradd },
{ "dictwatcherdel", 3, 3, f_dictwatcherdel },
{ "did_filetype", 0, 0, f_did_filetype },
@@ -8374,15 +8373,42 @@ static void f_deepcopy(typval_T *argvars, typval_T *rettv)
}
}
/*
* "delete()" function
*/
// "delete()" function
static void f_delete(typval_T *argvars, typval_T *rettv)
{
if (check_restricted() || check_secure())
rettv->vval.v_number = -1;
else
rettv->vval.v_number = os_remove((char *)get_tv_string(&argvars[0]));
char_u nbuf[NUMBUFLEN];
char_u *name;
char_u *flags;
rettv->vval.v_number = -1;
if (check_restricted() || check_secure()) {
return;
}
name = get_tv_string(&argvars[0]);
if (name == NULL || *name == NUL) {
EMSG(_(e_invarg));
return;
}
if (argvars[1].v_type != VAR_UNKNOWN) {
flags = get_tv_string_buf(&argvars[1], nbuf);
} else {
flags = (char_u *)"";
}
if (*flags == NUL) {
// delete a file
rettv->vval.v_number = os_remove((char *)name) == 0 ? 0 : -1;
} else if (STRCMP(flags, "d") == 0) {
// delete an empty directory
rettv->vval.v_number = os_rmdir((char *)name) == 0 ? 0 : -1;
} else if (STRCMP(flags, "rf") == 0) {
// delete a directory recursively
rettv->vval.v_number = delete_recursive(name);
} else {
EMSG2(_(e_invexpr2), flags);
}
}
// dictwatcheradd(dict, key, funcref) function

View File

@@ -50,7 +50,6 @@
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tag.h"
#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/window.h"

View File

@@ -45,7 +45,6 @@
#include "nvim/search.h"
#include "nvim/sha256.h"
#include "nvim/strings.h"
#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/types.h"
#include "nvim/undo.h"
@@ -5116,6 +5115,147 @@ void forward_slash(char_u *fname)
}
#endif
/// Name of Vim's own temp dir. Ends in a slash.
static char_u *vim_tempdir = NULL;
/// Create a directory for private use by this instance of Neovim.
/// This is done once, and the same directory is used for all temp files.
/// This method avoids security problems because of symlink attacks et al.
/// It's also a bit faster, because we only need to check for an existing
/// file when creating the directory and not for each temp file.
static void vim_maketempdir(void)
{
static const char *temp_dirs[] = TEMP_DIR_NAMES;
// Try the entries in `TEMP_DIR_NAMES` to create the temp directory.
char_u template[TEMP_FILE_PATH_MAXLEN];
char_u path[TEMP_FILE_PATH_MAXLEN];
for (size_t i = 0; i < ARRAY_SIZE(temp_dirs); i++) {
// Expand environment variables, leave room for "/nvimXXXXXX/999999999"
expand_env((char_u *)temp_dirs[i], template, TEMP_FILE_PATH_MAXLEN - 22);
if (!os_isdir(template)) { // directory doesn't exist
continue;
}
add_pathsep((char *)template);
// Concatenate with temporary directory name pattern
STRCAT(template, "nvimXXXXXX");
if (os_mkdtemp((const char *)template, (char *)path) != 0) {
continue;
}
if (vim_settempdir((char *)path)) {
// Successfully created and set temporary directory so stop trying.
break;
} else {
// Couldn't set `vim_tempdir` to `path` so remove created directory.
os_rmdir((char *)path);
}
}
}
/// Delete "name" and everything in it, recursively.
/// @param name The path which should be deleted.
/// @return 0 for success, -1 if some file was not deleted.
int delete_recursive(char_u *name)
{
int result = 0;
if (os_isrealdir(name)) {
snprintf((char *)NameBuff, MAXPATHL, "%s/*", name); // NOLINT
char_u **files;
int file_count;
char_u *exp = vim_strsave(NameBuff);
if (gen_expand_wildcards(1, &exp, &file_count, &files,
EW_DIR | EW_FILE | EW_SILENT | EW_ALLLINKS
| EW_DODOT | EW_EMPTYOK) == OK) {
for (int i = 0; i < file_count; i++) {
if (delete_recursive(files[i]) != 0) {
result = -1;
}
}
FreeWild(file_count, files);
} else {
result = -1;
}
xfree(exp);
os_rmdir((char *)name);
} else {
result = os_remove((char *)name) == 0 ? 0 : -1;
}
return result;
}
/// Delete the temp directory and all files it contains.
void vim_deltempdir(void)
{
if (vim_tempdir != NULL) {
// remove the trailing path separator
path_tail(vim_tempdir)[-1] = NUL;
delete_recursive(vim_tempdir);
xfree(vim_tempdir);
vim_tempdir = NULL;
}
}
/// Get the name of temp directory. This directory would be created on the first
/// call to this function.
char_u *vim_gettempdir(void)
{
if (vim_tempdir == NULL) {
vim_maketempdir();
}
return vim_tempdir;
}
/// Set Neovim own temporary directory name to `tempdir`. This directory should
/// be already created. Expand this name to a full path and put it in
/// `vim_tempdir`. This avoids that using `:cd` would confuse us.
///
/// @param tempdir must be no longer than MAXPATHL.
///
/// @return false if we run out of memory.
static bool vim_settempdir(char *tempdir)
{
char *buf = verbose_try_malloc(MAXPATHL + 2);
if (!buf) {
return false;
}
vim_FullName(tempdir, buf, MAXPATHL, false);
add_pathsep(buf);
vim_tempdir = (char_u *)xstrdup(buf);
xfree(buf);
return true;
}
/// Return a unique name that can be used for a temp file.
///
/// @note The temp file is NOT created.
///
/// @return pointer to the temp file name or NULL if Neovim can't create
/// temporary directory for its own temporary files.
char_u *vim_tempname(void)
{
// Temp filename counter.
static uint32_t temp_count;
char_u *tempdir = vim_gettempdir();
if (!tempdir) {
return NULL;
}
// There is no need to check if the file exists, because we own the directory
// and nobody else creates a file in it.
char_u template[TEMP_FILE_PATH_MAXLEN];
snprintf((char *)template, TEMP_FILE_PATH_MAXLEN,
"%s%" PRIu32, tempdir, temp_count++);
return vim_strsave(template);
}
/*
* Code for automatic commands.

View File

@@ -32,7 +32,6 @@
#include "nvim/syntax.h"
#include "nvim/ui.h"
#include "nvim/version.h"
#include "nvim/tempfile.h"
#include "nvim/os/os.h"
#include "nvim/os/input.h"

View File

@@ -27,7 +27,6 @@
#include "nvim/quickfix.h"
#include "nvim/strings.h"
#include "nvim/tag.h"
#include "nvim/tempfile.h"
#include "nvim/window.h"
#include "nvim/os/os.h"
#include "nvim/os/input.h"

View File

@@ -65,7 +65,6 @@
#include "nvim/strings.h"
#include "nvim/ui.h"
#include "nvim/version.h"
#include "nvim/tempfile.h"
#include "nvim/undo.h"
#include "nvim/window.h"
#include "nvim/os/os.h"

View File

@@ -43,7 +43,6 @@
#include "nvim/search.h"
#include "nvim/strings.h"
#include "nvim/tag.h"
#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/window.h"

View File

@@ -14,7 +14,7 @@
#include "nvim/vim.h"
#include "nvim/memory.h"
#include "nvim/log.h"
#include "nvim/tempfile.h"
#include "nvim/fileio.h"
#include "nvim/path.h"
#include "nvim/strings.h"

View File

@@ -59,6 +59,23 @@ int os_dirname(char_u *buf, size_t len)
return OK;
}
/// Check if the given path is a directory and not a symlink to a directory.
/// @return `true` if `name` is a directory and NOT a symlink to a directory.
/// `false` if `name` is not a directory or if an error occurred.
bool os_isrealdir(const char_u *name)
FUNC_ATTR_NONNULL_ALL
{
uv_fs_t request;
if (uv_fs_lstat(&fs_loop, &request, (char *)name, NULL) != kLibuvSuccess) {
return false;
}
if (S_ISLNK(request.statbuf.st_mode)) {
return false;
} else {
return S_ISDIR(request.statbuf.st_mode);
}
}
/// Check if the given path is a directory or not.
///
/// @return `true` if `fname` is a directory.

View File

@@ -34,7 +34,6 @@
#include "nvim/screen.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tempfile.h"
#include "nvim/ui.h"
#include "nvim/types.h"
#include "nvim/os/os.h"

View File

@@ -604,7 +604,7 @@ static size_t do_path_expand(garray_T *gap, const char_u *path,
starstar = true;
// convert the file pattern to a regexp pattern
int starts_with_dot = (*s == '.');
int starts_with_dot = *s == '.';
char_u *pat = file_pat_to_reg_pat(s, e, NULL, false);
if (pat == NULL) {
xfree(buf);
@@ -647,9 +647,12 @@ static size_t do_path_expand(garray_T *gap, const char_u *path,
if (os_file_is_readable(dirpath) && os_scandir(&dir, dirpath)) {
// Find all matching entries.
char_u *name;
scandir_next_with_dots(NULL /* initialize */);
while((name = (char_u *) scandir_next_with_dots(&dir)) && name != NULL) {
if ((name[0] != '.' || starts_with_dot)
scandir_next_with_dots(NULL); // initialize
while ((name = (char_u *) scandir_next_with_dots(&dir)) && name != NULL) {
if ((name[0] != '.'
|| starts_with_dot
|| ((flags & EW_DODOT)
&& name[1] != NUL && (name[1] != '.' || name[2] != NUL)))
&& ((regmatch.regprog != NULL && vim_regexec(&regmatch, name, 0))
|| ((flags & EW_NOTWILD)
&& fnamencmp(path + (s - buf), name, e - s) == 0))) {
@@ -1220,7 +1223,7 @@ int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file,
recursive = false;
return (ga.ga_data != NULL) ? OK : FAIL;
return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? OK : FAIL;
}

View File

@@ -21,6 +21,8 @@
/* Note: mostly EW_NOTFOUND and EW_SILENT are mutually exclusive: EW_NOTFOUND
* is used when executing commands and EW_SILENT for interactive expanding. */
#define EW_ALLLINKS 0x1000 // also links not pointing to existing file
#define EW_DODOT 0x4000 // also files starting with a dot
#define EW_EMPTYOK 0x8000 // no matches is not an error
/// Return value for the comparison of two files. Also @see path_full_compare.
typedef enum file_comparison {

View File

@@ -39,7 +39,6 @@
#include "nvim/search.h"
#include "nvim/strings.h"
#include "nvim/ui.h"
#include "nvim/tempfile.h"
#include "nvim/window.h"
#include "nvim/os/os.h"
#include "nvim/os/input.h"

View File

@@ -319,7 +319,6 @@
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/ui.h"
#include "nvim/tempfile.h"
#include "nvim/undo.h"
#include "nvim/os/os.h"
#include "nvim/os/input.h"

View File

@@ -1,139 +0,0 @@
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "nvim/ascii.h"
#include "nvim/memory.h"
#include "nvim/misc1.h"
#include "nvim/os/os.h"
#include "nvim/os/os_defs.h"
#include "nvim/path.h"
#include "nvim/strings.h"
#include "nvim/tempfile.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "tempfile.c.generated.h"
#endif
/// Name of Vim's own temp dir. Ends in a slash.
static char_u *vim_tempdir = NULL;
/// Create a directory for private use by this instance of Neovim.
/// This is done once, and the same directory is used for all temp files.
/// This method avoids security problems because of symlink attacks et al.
/// It's also a bit faster, because we only need to check for an existing
/// file when creating the directory and not for each temp file.
static void vim_maketempdir(void)
{
static const char *temp_dirs[] = TEMP_DIR_NAMES;
// Try the entries in `TEMP_DIR_NAMES` to create the temp directory.
char_u template[TEMP_FILE_PATH_MAXLEN];
char_u path[TEMP_FILE_PATH_MAXLEN];
for (size_t i = 0; i < ARRAY_SIZE(temp_dirs); ++i) {
// Expand environment variables, leave room for "/nvimXXXXXX/999999999"
// Skip the directory check if the expansion fails.
expand_env((char_u *)temp_dirs[i], template, TEMP_FILE_PATH_MAXLEN - 22);
if (template[0] == '$' || !os_isdir(template)) {
continue;
}
add_pathsep((char *)template);
// Concatenate with temporary directory name pattern
STRCAT(template, "nvimXXXXXX");
if (os_mkdtemp((const char *)template, (char *)path) != 0) {
continue;
}
if (vim_settempdir((char *)path)) {
// Successfully created and set temporary directory so stop trying.
break;
} else {
// Couldn't set `vim_tempdir` to `path` so remove created directory.
os_rmdir((char *)path);
}
}
}
/// Delete the temp directory and all files it contains.
void vim_deltempdir(void)
{
if (vim_tempdir != NULL) {
snprintf((char *)NameBuff, MAXPATHL, "%s*", vim_tempdir);
char_u **files;
int file_count;
// Note: We cannot just do `&NameBuff` because it is a statically
// sized array so `NameBuff == &NameBuff` according to C semantics.
char_u *buff_list[1] = {NameBuff};
if (gen_expand_wildcards(1, buff_list, &file_count, &files,
EW_DIR|EW_FILE|EW_SILENT) == OK) {
for (int i = 0; i < file_count; ++i) {
os_remove((char *)files[i]);
}
FreeWild(file_count, files);
}
path_tail(NameBuff)[-1] = NUL;
os_rmdir((char *)NameBuff);
xfree(vim_tempdir);
vim_tempdir = NULL;
}
}
/// Get the name of temp directory. This directory would be created on the first
/// call to this function.
char_u *vim_gettempdir(void)
{
if (vim_tempdir == NULL) {
vim_maketempdir();
}
return vim_tempdir;
}
/// Set Neovim own temporary directory name to `tempdir`. This directory should
/// be already created. Expand this name to a full path and put it in
/// `vim_tempdir`. This avoids that using `:cd` would confuse us.
///
/// @param tempdir must be no longer than MAXPATHL.
///
/// @return false if we run out of memory.
static bool vim_settempdir(char *tempdir)
{
char *buf = verbose_try_malloc(MAXPATHL + 2);
if (!buf) {
return false;
}
vim_FullName(tempdir, buf, MAXPATHL, false);
add_pathsep(buf);
vim_tempdir = (char_u *)xstrdup(buf);
xfree(buf);
return true;
}
/// Return a unique name that can be used for a temp file.
///
/// @note The temp file is NOT created.
///
/// @return pointer to the temp file name or NULL if Neovim can't create
/// temporary directory for its own temporary files.
char_u *vim_tempname(void)
{
// Temp filename counter.
static uint32_t temp_count;
char_u *tempdir = vim_gettempdir();
if (!tempdir) {
return NULL;
}
// There is no need to check if the file exists, because we own the directory
// and nobody else creates a file in it.
char_u template[TEMP_FILE_PATH_MAXLEN];
snprintf((char *)template, TEMP_FILE_PATH_MAXLEN,
"%s%" PRIu32, tempdir, temp_count++);
return vim_strsave(template);
}

View File

@@ -1,8 +0,0 @@
#ifndef NVIM_TEMPFILE_H
#define NVIM_TEMPFILE_H
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "tempfile.h.generated.h"
#endif
#endif // NVIM_TEMPFILE_H

View File

@@ -557,20 +557,20 @@ static int included_patches[] = {
// 1123,
// 1122 NA
// 1121,
// 1120,
1120,
// 1119,
// 1118,
// 1117,
// 1116,
1117,
1116,
// 1115 NA
// 1114,
1114,
1113,
1112,
// 1111,
// 1110,
// 1109 NA
// 1108,
// 1107,
1107,
// 1106 NA
1105,
// 1104 NA