Fix save_index issues

This commmit fixes some issues related to save_index:
* fixes a bug in save_index_class::remove() method.
  The method incorrectly tries to remove an attribute from root config instead of removing 'save' children.
  In effect, save files were not being removed from the save_index file.
* removes call to save_index_manager_->remove() in savegame::finish_save_game
  This call to remove() is incorrect and has no effect except causing unneeded call to write_save_index().
* introduces a new clean_up_index() method in save_index_class.
  The method removes non-existing save files from the index. Clean up is triggered only during the first
  synchronisation of the index to disc and only if the index has more save files than found on disk.
This commit is contained in:
Grzegorz Halat 2021-04-18 01:50:09 +02:00 committed by Steve Cotton
parent f5ea3b0906
commit 430d640408
3 changed files with 27 additions and 3 deletions

View file

@ -70,7 +70,7 @@ void save_index_class::rebuild(const std::string& name, const std::time_t& modif
void save_index_class::remove(const std::string& name)
{
config& root = data();
root.remove_attribute(name);
root.remove_children("save", [&name](const config& d) { return name == d["save"]; });
write_save_index();
}
@ -97,6 +97,22 @@ const std::string& save_index_class::dir() const
return dir_;
}
void save_index_class::clean_up_index()
{
config &root = data();
std::vector<std::string> filenames;
filesystem::get_files_in_dir(dir(), &filenames);
if(root.all_children_count() > filenames.size()) {
root.remove_children("save", [&filenames](const config& d)
{
return std::find(filenames.begin(), filenames.end(), d["save"]) == filenames.end();
}
);
}
}
void save_index_class::write_save_index()
{
log_scope("write_save_index()");
@ -106,6 +122,11 @@ void save_index_class::write_save_index()
return;
}
if(clean_up_index_) {
clean_up_index();
clean_up_index_ = false;
}
try {
filesystem::scoped_ostream stream = filesystem::ostream_file(filesystem::get_save_index_file());
@ -126,6 +147,7 @@ save_index_class::save_index_class(const std::string& dir)
, modified_()
, dir_(dir)
, read_only_(true)
, clean_up_index_(true)
{
}

View file

@ -124,6 +124,8 @@ private:
config& data();
static void fix_leader_image_path(config& data);
/** Deletes non-existent save files from the index. */
void clean_up_index();
bool loaded_;
config data_;
@ -134,5 +136,7 @@ private:
* write_save_index() and delete() are no-ops.
*/
bool read_only_;
/** Flag to only run the clean_up_index method once. */
bool clean_up_index_;
};
} // end of namespace savegame

View file

@ -541,8 +541,6 @@ void savegame::finish_save_game(const config_writer& out)
if(!out.good()) {
throw game::save_game_failed(_("Could not write to file"));
}
save_index_manager_->remove(gamestate_.classification().label);
} catch(const filesystem::io_exception& e) {
throw game::save_game_failed(e.what());
}