#include "Util.hpp"
#include "exceptions.hpp"
-#include "third_party/fmt/core.h"
-
AtomicFile::AtomicFile(const std::string& path, Mode mode) : m_path(path)
{
TemporaryFile tmp_file(path + ".tmp");
AtomicFile::write(const std::string& data)
{
if (fwrite(data.data(), data.size(), 1, m_stream) != 1) {
- throw Error(
- fmt::format("failed to write data to {}: {}", m_path, strerror(errno)));
+ throw Error("failed to write data to {}: {}", m_path, strerror(errno));
}
}
AtomicFile::write(const std::vector<uint8_t>& data)
{
if (fwrite(data.data(), data.size(), 1, m_stream) != 1) {
- throw Error(
- fmt::format("failed to write data to {}: {}", m_path, strerror(errno)));
+ throw Error("failed to write data to {}: {}", m_path, strerror(errno));
}
}
m_stream = nullptr;
if (result == EOF) {
Util::unlink_tmp(m_tmp_path);
- throw Error(
- fmt::format("failed to write data to {}: {}", m_path, strerror(errno)));
+ throw Error("failed to write data to {}: {}", m_path, strerror(errno));
}
Util::rename(m_tmp_path, m_path);
}
-// Copyright (C) 2019 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
Util::big_endian_to_int(header_bytes + 7, m_content_size);
if (memcmp(m_magic, expected_magic, sizeof(m_magic)) != 0) {
- throw Error(fmt::format("Bad magic value 0x{:02x}{:02x}{:02x}{:02x}",
- m_magic[0],
- m_magic[1],
- m_magic[2],
- m_magic[3]));
+ throw Error("Bad magic value 0x{:02x}{:02x}{:02x}{:02x}",
+ m_magic[0],
+ m_magic[1],
+ m_magic[2],
+ m_magic[3]);
}
if (m_version != expected_version) {
- throw Error(fmt::format(
- "Unknown version (actual {}, expected {})", m_version, expected_version));
+ throw Error(
+ "Unknown version (actual {}, expected {})", m_version, expected_version);
}
m_checksum.update(header_bytes, sizeof(header_bytes));
Util::big_endian_to_int(buffer, expected_digest);
if (actual_digest != expected_digest) {
- throw Error(
- fmt::format("Incorrect checksum (actual 0x{:016x}, expected 0x{:016x})",
- actual_digest,
- expected_digest));
+ throw Error("Incorrect checksum (actual 0x{:016x}, expected 0x{:016x})",
+ actual_digest,
+ expected_digest);
}
m_decompressor->finalize();
-// Copyright (C) 2019 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
return Type::zstd;
}
- throw Error(fmt::format("Unknown type: {}", type));
+ throw Error("Unknown type: {}", type);
}
std::string
if (value == "0" || lower_value == "false" || lower_value == "disable"
|| lower_value == "no") {
throw Error(
- fmt::format("invalid boolean environment variable value \"{}\" (did"
- " you mean to set \"CCACHE_{}{}=true\"?)",
- value,
- negate ? "" : "NO",
- *env_var_key));
+ "invalid boolean environment variable value \"{}\" (did you mean to"
+ " set \"CCACHE_{}{}=true\"?)",
+ value,
+ negate ? "" : "NO",
+ *env_var_key);
}
return !negate;
} else if (value == "true") {
} else if (value == "false") {
return false;
} else {
- throw Error(fmt::format("not a boolean value: \"{}\"", value));
+ throw Error("not a boolean value: \"{}\"", value);
}
}
throw Error(e.what());
}
if (end != value.size()) {
- throw Error(fmt::format("invalid floating point: \"{}\"", value));
+ throw Error("invalid floating point: \"{}\"", value);
}
return result;
}
size_t end;
uint32_t result = std::stoul(value, &end, 8);
if (end != value.size()) {
- throw Error(fmt::format("not an octal integer: \"{}\"", value));
+ throw Error("not an octal integer: \"{}\"", value);
}
return result;
}
verify_absolute_path(const std::string& value)
{
if (!Util::is_absolute_path(value)) {
- throw Error(fmt::format("not an absolute path: \"{}\"", value));
+ throw Error("not an absolute path: \"{}\"", value);
}
}
config_line_handler(line, key, value);
}
} catch (const Error& e) {
- throw Error(fmt::format("{}:{}: {}", path, line_number, e.what()));
+ throw Error("{}:{}: {}", path, line_number, e.what());
}
}
return true;
try {
set_item(config_key, value, key, negate, "environment");
} catch (const Error& e) {
- throw Error(
- fmt::format("CCACHE_{}{}: {}", negate ? "NO" : "", key, e.what()));
+ throw Error("CCACHE_{}{}: {}", negate ? "NO" : "", key, e.what());
}
}
}
{
auto it = k_config_key_table.find(key);
if (it == k_config_key_table.end()) {
- throw Error(fmt::format("unknown configuration option \"{}\"", key));
+ throw Error("unknown configuration option \"{}\"", key);
}
switch (it->second) {
const std::string& value)
{
if (k_config_key_table.find(key) == k_config_key_table.end()) {
- throw Error(fmt::format("unknown configuration option \"{}\"", key));
+ throw Error("unknown configuration option \"{}\"", key);
}
// Verify that the value is valid; set_item will throw if not.
output.write(fmt::format("{}\n", c_line));
}
})) {
- throw Error(fmt::format("failed to open {}: {}", path, strerror(errno)));
+ throw Error("failed to open {}: {}", path, strerror(errno));
}
if (!found) {
{
for (const auto& item : k_env_variable_table) {
if (k_config_key_table.find(item.second) == k_config_key_table.end()) {
- throw Error(fmt::format(
+ throw Error(
"env var {} mapped to {} which is missing from k_config_key_table",
item.first,
- item.second));
+ item.second);
}
}
}
-// Copyright (C) 2019 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
}
if (i != n_entries) {
- throw Error(
- fmt::format("Too few entries (read {}, expected {})", i, n_entries));
+ throw Error("Too few entries (read {}, expected {})", i, n_entries);
}
cache_entry_reader.finalize();
break;
default:
- throw Error(fmt::format("Unknown entry type: {}", marker));
+ throw Error("Unknown entry type: {}", marker);
}
UnderlyingFileTypeInt type;
auto raw_path = get_raw_file_path(m_result_path, entry_number);
auto st = Stat::stat(raw_path, Stat::OnError::throw_error);
if (st.size() != file_len) {
- throw Error(
- fmt::format("Bad file size of {} (actual {} bytes, expected {} bytes)",
- raw_path,
- st.size(),
- file_len));
+ throw Error("Bad file size of {} (actual {} bytes, expected {} bytes)",
+ raw_path,
+ st.size(),
+ file_len);
}
consumer.on_entry_start(entry_number, file_type, file_len, raw_path);
{
Fd file(open(path.c_str(), O_RDONLY | O_BINARY));
if (!file) {
- throw Error(fmt::format("Failed to open {} for reading", path));
+ throw Error("Failed to open {} for reading", path);
}
uint64_t remain = file_size;
if (errno == EINTR) {
continue;
}
- throw Error(
- fmt::format("Error reading from {}: {}", path, strerror(errno)));
+ throw Error("Error reading from {}: {}", path, strerror(errno));
}
if (bytes_read == 0) {
- throw Error(fmt::format("Error reading from {}: end of file", path));
+ throw Error("Error reading from {}: end of file", path);
}
writer.write(buf, n);
remain -= n;
try {
Util::clone_hard_link_or_copy_file(m_ctx, path, raw_file, true);
} catch (Error& e) {
- throw Error(fmt::format(
- "Failed to store {} as raw file {}: {}", path, raw_file, e.what()));
+ throw Error(
+ "Failed to store {} as raw file {}: {}", path, raw_file, e.what());
}
auto new_stat = Stat::stat(raw_file);
stats_update_size(m_ctx.counter_updates,
#include "Util.hpp"
-#include "third_party/nonstd/string_view.hpp"
-
-using nonstd::string_view;
-
ResultExtractor::ResultExtractor(const std::string& directory)
: m_directory(directory)
{
m_dest_fd = Fd(
open(m_dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
if (!m_dest_fd) {
- throw Error(fmt::format(
- "Failed to open {} for writing: {}", m_dest_path, strerror(errno)));
+ throw Error(
+ "Failed to open {} for writing: {}", m_dest_path, strerror(errno));
}
} else {
try {
Util::copy_file(*raw_file, m_dest_path, false);
} catch (Error& e) {
- throw Error(fmt::format(
- "Failed to copy {} to {}: {}", *raw_file, m_dest_path, e.what()));
+ throw Error(
+ "Failed to copy {} to {}: {}", *raw_file, m_dest_path, e.what());
}
}
}
try {
Util::write_fd(*m_dest_fd, data, size);
} catch (Error& e) {
- throw Error(
- fmt::format("Failed to write to {}: {}", m_dest_path, e.what()));
+ throw Error("Failed to write to {}: {}", m_dest_path, e.what());
}
}
#include "Context.hpp"
#include "Logging.hpp"
-#include "third_party/nonstd/string_view.hpp"
-
using Logging::log;
-using nonstd::string_view;
using Result::FileType;
ResultRetriever::ResultRetriever(Context& ctx, bool rewrite_dependency_target)
m_dest_fd = Fd(
open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
if (!m_dest_fd) {
- throw Error(fmt::format(
- "Failed to open {} for writing: {}", dest_path, strerror(errno)));
+ throw Error(
+ "Failed to open {} for writing: {}", dest_path, strerror(errno));
}
m_dest_path = dest_path;
}
try {
Util::write_fd(*m_dest_fd, data, size);
} catch (Error& e) {
- throw Error(
- fmt::format("Failed to write to {}: {}", m_dest_path, e.what()));
+ throw Error("Failed to write to {}: {}", m_dest_path, e.what());
}
}
}
m_dest_data.data() + start_pos,
m_dest_data.length() - start_pos);
} catch (Error& e) {
- throw Error(
- fmt::format("Failed to write to {}: {}", m_dest_path, e.what()));
+ throw Error("Failed to write to {}: {}", m_dest_path, e.what());
}
}
#include "Logging.hpp"
-#include "third_party/fmt/core.h"
-
using Logging::log;
Stat::Stat(StatFunction stat_function,
} else {
m_errno = errno;
if (on_error == OnError::throw_error) {
- throw Error(fmt::format("failed to stat {}: {}", path, strerror(errno)));
+ throw Error("failed to stat {}: {}", path, strerror(errno));
}
if (on_error == OnError::log) {
log("Failed to stat {}: {}", path, strerror(errno));
# if defined(__linux__)
Fd src_fd(open(src.c_str(), O_RDONLY));
if (!src_fd) {
- throw Error(fmt::format("{}: {}", src, strerror(errno)));
+ throw Error("{}: {}", src, strerror(errno));
}
Fd dest_fd;
dest_fd =
Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
if (!dest_fd) {
- throw Error(fmt::format("{}: {}", src, strerror(errno)));
+ throw Error("{}: {}", src, strerror(errno));
}
}
{
Fd src_fd(open(src.c_str(), O_RDONLY));
if (!src_fd) {
- throw Error(fmt::format("{}: {}", src, strerror(errno)));
+ throw Error("{}: {}", src, strerror(errno));
}
Fd dest_fd;
dest_fd =
Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
if (!dest_fd) {
- throw Error(fmt::format("{}: {}", dest, strerror(errno)));
+ throw Error("{}: {}", dest, strerror(errno));
}
}
++right;
}
if (curly && *right != '}') {
- throw Error(
- fmt::format("syntax error: missing '}}' after \"{}\"", left));
+ throw Error("syntax error: missing '}}' after \"{}\"", left);
}
if (right == left) {
// Special case: don't consider a single $ the left of a variable.
std::string name(left, right - left);
const char* value = getenv(name.c_str());
if (!value) {
- throw Error(fmt::format("environment variable \"{}\" not set", name));
+ throw Error("environment variable \"{}\" not set", name);
}
result += value;
if (!curly) {
factor = 1;
break;
default:
- throw Error(fmt::format(
- "invalid suffix (supported: d (day) and s (second)): \"{}\"", duration));
+ throw Error("invalid suffix (supported: d (day) and s (second)): \"{}\"",
+ duration);
}
return factor * parse_uint32(duration.substr(0, duration.length() - 1));
failed = true;
}
if (failed || end != value.size()) {
- throw Error(fmt::format("invalid integer: \"{}\"", value));
+ throw Error("invalid integer: \"{}\"", value);
}
return result;
}
char* p;
double result = strtod(value.c_str(), &p);
if (errno != 0 || result < 0 || p == value.c_str() || value.empty()) {
- throw Error(fmt::format("invalid size: \"{}\"", value));
+ throw Error("invalid size: \"{}\"", value);
}
while (isspace(*p)) {
result *= multiplier;
break;
default:
- throw Error(fmt::format("invalid size: \"{}\"", value));
+ throw Error("invalid size: \"{}\"", value);
}
} else {
// Default suffix: G.
}
if (failed || end != value.size() || result < 0
|| result > std::numeric_limits<uint32_t>::max()) {
- throw Error(fmt::format("invalid 32-bit unsigned integer: \"{}\"", value));
+ throw Error("invalid 32-bit unsigned integer: \"{}\"", value);
}
return result;
}
{
#ifndef _WIN32
if (::rename(oldpath.c_str(), newpath.c_str()) != 0) {
- throw Error(fmt::format(
- "failed to rename {} to {}: {}", oldpath, newpath, strerror(errno)));
+ throw Error(
+ "failed to rename {} to {}: {}", oldpath, newpath, strerror(errno));
}
#else
// Windows' rename() won't overwrite an existing file, so need to use
if (!MoveFileExA(
oldpath.c_str(), newpath.c_str(), MOVEFILE_REPLACE_EXISTING)) {
DWORD error = GetLastError();
- throw Error(fmt::format("failed to rename {} to {}: {}",
- oldpath,
- newpath,
- Win32Util::error_message(error)));
+ throw Error("failed to rename {} to {}: {}",
+ oldpath,
+ newpath,
+ Win32Util::error_message(error));
}
#endif
}
try {
write_fd(STDERR_FILENO, text_to_send->data(), text_to_send->length());
} catch (Error& e) {
- throw Error(fmt::format("Failed to write to stderr: {}", e.what()));
+ throw Error("Failed to write to stderr: {}", e.what());
}
}
if (stat.error_number() == ENOENT || stat.error_number() == ESTALE) {
continue;
}
- throw Error(fmt::format("failed to lstat {}: {}",
- entry_path,
- strerror(stat.error_number())));
+ throw Error("failed to lstat {}: {}",
+ entry_path,
+ strerror(stat.error_number()));
}
is_dir = stat.is_directory();
}
} else if (errno == ENOTDIR) {
visitor(path, false);
} else {
- throw Error(
- fmt::format("failed to open directory {}: {}", path, strerror(errno)));
+ throw Error("failed to open directory {}: {}", path, strerror(errno));
}
}
traverse(path, [](const std::string& p, bool is_dir) {
if (is_dir) {
if (rmdir(p.c_str()) != 0 && errno != ENOENT && errno != ESTALE) {
- throw Error(fmt::format("failed to rmdir {}: {}", p, strerror(errno)));
+ throw Error("failed to rmdir {}: {}", p, strerror(errno));
}
} else if (unlink(p.c_str()) != 0 && errno != ENOENT && errno != ESTALE) {
- throw Error(fmt::format("failed to unlink {}: {}", p, strerror(errno)));
+ throw Error("failed to unlink {}: {}", p, strerror(errno));
}
});
}
// for the -o=K=V case (key "=K" and value "V").
size_t eq_pos = arg.find('=', 1);
if (eq_pos == std::string::npos) {
- throw Error(fmt::format("missing equal sign in \"{}\"", arg));
+ throw Error("missing equal sign in \"{}\"", arg);
}
std::string key = arg.substr(0, eq_pos);
std::string value = arg.substr(eq_pos + 1);
{
File f(path, mode);
if (!f) {
- throw Error(
- fmt::format("failed to open {} for reading: {}", path, strerror(errno)));
+ throw Error("failed to open {} for reading: {}", path, strerror(errno));
}
return f;
}
create_reader(const CacheFile& cache_file, FILE* stream)
{
if (cache_file.type() == CacheFile::Type::unknown) {
- throw Error(fmt::format("unknown file type for {}", cache_file.path()));
+ throw Error("unknown file type for {}", cache_file.path());
}
switch (cache_file.type()) {
#include "third_party/nonstd/optional.hpp"
#include <stdexcept>
+#include <string>
#include <utility>
// Don't throw or catch ErrorBase directly, use a subclass.
// treated similar to FatalError.
class Error : public ErrorBase
{
- using ErrorBase::ErrorBase;
+public:
+ // Special case: If given only one string, don't parse it as a format string.
+ Error(const std::string& message);
+
+ // `args` are forwarded to `fmt::format`.
+ template<typename... T> inline Error(T&&... args);
};
+inline Error::Error(const std::string& message) : ErrorBase(message)
+{
+}
+
+template<typename... T>
+inline Error::Error(T&&... args)
+ : ErrorBase(fmt::format(std::forward<T>(args)...))
+{
+}
+
// Throw a FatalError to make ccache print the error message to stderr and exit
// with a non-zero exit code.
class FatalError : public ErrorBase
check_chdir(const std::string& dir)
{
if (chdir(dir.c_str()) != 0) {
- throw Error(fmt::format(
- "failed to change directory to {}: {}", dir, strerror(errno)));
+ throw Error("failed to change directory to {}: {}", dir, strerror(errno));
}
}