Skip to content

Commit

Permalink
chore(vhd-lib): move alias utils to aliases submodule
Browse files Browse the repository at this point in the history
Introduced in e15be7e
  • Loading branch information
julien-f committed Jan 18, 2022
1 parent c10601d commit 3aa6669
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
5 changes: 3 additions & 2 deletions @xen-orchestra/backups/_cleanVm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const assert = require('assert')
const sum = require('lodash/sum')
const { asyncMap } = require('@xen-orchestra/async-map')
const { Constants, isVhdAlias, mergeVhd, openVhd, VhdAbstract, VhdFile, resolveAlias } = require('vhd-lib')
const { Constants, mergeVhd, openVhd, VhdAbstract, VhdFile } = require('vhd-lib')
const { isVhdAlias, resolveVhdAlias } = require('vhd-lib/aliases')
const { dirname, resolve } = require('path')
const { DISK_TYPES } = Constants
const { isMetadataFile, isVhdFile, isXvaFile, isXvaSumFile } = require('./_backupType.js')
Expand Down Expand Up @@ -138,7 +139,7 @@ const listVhds = async (handler, vmDir) => {
async function checkAliases(aliasPaths, targetDataRepository, { handler, onLog = noop, remove = false }) {
const aliasFound = []
for (const path of aliasPaths) {
const target = await resolveAlias(handler, path)
const target = await resolveVhdAlias(handler, path)

if (!isVhdFile(target)) {
onLog(`Alias ${path} references a non vhd target: ${target}`)
Expand Down
4 changes: 2 additions & 2 deletions packages/vhd-lib/Vhd/VhdAbstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const assert = require('assert')
const path = require('path')
const asyncIteratorToStream = require('async-iterator-to-stream')
const { checksumStruct, fuFooter, fuHeader } = require('../_structs')
const { isVhdAlias, resolveAlias } = require('../_resolveAlias')
const { isVhdAlias, resolveVhdAlias } = require('../aliases')

exports.VhdAbstract = class VhdAbstract {
get bitmapSize() {
Expand Down Expand Up @@ -198,7 +198,7 @@ exports.VhdAbstract = class VhdAbstract {
}

static async unlink(handler, path) {
const resolved = await resolveAlias(handler, path)
const resolved = await resolveVhdAlias(handler, path)
try {
await handler.unlink(resolved)
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tmp = require('tmp')
const { getSyncedHandler } = require('@xen-orchestra/fs')
const { Disposable, pFromCallback } = require('promise-toolbox')

const { isVhdAlias, resolveAlias } = require('./_resolveAlias')
const { isVhdAlias, resolveVhdAlias } = require('./aliases')
const { ALIAS_MAX_PATH_LENGTH } = require('./_constants')

let tempDir
Expand All @@ -28,20 +28,20 @@ test('is vhd alias recognize only *.alias.vhd files', () => {
})

test('resolve return the path in argument for a non alias file ', async () => {
expect(await resolveAlias(null, 'filename.vhd')).toEqual('filename.vhd')
expect(await resolveVhdAlias(null, 'filename.vhd')).toEqual('filename.vhd')
})
test('resolve get the path of the target file for an alias', async () => {
await Disposable.use(async function* () {
// same directory
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
const alias = `alias.alias.vhd`
await handler.writeFile(alias, 'target.vhd')
await expect(await resolveAlias(handler, alias)).toEqual(`target.vhd`)
await expect(await resolveVhdAlias(handler, alias)).toEqual(`target.vhd`)

// different directory
await handler.mkdir(`sub`)
await handler.writeFile(alias, 'sub/target.vhd', { flags: 'w' })
await expect(await resolveAlias(handler, alias)).toEqual(`sub/target.vhd`)
await expect(await resolveVhdAlias(handler, alias)).toEqual(`sub/target.vhd`)
})
})

Expand All @@ -51,14 +51,14 @@ test('resolve throws an error an alias to an alias', async () => {
const alias = `alias.alias.vhd`
const target = `target.alias.vhd`
await handler.writeFile(alias, target)
await expect(async () => await resolveAlias(handler, alias)).rejects.toThrow(Error)
await expect(async () => await resolveVhdAlias(handler, alias)).rejects.toThrow(Error)
})
})

test('resolve throws an error on a file too big ', async () => {
await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: `file://${tempDir}` })
await handler.writeFile('toobig.alias.vhd', Buffer.alloc(ALIAS_MAX_PATH_LENGTH + 1, 0))
await expect(async () => await resolveAlias(handler, 'toobig.alias.vhd')).rejects.toThrow(Error)
await expect(async () => await resolveVhdAlias(handler, 'toobig.alias.vhd')).rejects.toThrow(Error)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function isVhdAlias(filename) {
}
exports.isVhdAlias = isVhdAlias

exports.resolveAlias = async function resolveAlias(handler, filename) {
exports.resolveVhdAlias = async function resolveVhdAlias(handler, filename) {
if (!isVhdAlias(filename)) {
return filename
}
Expand Down
3 changes: 0 additions & 3 deletions packages/vhd-lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ exports.VhdDirectory = require('./Vhd/VhdDirectory').VhdDirectory
exports.VhdFile = require('./Vhd/VhdFile').VhdFile
exports.VhdSynthetic = require('./Vhd/VhdSynthetic').VhdSynthetic
exports.Constants = require('./_constants')
const {isVhdAlias, resolveAlias} = require('./_resolveAlias')
exports.isVhdAlias = isVhdAlias
exports.resolveAlias = resolveAlias
4 changes: 2 additions & 2 deletions packages/vhd-lib/openVhd.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { resolveAlias } = require('./_resolveAlias')
const { resolveVhdAlias } = require('./aliases')
const { VhdDirectory } = require('./Vhd/VhdDirectory.js')
const { VhdFile } = require('./Vhd/VhdFile.js')

exports.openVhd = async function openVhd(handler, path, opts) {
const resolved = await resolveAlias(handler, path)
const resolved = await resolveVhdAlias(handler, path)
try {
return await VhdFile.open(handler, resolved, opts)
} catch (e) {
Expand Down

0 comments on commit 3aa6669

Please sign in to comment.