Skip to content

Commit

Permalink
Merge pull request #89 from rcowsill/fix/87
Browse files Browse the repository at this point in the history
Use path module instead of string manipulation
  • Loading branch information
satackey authored Dec 14, 2020
2 parents 653164a + 46c6909 commit 78ea282
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
35 changes: 15 additions & 20 deletions src/LayerCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LayerCache {
ids: string[] = []
unformattedSaveKey: string = ''
restoredRootKey: string = ''
imagesDir: string = path.resolve(`${__dirname}/../.adlc`)
imagesDir: string = path.join(__dirname, '..', '.adlc')
enabledParallel = true
concurrency: number = 4

Expand Down Expand Up @@ -49,8 +49,8 @@ class LayerCache {
}

private async saveImageAsUnpacked() {
await fs.mkdir(this.getSavedImageTarDir(), { recursive: true })
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady(this.ids)).join(`' '`)}' | tar xf - -C .`], { cwd: this.getSavedImageTarDir() })
await fs.mkdir(this.getUnpackedTarDir(), { recursive: true })
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady(this.ids)).join(`' '`)}' | tar xf - -C .`], { cwd: this.getUnpackedTarDir() })
}

private async makeRepotagsDockerSaveArgReady(repotags: string[]): Promise<string[]> {
Expand Down Expand Up @@ -91,14 +91,14 @@ class LayerCache {

private async moveLayerTarsInDir(fromDir: string, toDir: string) {
const layerTars = (await recursiveReaddir(fromDir))
.filter(path => path.endsWith(`/layer.tar`))
.map(path => path.replace(`${fromDir}/`, ``))
.filter(layerPath => path.basename(layerPath) === `layer.tar`)
.map(layerPath => path.relative(fromDir, layerPath))

const moveLayer = async (layer: string) => {
const from = path.resolve(`${fromDir}/${layer}`)
const to = path.resolve(`${toDir}/${layer}`)
const from = path.join(fromDir, layer)
const to = path.join(toDir, layer)
core.debug(`Moving layer tar from ${from} to ${to}`)
await fs.mkdir(`${path.dirname(to)}`, { recursive: true })
await fs.mkdir(path.dirname(to), { recursive: true })
await fs.rename(from, to)
}
await Promise.all(layerTars.map(moveLayer))
Expand Down Expand Up @@ -201,14 +201,14 @@ class LayerCache {
}

private async restoreSingleLayerBy(id: string): Promise<string> {
const path = this.genSingleLayerStorePath(id)
const layerPath = this.genSingleLayerStorePath(id)
const key = await this.recoverSingleLayerKey(id)
const dir = path.replace(/[^/\\]+$/, ``)
const dir = path.dirname(layerPath)

core.debug(JSON.stringify({ log: `restoreSingleLayerBy`, id, path, dir, key }))
core.debug(JSON.stringify({ log: `restoreSingleLayerBy`, id, layerPath, dir, key }))

await fs.mkdir(dir, { recursive: true })
const result = await cache.restoreCache([path], key)
const result = await cache.restoreCache([layerPath], key)

if (result == null) {
throw new Error(`${LayerCache.ERROR_LAYER_CACHE_NOT_FOUND_STR}: ${JSON.stringify({ id })}`)
Expand All @@ -232,23 +232,19 @@ class LayerCache {
}

getUnpackedTarDir(): string {
return path.resolve(`${this.getImagesDir()}/${this.getCurrentTarStoreDir()}`)
return path.join(this.getImagesDir(), this.getCurrentTarStoreDir())
}

getLayerCachesDir() {
return `${this.getUnpackedTarDir()}-layers`
}

getSavedImageTarDir(): string {
return path.resolve(`${this.getImagesDir()}/${this.getCurrentTarStoreDir()}`)
}

getCurrentTarStoreDir(): string {
return 'image'
}

genSingleLayerStorePath(id: string) {
return path.resolve(`${this.getLayerCachesDir()}/${id}/layer.tar`)
return path.join(this.getLayerCachesDir(), id, `layer.tar`)
}

async generateRootHashFromManifest(): Promise<string> {
Expand Down Expand Up @@ -296,8 +292,7 @@ class LayerCache {
}

async getLayerIds(): Promise<string[]> {
const getIdfromLayerRelativePath = (path: string) => path.replace('/layer.tar', '')
const layerIds = (await this.getLayerTarFiles()).map(getIdfromLayerRelativePath);
const layerIds = (await this.getLayerTarFiles()).map(path.dirname);
core.debug(JSON.stringify({ log: `getLayerIds`, layerIds }))
return layerIds
}
Expand Down
5 changes: 3 additions & 2 deletions src/Tar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertType } from 'typescript-is'
import { promises as fs } from 'fs'
import * as path from 'path'

export interface Manifest {
Config: string
Expand All @@ -13,8 +14,8 @@ export function assertManifests(x: unknown): asserts x is Manifests {
assertType<Manifests>(x)
}

export async function loadRawManifests(path: string) {
return (await fs.readFile(`${path}/manifest.json`)).toString()
export async function loadRawManifests(rootPath: string) {
return (await fs.readFile(path.join(rootPath, `manifest.json`))).toString()
}

export async function loadManifests(path: string) {
Expand Down

0 comments on commit 78ea282

Please sign in to comment.