mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
tempfile.c: fix style issues and comments
This commit is contained in:
parent
820694adb4
commit
0e49e16c4e
@ -15,56 +15,50 @@
|
|||||||
# include "tempfile.c.generated.h"
|
# include "tempfile.c.generated.h"
|
||||||
#endif
|
#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 char_u *vim_tempdir = NULL;
|
||||||
static uint32_t temp_count = 0; /* Temp filename counter. */
|
|
||||||
|
|
||||||
/*
|
/// Create a directory for private use by this instance of Neovim.
|
||||||
* 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 is done once, and the same directory is used for all temp files.
|
/// This method avoids security problems because of symlink attacks et al.
|
||||||
* 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
|
||||||
* 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.
|
||||||
* file when creating the directory and not for each temp file.
|
|
||||||
*/
|
|
||||||
static void vim_maketempdir(void)
|
static void vim_maketempdir(void)
|
||||||
{
|
{
|
||||||
static const char *temp_dirs[] = TEMP_DIR_NAMES;
|
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];
|
char_u itmp[TEMP_FILE_PATH_MAXLEN];
|
||||||
for (i = 0; i < (int)(sizeof(temp_dirs) / sizeof(char *)); ++i) {
|
for (size_t i = 0; i < sizeof(temp_dirs) / sizeof(char *); ++i) {
|
||||||
/* expand $TMP, 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 exists
|
||||||
add_pathsep(itmp);
|
add_pathsep(itmp);
|
||||||
|
|
||||||
/* Leave room for filename */
|
// Concatenate with temporary directory name pattern
|
||||||
STRCAT(itmp, "nvimXXXXXX");
|
STRCAT(itmp, "nvimXXXXXX");
|
||||||
if (os_mkdtemp((char *)itmp) != NULL)
|
if (os_mkdtemp((char *)itmp) != NULL) {
|
||||||
vim_settempdir(itmp);
|
vim_settempdir(itmp);
|
||||||
if (vim_tempdir != NULL)
|
}
|
||||||
|
if (vim_tempdir != NULL) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Delete the temp directory and all files it contains.
|
||||||
* Delete the temp directory and all files it contains.
|
|
||||||
*/
|
|
||||||
void vim_deltempdir(void)
|
void vim_deltempdir(void)
|
||||||
{
|
{
|
||||||
char_u **files;
|
|
||||||
int file_count;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (vim_tempdir != NULL) {
|
if (vim_tempdir != NULL) {
|
||||||
snprintf((char *)NameBuff, MAXPATHL, "%s*", vim_tempdir);
|
snprintf((char *)NameBuff, MAXPATHL, "%s*", vim_tempdir);
|
||||||
|
|
||||||
|
char_u **files;
|
||||||
|
int file_count;
|
||||||
if (gen_expand_wildcards(1, &NameBuff, &file_count, &files,
|
if (gen_expand_wildcards(1, &NameBuff, &file_count, &files,
|
||||||
EW_DIR|EW_FILE|EW_SILENT) == OK) {
|
EW_DIR|EW_FILE|EW_SILENT) == OK) {
|
||||||
for (i = 0; i < file_count; ++i)
|
for (int i = 0; i < file_count; ++i) {
|
||||||
os_remove((char *)files[i]);
|
os_remove((char *)files[i]);
|
||||||
|
}
|
||||||
FreeWild(file_count, files);
|
FreeWild(file_count, files);
|
||||||
}
|
}
|
||||||
path_tail(NameBuff)[-1] = NUL;
|
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)
|
char_u *vim_gettempdir(void)
|
||||||
{
|
{
|
||||||
if (vim_tempdir == NULL) {
|
if (vim_tempdir == NULL) {
|
||||||
@ -84,43 +80,44 @@ char_u *vim_gettempdir(void)
|
|||||||
return vim_tempdir;
|
return vim_tempdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Set Neovim own temporary directory name to `tempdir`. This directory should
|
||||||
* Directory "tempdir" was created. Expand this name to a full path and put
|
/// be already created. Expand this name to a full path and put it in
|
||||||
* it in "vim_tempdir". This avoids that using ":cd" would confuse us.
|
/// `vim_tempdir`. This avoids that using `:cd` would confuse us.
|
||||||
* "tempdir" must be no longer than MAXPATHL.
|
///
|
||||||
*/
|
/// @param tempdir must be no longer than MAXPATHL.
|
||||||
static void vim_settempdir(char_u *tempdir)
|
static void 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)
|
if (vim_FullName(tempdir, buf, MAXPATHL, false) == FAIL) {
|
||||||
STRCPY(buf, tempdir);
|
STRCPY(buf, tempdir);
|
||||||
|
}
|
||||||
add_pathsep(buf);
|
add_pathsep(buf);
|
||||||
vim_tempdir = vim_strsave(buf);
|
vim_tempdir = vim_strsave(buf);
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Return a unique name that can be used for a temp file.
|
||||||
* vim_tempname(): Return a unique name that can be used for a temp file.
|
///
|
||||||
*
|
/// @note The temp file is NOT created.
|
||||||
* The temp file is NOT created.
|
///
|
||||||
*
|
/// @return pointer to the temp file name or NULL if Neovim can't create
|
||||||
* The returned pointer is to allocated memory.
|
/// temporary directory for its own temporary files.
|
||||||
* The returned pointer is NULL if no valid name was found.
|
|
||||||
*/
|
|
||||||
char_u *vim_tempname(void)
|
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();
|
char_u *tempdir = vim_gettempdir();
|
||||||
if (tempdir != NULL) {
|
if (!tempdir) {
|
||||||
/* There is no need to check if the file exists, because we own the
|
return NULL;
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user