summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2022-11-10 09:38:37 +0100
committerJoel Rosdahl <joel@rosdahl.net>2022-11-27 21:33:51 +0100
commite186724c2e8e6cf6b831f9735915b0cc753eb995 (patch)
tree39ee5b67bf1f3eacda4052df5650fcf24ee94b1d /src
parenteaa5b40a880eaceb72567d82981cda27624d72ae (diff)
downloadccache-e186724c2e8e6cf6b831f9735915b0cc753eb995.tar.gz
refactor: Remove CacheFile, using Stat with path member instead
Diffstat (limited to 'src')
-rw-r--r--src/storage/local/CMakeLists.txt1
-rw-r--r--src/storage/local/CacheFile.cpp47
-rw-r--r--src/storage/local/CacheFile.hpp50
-rw-r--r--src/storage/local/LocalStorage.cpp15
-rw-r--r--src/storage/local/LocalStorage.hpp5
-rw-r--r--src/storage/local/LocalStorage_cleanup.cpp24
-rw-r--r--src/storage/local/LocalStorage_compress.cpp21
-rw-r--r--src/storage/local/util.cpp6
-rw-r--r--src/storage/local/util.hpp7
9 files changed, 47 insertions, 129 deletions
diff --git a/src/storage/local/CMakeLists.txt b/src/storage/local/CMakeLists.txt
index 7b0d4f05..c189aec4 100644
--- a/src/storage/local/CMakeLists.txt
+++ b/src/storage/local/CMakeLists.txt
@@ -1,6 +1,5 @@
set(
sources
- CacheFile.cpp
LocalStorage.cpp
LocalStorage_cleanup.cpp
LocalStorage_compress.cpp
diff --git a/src/storage/local/CacheFile.cpp b/src/storage/local/CacheFile.cpp
deleted file mode 100644
index a2716e8e..00000000
--- a/src/storage/local/CacheFile.cpp
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright (C) 2019-2022 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 <core/Manifest.hpp>
-#include <core/Result.hpp>
-#include <util/string.hpp>
-
-const Stat&
-CacheFile::lstat() const
-{
- if (!m_stat) {
- m_stat = Stat::lstat(m_path);
- }
-
- return *m_stat;
-}
-
-CacheFile::Type
-CacheFile::type() const
-{
- if (util::ends_with(m_path, "M")) {
- return Type::manifest;
- } else if (util::ends_with(m_path, "R")) {
- return Type::result;
- } else if (util::ends_with(m_path, "W")) {
- return Type::raw;
- } else {
- return Type::unknown;
- }
-}
diff --git a/src/storage/local/CacheFile.hpp b/src/storage/local/CacheFile.hpp
deleted file mode 100644
index e653363b..00000000
--- a/src/storage/local/CacheFile.hpp
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright (C) 2019-2022 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
-
-#pragma once
-
-#include <Stat.hpp>
-
-#include <optional>
-#include <string>
-
-class CacheFile
-{
-public:
- enum class Type { result, manifest, raw, unknown };
-
- explicit CacheFile(const std::string& path);
-
- const Stat& lstat() const;
- const std::string& path() const;
- Type type() const;
-
-private:
- std::string m_path;
- mutable std::optional<Stat> m_stat;
-};
-
-inline CacheFile::CacheFile(const std::string& path) : m_path(path)
-{
-}
-
-inline const std::string&
-CacheFile::path() const
-{
- return m_path;
-}
diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp
index 001e2a94..274a4460 100644
--- a/src/storage/local/LocalStorage.cpp
+++ b/src/storage/local/LocalStorage.cpp
@@ -30,6 +30,7 @@
#include <storage/local/StatsFile.hpp>
#include <util/Duration.hpp>
#include <util/file.hpp>
+#include <util/string.hpp>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
@@ -91,6 +92,20 @@ calculate_wanted_cache_level(const uint64_t files_in_level_1)
return k_max_cache_levels;
}
+FileType
+file_type_from_path(std::string_view path)
+{
+ if (util::ends_with(path, "M")) {
+ return FileType::manifest;
+ } else if (util::ends_with(path, "R")) {
+ return FileType::result;
+ } else if (util::ends_with(path, "W")) {
+ return FileType::raw;
+ } else {
+ return FileType::unknown;
+ }
+}
+
LocalStorage::LocalStorage(const Config& config) : m_config(config)
{
}
diff --git a/src/storage/local/LocalStorage.hpp b/src/storage/local/LocalStorage.hpp
index 54a8e9fe..7ba28707 100644
--- a/src/storage/local/LocalStorage.hpp
+++ b/src/storage/local/LocalStorage.hpp
@@ -31,6 +31,7 @@
#include <cstdint>
#include <optional>
+#include <string_view>
#include <vector>
class Config;
@@ -46,6 +47,10 @@ struct CompressionStatistics
uint64_t on_disk_size;
};
+enum class FileType { result, manifest, raw, unknown };
+
+FileType file_type_from_path(std::string_view path);
+
class LocalStorage
{
public:
diff --git a/src/storage/local/LocalStorage_cleanup.cpp b/src/storage/local/LocalStorage_cleanup.cpp
index 3039e838..1293af4a 100644
--- a/src/storage/local/LocalStorage_cleanup.cpp
+++ b/src/storage/local/LocalStorage_cleanup.cpp
@@ -28,7 +28,6 @@
#include <core/CacheEntry.hpp>
#include <core/exceptions.hpp>
#include <fmtmacros.hpp>
-#include <storage/local/CacheFile.hpp>
#include <storage/local/StatsFile.hpp>
#include <storage/local/util.hpp>
#include <util/file.hpp>
@@ -104,7 +103,7 @@ LocalStorage::clean_dir(const std::string& subdir,
{
LOG("Cleaning up cache directory {}", subdir);
- std::vector<CacheFile> files = get_level_1_files(
+ auto files = get_level_1_files(
subdir, [&](double progress) { progress_receiver(progress / 3); });
uint64_t cache_size = 0;
@@ -118,31 +117,31 @@ LocalStorage::clean_dir(const std::string& subdir,
++i, progress_receiver(1.0 / 3 + 1.0 * i / files.size() / 3)) {
const auto& file = files[i];
- if (!file.lstat().is_regular()) {
+ if (!file.is_regular()) {
// Not a file or missing file.
continue;
}
// Delete any tmp files older than 1 hour right away.
- if (file.lstat().mtime() + util::Duration(3600) < current_time
+ if (file.mtime() + util::Duration(3600) < current_time
&& TemporaryFile::is_tmp_file(file.path())) {
Util::unlink_tmp(file.path());
continue;
}
- if (namespace_ && file.type() == CacheFile::Type::raw) {
+ if (namespace_ && file_type_from_path(file.path()) == FileType::raw) {
const auto result_filename =
FMT("{}R", file.path().substr(0, file.path().length() - 2));
raw_files_map[result_filename].push_back(file.path());
}
- cache_size += file.lstat().size_on_disk();
+ cache_size += file.size_on_disk();
files_in_cache += 1;
}
// Sort according to modification time, oldest first.
std::sort(files.begin(), files.end(), [](const auto& f1, const auto& f2) {
- return f1.lstat().mtime() < f2.lstat().mtime();
+ return f1.mtime() < f2.mtime();
});
LOG("Before cleanup: {:.0f} KiB, {:.0f} files",
@@ -154,14 +153,14 @@ LocalStorage::clean_dir(const std::string& subdir,
++i, progress_receiver(2.0 / 3 + 1.0 * i / files.size() / 3)) {
const auto& file = files[i];
- if (!file.lstat() || file.lstat().is_directory()) {
+ if (!file || file.is_directory()) {
continue;
}
if ((max_size == 0 || cache_size <= max_size)
&& (max_files == 0 || files_in_cache <= max_files)
&& (!max_age
- || file.lstat().mtime() > (current_time - util::Duration(*max_age)))
+ || file.mtime() > (current_time - util::Duration(*max_age)))
&& (!namespace_ || max_age)) {
break;
}
@@ -179,7 +178,7 @@ LocalStorage::clean_dir(const std::string& subdir,
// For namespace eviction we need to remove raw files based on result
// filename since they don't have a header.
- if (file.type() == CacheFile::Type::result) {
+ if (file_type_from_path(file.path()) == FileType::result) {
const auto entry = raw_files_map.find(file.path());
if (entry != raw_files_map.end()) {
for (const auto& raw_file : entry->second) {
@@ -210,8 +209,7 @@ LocalStorage::clean_dir(const std::string& subdir,
delete_file(o_file, 0, nullptr, nullptr);
}
- delete_file(
- file.path(), file.lstat().size_on_disk(), &cache_size, &files_in_cache);
+ delete_file(file.path(), file.size_on_disk(), &cache_size, &files_in_cache);
cleaned = true;
}
@@ -250,7 +248,7 @@ wipe_dir(const std::string& subdir, const ProgressReceiver& progress_receiver)
{
LOG("Clearing out cache directory {}", subdir);
- const std::vector<CacheFile> files = get_level_1_files(
+ const auto files = get_level_1_files(
subdir, [&](double progress) { progress_receiver(progress / 2); });
for (size_t i = 0; i < files.size(); ++i) {
diff --git a/src/storage/local/LocalStorage_compress.cpp b/src/storage/local/LocalStorage_compress.cpp
index 81caa40b..6fbe0eba 100644
--- a/src/storage/local/LocalStorage_compress.cpp
+++ b/src/storage/local/LocalStorage_compress.cpp
@@ -58,19 +58,19 @@ LocalStorage::get_compression_statistics(
for_each_level_1_subdir(
m_config.cache_dir(),
[&](const auto& subdir, const auto& sub_progress_receiver) {
- const std::vector<CacheFile> files = get_level_1_files(
+ const auto files = get_level_1_files(
subdir, [&](double progress) { sub_progress_receiver(progress / 2); });
for (size_t i = 0; i < files.size(); ++i) {
const auto& cache_file = files[i];
- cs.on_disk_size += cache_file.lstat().size_on_disk();
+ cs.on_disk_size += cache_file.size_on_disk();
try {
core::CacheEntry::Header header(cache_file.path());
- cs.compr_size += cache_file.lstat().size();
+ cs.compr_size += cache_file.size();
cs.content_size += header.entry_size;
} catch (core::Error&) {
- cs.incompr_size += cache_file.lstat().size();
+ cs.incompr_size += cache_file.size();
}
sub_progress_receiver(1.0 / 2 + 1.0 * i / files.size() / 2);
@@ -96,17 +96,16 @@ LocalStorage::recompress(const std::optional<int8_t> level,
for_each_level_1_subdir(
m_config.cache_dir(),
[&](const auto& subdir, const auto& sub_progress_receiver) {
- std::vector<CacheFile> files =
- get_level_1_files(subdir, [&](double progress) {
- sub_progress_receiver(0.1 * progress);
- });
+ auto files = get_level_1_files(subdir, [&](double progress) {
+ sub_progress_receiver(0.1 * progress);
+ });
auto stats_file = subdir + "/stats";
for (size_t i = 0; i < files.size(); ++i) {
const auto& file = files[i];
- if (file.type() != CacheFile::Type::unknown) {
+ if (file_type_from_path(file.path()) != FileType::unknown) {
thread_pool.enqueue(
[&recompressor, &incompressible_size, level, stats_file, file] {
try {
@@ -118,11 +117,11 @@ LocalStorage::recompress(const std::optional<int8_t> level,
});
} catch (core::Error&) {
// Ignore for now.
- incompressible_size += file.lstat().size_on_disk();
+ incompressible_size += file.size_on_disk();
}
});
} else if (!TemporaryFile::is_tmp_file(file.path())) {
- incompressible_size += file.lstat().size_on_disk();
+ incompressible_size += file.size_on_disk();
}
sub_progress_receiver(0.1 + 0.9 * i / files.size());
diff --git a/src/storage/local/util.cpp b/src/storage/local/util.cpp
index 1035925d..6480906d 100644
--- a/src/storage/local/util.cpp
+++ b/src/storage/local/util.cpp
@@ -40,11 +40,11 @@ for_each_level_1_subdir(const std::string& cache_dir,
progress_receiver(1.0);
}
-std::vector<CacheFile>
+std::vector<Stat>
get_level_1_files(const std::string& dir,
const ProgressReceiver& progress_receiver)
{
- std::vector<CacheFile> files;
+ std::vector<Stat> files;
if (!Stat::stat(dir)) {
return files;
@@ -60,7 +60,7 @@ get_level_1_files(const std::string& dir,
}
if (!is_dir) {
- files.emplace_back(path);
+ files.emplace_back(Stat::lstat(path));
} else if (path != dir
&& path.find('/', dir.size() + 1) == std::string::npos) {
++level_2_directories;
diff --git a/src/storage/local/util.hpp b/src/storage/local/util.hpp
index 8aa9b873..325f04e7 100644
--- a/src/storage/local/util.hpp
+++ b/src/storage/local/util.hpp
@@ -18,7 +18,7 @@
#pragma once
-#include <storage/local/CacheFile.hpp>
+#include <Stat.hpp>
#include <functional>
#include <string>
@@ -55,8 +55,7 @@ void for_each_level_1_subdir(const std::string& cache_dir,
// Parameters:
// - dir: The directory to traverse recursively.
// - progress_receiver: Function that will be called for progress updates.
-std::vector<CacheFile>
-get_level_1_files(const std::string& dir,
- const ProgressReceiver& progress_receiver);
+std::vector<Stat> get_level_1_files(const std::string& dir,
+ const ProgressReceiver& progress_receiver);
} // namespace storage::local