Skip to content

Commit

Permalink
fix: avoid backup to be replaced at next startup
Browse files Browse the repository at this point in the history
  • Loading branch information
gauthier-th committed Oct 19, 2024
1 parent d443769 commit de2c3af
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions server/lib/settings/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export const runMigrations = async (
let migrated = settings;

try {
// let's create a backup of the settings
fs.writeFileSync(
SETTINGS_PATH.replace('.json', '.old.json'),
JSON.stringify(settings, undefined, ' ')
);
// we read old backup and create a backup of currents settings
const BACKUP_PATH = SETTINGS_PATH.replace('.json', '.old.json');
const oldBackup = fs.existsSync(BACKUP_PATH)
? fs.readFileSync(BACKUP_PATH)
: null;
fs.writeFileSync(BACKUP_PATH, JSON.stringify(settings, undefined, ' '));

Check failure

Code scanning / CodeQL

Potential file system race condition High

The file may have changed since it
was checked
.

const migrations = fs
.readdirSync(migrationsDir)
Expand Down Expand Up @@ -58,6 +59,10 @@ export const runMigrations = async (
// something went wrong while saving file
throw new Error('Unable to save settings after migration.');
}
} else if (oldBackup) {
// no migration occured
// we save the old backup (to avoid settings.json and settings.old.json being the same)
fs.writeFileSync(BACKUP_PATH, oldBackup.toString());

Check failure

Code scanning / CodeQL

Potential file system race condition High

The file may have changed since it
was checked
.
}
} catch (e) {
logger.error(
Expand Down

0 comments on commit de2c3af

Please sign in to comment.