| 476 | } |
| 477 | |
| 478 | static void autosaveClean() |
| 479 | { |
| 480 | try |
| 481 | { |
| 482 | auto autosaveDirectory = Environment::getPath(Environment::PathId::autosave); |
| 483 | if (fs::is_directory(autosaveDirectory)) |
| 484 | { |
| 485 | std::vector<fs::path> autosaveFiles; |
| 486 | |
| 487 | // Collect all the autosave files |
| 488 | for (auto& f : fs::directory_iterator(autosaveDirectory)) |
| 489 | { |
| 490 | if (f.is_regular_file()) |
| 491 | { |
| 492 | auto& path = f.path(); |
| 493 | auto filename = path.filename().u8string(); |
| 494 | if (Utility::startsWith(filename, "autosave_") && Utility::endsWith(filename, S5::extensionSV5, true)) |
| 495 | { |
| 496 | autosaveFiles.push_back(path); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | auto amountToKeep = static_cast<size_t>(std::max(1, Config::get().autosaveAmount)); |
| 502 | if (autosaveFiles.size() > amountToKeep) |
| 503 | { |
| 504 | // Sort them by name (which should correspond to date order) |
| 505 | std::sort(autosaveFiles.begin(), autosaveFiles.end()); |
| 506 | |
| 507 | // Delete excess files |
| 508 | auto numToDelete = autosaveFiles.size() - amountToKeep; |
| 509 | for (size_t i = 0; i < numToDelete; i++) |
| 510 | { |
| 511 | auto path8 = autosaveFiles[i].u8string(); |
| 512 | Logging::info("Deleting old autosave: {}", path8.c_str()); |
| 513 | fs::remove(autosaveFiles[i]); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | catch (const std::exception& e) |
| 519 | { |
| 520 | Logging::error("Unable to clean autosaves: {}", e.what()); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | static void autosave() |
| 525 | { |
no test coverage detected