diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-08-09 11:28:47 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-08-09 11:29:09 -0300 |
commit | 17e6000007b820f4b4986e48c7274ce460daadf1 (patch) | |
tree | e76e5f02c9baaecce742ec55c7ed36de0a05f4f3 /src | |
parent | 19f44fda8bc74abdcd13deca47be99b151f69239 (diff) | |
parent | 2838f2c3a62f3d6daeb071fee8f3260ac36bb42f (diff) | |
download | rneovim-17e6000007b820f4b4986e48c7274ce460daadf1.tar.gz rneovim-17e6000007b820f4b4986e48c7274ce460daadf1.tar.bz2 rneovim-17e6000007b820f4b4986e48c7274ce460daadf1.zip |
Merge pull request #1034 'Implement `os_mkdtemp` on top of `uv_fs_mkdtemp`'
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/os/fs.c | 30 | ||||
-rw-r--r-- | src/nvim/tempfile.c | 27 |
2 files changed, 28 insertions, 29 deletions
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 46aea2bf36..4820a4d165 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -3,13 +3,6 @@ #include <assert.h> -// TODO(hinidu): remove after implementing `os_mkdtemp` on top of libuv -#ifdef WIN32 -# include <io.h> -#else -# include <stdlib.h> -#endif - #include "nvim/os/os.h" #include "nvim/ascii.h" #include "nvim/memory.h" @@ -293,18 +286,21 @@ int os_mkdir(const char *path, int32_t mode) } /// Create a unique temporary directory. -/// TODO(hinidu): Implement on top of libuv. ref #850 /// -/// @param[in,out] template Template of the path to the directory with XXXXXX -/// which would be replaced by random chars. -/// @return Pointer to changed `template` for success, `NULL` for failure. -char *os_mkdtemp(char *template) +/// @param[in] template Template of the path to the directory with XXXXXX +/// which would be replaced by random chars. +/// @param[out] path Path to created directory for success, undefined for +/// failure. +/// @return `0` for success, non-zero for failure. +int os_mkdtemp(const char *template, char *path) { -#ifdef WIN32 - return _mktemp(template) && os_mkdir(template, 0700) == 0 ? template : NULL; -#else - return mkdtemp(template); -#endif + uv_fs_t request; + int result = uv_fs_mkdtemp(uv_default_loop(), &request, template, NULL); + if (result == kLibuvSuccess) { + strcpy(path, request.path); + } + uv_fs_req_cleanup(&request); + return result; } /// Remove a directory. diff --git a/src/nvim/tempfile.c b/src/nvim/tempfile.c index c25da77717..33d6f0f37d 100644 --- a/src/nvim/tempfile.c +++ b/src/nvim/tempfile.c @@ -28,26 +28,29 @@ 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 itmp[TEMP_FILE_PATH_MAXLEN]; + char_u template[TEMP_FILE_PATH_MAXLEN]; + char_u path[TEMP_FILE_PATH_MAXLEN]; for (size_t i = 0; i < sizeof(temp_dirs) / sizeof(char *); ++i) { // Expand environment variables, leave room for "/nvimXXXXXX/999999999" - expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22); - if (!os_isdir(itmp)) { // directory doesn't exist + expand_env((char_u *)temp_dirs[i], template, TEMP_FILE_PATH_MAXLEN - 22); + if (!os_isdir(template)) { // directory doesn't exist continue; } - add_pathsep(itmp); + add_pathsep(template); // Concatenate with temporary directory name pattern - STRCAT(itmp, "nvimXXXXXX"); - if (!os_mkdtemp((char *)itmp)) { + STRCAT(template, "nvimXXXXXX"); + + if (os_mkdtemp((const char *)template, (char *)path) != 0) { continue; } - if (vim_settempdir(itmp)) { + + if (vim_settempdir(path)) { // 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); + // Couldn't set `vim_tempdir` to `path` so remove created directory. + os_rmdir((char *)path); } } } @@ -128,8 +131,8 @@ char_u *vim_tempname(void) // 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 itmp[TEMP_FILE_PATH_MAXLEN]; - snprintf((char *)itmp, TEMP_FILE_PATH_MAXLEN, + char_u template[TEMP_FILE_PATH_MAXLEN]; + snprintf((char *)template, TEMP_FILE_PATH_MAXLEN, "%s%" PRIu32, tempdir, temp_count++); - return vim_strsave(itmp); + return vim_strsave(template); } |