diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2021-08-27 18:08:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-27 18:08:20 -0700 |
commit | ff7f7dd26b45164a41dbc36799cb7b08ebb9f66b (patch) | |
tree | da748f3a399eab5083d9a077ad0c9faf31b3a8f8 /src | |
parent | 32024787b6b1ef39c23bda1653c23b5dfbde9e81 (diff) | |
parent | e5d464d8e04e0745dfcbd9fc8b0e06d66cfffcbe (diff) | |
download | rneovim-ff7f7dd26b45164a41dbc36799cb7b08ebb9f66b.tar.gz rneovim-ff7f7dd26b45164a41dbc36799cb7b08ebb9f66b.tar.bz2 rneovim-ff7f7dd26b45164a41dbc36799cb7b08ebb9f66b.zip |
Merge #15433 defaults: auto-create backup dir
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/fileio.c | 38 | ||||
-rw-r--r-- | src/nvim/memline.c | 2 | ||||
-rw-r--r-- | src/nvim/option.c | 8 | ||||
-rw-r--r-- | src/nvim/undo.c | 10 |
4 files changed, 46 insertions, 12 deletions
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 3d058e1d09..92b48c36cb 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2688,9 +2688,22 @@ buf_write( /* * Isolate one directory name, using an entry in 'bdir'. */ - (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); - p = IObuff + STRLEN(IObuff); - if (after_pathsep((char *)IObuff, (char *)p) && p[-1] == p[-2]) { + size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); + p = IObuff + dir_len; + bool trailing_pathseps = after_pathsep((char *)IObuff, (char *)p) && p[-1] == p[-2]; + if (trailing_pathseps) { + IObuff[dir_len - 2] = NUL; + } + if (*dirp == NUL && !os_isdir(IObuff)) { + int ret; + char *failed_dir; + if ((ret = os_mkdir_recurse((char *)IObuff, 0755, &failed_dir)) != 0) { + EMSG3(_("E303: Unable to create directory \"%s\" for backup file: %s"), + failed_dir, os_strerror(ret)); + xfree(failed_dir); + } + } + if (trailing_pathseps) { // Ends with '//', Use Full path if ((p = (char_u *)make_percent_swname((char *)IObuff, (char *)fname)) != NULL) { @@ -2840,9 +2853,22 @@ nobackup: /* * Isolate one directory name and make the backup file name. */ - (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); - p = IObuff + STRLEN(IObuff); - if (after_pathsep((char *)IObuff, (char *)p) && p[-1] == p[-2]) { + size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); + p = IObuff + dir_len; + bool trailing_pathseps = after_pathsep((char *)IObuff, (char *)p) && p[-1] == p[-2]; + if (trailing_pathseps) { + IObuff[dir_len - 2] = NUL; + } + if (*dirp == NUL && !os_isdir(IObuff)) { + int ret; + char *failed_dir; + if ((ret = os_mkdir_recurse((char *)IObuff, 0755, &failed_dir)) != 0) { + EMSG3(_("E303: Unable to create directory \"%s\" for backup file: %s"), + failed_dir, os_strerror(ret)); + xfree(failed_dir); + } + } + if (trailing_pathseps) { // path ends with '//', use full path if ((p = (char_u *)make_percent_swname((char *)IObuff, (char *)fname)) != NULL) { diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 4d435bc99f..230361b997 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -1442,7 +1442,7 @@ recover_names ( * Append the full path to name with path separators made into percent * signs, to dir. An unnamed buffer is handled as "" (<currentdir>/"") */ -char *make_percent_swname(const char *dir, char *name) +char *make_percent_swname(const char *dir, const char *name) FUNC_ATTR_NONNULL_ARG(1) { char *d = NULL; diff --git a/src/nvim/option.c b/src/nvim/option.c index f7635fe5ec..d11bbc8ecc 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -483,17 +483,17 @@ void set_init_1(bool clean_arg) #endif false); - char *backupdir = stdpaths_user_data_subpath("backup", 0, true); + char *backupdir = stdpaths_user_data_subpath("backup", 2, true); const size_t backupdir_len = strlen(backupdir); backupdir = xrealloc(backupdir, backupdir_len + 3); memmove(backupdir + 2, backupdir, backupdir_len + 1); memmove(backupdir, ".,", 2); - set_string_default("viewdir", stdpaths_user_data_subpath("view", 0, true), - true); set_string_default("backupdir", backupdir, true); + set_string_default("viewdir", stdpaths_user_data_subpath("view", 2, true), + true); set_string_default("directory", stdpaths_user_data_subpath("swap", 2, true), true); - set_string_default("undodir", stdpaths_user_data_subpath("undo", 0, true), + set_string_default("undodir", stdpaths_user_data_subpath("undo", 2, true), true); // Set default for &runtimepath. All necessary expansions are performed in // this function. diff --git a/src/nvim/undo.c b/src/nvim/undo.c index e1a7dbb2d3..fb96d7e6ff 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -672,6 +672,7 @@ char *u_get_undo_file_name(const char *const buf_ffname, const bool reading) #ifdef HAVE_READLINK char fname_buf[MAXPATHL]; #endif + char *p; if (ffname == NULL) { return NULL; @@ -704,6 +705,13 @@ char *u_get_undo_file_name(const char *const buf_ffname, const bool reading) memmove(tail + tail_len + 1, ".un~", sizeof(".un~")); } else { dir_name[dir_len] = NUL; + + // Remove trailing pathseps from directory name + p = &dir_name[dir_len - 1]; + while (vim_ispathsep(*p)) { + *p-- = NUL; + } + bool has_directory = os_isdir((char_u *)dir_name); if (!has_directory && *dirp == NUL && !reading) { // Last directory in the list does not exist, create it. @@ -720,7 +728,7 @@ char *u_get_undo_file_name(const char *const buf_ffname, const bool reading) if (has_directory) { if (munged_name == NULL) { munged_name = xstrdup(ffname); - for (char *p = munged_name; *p != NUL; MB_PTR_ADV(p)) { + for (p = munged_name; *p != NUL; MB_PTR_ADV(p)) { if (vim_ispathsep(*p)) { *p = '%'; } |