From 46c6909e912f87be2604148d21405d25112d0e2a Mon Sep 17 00:00:00 2001 From: Rob Cowsill <42620235+rcowsill@users.noreply.github.com> Date: Wed, 9 Dec 2020 12:12:28 +0000 Subject: [PATCH] Use path module instead of string manipulation This fixes a Windows bug where layer content was uploaded in the root cache --- src/LayerCache.ts | 35 +++++++++++++++-------------------- src/Tar.ts | 5 +++-- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/LayerCache.ts b/src/LayerCache.ts index b2266758..5587712b 100644 --- a/src/LayerCache.ts +++ b/src/LayerCache.ts @@ -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 @@ -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 { @@ -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)) @@ -201,14 +201,14 @@ class LayerCache { } private async restoreSingleLayerBy(id: string): Promise { - 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 })}`) @@ -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 { @@ -296,8 +292,7 @@ class LayerCache { } async getLayerIds(): Promise { - 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 } diff --git a/src/Tar.ts b/src/Tar.ts index c40ebc2a..9602cacd 100644 --- a/src/Tar.ts +++ b/src/Tar.ts @@ -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 @@ -13,8 +14,8 @@ export function assertManifests(x: unknown): asserts x is Manifests { assertType(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) {