2022-02-22 12:29:26 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
2021-12-23 10:31:29 +01:00
|
|
|
const { dirname, relative } = require('path')
|
2018-05-14 04:48:16 -07:00
|
|
|
|
2021-12-23 10:31:29 +01:00
|
|
|
const { openVhd } = require('./openVhd')
|
|
|
|
|
const { DISK_TYPES } = require('./_constants')
|
|
|
|
|
const { Disposable } = require('promise-toolbox')
|
2018-05-14 04:48:16 -07:00
|
|
|
|
2021-12-23 10:31:29 +01:00
|
|
|
module.exports = async function chain(parentHandler, parentPath, childHandler, childPath, force = false) {
|
2021-11-22 15:50:30 +01:00
|
|
|
await Disposable.use(
|
2022-06-30 11:40:21 +02:00
|
|
|
[openVhd(parentHandler, parentPath), openVhd(childHandler, childPath, { flags: 'r+' })],
|
2021-11-22 15:50:30 +01:00
|
|
|
async ([parentVhd, childVhd]) => {
|
|
|
|
|
await childVhd.readHeaderAndFooter()
|
|
|
|
|
const { header, footer } = childVhd
|
2018-05-14 04:48:16 -07:00
|
|
|
|
2021-11-25 17:53:20 +01:00
|
|
|
if (footer.diskType !== DISK_TYPES.DIFFERENCING) {
|
2021-11-22 15:50:30 +01:00
|
|
|
if (!force) {
|
|
|
|
|
throw new Error('cannot chain disk of type ' + footer.diskType)
|
|
|
|
|
}
|
2021-11-25 17:53:20 +01:00
|
|
|
footer.diskType = DISK_TYPES.DIFFERENCING
|
2021-11-22 15:50:30 +01:00
|
|
|
}
|
|
|
|
|
await childVhd.readBlockAllocationTable()
|
2018-05-14 04:48:16 -07:00
|
|
|
|
2021-11-22 15:50:30 +01:00
|
|
|
const parentName = relative(dirname(childPath), parentPath)
|
|
|
|
|
header.parentUuid = parentVhd.footer.uuid
|
|
|
|
|
header.parentUnicodeName = parentName
|
|
|
|
|
await childVhd.setUniqueParentLocator(parentName)
|
|
|
|
|
await childVhd.writeHeader()
|
|
|
|
|
await childVhd.writeFooter()
|
2018-05-14 04:48:16 -07:00
|
|
|
}
|
2021-11-22 15:50:30 +01:00
|
|
|
)
|
2018-05-14 04:48:16 -07:00
|
|
|
}
|