Attempt to recover SRAM write failures gracefully ...

This commit is contained in:
Themaister 2011-08-22 17:35:31 +02:00
parent b4b0be9118
commit 596d8d9985
2 changed files with 46 additions and 11 deletions

41
file.c
View File

@ -322,12 +322,40 @@ static bool dump_to_file(const char *path, const void *data, size_t size)
return false; return false;
else else
{ {
fwrite(data, 1, size, file); bool ret = fwrite(data, 1, size, file) == size;
fclose(file); fclose(file);
return true; return ret;
} }
} }
// Attempt to save valuable RAM data somewhere ...
static void dump_to_file_desperate(const void *data, size_t size)
{
#ifdef _WIN32
const char *base = getenv("APPDATA");
#else
const char *base = getenv("HOME");
#endif
if (!base)
goto error;
static unsigned count = 0;
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/SSNES-recovery-%u\n", base, count++);
if (dump_to_file(path, data, size))
SSNES_WARN("Succeeded in recovering data to \"%s\". Phew! :D\n", path);
else
goto error;
return;
error:
SSNES_WARN("Failed ... Tough luck ... :(\n");
}
bool save_state(const char *path) bool save_state(const char *path)
{ {
SSNES_LOG("Saving state: \"%s\".\n", path); SSNES_LOG("Saving state: \"%s\".\n", path);
@ -393,7 +421,14 @@ void save_ram_file(const char* path, int type)
uint8_t *data = psnes_get_memory_data(type); uint8_t *data = psnes_get_memory_data(type);
if (data && size > 0) if (data && size > 0)
dump_to_file(path, data, size); {
if (!dump_to_file(path, data, size))
{
SSNES_ERR("Failed to save SNES RAM!\n");
SSNES_WARN("Attempting to recover ...\n");
dump_to_file_desperate(data, size);
}
}
} }
static char* load_xml_map(const char *path) static char* load_xml_map(const char *path)