From 6025c28efc97720b04a23640eab805114f73276d Mon Sep 17 00:00:00 2001 From: Brian Malehorn Date: Tue, 5 Feb 2019 20:36:38 -0800 Subject: [PATCH] Create function to retrieve tmpdir `/tmp` isn't present / writeable on every system. Instead of always using `/tmp`, try to use standard environment variables and configuration to find a temporary directory. Adapted from #3974, with updates based on those comments. Closes #3845. --- src/common.cpp | 23 +++++++++++++++++++++++ src/common.h | 7 +++++++ src/env.cpp | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/common.cpp b/src/common.cpp index 7a88e569f..3a02dadf5 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -2442,3 +2443,25 @@ std::string get_executable_path(const char *argv0) { return std::string(argv0 ? argv0 : ""); } +/// Return a path to a directory where we can store temporary files. +std::string get_path_to_tmp_dir() { + char *env_tmpdir = getenv("TMPDIR"); + if (env_tmpdir) { + return env_tmpdir; + } +#if defined(_CS_DARWIN_USER_TEMP_DIR) + char osx_tmpdir[PATH_MAX]; + size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, osx_tmpdir, PATH_MAX); + if (0 < n && n <= PATH_MAX) { + return osx_tmpdir; + } else { + return "/tmp"; + } +#elif defined(P_tmpdir) + return P_tmpdir; +#elif defined(_PATH_TMP) + return _PATH_TMP; +#else + return "/tmp"; +#endif +} diff --git a/src/common.h b/src/common.h index 8e94364be..fbf8c183c 100644 --- a/src/common.h +++ b/src/common.h @@ -19,6 +19,11 @@ #include "fallback.h" // IWYU pragma: keep #include "maybe.h" +// PATH_MAX may not exist. +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif + // Define a symbol we can use elsewhere in our code to determine if we're being built on MS Windows // under Cygwin. #if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(__CYGWIN__) || \ @@ -947,6 +952,8 @@ static const wchar_t *enum_to_str(T enum_val, const enum_map map[]) { void redirect_tty_output(); +std::string get_path_to_tmp_dir(); + // Minimum allowed terminal size and default size if the detected size is not reasonable. #define MIN_TERM_COL 20 #define MIN_TERM_ROW 2 diff --git a/src/env.cpp b/src/env.cpp index 56b6be201..ff787bc47 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -1741,7 +1741,7 @@ wcstring env_get_runtime_path() { auto pwuid = getpwuid(geteuid()); const char *uname = pwuid ? pwuid->pw_name : NULL; // /tmp/fish.user - std::string tmpdir = "/tmp/fish."; + std::string tmpdir = get_path_to_tmp_dir() + "/fish."; if (uname) { tmpdir.append(uname); }