tempfile.c: refactor vim_settempdir

- return result of setting and remove directory if the setting was not
  successful.
- don't do `STRCPY` in case of `vim_FullName` failure because
  `vim_FullName` already did it.
This commit is contained in:
Pavel Platto 2014-06-25 23:13:39 +03:00 committed by Nicolas Hillegeer
parent 0e49e16c4e
commit 8e2b570d6f

View File

@ -31,17 +31,22 @@ static void vim_maketempdir(void)
for (size_t i = 0; i < sizeof(temp_dirs) / sizeof(char *); ++i) { for (size_t i = 0; i < sizeof(temp_dirs) / sizeof(char *); ++i) {
// Expand environment variables, leave room for "/nvimXXXXXX/999999999" // Expand environment variables, leave room for "/nvimXXXXXX/999999999"
expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22); expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22);
if (os_isdir(itmp)) { // directory exists if (!os_isdir(itmp)) { // directory doesn't exist
add_pathsep(itmp); continue;
}
// Concatenate with temporary directory name pattern add_pathsep(itmp);
STRCAT(itmp, "nvimXXXXXX"); // Concatenate with temporary directory name pattern
if (os_mkdtemp((char *)itmp) != NULL) { STRCAT(itmp, "nvimXXXXXX");
vim_settempdir(itmp); if (!os_mkdtemp((char *)itmp)) {
} continue;
if (vim_tempdir != NULL) { }
break; if (vim_settempdir(itmp)) {
} // Successfully created and set temporary directory so stop trying.
break;
} else {
// Couldn't set `vim_tempdir` to itmp so remove created directory.
os_rmdir((char *)itmp);
} }
} }
} }
@ -85,17 +90,19 @@ char_u *vim_gettempdir(void)
/// `vim_tempdir`. This avoids that using `:cd` would confuse us. /// `vim_tempdir`. This avoids that using `:cd` would confuse us.
/// ///
/// @param tempdir must be no longer than MAXPATHL. /// @param tempdir must be no longer than MAXPATHL.
static void vim_settempdir(char_u *tempdir) ///
/// @return false if we run out of memory.
static bool vim_settempdir(char_u *tempdir)
{ {
char_u *buf = verbose_try_malloc((size_t)MAXPATHL + 2); char_u *buf = verbose_try_malloc((size_t)MAXPATHL + 2);
if (buf) { if (!buf) {
if (vim_FullName(tempdir, buf, MAXPATHL, false) == FAIL) { return false;
STRCPY(buf, tempdir);
}
add_pathsep(buf);
vim_tempdir = vim_strsave(buf);
free(buf);
} }
vim_FullName(tempdir, buf, MAXPATHL, false);
add_pathsep(buf);
vim_tempdir = vim_strsave(buf);
free(buf);
return true;
} }
/// Return a unique name that can be used for a temp file. /// Return a unique name that can be used for a temp file.