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.
return false
} catch (e) {}
const file = await fs.promises.open(filename, 'w')
const atomicWriteFilename = filename + '~'
const file = await fs.promises.open(atomicWriteFilename, 'w')
if (Settings.enablePdfCachingDark) {
// Write an empty file in dark mode.
buffers = []
}
try {
try {
for (const buffer of buffers) {
await file.write(buffer)
@ -133,6 +135,14 @@ async function writePdfStream(dir, hash, buffers) {
} finally {
await file.close()
}
await fs.promises.rename(atomicWriteFilename, filename)
} catch (err) {
try {
await fs.promises.unlink(atomicWriteFilename)
} catch (_) {
throw err
}
}
return true
}