From 8b91ba929b5d178ed36423c6fac27debfa3bdf38 Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Sun, 15 Jun 2014 22:18:43 +0300 Subject: Temporary os_mkdtemp implementation. Use it instead of mkdtemp. --- src/nvim/fileio.c | 2 +- src/nvim/os/fs.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index ea32e19b50..e4ef2bb014 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5254,7 +5254,7 @@ vim_tempname ( # ifdef HAVE_MKDTEMP /* Leave room for filename */ STRCAT(itmp, "vXXXXXX"); - if (mkdtemp((char *)itmp) != NULL) + if (os_mkdtemp((char *)itmp) != NULL) vim_settempdir(itmp); # else /* Get an arbitrary number of up to 6 digits. When it's diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index dce95eb3c9..46aea2bf36 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -3,6 +3,13 @@ #include +// TODO(hinidu): remove after implementing `os_mkdtemp` on top of libuv +#ifdef WIN32 +# include +#else +# include +#endif + #include "nvim/os/os.h" #include "nvim/ascii.h" #include "nvim/memory.h" @@ -285,6 +292,21 @@ int os_mkdir(const char *path, int32_t mode) return result; } +/// 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) +{ +#ifdef WIN32 + return _mktemp(template) && os_mkdir(template, 0700) == 0 ? template : NULL; +#else + return mkdtemp(template); +#endif +} + /// Remove a directory. /// /// @return `0` for success, non-zero for failure. -- cgit From f9710fba5243a87703e551052ae1fad1a787b015 Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Tue, 17 Jun 2014 12:33:32 +0300 Subject: Remove HAVE_MKDTEMP For now we provide simple `mkdtemp` for Windows, in the future we will use libuv for that. --- src/nvim/fileio.c | 53 ----------------------------------------------------- 1 file changed, 53 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index e4ef2bb014..c1ba9c89ff 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5240,68 +5240,15 @@ vim_tempname ( * Try the entries in TEMPDIRNAMES to create the temp directory. */ for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) { -# ifndef HAVE_MKDTEMP - size_t itmplen; - long nr; - long off; -# endif - /* expand $TMP, leave room for "/v1100000/999999999" */ expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); if (os_isdir(itmp)) { /* directory exists */ add_pathsep(itmp); -# ifdef HAVE_MKDTEMP /* Leave room for filename */ STRCAT(itmp, "vXXXXXX"); if (os_mkdtemp((char *)itmp) != NULL) vim_settempdir(itmp); -# else - /* Get an arbitrary number of up to 6 digits. When it's - * unlikely that it already exists it will be faster, - * otherwise it doesn't matter. The use of mkdir() avoids any - * security problems because of the predictable number. */ - nr = (os_get_pid() + (long)time(NULL)) % 1000000L; - itmplen = STRLEN(itmp); - - /* Try up to 10000 different values until we find a name that - * doesn't exist. */ - for (off = 0; off < 10000L; ++off) { - int r; -# if defined(UNIX) - mode_t umask_save; -# endif - - sprintf((char *)itmp + itmplen, "v%" PRId64, (int64_t)(nr + off)); -# ifndef EEXIST - /* If mkdir() does not set errno to EEXIST, check for - * existing file here. There is a race condition then, - * although it's fail-safe. */ - if (os_file_exists(itmp)) - continue; -# endif -# if defined(UNIX) - /* Make sure the umask doesn't remove the executable bit. - * "repl" has been reported to use "177". */ - umask_save = umask(077); -# endif - r = os_mkdir((char *)itmp, 0700); -# if defined(UNIX) - (void)umask(umask_save); -# endif - if (r == 0) { - vim_settempdir(itmp); - break; - } -# ifdef EEXIST - /* If the mkdir() didn't fail because the file/dir exists, - * we probably can't create any dir here, try another - * place. */ - if (errno != EEXIST) -# endif - break; - } -# endif /* HAVE_MKDTEMP */ if (vim_tempdir != NULL) break; } -- cgit From ed10eb6fa92989d5a3841bf225a38b524c910e2e Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Tue, 17 Jun 2014 12:41:55 +0300 Subject: Remove USE_TMPNAM tmpnam() is deprecated. --- src/nvim/fileio.c | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index c1ba9c89ff..e9a1545846 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5218,11 +5218,7 @@ vim_tempname ( int extra_char /* char to use in the name instead of '?' */ ) { -#ifdef USE_TMPNAM - char_u itmp[L_tmpnam]; /* use tmpnam() */ -#else char_u itmp[TEMPNAMELEN]; -#endif #ifdef TEMPDIRNAMES static char *(tempdirs[]) = {TEMPDIRNAMES}; @@ -5266,15 +5262,6 @@ vim_tempname ( #else /* TEMPDIRNAMES */ - -# ifdef USE_TMPNAM - char_u *p; - - /* tmpnam() will make its own name */ - p = tmpnam((char *)itmp); - if (p == NULL || *p == NUL) - return NULL; -# else char_u *p; STRCPY(itmp, TEMPNAME); @@ -5282,7 +5269,6 @@ vim_tempname ( *p = extra_char; if (mktemp((char *)itmp) == NULL) return NULL; -# endif return vim_strsave(itmp); #endif /* TEMPDIRNAMES */ -- cgit From edd7a8c5ddd99bd0c02b4218d43bccb562809d55 Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Thu, 19 Jun 2014 00:58:04 +0300 Subject: Remove #ifdefs TEMPDIRNAMES and add TEMPDIRNAMES for Windows Vim does not define TEMPDIRNAMES for all systems, but it is defined for all systems supported by Neovim. Temporary directory names for Windows was obtained from GetTempPath() function documentation at MSDN. Additionally small renamings were performed. --- src/nvim/diff.c | 4 ---- src/nvim/fileio.c | 29 +++++------------------------ src/nvim/globals.h | 2 -- src/nvim/memline.c | 2 -- src/nvim/os/os.h | 7 +++++++ src/nvim/os/unix_defs.h | 7 +++++++ src/nvim/os/win_defs.h | 9 +++++++++ src/nvim/os_unix_defs.h | 3 --- 8 files changed, 28 insertions(+), 35 deletions(-) create mode 100644 src/nvim/os/unix_defs.h create mode 100644 src/nvim/os/win_defs.h (limited to 'src') diff --git a/src/nvim/diff.c b/src/nvim/diff.c index e7feacd4fb..0ae3d5553e 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -893,15 +893,11 @@ void ex_diffpatch(exarg_T *eap) || (os_chdir((char *)dirbuf) != 0)) { dirbuf[0] = NUL; } else { -# ifdef TEMPDIRNAMES if (vim_tempdir != NULL) { ignored = os_chdir((char *)vim_tempdir); } else { ignored = os_chdir("/tmp"); } -# else - ignored = os_chdir("/tmp"); -# endif // ifdef TEMPDIRNAMES shorten_fnames(TRUE); } #endif // ifdef UNIX diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index e9a1545846..8dcc066258 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5156,7 +5156,6 @@ void write_lnum_adjust(linenr_T offset) curbuf->b_no_eol_lnum += offset; } -#if defined(TEMPDIRNAMES) || defined(PROTO) static long temp_count = 0; /* Temp filename counter. */ /* @@ -5184,9 +5183,6 @@ void vim_deltempdir(void) } } -#endif - -#ifdef TEMPDIRNAMES /* * Directory "tempdir" was created. Expand this name to a full path and put * it in "vim_tempdir". This avoids that using ":cd" would confuse us. @@ -5203,7 +5199,6 @@ static void vim_settempdir(char_u *tempdir) free(buf); } } -#endif /* * vim_tempname(): Return a unique name that can be used for a temp file. @@ -5218,10 +5213,9 @@ vim_tempname ( int extra_char /* char to use in the name instead of '?' */ ) { - char_u itmp[TEMPNAMELEN]; + char_u itmp[TEMP_FILE_PATH_MAXLEN]; -#ifdef TEMPDIRNAMES - static char *(tempdirs[]) = {TEMPDIRNAMES}; + static const char *temp_dirs[] = TEMP_DIR_NAMES; int i; /* @@ -5233,11 +5227,11 @@ vim_tempname ( */ if (vim_tempdir == NULL) { /* - * Try the entries in TEMPDIRNAMES to create the temp directory. + * Try the entries in `TEMP_DIR_NAMES` to create the temp directory. */ - for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) { + for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) { /* expand $TMP, leave room for "/v1100000/999999999" */ - expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); + expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 20); if (os_isdir(itmp)) { /* directory exists */ add_pathsep(itmp); @@ -5259,19 +5253,6 @@ vim_tempname ( } return NULL; - -#else /* TEMPDIRNAMES */ - - char_u *p; - - STRCPY(itmp, TEMPNAME); - if ((p = vim_strchr(itmp, '?')) != NULL) - *p = extra_char; - if (mktemp((char *)itmp) == NULL) - return NULL; - - return vim_strsave(itmp); -#endif /* TEMPDIRNAMES */ } #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 871674907b..436ddb768d 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -536,10 +536,8 @@ EXTERN int ru_col; /* column for ruler */ EXTERN int ru_wid; /* 'rulerfmt' width of ruler when non-zero */ EXTERN int sc_col; /* column for shown command */ -#ifdef TEMPDIRNAMES EXTERN char_u *vim_tempdir INIT(= NULL); /* Name of Vim's own temp dir. Ends in a slash. */ -#endif /* * When starting or exiting some things are done differently (e.g. screen diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 2fb404a148..f951eb9379 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -589,9 +589,7 @@ void ml_close_all(int del_file) ml_close(buf, del_file && ((buf->b_flags & BF_PRESERVED) == 0 || vim_strchr(p_cpo, CPO_PRESERVE) == NULL)); spell_delete_wordlist(); /* delete the internal wordlist */ -#ifdef TEMPDIRNAMES vim_deltempdir(); /* delete created temp directory */ -#endif } /* diff --git a/src/nvim/os/os.h b/src/nvim/os/os.h index b6148e72bf..b46989d9b4 100644 --- a/src/nvim/os/os.h +++ b/src/nvim/os/os.h @@ -7,10 +7,17 @@ #include "nvim/os/fs_defs.h" #include "nvim/vim.h" +#ifdef WIN32 +# include "nvim/os/win_defs.h" +#else +# include "nvim/os/unix_defs.h" +#endif + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "os/fs.h.generated.h" # include "os/mem.h.generated.h" # include "os/env.h.generated.h" # include "os/users.h.generated.h" #endif + #endif // NVIM_OS_OS_H diff --git a/src/nvim/os/unix_defs.h b/src/nvim/os/unix_defs.h new file mode 100644 index 0000000000..197a87bcc5 --- /dev/null +++ b/src/nvim/os/unix_defs.h @@ -0,0 +1,7 @@ +#ifndef NEOVIM_OS_UNIX_DEFS_H +#define NEOVIM_OS_UNIX_DEFS_H + +#define TEMP_DIR_NAMES {"$TMPDIR", "/tmp", ".", "$HOME"} +#define TEMP_FILE_PATH_MAXLEN 256 + +#endif // NEOVIM_OS_UNIX_DEFS_H diff --git a/src/nvim/os/win_defs.h b/src/nvim/os/win_defs.h new file mode 100644 index 0000000000..49fcb64777 --- /dev/null +++ b/src/nvim/os/win_defs.h @@ -0,0 +1,9 @@ +#ifndef NEOVIM_OS_WIN_DEFS_H +#define NEOVIM_OS_WIN_DEFS_H + +#include + +#define TEMP_DIR_NAMES {"$TMP", "$TEMP", "$USERPROFILE", ""} +#define TEMP_FILE_PATH_MAXLEN _MAX_PATH + +#endif // NEOVIM_OS_WIN_DEFS_H diff --git a/src/nvim/os_unix_defs.h b/src/nvim/os_unix_defs.h index 5ba9d2d98d..0d79117bfd 100644 --- a/src/nvim/os_unix_defs.h +++ b/src/nvim/os_unix_defs.h @@ -204,9 +204,6 @@ "~/.nvim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,~/.nvim/after" # endif -# define TEMPDIRNAMES "$TMPDIR", "/tmp", ".", "$HOME" -# define TEMPNAMELEN 256 - /* Special wildcards that need to be handled by the shell */ #define SPECIAL_WILDCHAR "`'{" -- cgit From 29e0cd1571b773feb7fd61118930cdac7d83e38c Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Thu, 19 Jun 2014 23:46:51 +0300 Subject: Refactor vim_tempname - temp_count is uint32_t now instead of long because it supposed to be at most 999999999 (comment on line 5227) temporary files. The most probably it was a long for compatibility with systems where int is 16-bit. - Use "nvim" as prefix for temp folder name instead of "v" - Remove unused parameter from vim_tempname --- src/nvim/diff.c | 10 +++++----- src/nvim/eval.c | 18 ++---------------- src/nvim/ex_cmds.c | 6 +++--- src/nvim/fileio.c | 19 ++++++++----------- src/nvim/hardcopy.c | 2 +- src/nvim/if_cscope.c | 2 +- src/nvim/memline.c | 2 +- src/nvim/misc1.c | 2 +- src/nvim/os/server.c | 2 +- src/nvim/os_unix.c | 2 +- src/nvim/quickfix.c | 2 +- src/nvim/spell.c | 2 +- 12 files changed, 26 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 0ae3d5553e..37210e447c 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -661,9 +661,9 @@ void ex_diffupdate(exarg_T *eap) } // We need three temp file names. - char_u *tmp_orig = vim_tempname('o'); - char_u *tmp_new = vim_tempname('n'); - char_u *tmp_diff = vim_tempname('d'); + char_u *tmp_orig = vim_tempname(); + char_u *tmp_new = vim_tempname(); + char_u *tmp_diff = vim_tempname(); if ((tmp_orig == NULL) || (tmp_new == NULL) || (tmp_diff == NULL)) { goto theend; @@ -852,9 +852,9 @@ void ex_diffpatch(exarg_T *eap) #endif // ifdef UNIX // We need two temp file names. // Name of original temp file. - char_u *tmp_orig = vim_tempname('o'); + char_u *tmp_orig = vim_tempname(); // Name of patched temp file. - char_u *tmp_new = vim_tempname('n'); + char_u *tmp_new = vim_tempname(); if ((tmp_orig == NULL) || (tmp_new == NULL)) { goto theend; diff --git a/src/nvim/eval.c b/src/nvim/eval.c index eafdd2446c..7dc95bcded 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -14030,7 +14030,7 @@ static void f_system(typval_T *argvars, typval_T *rettv) * Write the string to a temp file, to be used for input of the shell * command. */ - if ((infile = vim_tempname('i')) == NULL) { + if ((infile = vim_tempname()) == NULL) { EMSG(_(e_notmp)); goto done; } @@ -14231,22 +14231,8 @@ static void f_taglist(typval_T *argvars, typval_T *rettv) */ static void f_tempname(typval_T *argvars, typval_T *rettv) { - static int x = 'A'; - rettv->v_type = VAR_STRING; - rettv->vval.v_string = vim_tempname(x); - - /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different - * names. Skip 'I' and 'O', they are used for shell redirection. */ - do { - if (x == 'Z') - x = '0'; - else if (x == '9') - x = 'A'; - else { - ++x; - } - } while (x == 'I' || x == 'O'); + rettv->vval.v_string = vim_tempname(); } /* diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 78325fd4a4..05ca5bbb58 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1032,8 +1032,8 @@ do_filter ( curbuf->b_op_start.lnum = line1; curbuf->b_op_end.lnum = line2; curwin->w_cursor.lnum = line2; - } else if ((do_in && (itmp = vim_tempname('i')) == NULL) - || (do_out && (otmp = vim_tempname('o')) == NULL)) { + } else if ((do_in && (itmp = vim_tempname()) == NULL) + || (do_out && (otmp = vim_tempname()) == NULL)) { EMSG(_(e_notmp)); goto filterend; } @@ -1601,7 +1601,7 @@ void write_viminfo(char_u *file, int forceit) */ if (fp_out == NULL) { free(tempname); - if ((tempname = vim_tempname('o')) != NULL) + if ((tempname = vim_tempname()) != NULL) fp_out = mch_fopen((char *)tempname, WRITEBIN); } diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 8dcc066258..b8adff3bca 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2146,7 +2146,7 @@ readfile_charconvert ( char_u *tmpname; char_u *errmsg = NULL; - tmpname = vim_tempname('r'); + tmpname = vim_tempname(); if (tmpname == NULL) errmsg = (char_u *)_("Can't find temp file for conversion"); else { @@ -3163,7 +3163,7 @@ nobackup: * overwrite the original file. */ if (*p_ccv != NUL) { - wfname = vim_tempname('w'); + wfname = vim_tempname(); if (wfname == NULL) { /* Can't write without a tempfile! */ errmsg = (char_u *)_("E214: Can't find temp file for writing"); goto restore_backup; @@ -5156,7 +5156,7 @@ void write_lnum_adjust(linenr_T offset) curbuf->b_no_eol_lnum += offset; } -static long temp_count = 0; /* Temp filename counter. */ +static uint32_t temp_count = 0; /* Temp filename counter. */ /* * Delete the temp directory and all files it contains. @@ -5208,10 +5208,7 @@ static void vim_settempdir(char_u *tempdir) * The returned pointer is to allocated memory. * The returned pointer is NULL if no valid name was found. */ -char_u * -vim_tempname ( - int extra_char /* char to use in the name instead of '?' */ -) +char_u *vim_tempname(void) { char_u itmp[TEMP_FILE_PATH_MAXLEN]; @@ -5230,13 +5227,13 @@ vim_tempname ( * Try the entries in `TEMP_DIR_NAMES` to create the temp directory. */ for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) { - /* expand $TMP, leave room for "/v1100000/999999999" */ - expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 20); + /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */ + expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22); if (os_isdir(itmp)) { /* directory exists */ add_pathsep(itmp); /* Leave room for filename */ - STRCAT(itmp, "vXXXXXX"); + STRCAT(itmp, "nvimXXXXXX"); if (os_mkdtemp((char *)itmp) != NULL) vim_settempdir(itmp); if (vim_tempdir != NULL) @@ -5248,7 +5245,7 @@ vim_tempname ( if (vim_tempdir != NULL) { /* There is no need to check if the file exists, because we own the * directory and nobody else creates a file in it. */ - sprintf((char *)itmp, "%s%" PRId64, vim_tempdir, (int64_t)temp_count++); + sprintf((char *)itmp, "%s%" PRIu32, vim_tempdir, temp_count++); return vim_strsave(itmp); } diff --git a/src/nvim/hardcopy.c b/src/nvim/hardcopy.c index e67cb67c69..024564f700 100644 --- a/src/nvim/hardcopy.c +++ b/src/nvim/hardcopy.c @@ -2346,7 +2346,7 @@ int mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit) /* If the user didn't specify a file name, use a temp file. */ if (psettings->outfile == NULL) { - prt_ps_file_name = vim_tempname('p'); + prt_ps_file_name = vim_tempname(); if (prt_ps_file_name == NULL) { EMSG(_(e_notmp)); return FAIL; diff --git a/src/nvim/if_cscope.c b/src/nvim/if_cscope.c index 9ce0eb7fa0..1b7202dbab 100644 --- a/src/nvim/if_cscope.c +++ b/src/nvim/if_cscope.c @@ -1044,7 +1044,7 @@ static int cs_find_common(char *opt, char *pat, int forceit, int verbose, int us if (qfpos != NULL && *qfpos != '0' && totmatches > 0) { /* fill error list */ FILE *f; - char_u *tmp = vim_tempname('c'); + char_u *tmp = vim_tempname(); qf_info_T *qi = NULL; win_T *wp = NULL; diff --git a/src/nvim/memline.c b/src/nvim/memline.c index f951eb9379..c41942301f 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -490,7 +490,7 @@ void ml_open_file(buf_T *buf) /* For a spell buffer use a temp file name. */ if (buf->b_spell) { - fname = vim_tempname('s'); + fname = vim_tempname(); if (fname != NULL) (void)mf_open_file(mfp, fname); /* consumes fname! */ buf->b_may_swap = FALSE; diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c index 3fc078cb8a..4e3879ddef 100644 --- a/src/nvim/misc1.c +++ b/src/nvim/misc1.c @@ -3423,7 +3423,7 @@ get_cmd_output ( return NULL; /* get a name for the temp file */ - if ((tempname = vim_tempname('o')) == NULL) { + if ((tempname = vim_tempname()) == NULL) { EMSG(_(e_notmp)); return NULL; } diff --git a/src/nvim/os/server.c b/src/nvim/os/server.c index 23f9393122..2494dfeb9f 100644 --- a/src/nvim/os/server.c +++ b/src/nvim/os/server.c @@ -52,7 +52,7 @@ void server_init(void) servers = pmap_new(cstr_t)(); if (!os_getenv("NEOVIM_LISTEN_ADDRESS")) { - char *listen_address = (char *)vim_tempname('s'); + char *listen_address = (char *)vim_tempname(); os_setenv("NEOVIM_LISTEN_ADDRESS", listen_address, 1); free(listen_address); } diff --git a/src/nvim/os_unix.c b/src/nvim/os_unix.c index 2c657bebeb..239d7d81db 100644 --- a/src/nvim/os_unix.c +++ b/src/nvim/os_unix.c @@ -1034,7 +1034,7 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file, /* * get a name for the temp file */ - if ((tempname = vim_tempname('o')) == NULL) { + if ((tempname = vim_tempname()) == NULL) { EMSG(_(e_notmp)); return FAIL; } diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index ec95c74fef..60bd15701d 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -2537,7 +2537,7 @@ static char_u *get_mef_name(void) static int off = 0; if (*p_mef == NUL) { - name = vim_tempname('e'); + name = vim_tempname(); if (name == NULL) EMSG(_(e_notmp)); return name; diff --git a/src/nvim/spell.c b/src/nvim/spell.c index c2354bb2c6..4000235304 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -7814,7 +7814,7 @@ spell_add_word ( if (idx == 0) { // use internal wordlist if (int_wordlist == NULL) { - int_wordlist = vim_tempname('s'); + int_wordlist = vim_tempname(); if (int_wordlist == NULL) return; } -- cgit From 8cfa7b3d15f503ff4fd4fc1df26bca109b31497b Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Sat, 21 Jun 2014 14:07:51 +0300 Subject: Add vim_gettempdir(), remove global vim_tempdir vim_gettempdir() and vim_maketempdir() was extracted from vim_tempname(). --- src/nvim/diff.c | 8 +++--- src/nvim/fileio.c | 77 +++++++++++++++++++++++++++++++----------------------- src/nvim/globals.h | 3 --- 3 files changed, 49 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 37210e447c..be4a923d14 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -893,11 +893,11 @@ void ex_diffpatch(exarg_T *eap) || (os_chdir((char *)dirbuf) != 0)) { dirbuf[0] = NUL; } else { - if (vim_tempdir != NULL) { - ignored = os_chdir((char *)vim_tempdir); - } else { - ignored = os_chdir("/tmp"); + char *tempdir = (char *)vim_gettempdir(); + if (tempdir == NULL) { + tempdir = "/tmp"; } + os_chdir(tempdir); shorten_fnames(TRUE); } #endif // ifdef UNIX diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index b8adff3bca..23609ab92f 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5156,8 +5156,41 @@ void write_lnum_adjust(linenr_T offset) curbuf->b_no_eol_lnum += offset; } +/* Name of Vim's own temp dir. Ends in a slash. */ +static char_u *vim_tempdir = NULL; static uint32_t temp_count = 0; /* Temp filename counter. */ +/* + * This will create a directory for private use by this instance of Vim. + * 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; + int i; + /* + * Try the entries in `TEMP_DIR_NAMES` to create the temp directory. + */ + char_u itmp[TEMP_FILE_PATH_MAXLEN]; + for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) { + /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */ + expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22); + if (os_isdir(itmp)) { /* directory exists */ + add_pathsep(itmp); + + /* Leave room for filename */ + STRCAT(itmp, "nvimXXXXXX"); + if (os_mkdtemp((char *)itmp) != NULL) + vim_settempdir(itmp); + if (vim_tempdir != NULL) + break; + } + } +} + /* * Delete the temp directory and all files it contains. */ @@ -5183,6 +5216,15 @@ void vim_deltempdir(void) } } +char_u *vim_gettempdir(void) +{ + if (vim_tempdir == NULL) { + vim_maketempdir(); + } + + return vim_tempdir; +} + /* * Directory "tempdir" was created. Expand this name to a full path and put * it in "vim_tempdir". This avoids that using ":cd" would confuse us. @@ -5212,40 +5254,11 @@ char_u *vim_tempname(void) { char_u itmp[TEMP_FILE_PATH_MAXLEN]; - static const char *temp_dirs[] = TEMP_DIR_NAMES; - int i; - - /* - * This will create a directory for private use by this instance of Vim. - * 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. - */ - if (vim_tempdir == NULL) { - /* - * Try the entries in `TEMP_DIR_NAMES` to create the temp directory. - */ - for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) { - /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */ - expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22); - if (os_isdir(itmp)) { /* directory exists */ - add_pathsep(itmp); - - /* Leave room for filename */ - STRCAT(itmp, "nvimXXXXXX"); - if (os_mkdtemp((char *)itmp) != NULL) - vim_settempdir(itmp); - if (vim_tempdir != NULL) - break; - } - } - } - - if (vim_tempdir != NULL) { + char_u *tempdir = vim_gettempdir(); + if (tempdir != NULL) { /* There is no need to check if the file exists, because we own the * directory and nobody else creates a file in it. */ - sprintf((char *)itmp, "%s%" PRIu32, vim_tempdir, temp_count++); + sprintf((char *)itmp, "%s%" PRIu32, tempdir, temp_count++); return vim_strsave(itmp); } diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 436ddb768d..cdfa882a3e 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -536,9 +536,6 @@ EXTERN int ru_col; /* column for ruler */ EXTERN int ru_wid; /* 'rulerfmt' width of ruler when non-zero */ EXTERN int sc_col; /* column for shown command */ -EXTERN char_u *vim_tempdir INIT(= NULL); /* Name of Vim's own temp dir. - Ends in a slash. */ - /* * When starting or exiting some things are done differently (e.g. screen * updating). -- cgit From 286ce271e730a2e6883c244912b36bca007f6ddc Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Sun, 22 Jun 2014 13:03:08 +0300 Subject: Extract `tempfile` module from fileio Though this module is relatively small it has very clear boundaries. The last argument for extracting `tempfile` was the errors which I got when I was writing unittests for it: `cimport './src/nvim/fileio.h'` does not work for some reason. --- src/nvim/diff.c | 1 + src/nvim/eval.c | 1 + src/nvim/ex_cmds.c | 1 + src/nvim/fileio.c | 110 +-------------------------------------------- src/nvim/hardcopy.c | 1 + src/nvim/if_cscope.c | 1 + src/nvim/memline.c | 1 + src/nvim/misc1.c | 1 + src/nvim/os/server.c | 2 +- src/nvim/os_unix.c | 1 + src/nvim/quickfix.c | 1 + src/nvim/spell.c | 1 + src/nvim/tempfile.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/nvim/tempfile.h | 8 ++++ 14 files changed, 144 insertions(+), 110 deletions(-) create mode 100644 src/nvim/tempfile.c create mode 100644 src/nvim/tempfile.h (limited to 'src') diff --git a/src/nvim/diff.c b/src/nvim/diff.c index be4a923d14..ab0c80112f 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -30,6 +30,7 @@ #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" diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 7dc95bcded..3459b5c4a0 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -68,6 +68,7 @@ #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/tag.h" +#include "nvim/tempfile.h" #include "nvim/term.h" #include "nvim/ui.h" #include "nvim/undo.h" diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 05ca5bbb58..384eebe556 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -59,6 +59,7 @@ #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/tag.h" +#include "nvim/tempfile.h" #include "nvim/term.h" #include "nvim/ui.h" #include "nvim/undo.h" diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 23609ab92f..c867211a66 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -49,6 +49,7 @@ #include "nvim/search.h" #include "nvim/sha256.h" #include "nvim/strings.h" +#include "nvim/tempfile.h" #include "nvim/term.h" #include "nvim/ui.h" #include "nvim/undo.h" @@ -5156,115 +5157,6 @@ void write_lnum_adjust(linenr_T offset) curbuf->b_no_eol_lnum += offset; } -/* Name of Vim's own temp dir. Ends in a slash. */ -static char_u *vim_tempdir = NULL; -static uint32_t temp_count = 0; /* Temp filename counter. */ - -/* - * This will create a directory for private use by this instance of Vim. - * 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; - int i; - /* - * Try the entries in `TEMP_DIR_NAMES` to create the temp directory. - */ - char_u itmp[TEMP_FILE_PATH_MAXLEN]; - for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) { - /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */ - expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22); - if (os_isdir(itmp)) { /* directory exists */ - add_pathsep(itmp); - - /* Leave room for filename */ - STRCAT(itmp, "nvimXXXXXX"); - if (os_mkdtemp((char *)itmp) != NULL) - vim_settempdir(itmp); - if (vim_tempdir != NULL) - break; - } - } -} - -/* - * Delete the temp directory and all files it contains. - */ -void vim_deltempdir(void) -{ - char_u **files; - int file_count; - int i; - - if (vim_tempdir != NULL) { - sprintf((char *)NameBuff, "%s*", vim_tempdir); - if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, - EW_DIR|EW_FILE|EW_SILENT) == OK) { - for (i = 0; i < file_count; ++i) - os_remove((char *)files[i]); - FreeWild(file_count, files); - } - path_tail(NameBuff)[-1] = NUL; - os_rmdir((char *)NameBuff); - - free(vim_tempdir); - vim_tempdir = NULL; - } -} - -char_u *vim_gettempdir(void) -{ - if (vim_tempdir == NULL) { - vim_maketempdir(); - } - - return vim_tempdir; -} - -/* - * Directory "tempdir" was created. Expand this name to a full path and put - * it in "vim_tempdir". This avoids that using ":cd" would confuse us. - * "tempdir" must be no longer than MAXPATHL. - */ -static void vim_settempdir(char_u *tempdir) -{ - char_u *buf = verbose_try_malloc((size_t)MAXPATHL + 2); - if (buf) { - if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) - STRCPY(buf, tempdir); - add_pathsep(buf); - vim_tempdir = vim_strsave(buf); - free(buf); - } -} - -/* - * vim_tempname(): Return a unique name that can be used for a temp file. - * - * The temp file is NOT created. - * - * The returned pointer is to allocated memory. - * The returned pointer is NULL if no valid name was found. - */ -char_u *vim_tempname(void) -{ - char_u itmp[TEMP_FILE_PATH_MAXLEN]; - - char_u *tempdir = vim_gettempdir(); - if (tempdir != NULL) { - /* There is no need to check if the file exists, because we own the - * directory and nobody else creates a file in it. */ - sprintf((char *)itmp, "%s%" PRIu32, tempdir, temp_count++); - return vim_strsave(itmp); - } - - return NULL; -} - #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) /* * Convert all backslashes in fname to forward slashes in-place. diff --git a/src/nvim/hardcopy.c b/src/nvim/hardcopy.c index 024564f700..64a5879d7d 100644 --- a/src/nvim/hardcopy.c +++ b/src/nvim/hardcopy.c @@ -40,6 +40,7 @@ #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/term.h" +#include "nvim/tempfile.h" #include "nvim/ui.h" #include "nvim/os/os.h" diff --git a/src/nvim/if_cscope.c b/src/nvim/if_cscope.c index 1b7202dbab..9e07a60ee1 100644 --- a/src/nvim/if_cscope.c +++ b/src/nvim/if_cscope.c @@ -28,6 +28,7 @@ #include "nvim/quickfix.h" #include "nvim/strings.h" #include "nvim/tag.h" +#include "nvim/tempfile.h" #include "nvim/ui.h" #include "nvim/window.h" #include "nvim/os/os.h" diff --git a/src/nvim/memline.c b/src/nvim/memline.c index c41942301f..a10fc2b8c5 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -69,6 +69,7 @@ #include "nvim/spell.h" #include "nvim/strings.h" #include "nvim/term.h" +#include "nvim/tempfile.h" #include "nvim/ui.h" #include "nvim/undo.h" #include "nvim/window.h" diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c index 4e3879ddef..6ba1e68d64 100644 --- a/src/nvim/misc1.c +++ b/src/nvim/misc1.c @@ -52,6 +52,7 @@ #include "nvim/strings.h" #include "nvim/tag.h" #include "nvim/term.h" +#include "nvim/tempfile.h" #include "nvim/ui.h" #include "nvim/undo.h" #include "nvim/window.h" diff --git a/src/nvim/os/server.c b/src/nvim/os/server.c index 2494dfeb9f..2e8934ecfb 100644 --- a/src/nvim/os/server.c +++ b/src/nvim/os/server.c @@ -11,7 +11,7 @@ #include "nvim/vim.h" #include "nvim/memory.h" #include "nvim/message.h" -#include "nvim/fileio.h" +#include "nvim/tempfile.h" #include "nvim/map.h" #define MAX_CONNECTIONS 32 diff --git a/src/nvim/os_unix.c b/src/nvim/os_unix.c index 239d7d81db..4a0fbf5c18 100644 --- a/src/nvim/os_unix.c +++ b/src/nvim/os_unix.c @@ -52,6 +52,7 @@ #include "nvim/screen.h" #include "nvim/strings.h" #include "nvim/syntax.h" +#include "nvim/tempfile.h" #include "nvim/term.h" #include "nvim/ui.h" #include "nvim/os/os.h" diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 60bd15701d..ff2123bac1 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -47,6 +47,7 @@ #include "nvim/search.h" #include "nvim/strings.h" #include "nvim/term.h" +#include "nvim/tempfile.h" #include "nvim/ui.h" #include "nvim/window.h" #include "nvim/os/os.h" diff --git a/src/nvim/spell.c b/src/nvim/spell.c index 4000235304..8ef67c95f7 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -322,6 +322,7 @@ #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/term.h" +#include "nvim/tempfile.h" #include "nvim/ui.h" #include "nvim/undo.h" #include "nvim/os/os.h" diff --git a/src/nvim/tempfile.c b/src/nvim/tempfile.c new file mode 100644 index 0000000000..a9472bc66a --- /dev/null +++ b/src/nvim/tempfile.c @@ -0,0 +1,124 @@ +#include +#include +#include + +#include "nvim/ascii.h" +#include "nvim/memory.h" +#include "nvim/misc1.h" +#include "nvim/os/os.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; +static uint32_t temp_count = 0; /* Temp filename counter. */ + +/* + * This will create a directory for private use by this instance of Vim. + * 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; + int i; + /* + * Try the entries in `TEMP_DIR_NAMES` to create the temp directory. + */ + char_u itmp[TEMP_FILE_PATH_MAXLEN]; + for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) { + /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */ + expand_env((char_u *)temp_dirs[i], itmp, TEMP_FILE_PATH_MAXLEN - 22); + if (os_isdir(itmp)) { /* directory exists */ + add_pathsep(itmp); + + /* Leave room for filename */ + STRCAT(itmp, "nvimXXXXXX"); + if (os_mkdtemp((char *)itmp) != NULL) + vim_settempdir(itmp); + if (vim_tempdir != NULL) + break; + } + } +} + +/* + * Delete the temp directory and all files it contains. + */ +void vim_deltempdir(void) +{ + char_u **files; + int file_count; + int i; + + if (vim_tempdir != NULL) { + sprintf((char *)NameBuff, "%s*", vim_tempdir); + if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, + EW_DIR|EW_FILE|EW_SILENT) == OK) { + for (i = 0; i < file_count; ++i) + os_remove((char *)files[i]); + FreeWild(file_count, files); + } + path_tail(NameBuff)[-1] = NUL; + os_rmdir((char *)NameBuff); + + free(vim_tempdir); + vim_tempdir = NULL; + } +} + +char_u *vim_gettempdir(void) +{ + if (vim_tempdir == NULL) { + vim_maketempdir(); + } + + return vim_tempdir; +} + +/* + * Directory "tempdir" was created. Expand this name to a full path and put + * it in "vim_tempdir". This avoids that using ":cd" would confuse us. + * "tempdir" must be no longer than MAXPATHL. + */ +static void vim_settempdir(char_u *tempdir) +{ + char_u *buf = verbose_try_malloc((size_t)MAXPATHL + 2); + if (buf) { + if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) + STRCPY(buf, tempdir); + add_pathsep(buf); + vim_tempdir = vim_strsave(buf); + free(buf); + } +} + +/* + * vim_tempname(): Return a unique name that can be used for a temp file. + * + * The temp file is NOT created. + * + * The returned pointer is to allocated memory. + * The returned pointer is NULL if no valid name was found. + */ +char_u *vim_tempname(void) +{ + char_u itmp[TEMP_FILE_PATH_MAXLEN]; + + char_u *tempdir = vim_gettempdir(); + if (tempdir != NULL) { + /* There is no need to check if the file exists, because we own the + * directory and nobody else creates a file in it. */ + sprintf((char *)itmp, "%s%" PRIu32, tempdir, temp_count++); + return vim_strsave(itmp); + } + + return NULL; +} diff --git a/src/nvim/tempfile.h b/src/nvim/tempfile.h new file mode 100644 index 0000000000..c030a70eeb --- /dev/null +++ b/src/nvim/tempfile.h @@ -0,0 +1,8 @@ +#ifndef NVIM_TEMPFILE_H +#define NVIM_TEMPFILE_H + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "tempfile.h.generated.h" +#endif + +#endif // NVIM_TEMPFILE_H -- cgit From 34330cf69798a2a8667bdc0aa76f732fe715de84 Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Wed, 25 Jun 2014 21:27:01 +0300 Subject: tempfile.c: add to clint-files and fix warnings --- src/nvim/tempfile.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/tempfile.c b/src/nvim/tempfile.c index a9472bc66a..7b45a25cd7 100644 --- a/src/nvim/tempfile.c +++ b/src/nvim/tempfile.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -59,7 +60,7 @@ void vim_deltempdir(void) int i; if (vim_tempdir != NULL) { - sprintf((char *)NameBuff, "%s*", vim_tempdir); + snprintf((char *)NameBuff, MAXPATHL, "%s*", vim_tempdir); if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, EW_DIR|EW_FILE|EW_SILENT) == OK) { for (i = 0; i < file_count; ++i) @@ -92,7 +93,7 @@ static void vim_settempdir(char_u *tempdir) { char_u *buf = verbose_try_malloc((size_t)MAXPATHL + 2); if (buf) { - if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) + if (vim_FullName(tempdir, buf, MAXPATHL, false) == FAIL) STRCPY(buf, tempdir); add_pathsep(buf); vim_tempdir = vim_strsave(buf); @@ -116,7 +117,8 @@ char_u *vim_tempname(void) if (tempdir != NULL) { /* There is no need to check if the file exists, because we own the * directory and nobody else creates a file in it. */ - sprintf((char *)itmp, "%s%" PRIu32, tempdir, temp_count++); + snprintf((char *)itmp, TEMP_FILE_PATH_MAXLEN, + "%s%" PRIu32, tempdir, temp_count++); return vim_strsave(itmp); } -- cgit From 820694adb48156aed44649da010fafac524bc955 Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Wed, 25 Jun 2014 21:29:52 +0300 Subject: tempfile.c: enable -Wconversion --- src/nvim/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 6b0538c52d..4a23e76a1c 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -55,6 +55,7 @@ set(CONV_SRCS os/uv_helpers.c os/wstream.c os/msgpack_rpc.c + tempfile.c api/buffer.c api/private/helpers.c api/private/handle.c -- cgit From 0e49e16c4efe524e1459878d2249474f4f92cacb Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Wed, 25 Jun 2014 21:46:41 +0300 Subject: tempfile.c: fix style issues and comments --- src/nvim/tempfile.c | 97 ++++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/nvim/tempfile.c b/src/nvim/tempfile.c index 7b45a25cd7..a783f496dd 100644 --- a/src/nvim/tempfile.c +++ b/src/nvim/tempfile.c @@ -15,56 +15,50 @@ # include "tempfile.c.generated.h" #endif -/* Name of Vim's own temp dir. Ends in a slash. */ +/// Name of Vim's own temp dir. Ends in a slash. static char_u *vim_tempdir = NULL; -static uint32_t temp_count = 0; /* Temp filename counter. */ -/* - * This will create a directory for private use by this instance of Vim. - * 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. - */ +/// 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; - int i; - /* - * Try the entries in `TEMP_DIR_NAMES` to create the temp directory. - */ + // Try the entries in `TEMP_DIR_NAMES` to create the temp directory. char_u itmp[TEMP_FILE_PATH_MAXLEN]; - for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) { - /* expand $TMP, leave room for "/nvimXXXXXX/999999999" */ + 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 exists */ + if (os_isdir(itmp)) { // directory exists add_pathsep(itmp); - /* Leave room for filename */ + // Concatenate with temporary directory name pattern STRCAT(itmp, "nvimXXXXXX"); - if (os_mkdtemp((char *)itmp) != NULL) + if (os_mkdtemp((char *)itmp) != NULL) { vim_settempdir(itmp); - if (vim_tempdir != NULL) + } + if (vim_tempdir != NULL) { break; + } } } } -/* - * Delete the temp directory and all files it contains. - */ +/// Delete the temp directory and all files it contains. void vim_deltempdir(void) { - char_u **files; - int file_count; - int i; - if (vim_tempdir != NULL) { snprintf((char *)NameBuff, MAXPATHL, "%s*", vim_tempdir); + + char_u **files; + int file_count; if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, - EW_DIR|EW_FILE|EW_SILENT) == OK) { - for (i = 0; i < file_count; ++i) + 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; @@ -75,6 +69,8 @@ void vim_deltempdir(void) } } +/// 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) { @@ -84,43 +80,44 @@ char_u *vim_gettempdir(void) return vim_tempdir; } -/* - * Directory "tempdir" was created. Expand this name to a full path and put - * it in "vim_tempdir". This avoids that using ":cd" would confuse us. - * "tempdir" must be no longer than MAXPATHL. - */ +/// 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. static void vim_settempdir(char_u *tempdir) { char_u *buf = verbose_try_malloc((size_t)MAXPATHL + 2); if (buf) { - if (vim_FullName(tempdir, buf, MAXPATHL, false) == FAIL) + if (vim_FullName(tempdir, buf, MAXPATHL, false) == FAIL) { STRCPY(buf, tempdir); + } add_pathsep(buf); vim_tempdir = vim_strsave(buf); free(buf); } } -/* - * vim_tempname(): Return a unique name that can be used for a temp file. - * - * The temp file is NOT created. - * - * The returned pointer is to allocated memory. - * The returned pointer is NULL if no valid name was found. - */ +/// 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) { - char_u itmp[TEMP_FILE_PATH_MAXLEN]; + // Temp filename counter. + static uint32_t temp_count; char_u *tempdir = vim_gettempdir(); - if (tempdir != NULL) { - /* There is no need to check if the file exists, because we own the - * directory and nobody else creates a file in it. */ - snprintf((char *)itmp, TEMP_FILE_PATH_MAXLEN, - "%s%" PRIu32, tempdir, temp_count++); - return vim_strsave(itmp); + if (!tempdir) { + return NULL; } - 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 itmp[TEMP_FILE_PATH_MAXLEN]; + snprintf((char *)itmp, TEMP_FILE_PATH_MAXLEN, + "%s%" PRIu32, tempdir, temp_count++); + return vim_strsave(itmp); } -- cgit From 8e2b570d6f00b4469166869cccd2ec73a1a7396b Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Wed, 25 Jun 2014 23:13:39 +0300 Subject: 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. --- src/nvim/tempfile.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/nvim/tempfile.c b/src/nvim/tempfile.c index a783f496dd..35db75ca19 100644 --- a/src/nvim/tempfile.c +++ b/src/nvim/tempfile.c @@ -31,17 +31,22 @@ static void vim_maketempdir(void) 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 exists - add_pathsep(itmp); + if (!os_isdir(itmp)) { // directory doesn't exist + continue; + } - // Concatenate with temporary directory name pattern - STRCAT(itmp, "nvimXXXXXX"); - if (os_mkdtemp((char *)itmp) != NULL) { - vim_settempdir(itmp); - } - if (vim_tempdir != NULL) { - break; - } + add_pathsep(itmp); + // Concatenate with temporary directory name pattern + STRCAT(itmp, "nvimXXXXXX"); + if (!os_mkdtemp((char *)itmp)) { + continue; + } + 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. /// /// @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); - if (buf) { - if (vim_FullName(tempdir, buf, MAXPATHL, false) == FAIL) { - STRCPY(buf, tempdir); - } - add_pathsep(buf); - vim_tempdir = vim_strsave(buf); - free(buf); + if (!buf) { + return false; } + 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. -- cgit From 14239ae33128ae961a4ce9e68436ad3f0f557b90 Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Sat, 28 Jun 2014 21:32:04 +0300 Subject: Create os/os_defs.h for os specific definitions --- src/nvim/os/os.h | 6 ------ src/nvim/os/os_defs.h | 10 ++++++++++ src/nvim/tempfile.c | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 src/nvim/os/os_defs.h (limited to 'src') diff --git a/src/nvim/os/os.h b/src/nvim/os/os.h index b46989d9b4..69bd1ff4fd 100644 --- a/src/nvim/os/os.h +++ b/src/nvim/os/os.h @@ -7,12 +7,6 @@ #include "nvim/os/fs_defs.h" #include "nvim/vim.h" -#ifdef WIN32 -# include "nvim/os/win_defs.h" -#else -# include "nvim/os/unix_defs.h" -#endif - #ifdef INCLUDE_GENERATED_DECLARATIONS # include "os/fs.h.generated.h" # include "os/mem.h.generated.h" diff --git a/src/nvim/os/os_defs.h b/src/nvim/os/os_defs.h new file mode 100644 index 0000000000..aa6c5bccc3 --- /dev/null +++ b/src/nvim/os/os_defs.h @@ -0,0 +1,10 @@ +#ifndef NVIM_OS_OS_DEFS_H +#define NVIM_OS_OS_DEFS_H + +#ifdef WIN32 +# include "nvim/os/win_defs.h" +#else +# include "nvim/os/unix_defs.h" +#endif + +#endif // NVIM_OS_DEFS_H diff --git a/src/nvim/tempfile.c b/src/nvim/tempfile.c index 35db75ca19..41fc163936 100644 --- a/src/nvim/tempfile.c +++ b/src/nvim/tempfile.c @@ -7,6 +7,7 @@ #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" -- cgit