From: Joel Rosdahl Date: Wed, 19 Jul 2023 06:33:25 +0000 (+0200) Subject: refactor: Move Util::get_hostname to util X-Git-Tag: v4.9~100 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d4f1ce21afb918d6ef7a9a2b6b89e8b3df6b9fc;p=thirdparty%2Fccache.git refactor: Move Util::get_hostname to util --- diff --git a/src/Util.cpp b/src/Util.cpp index 4e34bd0ac..df42f5c1f 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -228,22 +228,6 @@ get_extension(std::string_view path) } } -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(std::string_view dir, std::string_view path) { diff --git a/src/Util.hpp b/src/Util.hpp index 6c4652582..9dc99a8f7 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -56,9 +56,6 @@ std::string_view dir_name(std::string_view path); // `path` has no file extension, an empty string_view is returned. std::string_view get_extension(std::string_view path); -// 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 6f8cc48e8..f84d4b8db 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2203,7 +2203,7 @@ initialize(Context& ctx, const char* const* argv, bool masquerading_as_compiler) } LOG("Command line: {}", util::format_argv_for_logging(argv)); - LOG("Hostname: {}", Util::get_hostname()); + LOG("Hostname: {}", util::get_hostname()); LOG("Working directory: {}", ctx.actual_cwd); if (ctx.apparent_cwd != ctx.actual_cwd) { LOG("Apparent working directory: {}", ctx.apparent_cwd); diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index fd9571637..bb3f92bec 100644 --- a/src/util/LockFile.cpp +++ b/src/util/LockFile.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include "third_party/fmt/core.h" @@ -221,8 +222,7 @@ bool LockFile::do_acquire(const bool blocking) { std::stringstream ss; - ss << Util::get_hostname() << '-' << getpid() << '-' - << std::this_thread::get_id(); + ss << get_hostname() << '-' << getpid() << '-' << std::this_thread::get_id(); const auto content_prefix = ss.str(); util::TimePoint last_seen_activity = [this] { diff --git a/src/util/process.cpp b/src/util/process.cpp index 64c6bfdfd..a9d250ecf 100644 --- a/src/util/process.cpp +++ b/src/util/process.cpp @@ -20,6 +20,12 @@ #include +#include + +#ifdef HAVE_UNISTD_H +# include +#endif + namespace { // Process umask, read and written by get_umask and set_umask. @@ -33,6 +39,22 @@ mode_t g_umask = [] { namespace util { +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; +} + mode_t get_umask() { diff --git a/src/util/process.hpp b/src/util/process.hpp index a37f3d59f..2e8e9e82f 100644 --- a/src/util/process.hpp +++ b/src/util/process.hpp @@ -22,6 +22,9 @@ namespace util { +// Return a static string with the current hostname. +const char* get_hostname(); + // Get process umask. mode_t get_umask();