Skip to content

Commit

Permalink
Optim: Get all container inodes in one find command
Browse files Browse the repository at this point in the history
This greatly speeds up the scanner when we are looking for corresponding
container mount points. The scanner now runs only one find command
instead of one per directory level in the layer path.
  • Loading branch information
deribaucourt committed Dec 6, 2023
1 parent e760df8 commit 2c25cc3
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions client/src/driver/BitBakeProjectScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ export class BitBakeProjectScanner {
}
}

private async getContainerInode (filepath: string): Promise<number> {
const commandResult = await this.executeBitBakeCommand(`stat -c %i ${filepath}`)
private async getContainerParentInodes (filepath: string): Promise<number[]> {
const commandResult = await this.executeBitBakeCommand(`f=${filepath}; while [[ $f != / ]]; do stat -c %i $f; f=$(dirname "$f"); done;`)
const stdout = commandResult.stdout.toString().trim()
const regex = /^\d+$/m
const match = stdout.match(regex)
const inode = (match != null) ? parseInt(match[0]) : NaN
return inode
const regex = /^\d+$/gm
const matches = stdout.match(regex)
return (matches != null) ? matches.map((match) => parseInt(match)) : [NaN]
}

/// Find corresponding mount point inode in layerPath/hostWorkdir and all parents
private async scanContainerMountPoint (layerPath: string, hostWorkdir: string): Promise<void> {
this.containerMountPoint = undefined
this.hostMountPoint = undefined
Expand All @@ -132,24 +132,24 @@ export class BitBakeProjectScanner {
return
}

let containerDir = layerPath
const containerDirInodes = await this.getContainerParentInodes(containerDir)
let hostDir = hostWorkdir

while (hostDir !== '/') {
const hostDirInode = fs.statSync(hostDir).ino

// Find inode in layerPath and all parents
// OPTIM we could run stat on all parent directories in one (find?) command, and store the result in a map
let containerDirInode = NaN
let containerDir = layerPath
let containerIdx = 0
while (containerDir !== '/') {
containerDirInode = await this.getContainerInode(containerDir)
const containerDirInode = containerDirInodes[containerIdx]
logger.debug('Comparing container inodes: ' + containerDir + ':' + containerDirInode + ' ' + hostDir + ':' + hostDirInode)
if (containerDirInode === hostDirInode) {
this.containerMountPoint = containerDir
this.hostMountPoint = hostDir
return
}
containerDir = path.dirname(containerDir)
containerIdx++
}
hostDir = path.dirname(hostDir)
}
Expand Down

0 comments on commit 2c25cc3

Please sign in to comment.