LibCore+Everywhere: Return ErrorOr from ConfigFile::sync()

Currently this method always succeeds, but that won't be true once we
switch to the Core::Stream API. :^)

Some of these places would ideally show an error message to the user,
since failure to save a file is significant, but let's not get
distracted right now.
This commit is contained in:
Sam Atkins 2022-02-06 14:26:33 +00:00 committed by Tim Flynn
parent b90dc408bd
commit cd0ffe5460
Notes: sideshowbarker 2024-07-17 18:39:04 +09:00
10 changed files with 39 additions and 24 deletions

View file

@ -374,7 +374,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
theme->write_entry("Paths", to_string(role), preview_widget.preview_palette().path(role));
}
theme->sync();
if (auto sync_result = theme->sync(); sync_result.is_error()) {
// FIXME: Expose this to the user, since failing to save is important to know about!
dbgln("Failed to save theme file: {}", sync_result.error());
}
};
TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {

View file

@ -61,7 +61,7 @@ ConfigFile::ConfigFile(String const&, NonnullRefPtr<File> open_file)
ConfigFile::~ConfigFile()
{
sync();
MUST(sync());
}
void ConfigFile::reparse()
@ -168,10 +168,10 @@ void ConfigFile::write_color_entry(String const& group, String const& key, Color
write_entry(group, key, String::formatted("{},{},{},{}", value.red(), value.green(), value.blue(), value.alpha()));
}
bool ConfigFile::sync()
ErrorOr<void> ConfigFile::sync()
{
if (!m_dirty)
return true;
return {};
m_file->truncate(0);
m_file->seek(0);
@ -184,7 +184,7 @@ bool ConfigFile::sync()
}
m_dirty = false;
return true;
return {};
}
void ConfigFile::dump() const

View file

@ -52,7 +52,7 @@ public:
bool is_dirty() const { return m_dirty; }
bool sync();
ErrorOr<void> sync();
void remove_group(String const& group);
void remove_entry(String const& group, String const& key);

View file

@ -191,7 +191,8 @@ void Mixer::request_setting_sync()
m_config_write_timer = Core::Timer::create_single_shot(
AUDIO_CONFIG_WRITE_INTERVAL,
[this] {
m_config->sync();
if (auto result = m_config->sync(); result.is_error())
dbgln("Failed to write audio mixer config: {}", result.error());
},
this);
m_config_write_timer->start();

View file

@ -131,7 +131,11 @@ void ClientConnection::sync_dirty_domains_to_disk()
dbgln("Syncing {} dirty domains to disk", dirty_domains.size());
for (auto domain : dirty_domains) {
auto& config = ensure_domain_config(domain);
config.sync();
if (auto result = config.sync(); result.is_error()) {
dbgln("Failed to write config '{}' to disk: {}", domain, result.error());
// Put it back in the list since it's still dirty.
m_dirty_domains.set(domain);
}
}
}

View file

@ -783,26 +783,26 @@ bool Compositor::set_background_color(const String& background_color)
auto& wm = WindowManager::the();
wm.config()->write_entry("Background", "Color", background_color);
bool ret_val = wm.config()->sync();
bool succeeded = !wm.config()->sync().is_error();
if (ret_val)
if (succeeded)
Compositor::invalidate_screen();
return ret_val;
return succeeded;
}
bool Compositor::set_wallpaper_mode(const String& mode)
{
auto& wm = WindowManager::the();
wm.config()->write_entry("Background", "Mode", mode);
bool ret_val = wm.config()->sync();
bool succeeded = !wm.config()->sync().is_error();
if (ret_val) {
if (succeeded) {
m_wallpaper_mode = mode_to_enum(mode);
Compositor::invalidate_screen();
}
return ret_val;
return succeeded;
}
bool Compositor::set_wallpaper(RefPtr<Gfx::Bitmap> bitmap)

View file

@ -271,7 +271,7 @@ bool ScreenLayout::save_config(Core::ConfigFile& config_file, bool sync) const
config_file.remove_group(group_name);
}
if (sync && !config_file.sync())
if (sync && config_file.sync().is_error())
return false;
return true;
}

View file

@ -254,7 +254,7 @@ bool WindowManager::apply_workspace_settings(unsigned rows, unsigned columns, bo
if (save) {
m_config->write_num_entry("Workspace", "Rows", window_stack_rows());
m_config->write_num_entry("Workspace", "Columns", window_stack_columns());
return m_config->sync();
return !m_config->sync().is_error();
}
return true;
}
@ -264,7 +264,8 @@ void WindowManager::set_acceleration_factor(double factor)
ScreenInput::the().set_acceleration_factor(factor);
dbgln("Saving acceleration factor {} to config file at {}", factor, m_config->filename());
m_config->write_entry("Mouse", "AccelerationFactor", String::formatted("{}", factor));
m_config->sync();
if (auto result = m_config->sync(); result.is_error())
dbgln("Failed to save config file: {}", result.error());
}
void WindowManager::set_scroll_step_size(unsigned step_size)
@ -272,7 +273,8 @@ void WindowManager::set_scroll_step_size(unsigned step_size)
ScreenInput::the().set_scroll_step_size(step_size);
dbgln("Saving scroll step size {} to config file at {}", step_size, m_config->filename());
m_config->write_entry("Mouse", "ScrollStepSize", String::number(step_size));
m_config->sync();
if (auto result = m_config->sync(); result.is_error())
dbgln("Failed to save config file: {}", result.error());
}
void WindowManager::set_double_click_speed(int speed)
@ -281,7 +283,8 @@ void WindowManager::set_double_click_speed(int speed)
m_double_click_speed = speed;
dbgln("Saving double-click speed {} to config file at {}", speed, m_config->filename());
m_config->write_entry("Input", "DoubleClickSpeed", String::number(speed));
m_config->sync();
if (auto result = m_config->sync(); result.is_error())
dbgln("Failed to save config file: {}", result.error());
}
int WindowManager::double_click_speed() const
@ -294,7 +297,8 @@ void WindowManager::set_buttons_switched(bool switched)
m_buttons_switched = switched;
dbgln("Saving mouse buttons switched state {} to config file at {}", switched, m_config->filename());
m_config->write_bool_entry("Mouse", "ButtonsSwitched", switched);
m_config->sync();
if (auto result = m_config->sync(); result.is_error())
dbgln("Failed to save config file: {}", result.error());
}
bool WindowManager::get_buttons_switched() const
@ -2080,7 +2084,10 @@ bool WindowManager::update_theme(String theme_path, String theme_name)
m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(new_theme);
m_config->write_entry("Theme", "Name", theme_name);
m_config->remove_entry("Background", "Color");
m_config->sync();
if (auto result = m_config->sync(); result.is_error()) {
dbgln("Failed to save config file: {}", result.error());
return false;
}
invalidate_after_theme_or_font_change();
return true;
}

View file

@ -35,7 +35,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (value_to_write) {
config->write_entry(group, key, value_to_write);
config->sync();
TRY(config->sync());
return 0;
}

View file

@ -76,7 +76,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto keymaps = String::join(',', mappings_vector);
mapper_config->write_entry("Mapping", "Keymaps", keymaps);
mapper_config->sync();
TRY(mapper_config->sync());
rc = set_keymap(mappings_vector.first());
if (rc != 0) {
return rc;
@ -90,7 +90,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (keymaps_vector.is_empty()) {
warnln("No keymaps configured - writing default configurations (en-us)");
mapper_config->write_entry("Mapping", "Keymaps", "en-us");
mapper_config->sync();
TRY(mapper_config->sync());
keymaps_vector.append("en-us");
}