Compare commits

...

2 Commits

Author SHA1 Message Date
Florent Beauchamp
dd0b71bca0 fix: rework optionnal depency 2022-09-22 14:07:52 +02:00
Florent Beauchamp
dc571a5811 fix(Backup/file restore): don't load fuse-vhd on platform not supported by fuse-native 2022-09-21 16:59:58 +02:00
3 changed files with 25 additions and 20 deletions

View File

@@ -1,12 +1,8 @@
'use strict'
const LRU = require('lru-cache')
const Fuse = require('fuse-native')
const { VhdSynthetic } = require('vhd-lib')
const { Disposable, fromCallback } = require('promise-toolbox')
const { createLogger } = require('@xen-orchestra/log')
const { warn } = createLogger('vates:fuse-vhd')
// build a s stat object from https://github.com/fuse-friends/fuse-native/blob/master/test/fixtures/stat.js
const stat = st => ({
@@ -20,6 +16,7 @@ const stat = st => ({
})
exports.mount = Disposable.factory(async function* mount(handler, diskPath, mountDir) {
const Fuse = require('fuse-native')
const vhd = yield VhdSynthetic.fromVhdChain(handler, diskPath)
const cache = new LRU({
@@ -57,9 +54,7 @@ exports.mount = Disposable.factory(async function* mount(handler, diskPath, moun
},
read(path, fd, buf, len, pos, cb) {
if (path === '/vhd0') {
return vhd
.readRawData(pos, len, cache, buf)
.then(cb)
return vhd.readRawData(pos, len, cache, buf).then(cb)
}
throw new Error(`read file ${path} not exists`)
},
@@ -67,5 +62,5 @@ exports.mount = Disposable.factory(async function* mount(handler, diskPath, moun
return new Disposable(
() => fromCallback(() => fuse.unmount()),
fromCallback(() => fuse.mount())
)
)
})

View File

@@ -19,11 +19,13 @@
},
"dependencies": {
"@xen-orchestra/log": "^0.3.0",
"fuse-native": "^2.2.6",
"lru-cache": "^7.14.0",
"promise-toolbox": "^0.21.0",
"vhd-lib": "^4.0.1"
},
"optionalDependencies": {
"fuse-native": "^2.2.6"
},
"scripts": {
"postversion": "npm publish --access public"
}

View File

@@ -28,10 +28,8 @@ const { isMetadataFile } = require('./_backupType.js')
const { isValidXva } = require('./_isValidXva.js')
const { listPartitions, LVM_PARTITION_TYPE } = require('./_listPartitions.js')
const { lvs, pvs } = require('./_lvm.js')
// @todo : this import is marked extraneous , sould be fixed when lib is published
const { mount } = require('@vates/fuse-vhd')
const { asyncEach } = require('@vates/async-each')
const { mount } = require('@vates/fuse-vhd')
const DIR_XO_CONFIG_BACKUPS = 'xo-config-backups'
exports.DIR_XO_CONFIG_BACKUPS = DIR_XO_CONFIG_BACKUPS
@@ -76,14 +74,16 @@ const debounceResourceFactory = factory =>
}
class RemoteAdapter {
constructor(handler, { debounceResource = res => res, dirMode, vhdDirectoryCompression, useGetDiskLegacy=false } = {}) {
constructor(
handler,
{ debounceResource = res => res, dirMode, vhdDirectoryCompression, useGetDiskLegacy = false } = {}
) {
this._debounceResource = debounceResource
this._dirMode = dirMode
this._handler = handler
this._vhdDirectoryCompression = vhdDirectoryCompression
this._readCacheListVmBackups = synchronized.withKey()(this._readCacheListVmBackups)
this._useGetDiskLegacy = useGetDiskLegacy
}
get handler() {
@@ -324,9 +324,7 @@ class RemoteAdapter {
return this.#useVhdDirectory()
}
async *#getDiskLegacy(diskId) {
const RE_VHDI = /^vhdi(\d+)$/
const handler = this._handler
@@ -358,15 +356,25 @@ class RemoteAdapter {
}
async *getDisk(diskId) {
if(this._useGetDiskLegacy){
yield * this.#getDiskLegacy(diskId)
if (this._useGetDiskLegacy) {
yield* this.#getDiskLegacy(diskId)
return
}
const handler = this._handler
// this is a disposable
const mountDir = yield getTmpDir()
// this is also a disposable
yield mount(handler, diskId, mountDir)
try {
// this is also a disposable
yield mount(handler, diskId, mountDir)
} catch (error) {
// fallback in case of missing dependency
if (error.code === 'MODULE_NOT_FOUND') {
yield* this.#getDiskLegacy(diskId)
} else {
throw error
}
}
// this will yield disk path to caller
yield `${mountDir}/vhd0`
}