2018-05-14 04:48:16 -07:00
|
|
|
import { dirname, relative } from 'path'
|
|
|
|
|
|
2021-11-22 15:50:30 +01:00
|
|
|
import { openVhd } from './'
|
2018-05-14 04:48:16 -07:00
|
|
|
import { DISK_TYPE_DIFFERENCING } from './_constants'
|
2021-11-22 15:50:30 +01:00
|
|
|
import { Disposable } from 'promise-toolbox'
|
2018-05-14 04:48:16 -07:00
|
|
|
|
2020-11-24 10:50:40 +01:00
|
|
|
export default async function chain(parentHandler, parentPath, childHandler, childPath, force = false) {
|
2021-11-22 15:50:30 +01:00
|
|
|
await Disposable.use(
|
|
|
|
|
[openVhd(parentHandler, parentPath), openVhd(childHandler, childPath)],
|
|
|
|
|
async ([parentVhd, childVhd]) => {
|
|
|
|
|
await childVhd.readHeaderAndFooter()
|
|
|
|
|
const { header, footer } = childVhd
|
2018-05-14 04:48:16 -07:00
|
|
|
|
2021-11-22 15:50:30 +01:00
|
|
|
if (footer.diskType !== DISK_TYPE_DIFFERENCING) {
|
|
|
|
|
if (!force) {
|
|
|
|
|
throw new Error('cannot chain disk of type ' + footer.diskType)
|
|
|
|
|
}
|
|
|
|
|
footer.diskType = DISK_TYPE_DIFFERENCING
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|