make backup of kartconfig before writing to it
This commit is contained in:
1 parent
8dda864909
commit
977ee8ccfc
2 files changed
+50
No files matched your search
@@ -334,6 +334,38 @@ boolean FIL_FileOK(char const *name)
|
||||
return access(name,6)+1; //R_OK|W_OK
|
||||
}
|
||||
|
||||
/** Copies contents of one file to another
|
||||
*
|
||||
* \param src File to copy from
|
||||
* \param dst File to write into
|
||||
* \return true if copy was succesful, false if it doesn't.
|
||||
*/
|
||||
boolean FIL_CopyFile(const char *src, const char *dst)
|
||||
{
|
||||
FILE *srcFile = fopen(src, "rb");
|
||||
|
||||
if (srcFile == NULL)
|
||||
return false;
|
||||
|
||||
FILE *dstFile = fopen(dst, "wb");
|
||||
|
||||
if (dstFile == NULL)
|
||||
{
|
||||
fclose(srcFile);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[1024];
|
||||
size_t n;
|
||||
|
||||
while ((n = fread(buffer, 1, sizeof(buffer), srcFile)) > 0)
|
||||
fwrite(buffer, 1, n, dstFile);
|
||||
|
||||
fclose(srcFile);
|
||||
fclose(dstFile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Checks if a pathname has a file extension and adds the extension provided
|
||||
* if not.
|
||||
@@ -525,11 +557,28 @@ void M_SaveConfig(const char *filename)
|
||||
{
|
||||
FILE *f;
|
||||
char *filepath;
|
||||
char backupfile[MAX_WADPATH+4];
|
||||
|
||||
// make sure not to write back the config until it's been correctly loaded
|
||||
if (!gameconfig_loaded)
|
||||
return;
|
||||
|
||||
// Create backup of the config file
|
||||
snprintf(backupfile, sizeof backupfile, "%s.bak", configfile);
|
||||
backupfile[sizeof backupfile - 1] = '\0';
|
||||
|
||||
FILE *config = fopen(configfile, "r");
|
||||
|
||||
if (config != NULL)
|
||||
{
|
||||
fclose(config);
|
||||
if (FIL_CopyFile(configfile, backupfile) != false)
|
||||
{
|
||||
CONS_Alert(CONS_WARNING,"Failed to create a backup of the configuration file. Will not attempt to write to file\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// can change the file name
|
||||
if (filename)
|
||||
{
|
||||
|
||||
@@ -54,6 +54,7 @@ boolean FIL_FileExists(const char *name);
|
||||
boolean FIL_WriteFileOK(char const *name);
|
||||
boolean FIL_ReadFileOK(char const *name);
|
||||
boolean FIL_FileOK(char const *name);
|
||||
boolean FIL_CopyFile(const char *src, const char *dst);
|
||||
|
||||
void FIL_DefaultExtension (char *path, const char *extension);
|
||||
void FIL_ForceExtension(char *path, const char *extension);
|
||||
|
||||
Reference in new issue
Block a user