]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::get_hostname to util
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 19 Jul 2023 06:33:25 +0000 (08:33 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 25 Jul 2023 06:54:43 +0000 (08:54 +0200)
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/util/LockFile.cpp
src/util/process.cpp
src/util/process.hpp

index 4e34bd0ac4c6c3d47dc8af6f6c3b9d7b48f00234..df42f5c1fc207e4a4317d1c006a3a7f7dd381d0a 100644 (file)
@@ -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)
 {
index 6c4652582b2ae9f4d7c1e5c1a2dc5ca1cb7e26bb..9dc99a8f777cd6f60c7ac6e399aa9d2ee142ed89 100644 (file)
@@ -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
index 6f8cc48e8981b182f45eedad29d056cc7d872e83..f84d4b8db9f989c7de952cb5eb056c0b5768dd54 100644 (file)
@@ -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);
index fd9571637d93d998cb6ab9a262839739ec8a5f74..bb3f92bec1eb812ce38a0bbdfe9bb843e28405e1 100644 (file)
@@ -29,6 +29,7 @@
 #include <core/wincompat.hpp>
 #include <util/file.hpp>
 #include <util/filesystem.hpp>
+#include <util/process.hpp>
 
 #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] {
index 64c6bfdfd94b5208ef2a3814ef5e7e9341473404..a9d250ecf656977b668b491925cd825559cd6c76 100644 (file)
 
 #include <core/wincompat.hpp>
 
+#include <cstring>
+
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>
+#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()
 {
index a37f3d59ff43d3d77248cf10f38badb039b38842..2e8e9e82f40acdb895cd5b958e78d983d177f204 100644 (file)
@@ -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();