chore(xo-server/vhd-merge): abstract FD handling (#2757)

Due to our smart implementation, the Vhd class does not need to be aware of the fact that the file is already opened.
This commit is contained in:
Julien Fontanet 2018-03-13 15:09:21 +01:00 committed by GitHub
parent 782505b292
commit f45935aa44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -194,7 +194,7 @@ export class Vhd {
// ================================================================= // =================================================================
_readStream (start, n) { _readStream (start, n) {
return this._handler.createReadStream(this._fd ? this._fd : this._path, { return this._handler.createReadStream(this._path, {
start, start,
end: start + n - 1, // end is inclusive end: start + n - 1, // end is inclusive
}) })
@ -394,13 +394,10 @@ export class Vhd {
}` }`
) )
// TODO: could probably be merged in remote handlers. // TODO: could probably be merged in remote handlers.
const stream = await this._handler.createOutputStream( const stream = await this._handler.createOutputStream(this._path, {
this._fd ? this._fd : this._path, flags: 'r+',
{ start: offset,
flags: 'r+', })
start: offset,
}
)
return Buffer.isBuffer(data) return Buffer.isBuffer(data)
? new Promise((resolve, reject) => { ? new Promise((resolve, reject) => {
stream.on('error', reject) stream.on('error', reject)
@ -648,12 +645,13 @@ export default concurrency(2)(async function vhdMerge (
childHandler, childHandler,
childPath childPath
) { ) {
const parentVhd = new Vhd(parentHandler, parentPath) const parentFd = await parentHandler.openFile(parentPath, 'r+')
parentVhd._fd = await parentHandler.openFile(parentPath, 'r+')
try { try {
const childVhd = new Vhd(childHandler, childPath) const parentVhd = new Vhd(parentHandler, parentFd)
childVhd._fd = await childHandler.openFile(childPath, 'r') const childFd = await childHandler.openFile(childPath, 'r')
try { try {
const childVhd = new Vhd(childHandler, childFd)
// Reading footer and header. // Reading footer and header.
await Promise.all([ await Promise.all([
parentVhd.readHeaderAndFooter(), parentVhd.readHeaderAndFooter(),
@ -706,10 +704,10 @@ export default concurrency(2)(async function vhdMerge (
return mergedDataSize return mergedDataSize
} finally { } finally {
await childHandler.closeFile(childVhd._fd) await childHandler.closeFile(childFd)
} }
} finally { } finally {
await parentHandler.closeFile(parentVhd._fd) await parentHandler.closeFile(parentFd)
} }
}) })