Merge pull request #235 from overleaf/jpa-atomic-writes

[ContentCacheManager] write streams to disk atomically
This commit is contained in:
Jakob Ackermann 2021-05-18 11:44:16 +02:00 committed by GitHub
commit e787106eed

View file

@ -121,11 +121,13 @@ async function writePdfStream(dir, hash, buffers) {
// ETags used for client side caching via browser internals. // ETags used for client side caching via browser internals.
return false return false
} catch (e) {} } catch (e) {}
const file = await fs.promises.open(filename, 'w') const atomicWriteFilename = filename + '~'
const file = await fs.promises.open(atomicWriteFilename, 'w')
if (Settings.enablePdfCachingDark) { if (Settings.enablePdfCachingDark) {
// Write an empty file in dark mode. // Write an empty file in dark mode.
buffers = [] buffers = []
} }
try {
try { try {
for (const buffer of buffers) { for (const buffer of buffers) {
await file.write(buffer) await file.write(buffer)
@ -133,6 +135,14 @@ async function writePdfStream(dir, hash, buffers) {
} finally { } finally {
await file.close() await file.close()
} }
await fs.promises.rename(atomicWriteFilename, filename)
} catch (err) {
try {
await fs.promises.unlink(atomicWriteFilename)
} catch (_) {
throw err
}
}
return true return true
} }