mirror of
https://github.com/neovim/neovim.git
synced 2025-10-04 17:06:30 +00:00
vim-patch:7.4.1107
Problem: Vim can create a directory but not delete it.
Solution: Add an argument to delete() to make it possible to delete a
directory, also recursively.
da440d21a6
This commit is contained in:
@@ -6702,7 +6702,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 +8374,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
|
||||
|
@@ -56,28 +56,47 @@ static void vim_maketempdir(void)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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_isdir(name)) {
|
||||
snprintf((char *)NameBuff, MAXPATHL, "%s/*", name);
|
||||
|
||||
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) == 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) {
|
||||
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);
|
||||
|
||||
// remove the trailing path separator
|
||||
path_tail(vim_tempdir)[-1] = NUL;
|
||||
delete_recursive(vim_tempdir);
|
||||
xfree(vim_tempdir);
|
||||
vim_tempdir = NULL;
|
||||
}
|
||||
|
@@ -257,7 +257,7 @@ static int included_patches[] = {
|
||||
// 1110,
|
||||
// 1109 NA
|
||||
// 1108,
|
||||
// 1107,
|
||||
1107,
|
||||
// 1106 NA
|
||||
1105,
|
||||
// 1104 NA
|
||||
|
Reference in New Issue
Block a user