1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-12 10:14:10 +01:00

Do not lock until after the file is created (#274)

Proper-lockfile throws if the file it's locking does not exist.
Lock around adapter creation rather than file creation.
This commit is contained in:
Matt Gibson 2021-02-16 22:29:57 -06:00 committed by GitHub
parent 7941664a59
commit fc1275aeb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,17 +28,17 @@ export class LowdbStorageService implements StorageService {
this.logService.info(`Created dir "${this.dir}".`); this.logService.info(`Created dir "${this.dir}".`);
} }
this.dataFilePath = path.join(this.dir, 'data.json'); this.dataFilePath = path.join(this.dir, 'data.json');
if (!fs.existsSync(this.dataFilePath)) {
this.logService.warning(`Could not find data file, "${this.dataFilePath}"; creating it instead.`);
fs.writeFileSync(this.dataFilePath, '', { mode: 0o600 });
fs.chmodSync(this.dataFilePath, 0o600);
this.logService.info(`Created data file "${this.dataFilePath}" with chmod 600.`);
} else {
this.logService.info(`db file "${this.dataFilePath} already exists"; using existing db`);
}
await this.lockDbFile(() => { await this.lockDbFile(() => {
if (!fs.existsSync(this.dataFilePath)) { adapter = new FileSync(this.dataFilePath);
this.logService.warning(`Could not find data file, "${this.dataFilePath}"; creating it instead.`);
fs.writeFileSync(this.dataFilePath, '', { mode: 0o600 });
fs.chmodSync(this.dataFilePath, 0o600);
this.logService.info(`Created data file "${this.dataFilePath}" with chmod 600.`);
} else {
this.logService.info(`db file "${this.dataFilePath} already exists"; using existing db`);
}
}); });
adapter = new FileSync(this.dataFilePath);
} }
try { try {
this.logService.info('Attempting to create lowdb storage adapter.'); this.logService.info('Attempting to create lowdb storage adapter.');