Skip to content

Commit

Permalink
refactor: don't handke immutability in fs
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeauchamp committed Jan 29, 2024
1 parent 63574e4 commit dc3462f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 40 deletions.
36 changes: 21 additions & 15 deletions @xen-orchestra/backups/RemoteAdapter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ import { isValidXva } from './_isValidXva.mjs'
import { listPartitions, LVM_PARTITION_TYPE } from './_listPartitions.mjs'
import { lvs, pvs } from './_lvm.mjs'
import { watchStreamSize } from './_watchStreamSize.mjs'
import { fstat } from 'node:fs'

export const DIR_XO_CONFIG_BACKUPS = 'xo-config-backups'

export const DIR_XO_POOL_METADATA_BACKUPS = 'xo-pool-metadata-backups'


const IMMUTABILTY_METADATA_FILENAME = 'immutability.json'

const { debug, warn } = createLogger('xo:backups:RemoteAdapter')

const compareTimestamp = (a, b) => a.timestamp - b.timestamp
Expand Down Expand Up @@ -749,29 +753,31 @@ export class RemoteAdapter {
}

async readVmBackupMetadata(path) {
const handler = this._handler
let json
let isImmutable = false

Check failure on line 757 in @xen-orchestra/backups/RemoteAdapter.mjs

View workflow job for this annotation

GitHub Actions / CI

'isImmutable' is never reassigned. Use 'const' instead
let remoteIsImmutable = false
// if the remote is immutable, check if this metadatas are also immutables
if (handler.immutability !== undefined) {
try{
await fstat.stat( IMMUTABILTY_METADATA_FILENAME)
remoteIsImmutable = true
}catch(error){
if(error.code !== 'ENOENT'){
throw error
}
}
if (remoteIsImmutable) {
let fd
try {
// this will trigger e EPERM error if the file is immutable
fd = (await handler.openFile(path, 'r+')).fd
json = await this.handler.readFile(fd)
// this will trigger an EPERM error if the file is immutable
json = await this.handler.readFile(fd, {flags: 'r+'})
} catch (err) {
if (err.code === 'EPERM') {
isImmutable = true
// retry without triggerring a potential change
if (err.code === 'EPERM' && remoteIsImmutable ) {
json = await this._handler.readFile(path)
}
} finally {
if (fd !== undefined) {
await handler.closeFile(fd)
}
}
}
if (json === undefined) {
json = await this._handler.readFile(path)
}
}

// _filename is a private field used to compute the backup id
//
// it's enumerable to make it cacheable
Expand Down
16 changes: 1 addition & 15 deletions @xen-orchestra/fs/src/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const DEFAULT_MAX_PARALLEL_OPERATIONS = 10

const ENCRYPTION_DESC_FILENAME = 'encryption.json'
const ENCRYPTION_METADATA_FILENAME = 'metadata.json'
const IMMUTABILTY_METADATA_FILENAME = 'immutability.json'

const ignoreEnoent = error => {
if (error == null || error.code !== 'ENOENT') {
Expand Down Expand Up @@ -77,7 +76,6 @@ class PrefixWrapper {

export default class RemoteHandlerAbstract {
#rawEncryptor
#immutability

get #encryptor() {
if (this.#rawEncryptor === undefined) {
Expand All @@ -86,10 +84,6 @@ export default class RemoteHandlerAbstract {
return this.#rawEncryptor
}

get immutability(){
return this.#immutability
}

constructor(remote, options = {}) {
if (remote.url === 'test://') {
this._remote = remote
Expand Down Expand Up @@ -287,7 +281,7 @@ export default class RemoteHandlerAbstract {
}

async __readFile(file, { flags = 'r' } = {}) {
const data = await this._readFile(typeof file === 'string' ? normalizePath(file) : file, { flags })
const data = await this._readFile(normalizePath(file), { flags })
return this.#encryptor.decryptData(data)
}

Expand Down Expand Up @@ -403,14 +397,6 @@ export default class RemoteHandlerAbstract {
}
}
}

try{
this.#immutability = JSON.parse( await this._readFile(normalizePath(IMMUTABILTY_METADATA_FILENAME), 'utf-8'))
}catch(error){
if (error.code !== 'ENOENT') {
throw error
}
}
}

async test() {
Expand Down
12 changes: 2 additions & 10 deletions @xen-orchestra/fs/src/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,8 @@ export default class LocalHandler extends RemoteHandlerAbstract {
}

async _readFile(file, options) {
const isPath = typeof file === 'string'
return await this.#addSyncStackTrace(retry, () => {
if(isPath){
const filePath = this.getFilePath(file)
return fs.readFile(filePath, options)
} else {
return fs.readFile(file, options)
}
}, this.#retriesOnEagain)

const filePath = this.getFilePath(file)
return await this.#addSyncStackTrace(retry, () => fs.readFile(filePath, options), this.#retriesOnEagain)
}

async _rename(oldPath, newPath) {
Expand Down

0 comments on commit dc3462f

Please sign in to comment.