Merge pull request #523 from Keppl/backup_saves_max_patch

Limit maximum number of backups files for load state backup
This commit is contained in:
zeromus 2022-08-04 17:33:43 -04:00 committed by GitHub
commit 3a6c8cf9f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 14 deletions

View File

@ -732,7 +732,11 @@ void loadstate_slot(int num)
//save the state before we load the state, to give people a path for recovery in case they hit the wrong key
#ifdef HOST_WINDOWS
if(!GetPrivateProfileInt("General", "BackupSavestateSuppress", 0, IniName))
// Maximum backup number can be set in config: [General] BackupSavesMax=<n>
// Setting BackupSavesMax=0 would disable backups
int max_index = GetPrivateProfileInt("General", "BackupSavesMax", -1, IniName); // 0 for disabled
int suppress_backups = GetPrivateProfileInt("General", "BackupSavestateSuppress", 0, IniName);
if(!suppress_backups && max_index != 0 )
#endif
{
if(movieMode == MOVIEMODE_INACTIVE)
@ -744,25 +748,42 @@ void loadstate_slot(int num)
static const char PSS = '/';
#endif
mkdir(dirname.c_str(),0777);
static unsigned seed;
for(;;)
int cur_index = -1; // setup index, -1 in case it is first instance
if (max_index == -1) { // If not set
max_index = 200;
}
std::string index_fname = dirname + PSS + "backup.index";
FILE* index_file = fopen(index_fname.c_str(), "r+"); // Read/update but don't create
if (index_file) {
fscanf(index_file, "%d", &cur_index);
rewind(index_file); // prepare to overwrite
}
else
{
index_file = fopen(index_fname.c_str(), "w"); // Create if doesn't exist
}
if (index_file) {
cur_index = (cur_index + 1) % max_index; // next
fprintf(index_file, "%d", cur_index); // Store new index
fprintf(index_file, "%d", EOF); // Avoid overwriting just most significant digits(e.g.: 1 -> 200 = 100 )
fclose(index_file);
std::string fname = dirname + PSS;
char mini[100];
sprintf(mini,"%u",seed);
sprintf(mini, "%u", cur_index);
fname += mini + (std::string)".dst";
FILE* f = fopen(fname.c_str(),"rb");
if(f)
{
seed = rand()*16000+rand();
fclose(f);
continue;
}
seed++;
savestate_save(fname.c_str());
printf("Creating backup of current state prior to loadstate as path: %s\n",fname.c_str());
break;
printf("Creating backup of current state prior to loadstate as path: %s\n", fname.c_str());
}
else {
printf("Failed to open indexing file %s\n Prior state backup not created, check location write permissions.\n", index_fname.c_str());
}
}
}