#include "assertions.hpp"
#include "fmtmacros.hpp"
+#include <UmaskScope.hpp>
#include <compression/types.hpp>
#include <core/exceptions.hpp>
#include <core/wincompat.hpp>
void
Config::set_value_in_file(const std::string& path,
const std::string& key,
- const std::string& value)
+ const std::string& value) const
{
+ UmaskScope umask_scope(m_umask);
+
if (k_config_key_table.find(key) == k_config_key_table.end()) {
throw core::Error("unknown configuration option \"{}\"", key);
}
void visit_items(const ItemVisitor& item_visitor) const;
- static void set_value_in_file(const std::string& path,
- const std::string& key,
- const std::string& value);
+ void set_value_in_file(const std::string& path,
+ const std::string& key,
+ const std::string& value) const;
// Called from unit tests.
static void check_key_tables_consistency();
case 'F': { // --max-files
auto files = util::value_or_throw<core::Error>(util::parse_unsigned(arg));
- Config::set_value_in_file(
+ ctx.config.set_value_in_file(
ctx.config.primary_config_path(), "max_files", arg);
if (files == 0) {
PRINT_RAW(stdout, "Unset cache file limit\n");
case 'M': { // --max-size
uint64_t size = Util::parse_size(arg);
- Config::set_value_in_file(
+ ctx.config.set_value_in_file(
ctx.config.primary_config_path(), "max_size", arg);
if (size == 0) {
PRINT_RAW(stdout, "Unset cache size limit\n");
}
std::string key = arg.substr(0, eq_pos);
std::string value = arg.substr(eq_pos + 1);
- Config::set_value_in_file(ctx.config.primary_config_path(), key, value);
+ ctx.config.set_value_in_file(
+ ctx.config.primary_config_path(), key, value);
break;
}
TEST_CASE("Config::set_value_in_file")
{
TestContext test_context;
+ Config config;
SUBCASE("set new value")
{
Util::write_file("ccache.conf", "path = vanilla\n");
- Config::set_value_in_file("ccache.conf", "compiler", "chocolate");
+ config.set_value_in_file("ccache.conf", "compiler", "chocolate");
std::string content = Util::read_file("ccache.conf");
CHECK(content == "path = vanilla\ncompiler = chocolate\n");
}
SUBCASE("existing value")
{
Util::write_file("ccache.conf", "path = chocolate\nstats = chocolate\n");
- Config::set_value_in_file("ccache.conf", "path", "vanilla");
+ config.set_value_in_file("ccache.conf", "path", "vanilla");
std::string content = Util::read_file("ccache.conf");
CHECK(content == "path = vanilla\nstats = chocolate\n");
}
{
Util::write_file("ccache.conf", "path = chocolate\nstats = chocolate\n");
try {
- Config::set_value_in_file("ccache.conf", "foo", "bar");
+ config.set_value_in_file("ccache.conf", "foo", "bar");
CHECK(false);
} catch (const core::Error& e) {
CHECK(std::string(e.what()) == "unknown configuration option \"foo\"");
SUBCASE("unknown sloppiness")
{
Util::write_file("ccache.conf", "path = vanilla\n");
- Config::set_value_in_file("ccache.conf", "sloppiness", "foo");
+ config.set_value_in_file("ccache.conf", "sloppiness", "foo");
std::string content = Util::read_file("ccache.conf");
CHECK(content == "path = vanilla\nsloppiness = foo\n");
}
SUBCASE("comments are kept")
{
Util::write_file("ccache.conf", "# c1\npath = blueberry\n#c2\n");
- Config::set_value_in_file("ccache.conf", "path", "vanilla");
- Config::set_value_in_file("ccache.conf", "compiler", "chocolate");
+ config.set_value_in_file("ccache.conf", "path", "vanilla");
+ config.set_value_in_file("ccache.conf", "compiler", "chocolate");
std::string content = Util::read_file("ccache.conf");
CHECK(content == "# c1\npath = vanilla\n#c2\ncompiler = chocolate\n");
}