From: Joel Rosdahl Date: Thu, 30 Jul 2020 05:16:00 +0000 (+0200) Subject: Move get_hostname to Util X-Git-Tag: v4.0~257 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b3d5f92e418b5e9eab3d984e837a297f49b61cc7;p=thirdparty%2Fccache.git Move get_hostname to Util --- diff --git a/src/Lockfile.cpp b/src/Lockfile.cpp index e07d0c2c7..97e31b1e4 100644 --- a/src/Lockfile.cpp +++ b/src/Lockfile.cpp @@ -42,7 +42,7 @@ do_acquire_posix(const std::string& lockfile, uint32_t staleness_limit) while (true) { std::string my_content = - fmt::format("{}:{}:{}", get_hostname(), getpid(), time(nullptr)); + fmt::format("{}:{}:{}", Util::get_hostname(), getpid(), time(nullptr)); if (symlink(my_content.c_str(), lockfile.c_str()) == 0) { // We got the lock. diff --git a/src/Util.cpp b/src/Util.cpp index a6db3ccb1..fd19e1a4c 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -590,6 +590,22 @@ get_level_1_files(const std::string& dir, progress_receiver(1.0); } +const char* +get_hostname() +{ + static char hostname[260] = ""; + + if (hostname[0]) { + return hostname; + } + + if (gethostname(hostname, sizeof(hostname)) != 0) { + strcpy(hostname, "unknown"); + } + hostname[sizeof(hostname) - 1] = 0; + return hostname; +} + std::string get_relative_path(string_view dir, string_view path) { diff --git a/src/Util.hpp b/src/Util.hpp index 7028caf8a..ef9a3a247 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -190,6 +190,9 @@ void get_level_1_files(const std::string& dir, const ProgressReceiver& progress_receiver, std::vector>& files); +// Return a static string with the current hostname. +const char* get_hostname(); + // Compute a relative path from `dir` (an absolute path to a directory) to // `path` (an absolute path). Assumes that both `dir` and `path` are normalized. // The algorithm does *not* follow symlinks, so the result may not actually diff --git a/src/ccache.cpp b/src/ccache.cpp index b2c4d9b48..a5d36099e 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1971,7 +1971,7 @@ do_cache_compilation(Context& ctx, const char* const* argv) MTR_END("main", "set_up_uncached_err"); cc_log_argv("Command line: ", argv); - cc_log("Hostname: %s", get_hostname()); + cc_log("Hostname: %s", Util::get_hostname()); cc_log("Working directory: %s", ctx.actual_cwd.c_str()); if (ctx.apparent_cwd != ctx.actual_cwd) { cc_log("Apparent working directory: %s", ctx.apparent_cwd.c_str()); diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index fe7cbd94b..aa962abcf 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -34,23 +34,6 @@ # include #endif -// Return a static string with the current hostname. -const char* -get_hostname() -{ - static char hostname[260] = ""; - - if (hostname[0]) { - return hostname; - } - - if (gethostname(hostname, sizeof(hostname)) != 0) { - strcpy(hostname, "unknown"); - } - hostname[sizeof(hostname) - 1] = 0; - return hostname; -} - // This is like setenv. void x_setenv(const char* name, const char* value) diff --git a/src/legacy_util.hpp b/src/legacy_util.hpp index e91fba8df..8b39c63f2 100644 --- a/src/legacy_util.hpp +++ b/src/legacy_util.hpp @@ -22,7 +22,6 @@ #include -const char* get_hostname(); void x_setenv(const char* name, const char* value); void x_unsetenv(const char* name); #ifndef HAVE_LOCALTIME_R