Skip to content

Commit

Permalink
feat(vhd-lib): mergeBlock now return the vhd size variation
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeauchamp committed Nov 25, 2024
1 parent cb42c41 commit 631858d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/vhd-lib/Vhd/VhdAbstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ exports.VhdAbstract = class VhdAbstract {
* @returns {number} the merged data size
*/
async mergeBlock(child, blockId) {
const isBlockPresent = this.containsBlock(blockId)
const block = await child.readBlock(blockId)
await this.writeEntireBlock(block)
return block.data.length
return isBlockPresent ? 0 : this.fullBlockSize
}

/**
Expand Down
11 changes: 10 additions & 1 deletion packages/vhd-lib/Vhd/VhdDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ exports.VhdDirectory = class VhdDirectory extends VhdAbstract {
// and if the compression type is same on both sides
async mergeBlock(child, blockId, isResumingMerge = false) {
const childBlockPath = child._getFullBlockPath?.(blockId)
let initialSize = 0
if (
childBlockPath === undefined ||
this._handler !== child._handler ||
Expand All @@ -262,7 +263,12 @@ exports.VhdDirectory = class VhdDirectory extends VhdAbstract {
}
try {
const blockExists = this.containsBlock(blockId)
if (blockExists) {
initialSize = this._handler.getSizeOnDisk(this._getFullBlockPath(blockId))
}

await this._handler.rename(childBlockPath, this._getFullBlockPath(blockId))

if (!blockExists) {
setBitmap(this.#blockTable, blockId)
await this.writeBlockAllocationTable()
Expand All @@ -274,14 +280,17 @@ exports.VhdDirectory = class VhdDirectory extends VhdAbstract {

// it will throw an error if block is missing in parent
// won't detect if the block was already in parent and is broken/missing in child

// since we can't know the initizal size, this will create a discrepency
// on the size
const { data } = await this.readBlock(blockId)
assert.strictEqual(data.length, this.header.blockSize)
} else {
throw error
}
}
setBitmap(this.#blockTable, blockId)
return this._handler.getSizeOnDisk(this._getFullBlockPath(blockId))
return this._handler.getSizeOnDisk(this._getFullBlockPath(blockId)) - initialSize
}

async writeEntireBlock(block) {
Expand Down

0 comments on commit 631858d

Please sign in to comment.