Skip to content

Commit

Permalink
wip-use-relative-layer-paths-for-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
deribaucourt committed Dec 1, 2023
1 parent 25b9d43 commit 35dc8c7
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions client/src/driver/BitBakeProjectScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {

import { type BitbakeDriver } from './BitbakeDriver'
import { type LanguageClient } from 'vscode-languageclient/node'
import fs from 'fs'

interface ScannStatus {
scanIsRunning: boolean
Expand All @@ -39,6 +40,7 @@ export class BitBakeProjectScanner {
private _shouldDeepExamine: boolean = false
private _bitbakeDriver: BitbakeDriver | undefined
private _languageClient: LanguageClient | undefined
private containerWorkdir: string | undefined

setDriver (bitbakeDriver: BitbakeDriver): void {
this._bitbakeDriver = bitbakeDriver
Expand Down Expand Up @@ -109,6 +111,37 @@ export class BitBakeProjectScanner {
}
}

private async getContainerInode (filepath: string): Promise<number> {
const commandResult = await this.executeBitBakeCommand(`stat -c %i ${filepath}`)
const inode = parseInt(commandResult.stdout.toString().trim())
return inode
}

private async scanContainerWorkdir (layerPath: string, hostWorkdir: string): Promise<void> {
if (fs.existsSync(layerPath)) {
// We're not inside a container, or the container is not using a different workdir
return
}

// Get inode of hostWorkdir
const hostWorkdirStats = fs.statSync(hostWorkdir)
const hostWorkdirInode = hostWorkdirStats.ino

// Find inode in layerPath and all parents
let parentInode = NaN
let parentPath = layerPath
while (parentPath !== '/') {
parentInode = await this.getContainerInode(parentPath)
if (parentInode === hostWorkdirInode) {
this.containerWorkdir = parentPath
return
}
parentPath = path.dirname(parentPath)
}

this.containerWorkdir = undefined
}

private printScanStatistic (): void {
logger.info('Scan results:')
logger.info('******************************************************************')
Expand Down Expand Up @@ -145,15 +178,15 @@ export class BitBakeProjectScanner {
}

for (const element of outputLines.slice(layersFirstLine + 2)) {
const tempElement: string[] = element.split(/\s+/)
const tempElement = element.split(/\s+/)
const layerElement = {
name: tempElement[0],
path: tempElement[1],
path: await this.resolveContainerPath(tempElement[1]),
priority: parseInt(tempElement[2])
}

if ((layerElement.name !== undefined) && (layerElement.path !== undefined) && layerElement.priority !== undefined) {
this._bitbakeScanResult._layers.push(layerElement)
if ((layerElement.name !== undefined) && (layerElement.path !== undefined) && (layerElement.priority !== undefined)) {
this._bitbakeScanResult._layers.push(layerElement as LayerInfo)
}
}
} else {
Expand All @@ -162,6 +195,25 @@ export class BitBakeProjectScanner {
}
}

/// If a docker container is used, the workdir may be different from the host system.
/// This function resolves the path to the host system.
private async resolveContainerPath (layerPath: string | undefined): Promise<string | undefined> {
if (layerPath === undefined) {
return undefined
}
const hostWorkdir = this.bitbakeDriver?.bitbakeSettings.workingDirectory
if (hostWorkdir === undefined) {
throw new Error('hostWorkdir is undefined')
}
// TODO Should be done only once per scan
await this.scanContainerWorkdir(layerPath, hostWorkdir)
if (this.containerWorkdir === undefined) {
return layerPath
}
const relativePath = path.relative(this.containerWorkdir, layerPath)
return path.resolve(hostWorkdir, relativePath)
}

private searchFiles (pattern: string): ElementInfo[] {
const elements: ElementInfo[] = new Array < ElementInfo >()

Expand All @@ -181,6 +233,7 @@ export class BitBakeProjectScanner {
elements.push(element)
}
} catch (error) {
// EDRT REMOVE THIS BEFORE MERGING Fails here
logger.error(`find error: pattern: ${pattern} layer.path: ${layer.path} error: ${JSON.stringify(error)}`)
throw error
}
Expand Down

0 comments on commit 35dc8c7

Please sign in to comment.