fix(xva): last block path

the last block may be added by a different code path where the block counter wasn't padded, leading to trying to allocate a few Ettabytes
This commit is contained in:
Florent BEAUCHAMP 2024-02-02 17:10:15 +01:00 committed by Julien Fontanet
parent 3314ba6e08
commit 79abb97b1f
2 changed files with 6 additions and 2 deletions

View File

@ -0,0 +1,3 @@
const formatCounter = counter => String(counter).padStart(8, '0')
export const formatBlockPath = (basePath, counter) => `${basePath}/${formatCounter(counter)}`

View File

@ -1,3 +1,4 @@
import { formatBlockPath } from './_formatBlockPath.mjs'
import { fromCallback } from 'promise-toolbox'
import { readChunkStrict } from '@vates/read-chunk'
import { xxhash64 } from 'hash-wasm'
@ -33,7 +34,7 @@ export default async function addDisk(pack, vhd, basePath) {
Date.now() - lastBlockWrittenAt > MAX_INTERVAL_BETWEEN_BLOCKS
) {
written = true
await writeBlock(pack, data, `${basePath}/${('' + counter).padStart(8, '0')}`)
await writeBlock(pack, data, formatBlockPath(basePath, counter))
lastBlockWrittenAt = Date.now()
} else {
written = false
@ -42,6 +43,6 @@ export default async function addDisk(pack, vhd, basePath) {
}
if (!written) {
// last block must be present
writeBlock(pack, empty.slice(0, lastBlockLength), `${basePath}/${counter}`)
writeBlock(pack, empty.slice(0, lastBlockLength), `${basePath}/${('' + counter).padStart(8, '0')}`)
}
}