]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add and use CacheFile::Type and CacheFile::type
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 8 Sep 2019 17:29:12 +0000 (19:29 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 5 Oct 2019 20:57:20 +0000 (22:57 +0200)
Makefile.in
src/CacheFile.cpp [new file with mode: 0644]
src/CacheFile.hpp
src/compress.cpp

index aaa5822518186b50ffbfff6fc44ab0d04a86eb15..36d1eb53123f98aeff8733d6dc4fd0a18174b169 100644 (file)
@@ -32,6 +32,7 @@ Q=$(if $(quiet),@)
 
 non_third_party_sources = \
     src/AtomicFile.cpp \
+    src/CacheFile.cpp \
     src/Config.cpp \
     src/ProgressBar.cpp \
     src/args.cpp \
diff --git a/src/CacheFile.cpp b/src/CacheFile.cpp
new file mode 100644 (file)
index 0000000..4a30103
--- /dev/null
@@ -0,0 +1,60 @@
+// Copyright (C) 2019 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "CacheFile.hpp"
+
+#include "util.hpp"
+
+const struct stat&
+CacheFile::stat() const
+{
+  if (!m_stated) {
+#ifdef _WIN32
+    int result = ::stat(m_path.c_str(), &m_stat);
+#else
+    int result = lstat(m_path.c_str(), &m_stat);
+#endif
+    if (result != 0) {
+      if (errno != ENOENT && errno != ESTALE) {
+        throw Error(
+          fmt::format("lstat {} failed: {}", m_path, strerror(errno)));
+      }
+
+      // The file is missing, so just zero fill the stat structure. This will
+      // make e.g. S_ISREG(stat().st_mode) return false and stat().st_mtime
+      // will be, etc.
+      memset(&m_stat, '\0', sizeof(m_stat));
+    }
+
+    m_stated = true;
+  }
+
+  return m_stat;
+}
+
+CacheFile::Type
+CacheFile::type() const
+{
+  if (util::ends_with(m_path, ".manifest")) {
+    return Type::manifest;
+  } else if (util::ends_with(m_path, ".result")) {
+    return Type::result;
+  } else {
+    return Type::unknown;
+  }
+}
index 4a6c8fcdb9f7d6fab24b733e86cec94e78ff2aa1..04d4ba3ba72788718079e113b437f2d3661d6a53 100644 (file)
@@ -31,6 +31,8 @@
 class CacheFile
 {
 public:
+  enum class Type { result, manifest, unknown };
+
   explicit CacheFile(const std::string& path);
 
   CacheFile(const CacheFile&) = delete;
@@ -38,6 +40,7 @@ public:
 
   const std::string& path() const;
   const struct stat& stat() const;
+  Type type() const;
 
 private:
   const std::string m_path;
@@ -54,30 +57,3 @@ CacheFile::path() const
 {
   return m_path;
 }
-
-inline const struct stat&
-CacheFile::stat() const
-{
-  if (!m_stated) {
-#ifdef _WIN32
-    int result = ::stat(m_path.c_str(), &m_stat);
-#else
-    int result = lstat(m_path.c_str(), &m_stat);
-#endif
-    if (result != 0) {
-      if (errno != ENOENT && errno != ESTALE) {
-        throw Error(
-          fmt::format("lstat {} failed: {}", m_path, strerror(errno)));
-      }
-
-      // The file is missing, so just zero fill the stat structure. This will
-      // make e.g. S_ISREG(stat().st_mode) return false and stat().st_mtime
-      // will be, etc.
-      memset(&m_stat, '\0', sizeof(m_stat));
-    }
-
-    m_stated = true;
-  }
-
-  return m_stat;
-}
index ef2dc0645dfe8c184dff91ec4fd07732eda50cbb..3ae295aab495bf4d118d4175d80767c63f776a19 100644 (file)
@@ -73,13 +73,15 @@ compress_stats(const Config& config,
         on_disk_size += file_size(&file->stat());
 
         size_t content_size = 0;
-        bool is_compressible = false;
-        if (util::ends_with(file->path(), ".manifest")) {
+        bool is_compressible;
+        if (file->type() == CacheFile::Type::manifest) {
           is_compressible = get_content_size(
             file->path(), MANIFEST_MAGIC, MANIFEST_VERSION, &content_size);
-        } else if (util::ends_with(file->path(), ".result")) {
+        } else if (file->type() == CacheFile::Type::result) {
           is_compressible = get_content_size(
             file->path(), RESULT_MAGIC, RESULT_VERSION, &content_size);
+        } else {
+          is_compressible = false;
         }
 
         if (is_compressible) {